xmd79

Heiken Ashi Algo Oscillator

Jun 1st, 2023
782
2
Never
1
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.20 KB | None | 2 0
  1. //@version=5
  2. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  3. // Copyright (c) 2019-present, Eric Robinson (@CoffeeshopCrypto) https://www.tradingview.com/u/CoffeeshopCrypto/
  4. // CoffeeShopCrypto HARSI script may be freely distributed under the terms of the GPL-3.0 license.
  5. // This is an ongoing project that i will continue to develope so it can match more markets as an all in one.
  6. //Currently this is widely used for CRYPTO and FOREX markets.
  7.  
  8. indicator(title="Heiken Ashi Algo Oscillator", shorttitle="Heiken Ashi Algo v3", format=format.price, precision=2, explicit_plot_zorder = true)
  9.  
  10. //Plotting the HA Candles according to the RSI level
  11. //This is the new 2022 version of the HARSI Originally done by @JayRogers
  12. //Heiken Ashi
  13.  
  14.  
  15. // inputs for a later use
  16. //RSILengthInput = 14
  17. //RSILength2Input = 3.5 * 14
  18. //HAlengthInput = RSILengthInput
  19. //RSIMA2Lenth = RSILengthInput
  20.  
  21.  
  22. HAp1 = input(14, title = "Open Length", tooltip="Set each of these to the bar length for your trading style.", group="Heiken Ashi O-H-L-C Smoothing")
  23. HAp2 = input(14, title = "High Length", group="Heiken Ashi O-H-L-C Smoothing")
  24. HAp3 = input(14, title = "Low Length", group="Heiken Ashi O-H-L-C Smoothing")
  25. HAp4 = input(14, title = "Close Length", group="Heiken Ashi O-H-L-C Smoothing")
  26.  
  27. //_____________________________________________________
  28. //calcs
  29.  
  30. // 4 rsi calculations
  31. r1 = ta.rsi(close, HAp1)
  32. r2 = ta.rsi(close, HAp2)
  33. r3 = ta.rsi(close, HAp3)
  34. r4 = ta.rsi(close, HAp4)
  35.  
  36. //array, stores rsi values, sorts them
  37. heikinarray = array.new_float(0, 0)
  38. array.push(heikinarray, r1)
  39. array.push(heikinarray, r2)
  40. array.push(heikinarray, r3)
  41. array.push(heikinarray, r4)
  42. array.sort(heikinarray, order.descending)
  43.  
  44. // assign open, high, low, ,close
  45. h = array.get(heikinarray, 1)
  46. c = array.get(heikinarray, 3)
  47. o = array.get(heikinarray, 0)
  48. l = array.get(heikinarray, 2)
  49.  
  50. //_____________________________________________________
  51. // Heikin Ashi source function allows for different inputs
  52.  
  53. src(_o, _h, _l, _c, _src) =>
  54. Close = (_o+_h+_l+_c)/4
  55. Open = float(na)
  56. Open := na(Open[1]) ? (_o + _c) / 2 : (nz(Open[1]) + nz(Close[1])) / 2
  57. High = math.max(_h, math.max(Open, Close))
  58. Low = math.min(_l, math.min(Open, Close))
  59. HL2 = math.avg(High, Low)
  60. HLC3 = math.avg(High, Low, Close)
  61. OHLC4 = math.avg(Open, High, Low, Close)
  62. Price = _src == 'close' ? Close : _src == 'open' ? Open : _src == 'high' ? High : _src == 'low' ? Low : _src == 'hl2' ? HL2 : _src == 'hlc3' ? HLC3 : OHLC4
  63.  
  64. hi = src(o, h, l, c, 'high' ) - 50
  65. cl = src(o, h, l, c, 'close') - 50
  66. op = src(o, h, l, c, 'open' ) - 50
  67. lo = src(o, h, l, c, 'low' ) - 50
  68.  
  69. hi1 = src(op, hi, lo, cl, 'high' )
  70. cl1 = src(op, hi, lo, cl, 'close')
  71. op1 = src(op, hi, lo, cl, 'open' )
  72. lo1 = src(op, hi, lo, cl, 'low' )
  73.  
  74. //_____________________________________________________
  75.  
  76. barColor = cl1 > op1 ? color.new(#4caf50, 25) : color.new(#f77c80, 25) // series color
  77. wikColor = cl1 > op1 ? color.new(#4caf50, 25) : color.new(#f77c80, 25)
  78. brDrcolor = cl1 > op1 ? color.new(#131722, 25) : color.new(#131722, 25)
  79.  
  80. /////End Heiken Ashi Candles
  81.  
  82. //Lets get to working on the Slow Stochastic RSI
  83. smoothK = input.int(8, title="Stochastic K%", minval=1, group="Slow Stochastic Inputs")
  84. smoothD = input.int(3, title="Stochastic D%", minval=1, group="Slow Stochastic Inputs")
  85. lengthRSI = input.int(21, "Stochastic RSI Length", minval=1, group="Slow Stochastic Inputs")
  86. lengthStoch = input.int(50, "Stochastic Length", minval=1, group="Slow Stochastic Inputs")
  87. src = input(close, title="Stochastic RSI Source", group="Slow Stochastic Inputs")
  88. rsi1 = ta.rsi(src, lengthRSI)
  89. k = ta.sma(ta.stoch(rsi1, rsi1, rsi1, lengthStoch), smoothK) - 50
  90. d = ta.sma(k, smoothD)
  91. p1 = plot(k, title="Stochastic K%", color=color.new(#5de661, 100), editable=true, display=display.none - display.status_line)
  92. p2 = plot(d, title="Stochastic D%", color=color.new(#a12a2a, 100), editable=true, display=display.none - display.status_line)
  93. h0 = hline(80, "Upper Band", color=#787B86, editable=false, display=display.none)
  94. hline0 = hline(0, "Middle Band", color=color.new(#cc5b1f, 20))
  95. h1 = hline(20, "Lower Band", color=#787B86,editable=false, display=display.none)
  96. fill(h0, h1, color=color.rgb(33, 150, 243, 90), title="Background", editable=false, display=display.none)
  97. fill(title = "Slow Stochastic RSI Ribbon", plot1 = p1, plot2 = p2, color = k >= d ? color.new(#5de661, 15) : color.new(#a12a2a, 15), display=display.all)
  98.  
  99. ///////Lets get back to the rest of the plotting. OH THE PLOTTING HAS ME PLOTZED//
  100. //plot(rsiMA, "RSI-based MA", color=color.orange, linewidth=1, editable=false, style=plot.style_line, display=display.none)
  101.  
  102.  
  103. rsiUpperBandOB = hline(30, "RSI Overbought", color=#787B86, display=display.none)
  104. rsiUpperBandOS = hline(-30, "RSI OverSold", color=#787B86, display=display.none)
  105. rsiUpperBand = hline(20, "RSI Upper Band", color=#787B86, display=display.none)
  106. rsiLowerBand = hline(-20, "RSI Lower Band", color=#787B86, display=display.none)
  107. hMIDhi = hline(10, "Upper Mid Band", color=color.new(#00e677, 60), linestyle=hline.style_solid)
  108. hMIDlo = hline(-10, "Lower Mid Band", color=color.new(#a12a2a, 60), linestyle=hline.style_solid)
  109. fill(rsiUpperBand, rsiLowerBand, color=color.rgb(126, 87, 194, 90), title="RSI Background", display=display.none)
  110. fill(rsiUpperBandOB, rsiUpperBand, color=color.new(#a12a2a, 80), title="Overbought Background", display = display.none)
  111. fill(rsiUpperBandOS, rsiLowerBand, color=color.new(#00E676, 80), title="Oversold Background", display = display.none)
  112. fill(hMIDhi, hMIDlo, color=color.new(#3179f5, 94), title="Midrange Background", display = display.none)
  113.  
  114. fill(hline0, rsiUpperBandOB, 30, 0, top_color = color.rgb(21, 122, 34, 70), bottom_color = color.rgb(186, 189, 185, 100))
  115. fill(hline0, rsiUpperBandOS, 0, -30, top_color = color.rgb(186, 189, 185, 100), bottom_color = color.rgb(189, 60, 27, 70))
  116.  
  117. //-----------------------END RSI UPDATED-----------------------------
  118.  
  119. //Lets start with the secondary Stripped RSI with only the Moving averages.
  120. //------------------------------------------------------------------------------
  121. //RSI Stripped plotted
  122.  
  123. ma2(source, length, type) =>
  124. switch type
  125. "SMA" => ta.sma(source, length)
  126. "EMA" => ta.ema(source, length)
  127. "SMMA (RMA)" => ta.rma(source, length)
  128. "WMA" => ta.wma(source, length)
  129. "VWMA" => ta.vwma(source, length)
  130. "LSMA" => ta.linreg(source, length, offset=0)
  131. "VWAP" => ta.vwap(source)
  132.  
  133.  
  134. rsi2LengthInput = input.int(14, title="RSI Length", group="Relative Strength Input Settings")
  135. rsi2SourceInput = input(close, "RSI Source", group="Relative Strength Input Settings")
  136. ma2LengthInput = input.int(14, title="RSI MA Length", group="RSI MA Settings")
  137. ma2TypeInput = input.string("SMA", title="RSI MA Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "LSMA", "VWAP"], tooltip="Smooths the HA candles.", group="RSI MA Settings")
  138. ma3TypeInput = input.string("SMA", title="RSI Trend MA Type", options=["SMA", "EMA", "SMMA (RMA)", "WMA", "VWMA", "LSMA", "VWAP"], tooltip="Smooths the HA candles.", group="RSI MA Settings")
  139.  
  140.  
  141.  
  142. //Coloring the RSIma
  143. color rsi2MABull = input.color(color.new(#66bb6a, 0), title="Bull RSI MA", group="RSI MA Settings")
  144. color rsi2MABear = input.color(color.new(#eb4949, 0), title="Bear RSI MA", group="RSI MA Settings")
  145.  
  146. float RSIma2 = ta.sma(rsi2SourceInput, rsi2LengthInput)
  147.  
  148. //Coloring the RSIma2
  149. color rsi3MABull = input.color(color.new(#66bb6a, 0), title="Bull RSI Trend", group="RSI MA Settings")
  150. color rsi3MABear = input.color(color.new(#eb4949, 0), title="Bear RSI Trend", group="RSI MA Settings")
  151.  
  152. float RSIma3 = ta.sma(rsi2SourceInput, rsi2LengthInput)
  153. /// MA Colors End//
  154.  
  155. //twooffset=input(title="RSI MA Offset", defval=0, group="RSI MA Settings")
  156.  
  157. //Save the Rising Falling ma color status
  158. bool rsi2MARising = ta.rising(RSIma2, 1)
  159. bool rsi3MARising = ta.rising(RSIma2, 1)
  160.  
  161. twoup = ta.rma(math.max(ta.change(rsi2SourceInput), 0), rsi2LengthInput)
  162. twodown = ta.rma(-math.min(ta.change(rsi2SourceInput), 0), rsi2LengthInput)
  163. rsi2 = twodown == 0 ? 100 : twoup == 0 ? 0 : 100 - (100 / (1 + twoup / twodown)) - 50
  164. rsiMA2 = ma2(rsi2, ma2LengthInput, ma2TypeInput)
  165. rsiMA3 = ma2(rsi2, ma2LengthInput, ma3TypeInput) / 3.5
  166.  
  167. ///RSI Pivots//
  168. // Pivot Trendline Inputs
  169. pivot_high_low_trendline_lookback = input.int(7, title="RSI Trendline Lookback", group="Trendlines and Pivots")
  170.  
  171. // Pivot high/low calculation
  172. pivot_high = rsi2 == ta.highest(rsi2, pivot_high_low_trendline_lookback)
  173. pivot_low = rsi2 == ta.lowest(rsi2, pivot_high_low_trendline_lookback)
  174. /////End Saved MA Colors and status////
  175.  
  176. //////////////Secondary RSI MA////////////////////////////
  177.  
  178. //BUY and SELL indications
  179. //Buy sell Strategy
  180. // Buy sell Plots
  181.  
  182.  
  183. //Buy Sell Alerts
  184.  
  185.  
  186. ///Setting Colors and limitations for the RSI Trend Cloud with YELLOW rsi trend
  187. float maxLine = -10
  188. float minLine = 10
  189. bool rsiBullish = rsi2 > rsiMA2
  190. color topColor = rsiBullish ? color.new(#4b0000, 0) : color.new(#f23645, 20)
  191. color botColor = rsiBullish ? color.new(#ffeb3b, 20) : color.new(#040c09, 0)
  192. float topLineValue = rsiBullish ? maxLine : maxLine
  193. float botLineValue = rsiBullish ? minLine : minLine
  194.  
  195. ///Adjusting for background color of RSI MA trend
  196. //rsiup = rsi2 > rsiMA2 and rsiMA2 > rsiMA3 and rsi2 > 0 and op1 < cl1 and cl1 > 10
  197. rsiup = rsi2 > rsiMA2 and rsi2 > 0 and op1 < cl1 and op1 > rsiMA3
  198. rsidown = rsi2 < rsiMA2 and rsiMA2 < rsiMA3 and rsi2 < 0 and op1 > cl1 and cl1 < 10
  199. RSIMAbackgroundColour = rsiup ? color.new(#07d40e, 75) : rsidown ? color.new(#f7525f, 75) : na
  200.  
  201. //////////////////////
  202.  
  203.  
  204. ///Setting Colors and limitations for the RSIMA Trend Cloud
  205. float MAmaxLine = -20
  206. float MAminLine = 20
  207. bool rsiMABullish = rsiMA2 > rsiMA3
  208. color MAtopColor = rsiMABullish ? color.new(#0bff14, 0) : color.new(#3e0a0f, 20)
  209. color MAbotColor = rsiMABullish ? color.new(#145a16, 55) : color.new(#5e1b1b, 0)
  210. float MAtopLineValue = rsiMABullish ? MAmaxLine : MAmaxLine
  211. float MAbotLineValue = rsiMABullish ? MAminLine : MAminLine
  212.  
  213. //Plotting the RSI Trend Cloud
  214. rsiPlot = plot(rsi2, title="RSI", color=#f6fa0500, linewidth=1, style=plot.style_line)
  215. //RSI MA Trend Cloud
  216. //Up and down color plotting for the RSI MA///
  217. color cld_ma = rsiMA2 > 0 ? color.green : color.red
  218. //color cld_ma = rsi2MARising ? rsi2MABull : rsi2MABear
  219. color cld_ma2 = rsi3MARising ? rsi2MABull : rsi2MABear
  220.  
  221.  
  222. // Plot pivot high/low
  223. plot(pivot_high ? rsi2 : na, color=color.green, style=plot.style_line, linewidth=1, title="Pivot High Trendline", display = display.all - display.status_line)
  224. plot(pivot_low ? rsi2 : na, color=color.red, style=plot.style_line, linewidth=1, title="Pivot Low Trendline", display = display.all - display.status_line)
  225. plot(rsiMA2, title="ma", color=cld_ma, linewidth=2, style=plot.style_line, display=display.all)
  226.  
  227. // Plot blue horizontal line from pivot low to next pivot low with a lower value
  228. var float pivot_low_trendline = na
  229. for i = 0 to rsi2LengthInput - 1
  230. if pivot_low[i]
  231. pivot_low_trendline := rsi2[i]
  232. for j = i + 1 to rsi2LengthInput - 1
  233. if pivot_low[j] and rsi2[j] > pivot_low_trendline
  234. pivot_low_trendline := rsi2[j]
  235. break
  236. //
  237. var float pivot_high_trendline = na
  238. for i = 0 to rsi2LengthInput - 1
  239. if pivot_high[i]
  240. pivot_high_trendline := rsi2[i]
  241. for j = i + 1 to rsi2LengthInput - 1
  242. if pivot_high[j] and rsi2[j] < pivot_high_trendline
  243. pivot_high_trendline := rsi2[j]
  244. break
  245.  
  246. plot(pivot_high_trendline, color=color.rgb(0, 226, 215), linewidth=1, title="Pivot High", style=plot.style_line, display = display.all - display.status_line)
  247. plot(pivot_low_trendline, color=color.rgb(190, 122, 19), linewidth=1, title="Pivot Low", style=plot.style_line, display = display.all - display.status_line)
  248. //////////////////////////////////////////////
  249.  
  250. //Study for resistance support long and short
  251. rsi_resistance = (rsi2[1] == pivot_high_trendline) and rsi2 < pivot_high_trendline
  252. rsi_support = (rsi2[1] == pivot_low_trendline) and rsi2 > pivot_low_trendline
  253.  
  254. plotshape(rsi_resistance, title="Resistance", style=shape.labeldown, location=location.top, color=color.red, text="R", textcolor = color.rgb(224, 208, 208), display=display.all - display.status_line)
  255. plotshape(rsi_support, title="Support", style=shape.labelup, location=location.bottom, color=color.green, text="S", textcolor = color.rgb(255, 255, 255, 12), display=display.all - display.status_line)
  256.  
  257. ////End of new color condition plotting///
  258. //Study to mark all DOJI HA Candles
  259.  
  260. BullDoji = op1 < cl1 and hi1 > cl1 and lo1 < op1
  261. BullDoLong = op1 < cl1 and op1 < 10 and cl1 > 10 and op1[1] < cl1[1] and hi1[1] > cl1[1] and lo1[1] < op1[1]
  262. BearDoShort = op1 > cl1 and op1 > -10 and cl1 < -10 and op1[1] > cl1[1] and hi1[1] > op1[1] and lo1[1] < cl1[1]
  263.  
  264. //DojLongExit = op1[1] < cl1[1] and op1 > cl1 and lo1 < lo1[1] and hi1 > op1 and lo1 < cl1 and rsi2[1] > 20 and cl1[1] > 20 and ultraSmooth_stochastic[1] > ultraSmooth_stochastic
  265. //DojShortExit = op1[1] > cl1[1] and op1 < cl1 and hi1 > hi1[1] and lo1 < op1 and hi1 > cl1 and rsi2[1] < -20 and cl1[1] < -20 and ultraSmooth_stochastic[1] < ultraSmooth_stochastic
  266. BearDoji = op1 > cl1 and hi1 > op1 and lo1 < cl1
  267.  
  268. plotshape(BullDoLong, title="Imbalance", style=shape.labelup, location=location.bottom, color=color.rgb(35, 167, 8), textcolor=color.rgb(255, 255, 255), text="Imbalance", display=display.all - display.status_line)
  269. plotshape(BearDoShort, title="Imbalance", style=shape.labeldown, location=location.top, color=color.rgb(221, 14, 14), textcolor=color.rgb(255, 255, 255), text="Imbalance", display=display.all - display.status_line)
  270. alertcondition(BullDoLong or BearDoShort, title="Imbalance", message="Imbalance Alert")
  271. //plotshape(DojLongExit, title="LDojExit", style=shape.labeldown, location=location.top, color=color.rgb(221, 14, 14), textcolor=color.rgb(255, 255, 255), text="Doj Long Exit", display=display.none - display.status_line)
  272. //plotshape(DojShortExit, title="SDojExit", style=shape.labelup, location=location.bottom, color=color.rgb(35, 167, 8), textcolor=color.rgb(255, 255, 255), text="Doj Short Exit", display=display.none - display.status_line)
  273. ////////////////////////////////////
  274. //Poltting Order Blocks
  275. //condition = DojLongExit
  276. //Orderblock = ta.valuewhen(DojLongExit, open, 1)
  277. //DLE = array.new_line(1, DojLongExit)
  278. //line.delete(DLE[5])
  279.  
  280.  
  281. rsiAveragePlot = plot(rsiMA2, title="Average RSI Up Down", color = cld_ma, linewidth=1, display=display.none)
  282.  
  283. //bgcolor(color=RSIMAbackgroundColour, transp=85,
  284. // title="RSIMA coloured background")
  285. rsiAveragePlot2 = plot(rsiMA3, title="RSI Trend Direction", color = cld_ma2, linewidth=1, display=display.none)
  286.  
  287. //rsiAveragePlot = plot(rsiMA2, title="RSI Moving Average", color=color.new(#0096a7, 100), linewidth=1, style=plot.style_line)
  288. //Plot for Trend Fill
  289.  
  290. colUp = rsi2 > 0 ? input.color(color.new(#fffdfd, 0), '', group= 'RSI Trend Colors', inline= 'up') :
  291. input.color(color.rgb(167, 9, 9, 38), '', group= 'RSI Trend Colors', inline= 'dn')
  292. colDn = rsi2 > 0 ? input.color(color.new(#207407, 48), '', group= 'RSI Trend Colors', inline= 'up') :
  293. input.color(color.new(#ffe602, 0), '', group= 'RSI Trend Colors', inline= 'dn')
  294. //
  295. color itopColor = rsiBullish ? color.rgb(75, 0, 0) : color.new(#f23645, 20)
  296. color ibotColor = rsiBullish ? color.new(#ffeb3b, 20) : color.new(#040c09, 0)
  297. hline_center = plot(0, title="hidden midline", display = display.none, editable = false)
  298. fill(rsiPlot, hline_center, rsi2 > 0 ? 30 : 0, rsi2 > 0 ? 0 : -30, colUp, colDn, title="RSI Trend")
  299. fill(rsiAveragePlot, rsiAveragePlot2, MAtopLineValue, MAbotLineValue, MAtopColor, MAbotColor, title="RSI Moving Average Trend", display = display.none)
  300. fill(rsiPlot, rsiAveragePlot, topLineValue, botLineValue, topColor, botColor, title="RSI + MA Trend", display = display.none)
  301. bgcolor(RSIMAbackgroundColour, title="Trend background Color", display = display.none)
  302.  
  303. //HA Candles
  304. plotcandle(op1, hi1, lo1, cl1, title = "HA Candle", color = barColor, wickcolor = wikColor, bordercolor = brDrcolor)
  305. //Original RSI / RSI-MA Plots
  306. //plot(rsiMA, title="RSI Moving Average", color=color.orange, linewidth=2, editable=false, style=plot.style_line)
Advertisement
Comments
  • hemal9022
    3 years
    # text 0.12 KB | 0 0
    1. they have all types of premium tradingview indicators codes available on telegram - @tradingview_premium_indicator (tpi)
Add Comment
Please, Sign In to add comment