Files
xPineScript/xOscillators/x-saherelm.hull.butterfly.pine
2024-01-25 04:07:49 +03:30

159 lines
3.9 KiB
Plaintext

///////////////////////////////////////////////////////
//
// SaherElm IT Center Hull butterfly Oscillator
// ---------------------------------------------
// saherelm implementation of above oscillator ...
//
// Maintainer:
// ------------
// Hadi Khazaee Asl (hadi_khazaee_asl@yahoo.com)
//
//////////////////////////////////////////////////////
//
//@version=5
indicator(title="SaherElm Hull butterfly Oscillator", shorttitle = "XS_HUBF_OSC", overlay = false)
//
// START Inputs ....
//
//
HUBFSrc = input.source(defval = close, title = "Source")
//
HUBFLength = input.int(defval = 11, title = "Length")
//
HUBFMult = input.float(defval = 2.0, title = "Levels Multiplier")
//
// END Inputs ....
//
//
// START Functions ....
//
//
// END Functions ....
//
//
// START Calculations ....
//
//
var bullishColor = color.lime
var bullishColor1 = color.new(bullishColor, 50)
//
var bearishColor = color.red
var bearishColor1 = color.new(bearishColor, 50)
//
var HUBFShortLen = int(HUBFLength / 2)
var HUBFHullLen = int(math.sqrt(HUBFLength))
//
var HUBFDen1 = HUBFShortLen * (HUBFShortLen + 1) / 2
var HUBFDen2 = HUBFLength * (HUBFLength + 1) / 2
var HUBFDen3 = HUBFHullLen * (HUBFHullLen + 1) / 2
//
var HUBFLcwaCoeffs = array.new_float(HUBFHullLen, 0)
var HUBFHullCoeffs = array.new_float(0)
//
if barstate.isfirst
//
// Linearly combined WMA coeffs ...
for i = 0 to HUBFLength - 1
//
HUBFSum1 = math.max(HUBFShortLen - i, 0)
HUBFSum2 = HUBFLength - i
//
array.unshift(HUBFLcwaCoeffs, 2 * (HUBFSum1 / HUBFDen1) - (HUBFSum2 / HUBFDen2))
//
// Zero padding of linearly combined WMA coeffs ...
for i = 0 to HUBFHullLen - 2
array.unshift(HUBFLcwaCoeffs, 0)
//
// WMA convolution of linearly combined WMA coeffs ...
for i = HUBFHullLen to array.size(HUBFLcwaCoeffs) - 1
//
HUBFSum3 = 0.0
for j = i - HUBFHullLen to i - 1
HUBFSum3 += array.get(HUBFLcwaCoeffs, j) * (i - j)
//
array.unshift(HUBFHullCoeffs, HUBFSum3 / HUBFDen3)
//
var HUBFOs = 0
var HUBFLen = array.size(HUBFHullCoeffs) - 1
//
HUBFHma = 0.0
HUBFInvHma = 0.0
//
for i = 0 to HUBFLen
HUBFHma += HUBFSrc[i] * array.get(HUBFHullCoeffs, i)
HUBFInvHma += HUBFSrc[HUBFLen - i] * array.get(HUBFHullCoeffs, i)
//
HUBFHso = HUBFHma - HUBFInvHma
//
HUBFCmean = ta.cum(math.abs(HUBFHso)) / bar_index * HUBFMult
//
HUBFOs := ta.cross(HUBFHso, HUBFCmean) or ta.cross(HUBFHso, - HUBFCmean) ? 0 : HUBFHso < HUBFHso[1] and HUBFHso > HUBFCmean ? -1 : HUBFHso > HUBFHso[1] and HUBFHso < -HUBFCmean ? 1 : HUBFOs
//
HUBFCss0 = color.from_gradient(HUBFHso, 0, HUBFCmean, bullishColor1, bullishColor)
HUBFCss1 = color.from_gradient(HUBFHso, - HUBFCmean, 0, bearishColor, bearishColor1)
HUBFCss = HUBFHso > 0 ? HUBFCss0 : HUBFCss1
//
// END Calculations ....
//
//
// START Plots ....
//
//
// Oscillator line/histogram ...
plot(series = HUBFHso, title = "Hull Butterfly", color = HUBFCss, style = plot.style_histogram)
plot(series = HUBFHso, title = "Hull Butterfly", color = chart.fg_color)
//
// Bullish Dots ...
plot(series = HUBFOs > HUBFOs[1] and HUBFOs == 1 ? HUBFHso : na, title = "Bullish Dot", color = bullishColor, linewidth = 2, style = plot.style_columns)
//
// Bearish Dots ...
plot(series = HUBFOs < HUBFOs[1] and HUBFOs == -1 ? HUBFHso : na, title = "Bearish Dot", color = bearishColor, linewidth = 2, style = plot.style_columns)
//
// Levels ...
plot(series = HUBFCmean, color = color.gray, editable = false)
plot(series = HUBFCmean / 2, color = color.gray, editable = false)
plot(series = -HUBFCmean / 2, color = color.gray, editable = false)
plot(series = -HUBFCmean, color = color.gray, editable = false)
//
// END Plots ....
//
//
// START Alerts ...
//
//
// END Alerts ...
//
//////////////////////////////////////////////////////
// TODOS:
// -------
// - []
//////////////////////////////////////////////////////