Advertisement
xmd79

RSI with Self-Adjusting Linear Regression Bands (Expo)

Jan 12th, 2023
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. //The information contained in my scripts/indicators/ideas does not constitute financial advice or a solicitation to buy or sell any securities of any type.
  2. //I will not accept liability for any loss or damage, including without limitation any loss of profit, which may arise directly or indirectly from use of or reliance on such information.
  3. //All investments involve risk, and the past performance of a security, industry, sector, market, financial product, trading strategy, or individual’s trading does not guarantee future results or returns.
  4. //Investors are fully responsible for any investment decisions they make. Such decisions should be based solely on an evaluation of their financial circumstances, investment objectives, risk tolerance, and liquidity needs.
  5. //My scripts/indicators/ideas are only for educational purposes!
  6. //Copyright by Zeiierman
  7. // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
  8. // © Zeiierman
  9.  
  10. //@version=5
  11. indicator('RSI with Self-Adjusting Linear Regression Bands (Expo)', shorttitle='RSI + Linreg Bands (Expo)', max_bars_back=500, precision=1)
  12.  
  13. //Credit to: RafaelZioni
  14. //Adjustment on the following code
  15. //https://es.tradingview.com/script/wwWnxGus-Linear-Regression-Trend-bands/
  16.  
  17. showlast = input(1000)
  18. // --- settings
  19. section1 = input(true, '─────────── 1.0 RSI Settings ───────────')
  20. rsi_src_ = input.source(close, title='RSI Source', group='1.0 Source')
  21. len = input.int(14, title='RSI Length', group='1.1 RSI Length')
  22.  
  23. section2 = input(true, '─────────── 2.0 Linear Regression Settings ───────────')
  24. period = input.int(100, title='Period', group='2.0 Linear Regression Period')
  25. deviations = input.float(2.5, step=0.1, title='Deviation', group='2.1 Deviation')
  26. // --- end of settings
  27.  
  28. // --- Line
  29. section3 = input(true, '─────────── 3.0 Overbought & Oversold Settings ───────────')
  30. Overbought = input.int(80, title='> Overbought', group='3.0 Overbought & Oversold', inline='Overbought')
  31. Oversold = input.int(20, title='> Oversold', group='3.0 Overbought & Oversold', inline='Overbought')
  32.  
  33. // --- Calc
  34. rsi = ta.wma(ta.rsi(rsi_src_, len), 5)
  35. periodMinusOne = period - 1
  36.  
  37. Ex = 0.0
  38. Ey = 0.0
  39. Ex2 = 0.0
  40. Exy = 0.0
  41. ExEx = 0.0
  42. for i = 0 to periodMinusOne by 1
  43. closeI = rsi[i]
  44. Ex += i
  45. Ey += closeI
  46. Ex2 += i * i
  47. Exy += closeI * i
  48. ExEx := Ex * Ex
  49. ExEx
  50.  
  51.  
  52. //slope
  53. float slope = na
  54. if Ex2 != ExEx
  55. slope := (period * Exy - Ex * Ey) / (period * Ex2 - ExEx)
  56. slope
  57.  
  58.  
  59. ilinearRegression = (Ey - slope * Ex) / period
  60. intercept = ilinearRegression + bar_index * slope
  61. deviation = 0.0
  62. for i = 0 to periodMinusOne by 1
  63. deviation += math.pow(rsi[i], 2) - (intercept - slope * bar_index[i])
  64. break
  65.  
  66. deviationNew = deviations * math.sqrt(deviation / periodMinusOne)
  67. startingPointY = ta.wma(ilinearRegression + slope / periodMinusOne, 5)
  68.  
  69.  
  70. // RSI_Adjust Up and Down
  71. adjust_up = startingPointY + ta.stdev(rsi, 20)
  72. adjust_down = startingPointY - ta.stdev(rsi, 20)
  73.  
  74. // Final Conditions
  75. a = startingPointY - deviationNew + adjust_down / 20
  76. c1 = startingPointY
  77. b = startingPointY + deviationNew - adjust_up / 20
  78.  
  79. //lineColor
  80. col = startingPointY > startingPointY[1] ? color.blue : color.red
  81.  
  82. // Rsi Plot
  83. prsi = plot(rsi, color=col, title='RSI', show_last=showlast)
  84.  
  85. // Curve Plot
  86. Lower_Curve = ta.wma(a, 5)
  87. Upper_Curve = ta.wma(b, 5)
  88.  
  89. LowerCurve = plot(Lower_Curve, color=color.new(color.red, 0), title='Lower Curve', show_last=showlast)
  90. plot(c1, color=color.new(color.aqua, 20), title='Mid Curve', show_last=showlast)
  91. UpperCurve = plot(Upper_Curve, color=color.new(color.green, 0), title='Upper Curve', show_last=showlast)
  92.  
  93.  
  94. fill(prsi, LowerCurve, color=rsi < a ? color.red : color.new(color.blue, 100), title='Lower Curve Fill', show_last=showlast, transp=90)
  95. fill(prsi, UpperCurve, color=rsi > b ? color.green : color.new(color.blue, 100), title='Upper Curve Fill', show_last=showlast, transp=90)
  96.  
  97. abcolor = color.new(color.blue, 100)
  98. fill(LowerCurve, UpperCurve, color=abcolor, title='Fill', show_last=showlast, transp=100)
  99.  
  100. // ---
  101. hline(Overbought, title='Overbought')
  102. hline(50, title='Mid Line')
  103. hline(Oversold, title='Oversold')
  104.  
  105.  
  106.  
  107. //------------------------------------------------------------------------------------
  108. // Alerts
  109. //------------------------------------------------------------------------------------
  110.  
  111.  
  112. RSI_Upper_Curve_CrossUp = ta.crossover(rsi, Upper_Curve)
  113. RSI_Upper_Curve_CrossDn = ta.crossunder(rsi, Upper_Curve)
  114.  
  115. RSI_Lower_Curve_CrossDn = ta.crossunder(rsi, Lower_Curve)
  116. RSI_Lower_Curve_CrossUp = ta.crossover(rsi, Lower_Curve)
  117.  
  118. alertcondition(RSI_Upper_Curve_CrossUp, title='1.0 RSI CrossOver Upper Curve', message='RSI CrossOver Upper Curve')
  119. alertcondition(RSI_Upper_Curve_CrossDn, title='1.1 RSI CrossUnder Upper Curve', message='RSI CrossUnder Upper Curve')
  120.  
  121. alertcondition(RSI_Lower_Curve_CrossDn, title='2.0 RSI CrossUnder Lower Curve', message='RSI CrossUnder Lower Curve')
  122. alertcondition(RSI_Lower_Curve_CrossUp, title='2.1 RSI CrossOver Lower Curve', message='RSI CrossOver Lower Curve')
  123.  
  124.  
  125. // + Overbought/Oversold
  126. Overbought_ = rsi > Overbought
  127. Oversold_ = rsi < Oversold
  128.  
  129. RSI_Upper_Curve_CrossUp_Overbought = ta.crossover(rsi, Upper_Curve) and Overbought_
  130. RSI_Upper_Curve_CrossDn_Overbought = ta.crossunder(rsi, Upper_Curve) and Overbought_
  131.  
  132. RSI_Lower_Curve_CrossDn_Oversold = ta.crossunder(rsi, Lower_Curve) and Oversold_
  133. RSI_Lower_Curve_CrossUp_Oversold = ta.crossover(rsi, Lower_Curve) and Oversold_
  134.  
  135.  
  136. alertcondition(RSI_Upper_Curve_CrossUp_Overbought, title='3.0 RSI Overbought & CrossOver Upper Curve', message='RSI Overbought & CrossOver Upper Curve')
  137. alertcondition(RSI_Upper_Curve_CrossDn_Overbought, title='3.1 RSI Overbought & CrossUnder Upper Curve', message='RSI Overbought & CrossUnder Upper Curve')
  138.  
  139. alertcondition(RSI_Lower_Curve_CrossDn_Oversold, title='4.0 RSI Oversold & CrossUnder Lower Curve', message='RSI Oversold & CrossUnder Lower Curve')
  140. alertcondition(RSI_Lower_Curve_CrossUp_Oversold, title='4.1 RSI Oversold & CrossOver Lower Curve', message='RSI Oversold & CrossOver Lower Curve')
  141.  
  142.  
  143. // Trend Alert
  144. RSI_Over_Upper_Curve = rsi > Upper_Curve
  145. RSI_Under_Lower_Curve = rsi < Lower_Curve
  146.  
  147. RSI_Shift_up = rsi[2] > rsi[1] and rsi > rsi[1]
  148. RSI_Shift_dn = rsi[2] < rsi[1] and rsi < rsi[1]
  149.  
  150. TrendUp = RSI_Over_Upper_Curve and RSI_Shift_up
  151. TrendDn = RSI_Under_Lower_Curve and RSI_Shift_dn
  152.  
  153.  
  154. alertcondition(TrendUp and not TrendUp[1], title='5.0 Trend Continuation Up ', message='Trend Continuation Up')
  155. alertcondition(TrendDn and not TrendDn[1], title='5.1 Trend Continuation Dn', message='Trend Continuation Dn')
  156.  
  157.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement