xmd79

Chaos Theory

Dec 4th, 2022
289
0
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.50 KB | None | 0 0
  1. //@version=5
  2. indicator('Chaos Theory', overlay=true)
  3. show_Baseline = input(title='Ultimate MA', defval=true)
  4. show_Baseline1 = input(title='Ultimate MA', defval=true)
  5. show_Baseline2 = input(title='Ultimate MA', defval=true)
  6.  
  7.  
  8. ////BASELINE / SSL1 / SSL2 / EXIT MOVING AVERAGE VALUES
  9. maType = input.string(title='MA Source', defval='Chaos MA', options=['Chaos MA', 'SMA', 'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'JMA', 'EDSMA'])
  10. len = input(title='Lookback', defval=60)
  11.  
  12.  
  13. maType1 = input.string(title='MA Source', defval='Chaos MA', options=['Chaos MA', 'SMA', 'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'JMA', 'EDSMA'])
  14. len1 = input(title='Lookback', defval=100)
  15.  
  16.  
  17. maType2 = input.string(title='MA Source', defval='Chaos MA', options=['Chaos MA', 'SMA', 'EMA', 'DEMA', 'TEMA', 'LSMA', 'WMA', 'MF', 'VAMA', 'TMA', 'JMA', 'EDSMA'])
  18. len2 = input(title='Lookback', defval=200)
  19.  
  20. //
  21. SSL3Type = input.string(title='Signal Algo', defval='Chaos MA', options=['Chaos MA', 'DEMA', 'TEMA', 'LSMA', 'VAMA', 'TMA', 'JMA', 'MF'])
  22. len3 = input(title='Signal Period', defval=15)
  23. src = input(title='Signal Source', defval=close)
  24.  
  25. //
  26. tema(src, len) =>
  27. ema1 = ta.ema(src, len)
  28. ema2 = ta.ema(ema1, len)
  29. ema3 = ta.ema(ema2, len)
  30. 3 * ema1 - 3 * ema2 + ema3
  31. kidiv = input.int(defval=1, maxval=4, title='Kijun Sen Divider')
  32.  
  33. jurik_phase = input(title='JMA Phase', defval=3)
  34. jurik_power = input(title='JMA Power', defval=1)
  35. volatility_lookback = input(10, title='Volatility Adjusted Lookback Period')
  36. //MF
  37. beta = input.float(0.8, minval=0, maxval=1, step=0.1, title='Modular Filter')
  38. feedback = input(true, title='Modular Filter Feedback')
  39. z = input.float(0.5, title='Modular Filter Feedback Weighting', step=0.1, minval=0, maxval=1)
  40. //EDSMA
  41. ssfLength = input.int(title='EDSMA Filter Length', minval=1, defval=20)
  42. ssfPoles = input.int(title='EDSMA Filter Poles', defval=2, options=[2, 3])
  43.  
  44. //----
  45.  
  46. //EDSMA
  47. get2PoleSSF(src, length) =>
  48. PI = 2 * math.asin(1)
  49. arg = math.sqrt(2) * PI / length
  50. a1 = math.exp(-arg)
  51. b1 = 2 * a1 * math.cos(arg)
  52. c2 = b1
  53. c3 = -math.pow(a1, 2)
  54. c1 = 1 - c2 - c3
  55.  
  56. ssf = 0.0
  57. ssf := c1 * src + c2 * nz(ssf[1]) + c3 * nz(ssf[2])
  58. ssf
  59.  
  60. get3PoleSSF(src, length) =>
  61. PI = 2 * math.asin(1)
  62.  
  63. arg = PI / length
  64. a1 = math.exp(-arg)
  65. b1 = 2 * a1 * math.cos(1.738 * arg)
  66. c1 = math.pow(a1, 2)
  67.  
  68. coef2 = b1 + c1
  69. coef3 = -(c1 + b1 * c1)
  70. coef4 = math.pow(c1, 2)
  71. coef1 = 1 - coef2 - coef3 - coef4
  72.  
  73. ssf = 0.0
  74. ssf := coef1 * src + coef2 * nz(ssf[1]) + coef3 * nz(ssf[2]) + coef4 * nz(ssf[3])
  75. ssf
  76.  
  77. ma(type, src, len) =>
  78. float result = 0
  79. if type == 'TMA'
  80. result := ta.sma(ta.sma(src, math.ceil(len / 2)), math.floor(len / 2) + 1)
  81. result
  82. if type == 'MF'
  83. ts = 0.
  84. b = 0.
  85. c = 0.
  86. os = 0.
  87. //----
  88. alpha = 2 / (len + 1)
  89. a = feedback ? z * src + (1 - z) * nz(ts[1], src) : src
  90. //----
  91. b := a > alpha * a + (1 - alpha) * nz(b[1], a) ? a : alpha * a + (1 - alpha) * nz(b[1], a)
  92. c := a < alpha * a + (1 - alpha) * nz(c[1], a) ? a : alpha * a + (1 - alpha) * nz(c[1], a)
  93. os := a == b ? 1 : a == c ? 0 : os[1]
  94. //----
  95. upper = beta * b + (1 - beta) * c
  96. lower = beta * c + (1 - beta) * b
  97. ts := os * upper + (1 - os) * lower
  98. result := ts
  99. result
  100. if type == 'LSMA'
  101. result := ta.linreg(src, len, 0)
  102. result
  103. if type == 'SMA' // Simple
  104. result := ta.sma(src, len)
  105. result
  106. if type == 'EMA' // Exponential
  107. result := ta.ema(src, len)
  108. result
  109. if type == 'DEMA' // Double Exponential
  110. e = ta.ema(src, len)
  111. result := 2 * e - ta.ema(e, len)
  112. result
  113. if type == 'TEMA' // Triple Exponential
  114. e = ta.ema(src, len)
  115. result := 3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len)
  116. result
  117. if type == 'WMA' // Weighted
  118. result := ta.wma(src, len)
  119. result
  120. if type == 'VAMA' // Volatility Adjusted
  121. /// Copyright © 2019 to present, Joris Duyck (JD)
  122. mid = ta.ema(src, len)
  123. dev = src - mid
  124. vol_up = ta.highest(dev, volatility_lookback)
  125. vol_down = ta.lowest(dev, volatility_lookback)
  126. result := mid + math.avg(vol_up, vol_down)
  127. result
  128. if type == 'Chaos MA' // Hull
  129. result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len)))
  130. result
  131. if type == 'JMA' // Jurik
  132. /// Copyright © 2018 Alex Orekhov (everget)
  133. /// Copyright © 2017 Jurik Research and Consulting.
  134. phaseRatio = jurik_phase < -100 ? 0.5 : jurik_phase > 100 ? 2.5 : jurik_phase / 100 + 1.5
  135. beta = 0.45 * (len - 1) / (0.45 * (len - 1) + 2)
  136. alpha = math.pow(beta, jurik_power)
  137. jma = 0.0
  138. e0 = 0.0
  139. e0 := (1 - alpha) * src + alpha * nz(e0[1])
  140. e1 = 0.0
  141. e1 := (src - e0) * (1 - beta) + beta * nz(e1[1])
  142. e2 = 0.0
  143. e2 := (e0 + phaseRatio * e1 - nz(jma[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(e2[1])
  144. jma := e2 + nz(jma[1])
  145. result := jma
  146. result
  147. if type == 'Kijun v2'
  148. kijun = math.avg(ta.lowest(len), ta.highest(len)) //, (open + close)/2)
  149. conversionLine = math.avg(ta.lowest(len / kidiv), ta.highest(len / kidiv))
  150. delta = (kijun + conversionLine) / 2
  151. result := delta
  152. result
  153. if type == 'McGinley'
  154. mg = 0.0
  155. mg := na(mg[1]) ? ta.ema(src, len) : mg[1] + (src - mg[1]) / (len * math.pow(src / mg[1], 4))
  156. result := mg
  157. result
  158. if type == 'EDSMA'
  159.  
  160. zeros = src - nz(src[2])
  161. avgZeros = (zeros + zeros[1]) / 2
  162.  
  163. // Ehlers Super Smoother Filter
  164. ssf = ssfPoles == 2 ? get2PoleSSF(avgZeros, ssfLength) : get3PoleSSF(avgZeros, ssfLength)
  165.  
  166. // Rescale filter in terms of Standard Deviations
  167. stdev = ta.stdev(ssf, len)
  168. scaledFilter = stdev != 0 ? ssf / stdev : 0
  169.  
  170. alpha = 5 * math.abs(scaledFilter) / len
  171.  
  172. edsma = 0.0
  173. edsma := alpha * src + (1 - alpha) * nz(edsma[1])
  174. result := edsma
  175. result
  176. result
  177.  
  178. ///SSL 1 and SSL2
  179. emaHigh = ma(maType, high, len)
  180. emaLow = ma(maType, low, len)
  181.  
  182.  
  183. emaHigh1 = ma(maType1, high, len1)
  184. emaLow1 = ma(maType1, low, len1)
  185.  
  186. emaHigh2 = ma(maType1, high, len2)
  187. emaLow2 = ma(maType1, low, len2)
  188.  
  189.  
  190. ///EXIT
  191. ExitHigh = ma(SSL3Type, high, len3)
  192. ExitLow = ma(SSL3Type, low, len3)
  193.  
  194. ///Keltner Baseline Channel
  195. BBMC = ma(maType, close, len)
  196. Range = input(true)
  197. multy = input.float(0.2, step=0.05, title='Base Channel Multiplier')
  198. Keltma = ma(maType, src, len)
  199. range_1 = Range ? ta.tr : high - low
  200. rangema = ta.ema(range_1, len)
  201. upperk = Keltma + rangema * multy
  202. lowerk = Keltma - rangema * multy
  203.  
  204. BBMC1 = ma(maType1, close, len1)
  205. Range1 = input(true)
  206. multy1 = input.float(0.2, step=0.05, title='Base Channel Multiplier')
  207. Keltma1 = ma(maType1, src, len1)
  208. range_11 = Range1 ? ta.tr : high - low
  209. rangema1 = ta.ema(range_11, len1)
  210. upperk1 = Keltma1 + rangema1 * multy1
  211. lowerk1 = Keltma1 - rangema1 * multy1
  212.  
  213. BBMC2 = ma(maType1, close, len2)
  214.  
  215.  
  216. Range2 = input(true)
  217. multy2 = input.float(0.2, step=0.05, title='Base Channel Multiplier')
  218. Keltma2 = ma(maType2, src, len2)
  219. range_12 = Range2 ? ta.tr : high - low
  220. rangema2 = ta.ema(range_12, len)
  221. upperk2 = Keltma2 + rangema2 * multy2
  222. lowerk2 = Keltma2 - rangema2 * multy2
  223.  
  224. //Baseline Violation Candle
  225. open_pos = open * 1
  226. close_pos = close * 1
  227. difference = math.abs(close_pos - open_pos)
  228.  
  229.  
  230. //SSL1 VALUES
  231. Hlv = int(na)
  232. Hlv := close > emaHigh ? 1 : close < emaLow ? -1 : Hlv[1]
  233. sslDown = Hlv < 0 ? emaHigh : emaLow
  234.  
  235.  
  236. Hlv1 = int(na)
  237. Hlv1 := close > emaHigh1 ? 1 : close < emaLow1 ? -1 : Hlv1[1]
  238. sslDown1 = Hlv < 0 ? emaHigh1 : emaLow1
  239.  
  240.  
  241. Hlv2 = int(na)
  242. Hlv2 := close > emaHigh2 ? 1 : close < emaLow2 ? -1 : Hlv2[1]
  243. sslDown2 = Hlv < 0 ? emaHigh2 : emaLow2
  244.  
  245. //EXIT VALUES
  246. Hlv3 = int(na)
  247. Hlv3 := close > ExitHigh ? 1 : close < ExitLow ? -1 : Hlv3[1]
  248. sslExit = Hlv3 < 0 ? ExitHigh : ExitLow
  249. base_cross_Long = ta.crossover(close, sslExit)
  250. base_cross_Short = ta.crossover(sslExit, close)
  251. codiff = base_cross_Long ? 1 : base_cross_Short ? -1 : na
  252.  
  253. //COLORS
  254. color_bar = close > upperk ? #00FF00 : close < lowerk ? #FF0000 : color.gray
  255.  
  256. color_bar1 = close > upperk1 ? #00FF00 : close < lowerk1 ? #FF0000 : color.gray
  257.  
  258. color_bar2 = close > upperk2 ? #00FF00 : close < lowerk2 ? #FF0000 : color.gray
  259. //PLOTS
  260. plotarrow(codiff, colorup=color.new(#00FF00, 20), colordown=color.new(#FF0000, 20), title='Signals', maxheight=20, offset=0)
  261. p1 = plot(show_Baseline ? BBMC : na, color=color_bar, linewidth=1, title='Chaos MA', transp=0)
  262.  
  263. p2 = plot(show_Baseline1 ? BBMC1 : na, color=color_bar1, linewidth=1, title='Chaos MA', transp=0)
  264.  
  265.  
  266. p3 = plot(show_Baseline2 ? BBMC2 : na, color=color_bar2, linewidth=1, title='Chaos MA', transp=0)
  267.  
  268. //ALERTS
  269. alertcondition(ta.crossover(close, sslDown), title='SSL Cross Alert', message='SSL1 has crossed.')
  270. alertcondition(ta.crossover(close, sslExit), title='Exit Sell', message='Exit Sell Alert.')
  271. alertcondition(ta.crossover(sslExit, close), title='Exit Buy', message='Exit Buy Alert.')
  272. alertcondition(ta.crossover(close, upperk), title='Baseline Buy Entry', message='Base Buy Alert.')
  273. alertcondition(ta.crossover(lowerk, close), title='Baseline Sell Entry', message='Base Sell Alert.')
  274.  
  275.  
  276.  
  277.  
  278. ////
  279.  
  280.  
  281.  
  282.  
  283.  
  284. // Inputs
  285. 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.")
  286.  
  287. 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. " +
  288. "This will usually be between 1 and 3.")
  289. 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. " +
  290. "For this band, 'high' and 'close' (default) are generally the most appropriate values.")
  291.  
  292. 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. " +
  293. "This will usually be between 1 and 3.")
  294. 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. " +
  295. "For this band, 'low' and 'close' (default) are generally the most appropriate values.")
  296.  
  297. // ATR
  298. atr = ta.atr(atrPeriod)
  299.  
  300. // Plotting
  301. plot(srcUpper + atr * atrMultiplierUpper, title="Shorts Stop Loss", color=color.rgb(100, 100, 100, 60), linewidth=1, style=plot.style_stepline)
  302. plot(srcLower - atr * atrMultiplierLower, title="Longs Stop Loss", color=color.rgb(100, 100, 100, 60), linewidth=1, style=plot.style_stepline)
  303.  
  304.  
  305. //////
  306.  
  307. line_width = 1
  308. sr_tf = input.timeframe('', title='S/R Timeframe')
  309. //Legacy RSI calc
  310. rsi_src = close
  311. len7 = 9
  312. up1 = ta.rma(math.max(ta.change(rsi_src), 0), len7)
  313. down1 = ta.rma(-math.min(ta.change(rsi_src), 0), len7)
  314. legacy_rsi = down1 == 0 ? 100 : up1 == 0 ? 0 : 100 - 100 / (1 + up1 / down1)
  315. //CMO based on HMA
  316. length12 = 1
  317. src12 = ta.hma(open, 5)[1] // legacy hma(5) calculation gives a resul with one candle shift, thus use hma()[1]
  318. src22 = ta.hma(close, 12)
  319. momm1 = ta.change(src12)
  320. momm2 = ta.change(src22)
  321. f1(m, n) =>
  322. m >= n ? m : 0.0
  323. f2(m, n) =>
  324. m >= n ? 0.0 : -m
  325. m1 = f1(momm1, momm2)
  326. m2 = f2(momm1, momm2)
  327. sm1 = math.sum(m1, length12)
  328. sm2 = math.sum(m2, length12)
  329. percent(nom, div) =>
  330. 100 * nom / div
  331. cmo_new = percent(sm1 - sm2, sm1 + sm2)
  332. //Legacy Close Pivots calcs.
  333. len5 = 2
  334. h = ta.highest(len5)
  335. h1 = ta.dev(h, len5) ? na : h
  336. hpivot = fixnan(h1)
  337. l4 = ta.lowest(len5)
  338. l1 = ta.dev(l4, len5) ? na : l4
  339. lpivot = fixnan(l1)
  340. //Calc Values
  341. rsi_new = ta.rsi(close, 9)
  342. lpivot_new = lpivot
  343. hpivot_new = hpivot
  344. sup = rsi_new < 25 and cmo_new > 50 and lpivot_new
  345. res = rsi_new > 75 and cmo_new < -50 and hpivot_new
  346. calcXup() =>
  347. var xup = 0.0
  348. xup := sup ? low : xup[1]
  349. xup
  350. calcXdown() =>
  351. var xdown = 0.0
  352. xdown := res ? high : xdown[1]
  353. xdown
  354. //Lines drawing variables
  355. tf1 = request.security(syminfo.tickerid, sr_tf, calcXup(), lookahead=barmerge.lookahead_on)
  356. tf2 = request.security(syminfo.tickerid, sr_tf, calcXdown(), lookahead=barmerge.lookahead_on)
  357. //SR Line plotting
  358. var tf1_line = line.new(0, 0, 0, 0)
  359. var tf1_bi_start = 0
  360. var tf1_bi_end = 0
  361. tf1_bi_start := ta.change(tf1) ? bar_index : tf1_bi_start[1]
  362. tf1_bi_end := ta.change(tf1) ? tf1_bi_start : bar_index
  363. if ta.change(tf1)
  364. tf1_line := line.new(tf1_bi_start, tf1, tf1_bi_end, tf1, color=color.yellow, width=line_width)
  365. tf1_line
  366. line.set_x2(tf1_line, tf1_bi_end)
  367. var tf2_line = line.new(0, 0, 0, 0)
  368. var tf2_bi_start = 0
  369. var tf2_bi_end = 0
  370. tf2_bi_start := ta.change(tf2) ? bar_index : tf2_bi_start[1]
  371. tf2_bi_end := ta.change(tf2) ? tf2_bi_start : bar_index
  372. if ta.change(tf2)
  373. tf2_line := line.new(tf2_bi_start, tf2, tf2_bi_end, tf2, color=color.yellow, width=line_width)
  374. tf2_line
  375. line.set_x2(tf2_line, tf2_bi_end)
  376. alertcondition(ta.change(tf1) != 0 or ta.change(tf2) != 0 ,title="New S/R line", message = "New S/R line" )
Advertisement
Comments
  • # text 0.12 KB | 0 0
    1. download all types of premium tradingview indicators codes available on telegram - https://t.me/tradingview_premium_indicator
Add Comment
Please, Sign In to add comment