Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@version=5
- indicator('Chaos Theory', overlay=true)
- show_Baseline = input(title='Ultimate MA', defval=true)
- show_Baseline1 = input(title='Ultimate MA', defval=true)
- show_Baseline2 = input(title='Ultimate MA', defval=true)
- ////BASELINE / SSL1 / SSL2 / EXIT MOVING AVERAGE VALUES
- maType = input.string(title='MA Source', defval='Chaos MA', options=['Chaos MA', 'SMA', 'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'JMA', 'EDSMA'])
- len = input(title='Lookback', defval=60)
- maType1 = input.string(title='MA Source', defval='Chaos MA', options=['Chaos MA', 'SMA', 'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'JMA', 'EDSMA'])
- len1 = input(title='Lookback', defval=100)
- maType2 = input.string(title='MA Source', defval='Chaos MA', options=['Chaos MA', 'SMA', 'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'JMA', 'EDSMA'])
- len2 = input(title='Lookback', defval=200)
- //
- SSL3Type = input.string(title='Signal Algo', defval='Chaos MA', options=['Chaos MA', 'DEMA', 'TEMA', 'LSMA', 'VAMA', 'TMA', 'JMA', 'MF'])
- len3 = input(title='Signal Period', defval=15)
- src = input(title='Signal Source', defval=close)
- //
- tema(src, len) =>
- ema1 = ta.ema(src, len)
- ema2 = ta.ema(ema1, len)
- ema3 = ta.ema(ema2, len)
- 3 * ema1 - 3 * ema2 + ema3
- kidiv = input.int(defval=1, maxval=4, title='Kijun Sen Divider')
- jurik_phase = input(title='JMA Phase', defval=3)
- jurik_power = input(title='JMA Power', defval=1)
- volatility_lookback = input(10, title='Volatility Adjusted Lookback Period')
- //MF
- beta = input.float(0.8, minval=0, maxval=1, step=0.1, title='Modular Filter')
- feedback = input(true, title='Modular Filter Feedback')
- z = input.float(0.5, title='Modular Filter Feedback Weighting', step=0.1, minval=0, maxval=1)
- //EDSMA
- ssfLength = input.int(title='EDSMA Filter Length', minval=1, defval=20)
- ssfPoles = input.int(title='EDSMA Filter Poles', defval=2, options=[2, 3])
- //----
- //EDSMA
- get2PoleSSF(src, length) =>
- PI = 2 * math.asin(1)
- arg = math.sqrt(2) * PI / length
- a1 = math.exp(-arg)
- b1 = 2 * a1 * math.cos(arg)
- c2 = b1
- c3 = -math.pow(a1, 2)
- c1 = 1 - c2 - c3
- ssf = 0.0
- ssf := c1 * src + c2 * nz(ssf[1]) + c3 * nz(ssf[2])
- ssf
- get3PoleSSF(src, length) =>
- PI = 2 * math.asin(1)
- arg = PI / length
- a1 = math.exp(-arg)
- b1 = 2 * a1 * math.cos(1.738 * arg)
- c1 = math.pow(a1, 2)
- coef2 = b1 + c1
- coef3 = -(c1 + b1 * c1)
- coef4 = math.pow(c1, 2)
- coef1 = 1 - coef2 - coef3 - coef4
- ssf = 0.0
- ssf := coef1 * src + coef2 * nz(ssf[1]) + coef3 * nz(ssf[2]) + coef4 * nz(ssf[3])
- ssf
- ma(type, src, len) =>
- float result = 0
- if type == 'TMA'
- result := ta.sma(ta.sma(src, math.ceil(len / 2)), math.floor(len / 2) + 1)
- result
- if type == 'MF'
- ts = 0.
- b = 0.
- c = 0.
- os = 0.
- //----
- alpha = 2 / (len + 1)
- a = feedback ? z * src + (1 - z) * nz(ts[1], src) : src
- //----
- b := a > alpha * a + (1 - alpha) * nz(b[1], a) ? a : alpha * a + (1 - alpha) * nz(b[1], a)
- c := a < alpha * a + (1 - alpha) * nz(c[1], a) ? a : alpha * a + (1 - alpha) * nz(c[1], a)
- os := a == b ? 1 : a == c ? 0 : os[1]
- //----
- upper = beta * b + (1 - beta) * c
- lower = beta * c + (1 - beta) * b
- ts := os * upper + (1 - os) * lower
- result := ts
- result
- if type == 'LSMA'
- result := ta.linreg(src, len, 0)
- result
- if type == 'SMA' // Simple
- result := ta.sma(src, len)
- result
- if type == 'EMA' // Exponential
- result := ta.ema(src, len)
- result
- if type == 'DEMA' // Double Exponential
- e = ta.ema(src, len)
- result := 2 * e - ta.ema(e, len)
- result
- if type == 'TEMA' // Triple Exponential
- e = ta.ema(src, len)
- result := 3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
- result
- if type == 'WMA' // Weighted
- result := ta.wma(src, len)
- result
- if type == 'VAMA' // Volatility Adjusted
- /// Copyright © 2019 to present, Joris Duyck (JD)
- mid = ta.ema(src, len)
- dev = src - mid
- vol_up = ta.highest(dev, volatility_lookback)
- vol_down = ta.lowest(dev, volatility_lookback)
- result := mid + math.avg(vol_up, vol_down)
- result
- if type == 'Chaos MA' // Hull
- result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len)))
- result
- if type == 'JMA' // Jurik
- /// Copyright © 2018 Alex Orekhov (everget)
- /// Copyright © 2017 Jurik Research and Consulting.
- phaseRatio = jurik_phase < -100 ? 0.5 : jurik_phase > 100 ? 2.5 : jurik_phase / 100 + 1.5
- beta = 0.45 * (len - 1) / (0.45 * (len - 1) + 2)
- alpha = math.pow(beta, jurik_power)
- jma = 0.0
- e0 = 0.0
- e0 := (1 - alpha) * src + alpha * nz(e0[1])
- e1 = 0.0
- e1 := (src - e0) * (1 - beta) + beta * nz(e1[1])
- e2 = 0.0
- e2 := (e0 + phaseRatio * e1 - nz(jma[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(e2[1])
- jma := e2 + nz(jma[1])
- result := jma
- result
- if type == 'Kijun v2'
- kijun = math.avg(ta.lowest(len), ta.highest(len)) //, (open + close)/2)
- conversionLine = math.avg(ta.lowest(len / kidiv), ta.highest(len / kidiv))
- delta = (kijun + conversionLine) / 2
- result := delta
- result
- if type == 'McGinley'
- mg = 0.0
- mg := na(mg[1]) ? ta.ema(src, len) : mg[1] + (src - mg[1]) / (len * math.pow(src / mg[1], 4))
- result := mg
- result
- if type == 'EDSMA'
- zeros = src - nz(src[2])
- avgZeros = (zeros + zeros[1]) / 2
- // Ehlers Super Smoother Filter
- ssf = ssfPoles == 2 ? get2PoleSSF(avgZeros, ssfLength) : get3PoleSSF(avgZeros, ssfLength)
- // Rescale filter in terms of Standard Deviations
- stdev = ta.stdev(ssf, len)
- scaledFilter = stdev != 0 ? ssf / stdev : 0
- alpha = 5 * math.abs(scaledFilter) / len
- edsma = 0.0
- edsma := alpha * src + (1 - alpha) * nz(edsma[1])
- result := edsma
- result
- result
- ///SSL 1 and SSL2
- emaHigh = ma(maType, high, len)
- emaLow = ma(maType, low, len)
- emaHigh1 = ma(maType1, high, len1)
- emaLow1 = ma(maType1, low, len1)
- emaHigh2 = ma(maType1, high, len2)
- emaLow2 = ma(maType1, low, len2)
- ///EXIT
- ExitHigh = ma(SSL3Type, high, len3)
- ExitLow = ma(SSL3Type, low, len3)
- ///Keltner Baseline Channel
- BBMC = ma(maType, close, len)
- Range = input(true)
- multy = input.float(0.2, step=0.05, title='Base Channel Multiplier')
- Keltma = ma(maType, src, len)
- range_1 = Range ? ta.tr : high - low
- rangema = ta.ema(range_1, len)
- upperk = Keltma + rangema * multy
- lowerk = Keltma - rangema * multy
- BBMC1 = ma(maType1, close, len1)
- Range1 = input(true)
- multy1 = input.float(0.2, step=0.05, title='Base Channel Multiplier')
- Keltma1 = ma(maType1, src, len1)
- range_11 = Range1 ? ta.tr : high - low
- rangema1 = ta.ema(range_11, len1)
- upperk1 = Keltma1 + rangema1 * multy1
- lowerk1 = Keltma1 - rangema1 * multy1
- BBMC2 = ma(maType1, close, len2)
- Range2 = input(true)
- multy2 = input.float(0.2, step=0.05, title='Base Channel Multiplier')
- Keltma2 = ma(maType2, src, len2)
- range_12 = Range2 ? ta.tr : high - low
- rangema2 = ta.ema(range_12, len)
- upperk2 = Keltma2 + rangema2 * multy2
- lowerk2 = Keltma2 - rangema2 * multy2
- //Baseline Violation Candle
- open_pos = open * 1
- close_pos = close * 1
- difference = math.abs(close_pos - open_pos)
- //SSL1 VALUES
- Hlv = int(na)
- Hlv := close > emaHigh ? 1 : close < emaLow ? -1 : Hlv[1]
- sslDown = Hlv < 0 ? emaHigh : emaLow
- Hlv1 = int(na)
- Hlv1 := close > emaHigh1 ? 1 : close < emaLow1 ? -1 : Hlv1[1]
- sslDown1 = Hlv < 0 ? emaHigh1 : emaLow1
- Hlv2 = int(na)
- Hlv2 := close > emaHigh2 ? 1 : close < emaLow2 ? -1 : Hlv2[1]
- sslDown2 = Hlv < 0 ? emaHigh2 : emaLow2
- //EXIT VALUES
- Hlv3 = int(na)
- Hlv3 := close > ExitHigh ? 1 : close < ExitLow ? -1 : Hlv3[1]
- sslExit = Hlv3 < 0 ? ExitHigh : ExitLow
- base_cross_Long = ta.crossover(close, sslExit)
- base_cross_Short = ta.crossover(sslExit, close)
- codiff = base_cross_Long ? 1 : base_cross_Short ? -1 : na
- //COLORS
- color_bar = close > upperk ? #00FF00 : close < lowerk ? #FF0000 : color.gray
- color_bar1 = close > upperk1 ? #00FF00 : close < lowerk1 ? #FF0000 : color.gray
- color_bar2 = close > upperk2 ? #00FF00 : close < lowerk2 ? #FF0000 : color.gray
- //PLOTS
- plotarrow(codiff, colorup=color.new(#00FF00, 20), colordown=color.new(#FF0000, 20), title='Signals', maxheight=20, offset=0)
- p1 = plot(show_Baseline ? BBMC : na, color=color_bar, linewidth=1, title='Chaos MA', transp=0)
- p2 = plot(show_Baseline1 ? BBMC1 : na, color=color_bar1, linewidth=1, title='Chaos MA', transp=0)
- p3 = plot(show_Baseline2 ? BBMC2 : na, color=color_bar2, linewidth=1, title='Chaos MA', transp=0)
- //ALERTS
- alertcondition(ta.crossover(close, sslDown), title='SSL Cross Alert', message='SSL1 has crossed.')
- alertcondition(ta.crossover(close, sslExit), title='Exit Sell', message='Exit Sell Alert.')
- alertcondition(ta.crossover(sslExit, close), title='Exit Buy', message='Exit Buy Alert.')
- alertcondition(ta.crossover(close, upperk), title='Baseline Buy Entry', message='Base Buy Alert.')
- alertcondition(ta.crossover(lowerk, close), title='Baseline Sell Entry', message='Base Sell Alert.')
- ////
- // Inputs
- atrPeriod = input.int(title='ATR Period', defval=3, minval=1, group="ATR Bands Global Settings", tooltip="This setting is used in the raw ATR value calculation. Most often this is set at either 14 (default) or 21.")
- atrMultiplierUpper = input.float(title='ATR Upper Band Scale Factor', defval=1, step=0.1, minval=0.01, group="ATR Upper Band Settings", tooltip="Scaling factor (aka multiplier) for the ATR to use for plotting the ATR bands. " +
- "This will usually be between 1 and 3.")
- srcUpper = input.source(title='ATR Upper Offset Source', defval=close, group="ATR Upper Band Settings", tooltip="This setting determines the offset point for ATR bands. " +
- "For this band, 'high' and 'close' (default) are generally the most appropriate values.")
- atrMultiplierLower = input.float(title='ATR Lower Band Scale Factor', defval=1, step=0.1, minval=0.01, group="ATR Lower Band Settings", tooltip="Scaling factor (aka multiplier) for the ATR to use for plotting the ATR bands. " +
- "This will usually be between 1 and 3.")
- srcLower = input.source(title='ATR Lower Offset Source', defval=close, group="ATR Lower Band Settings", tooltip="This setting determines the offset point for ATR bands. " +
- "For this band, 'low' and 'close' (default) are generally the most appropriate values.")
- // ATR
- atr = ta.atr(atrPeriod)
- // Plotting
- plot(srcUpper + atr * atrMultiplierUpper, title="Shorts Stop Loss", color=color.rgb(100, 100, 100, 60), linewidth=1, style=plot.style_stepline)
- plot(srcLower - atr * atrMultiplierLower, title="Longs Stop Loss", color=color.rgb(100, 100, 100, 60), linewidth=1, style=plot.style_stepline)
- //////
- line_width = 1
- sr_tf = input.timeframe('', title='S/R Timeframe')
- //Legacy RSI calc
- rsi_src = close
- len7 = 9
- up1 = ta.rma(math.max(ta.change(rsi_src), 0), len7)
- down1 = ta.rma(-math.min(ta.change(rsi_src), 0), len7)
- legacy_rsi = down1 == 0 ? 100 : up1 == 0 ? 0 : 100 - 100 / (1 + up1 / down1)
- //CMO based on HMA
- length12 = 1
- src12 = ta.hma(open, 5)[1] // legacy hma(5) calculation gives a resul with one candle shift, thus use hma()[1]
- src22 = ta.hma(close, 12)
- momm1 = ta.change(src12)
- momm2 = ta.change(src22)
- f1(m, n) =>
- m >= n ? m : 0.0
- f2(m, n) =>
- m >= n ? 0.0 : -m
- m1 = f1(momm1, momm2)
- m2 = f2(momm1, momm2)
- sm1 = math.sum(m1, length12)
- sm2 = math.sum(m2, length12)
- percent(nom, div) =>
- 100 * nom / div
- cmo_new = percent(sm1 - sm2, sm1 + sm2)
- //Legacy Close Pivots calcs.
- len5 = 2
- h = ta.highest(len5)
- h1 = ta.dev(h, len5) ? na : h
- hpivot = fixnan(h1)
- l4 = ta.lowest(len5)
- l1 = ta.dev(l4, len5) ? na : l4
- lpivot = fixnan(l1)
- //Calc Values
- rsi_new = ta.rsi(close, 9)
- lpivot_new = lpivot
- hpivot_new = hpivot
- sup = rsi_new < 25 and cmo_new > 50 and lpivot_new
- res = rsi_new > 75 and cmo_new < -50 and hpivot_new
- calcXup() =>
- var xup = 0.0
- xup := sup ? low : xup[1]
- xup
- calcXdown() =>
- var xdown = 0.0
- xdown := res ? high : xdown[1]
- xdown
- //Lines drawing variables
- tf1 = request.security(syminfo.tickerid, sr_tf, calcXup(), lookahead=barmerge.lookahead_on)
- tf2 = request.security(syminfo.tickerid, sr_tf, calcXdown(), lookahead=barmerge.lookahead_on)
- //SR Line plotting
- var tf1_line = line.new(0, 0, 0, 0)
- var tf1_bi_start = 0
- var tf1_bi_end = 0
- tf1_bi_start := ta.change(tf1) ? bar_index : tf1_bi_start[1]
- tf1_bi_end := ta.change(tf1) ? tf1_bi_start : bar_index
- if ta.change(tf1)
- tf1_line := line.new(tf1_bi_start, tf1, tf1_bi_end, tf1, color=color.yellow, width=line_width)
- tf1_line
- line.set_x2(tf1_line, tf1_bi_end)
- var tf2_line = line.new(0, 0, 0, 0)
- var tf2_bi_start = 0
- var tf2_bi_end = 0
- tf2_bi_start := ta.change(tf2) ? bar_index : tf2_bi_start[1]
- tf2_bi_end := ta.change(tf2) ? tf2_bi_start : bar_index
- if ta.change(tf2)
- tf2_line := line.new(tf2_bi_start, tf2, tf2_bi_end, tf2, color=color.yellow, width=line_width)
- tf2_line
- line.set_x2(tf2_line, tf2_bi_end)
- alertcondition(ta.change(tf1) != 0 or ta.change(tf2) != 0 ,title="New S/R line", message = "New S/R line" )
Advertisement