Advertisement
xmd79

RSI with Self-Adjusting Linear Regression Bands (Expo)

Jun 4th, 2021
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.57 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=4
  11. study("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(close, title="RSI Source", group="1.0 Source")
  21. len = input(14, title="RSI Length", group="1.1 RSI Length")
  22.  
  23. section2 = input(true, "─────────── 2.0 Linear Regression Settings ───────────")
  24. period = input(100, title="Period", group="2.0 Linear Regression Period")
  25. deviations = input(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(80, title="> Overbought", group="3.0 Overbought & Oversold", inline="Overbought")
  31. Oversold = input(20, title="> Oversold", group="3.0 Overbought & Oversold", inline="Overbought")
  32.  
  33. // --- Calc
  34. rsi = wma(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
  43. closeI = (rsi[i])
  44. Ex := Ex + i
  45. Ey := Ey + closeI
  46. Ex2 := Ex2 + (i * i)
  47. Exy := Exy + (closeI * i)
  48. ExEx := Ex * Ex
  49.  
  50.  
  51. //slope
  52. float slope = na
  53. if Ex2!=ExEx
  54. slope:= ((period * Exy) - (Ex * Ey)) / ((period * Ex2) - ExEx)
  55.  
  56.  
  57. ilinearRegression = ((Ey - slope * Ex) / period)
  58. intercept = ilinearRegression + bar_index * slope
  59. deviation = 0.0
  60. for i=0 to periodMinusOne
  61. deviation := deviation + ((pow(rsi[i],2)) - (intercept - slope * (bar_index[i])))
  62. break
  63.  
  64. deviationNew = deviations * sqrt(deviation / periodMinusOne)
  65. startingPointY = wma((ilinearRegression + slope / periodMinusOne),5)
  66.  
  67.  
  68. // RSI_Adjust Up and Down
  69. adjust_up = startingPointY+ stdev(rsi,20)
  70. adjust_down = startingPointY - stdev(rsi,20)
  71.  
  72. // Final Conditions
  73. a = startingPointY-deviationNew + adjust_down/20
  74. c1 = startingPointY
  75. b = startingPointY+deviationNew - adjust_up/20
  76.  
  77. //lineColor
  78. col=startingPointY > startingPointY[1]?color.blue:color.red
  79.  
  80. // Rsi Plot
  81. prsi=plot(rsi, color=col, title="RSI", show_last=showlast)
  82.  
  83. // Curve Plot
  84. Lower_Curve = wma(a,5)
  85. Upper_Curve = wma(b,5)
  86.  
  87. LowerCurve=plot(Lower_Curve, color=color.red, title="Lower Curve", show_last=showlast)
  88. plot(c1, color=color.aqua, title="Mid Curve", show_last=showlast, transp=20)
  89. UpperCurve=plot(Upper_Curve, color=color.green, title="Upper Curve", show_last=showlast)
  90.  
  91.  
  92. fill(prsi,LowerCurve, color=rsi<a?color.red:color.new(color.blue,100), title="Lower Curve Fill", show_last=showlast)
  93. fill(prsi,UpperCurve, color=rsi>b?color.green:color.new(color.blue,100), title="Upper Curve Fill", show_last=showlast)
  94.  
  95. abcolor = color.new(color.blue,100)
  96. fill(LowerCurve,UpperCurve, color=abcolor,transp=100, title="Fill", show_last=showlast)
  97.  
  98. // ---
  99. hline(Overbought, title="Overbought"), hline(50, title="Mid Line"), hline(Oversold, title="Oversold")
  100.  
  101.  
  102.  
  103. //------------------------------------------------------------------------------------
  104. // Alerts
  105. //------------------------------------------------------------------------------------
  106.  
  107.  
  108. RSI_Upper_Curve_CrossUp = crossover(rsi, Upper_Curve)
  109. RSI_Upper_Curve_CrossDn = crossunder(rsi, Upper_Curve)
  110.  
  111. RSI_Lower_Curve_CrossDn = crossunder(rsi, Lower_Curve)
  112. RSI_Lower_Curve_CrossUp = crossover(rsi, Lower_Curve)
  113.  
  114. alertcondition(RSI_Upper_Curve_CrossUp, title="1.0 RSI CrossOver Upper Curve", message="RSI CrossOver Upper Curve")
  115. alertcondition(RSI_Upper_Curve_CrossDn, title="1.1 RSI CrossUnder Upper Curve", message="RSI CrossUnder Upper Curve")
  116.  
  117. alertcondition(RSI_Lower_Curve_CrossDn, title="2.0 RSI CrossUnder Lower Curve", message="RSI CrossUnder Lower Curve")
  118. alertcondition(RSI_Lower_Curve_CrossUp, title="2.1 RSI CrossOver Lower Curve", message="RSI CrossOver Lower Curve")
  119.  
  120.  
  121. // + Overbought/Oversold
  122. Overbought_ = rsi>Overbought
  123. Oversold_ = rsi<Oversold
  124.  
  125. RSI_Upper_Curve_CrossUp_Overbought = crossover(rsi, Upper_Curve) and Overbought_
  126. RSI_Upper_Curve_CrossDn_Overbought = crossunder(rsi, Upper_Curve) and Overbought_
  127.  
  128. RSI_Lower_Curve_CrossDn_Oversold = crossunder(rsi, Lower_Curve) and Oversold_
  129. RSI_Lower_Curve_CrossUp_Oversold = crossover(rsi, Lower_Curve) and Oversold_
  130.  
  131.  
  132. alertcondition(RSI_Upper_Curve_CrossUp_Overbought, title="3.0 RSI Overbought & CrossOver Upper Curve", message="RSI Overbought & CrossOver Upper Curve")
  133. alertcondition(RSI_Upper_Curve_CrossDn_Overbought, title="3.1 RSI Overbought & CrossUnder Upper Curve", message="RSI Overbought & CrossUnder Upper Curve")
  134.  
  135. alertcondition(RSI_Lower_Curve_CrossDn_Oversold, title="4.0 RSI Oversold & CrossUnder Lower Curve", message="RSI Oversold & CrossUnder Lower Curve")
  136. alertcondition(RSI_Lower_Curve_CrossUp_Oversold, title="4.1 RSI Oversold & CrossOver Lower Curve", message="RSI Oversold & CrossOver Lower Curve")
  137.  
  138.  
  139. // Trend Alert
  140. RSI_Over_Upper_Curve = rsi>Upper_Curve
  141. RSI_Under_Lower_Curve = rsi<Lower_Curve
  142.  
  143. RSI_Shift_up = rsi[2]>rsi[1] and rsi>rsi[1]
  144. RSI_Shift_dn = rsi[2]<rsi[1] and rsi<rsi[1]
  145.  
  146. TrendUp = RSI_Over_Upper_Curve and RSI_Shift_up
  147. TrendDn = RSI_Under_Lower_Curve and RSI_Shift_dn
  148.  
  149.  
  150. alertcondition(TrendUp and not TrendUp[1], title="5.0 Trend Continuation Up ", message="Trend Continuation Up")
  151. alertcondition(TrendDn and not TrendDn[1], title="5.1 Trend Continuation Dn", message="Trend Continuation Dn")
  152.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement