Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //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.
- //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.
- //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.
- //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.
- //My scripts/indicators/ideas are only for educational purposes!
- //Copyright by Zeiierman
- // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
- // © Zeiierman
- //@version=4
- study("RSI with Self-Adjusting Linear Regression Bands (Expo)", shorttitle="RSI + Linreg Bands (Expo)", max_bars_back=500, precision=1)
- //Credit to: RafaelZioni
- //Adjustment on the following code
- //https://es.tradingview.com/script/wwWnxGus-Linear-Regression-Trend-bands/
- showlast = input(1000)
- // --- settings
- section1 = input(true, "─────────── 1.0 RSI Settings ───────────")
- rsi_src_ = input(close, title="RSI Source", group="1.0 Source")
- len = input(14, title="RSI Length", group="1.1 RSI Length")
- section2 = input(true, "─────────── 2.0 Linear Regression Settings ───────────")
- period = input(100, title="Period", group="2.0 Linear Regression Period")
- deviations = input(2.5, step=0.1, title="Deviation", group="2.1 Deviation")
- // --- end of settings
- // --- Line
- section3 = input(true, "─────────── 3.0 Overbought & Oversold Settings ───────────")
- Overbought = input(80, title="> Overbought", group="3.0 Overbought & Oversold", inline="Overbought")
- Oversold = input(20, title="> Oversold", group="3.0 Overbought & Oversold", inline="Overbought")
- // --- Calc
- rsi = wma(rsi(rsi_src_,len),5)
- periodMinusOne = period-1
- Ex = 0.0
- Ey = 0.0
- Ex2 = 0.0
- Exy = 0.0
- ExEx = 0.0
- for i=0 to periodMinusOne
- closeI = (rsi[i])
- Ex := Ex + i
- Ey := Ey + closeI
- Ex2 := Ex2 + (i * i)
- Exy := Exy + (closeI * i)
- ExEx := Ex * Ex
- //slope
- float slope = na
- if Ex2!=ExEx
- slope:= ((period * Exy) - (Ex * Ey)) / ((period * Ex2) - ExEx)
- ilinearRegression = ((Ey - slope * Ex) / period)
- intercept = ilinearRegression + bar_index * slope
- deviation = 0.0
- for i=0 to periodMinusOne
- deviation := deviation + ((pow(rsi[i],2)) - (intercept - slope * (bar_index[i])))
- break
- deviationNew = deviations * sqrt(deviation / periodMinusOne)
- startingPointY = wma((ilinearRegression + slope / periodMinusOne),5)
- // RSI_Adjust Up and Down
- adjust_up = startingPointY+ stdev(rsi,20)
- adjust_down = startingPointY - stdev(rsi,20)
- // Final Conditions
- a = startingPointY-deviationNew + adjust_down/20
- c1 = startingPointY
- b = startingPointY+deviationNew - adjust_up/20
- //lineColor
- col=startingPointY > startingPointY[1]?color.blue:color.red
- // Rsi Plot
- prsi=plot(rsi, color=col, title="RSI", show_last=showlast)
- // Curve Plot
- Lower_Curve = wma(a,5)
- Upper_Curve = wma(b,5)
- LowerCurve=plot(Lower_Curve, color=color.red, title="Lower Curve", show_last=showlast)
- plot(c1, color=color.aqua, title="Mid Curve", show_last=showlast, transp=20)
- UpperCurve=plot(Upper_Curve, color=color.green, title="Upper Curve", show_last=showlast)
- fill(prsi,LowerCurve, color=rsi<a?color.red:color.new(color.blue,100), title="Lower Curve Fill", show_last=showlast)
- fill(prsi,UpperCurve, color=rsi>b?color.green:color.new(color.blue,100), title="Upper Curve Fill", show_last=showlast)
- abcolor = color.new(color.blue,100)
- fill(LowerCurve,UpperCurve, color=abcolor,transp=100, title="Fill", show_last=showlast)
- // ---
- hline(Overbought, title="Overbought"), hline(50, title="Mid Line"), hline(Oversold, title="Oversold")
- //------------------------------------------------------------------------------------
- // Alerts
- //------------------------------------------------------------------------------------
- RSI_Upper_Curve_CrossUp = crossover(rsi, Upper_Curve)
- RSI_Upper_Curve_CrossDn = crossunder(rsi, Upper_Curve)
- RSI_Lower_Curve_CrossDn = crossunder(rsi, Lower_Curve)
- RSI_Lower_Curve_CrossUp = crossover(rsi, Lower_Curve)
- alertcondition(RSI_Upper_Curve_CrossUp, title="1.0 RSI CrossOver Upper Curve", message="RSI CrossOver Upper Curve")
- alertcondition(RSI_Upper_Curve_CrossDn, title="1.1 RSI CrossUnder Upper Curve", message="RSI CrossUnder Upper Curve")
- alertcondition(RSI_Lower_Curve_CrossDn, title="2.0 RSI CrossUnder Lower Curve", message="RSI CrossUnder Lower Curve")
- alertcondition(RSI_Lower_Curve_CrossUp, title="2.1 RSI CrossOver Lower Curve", message="RSI CrossOver Lower Curve")
- // + Overbought/Oversold
- Overbought_ = rsi>Overbought
- Oversold_ = rsi<Oversold
- RSI_Upper_Curve_CrossUp_Overbought = crossover(rsi, Upper_Curve) and Overbought_
- RSI_Upper_Curve_CrossDn_Overbought = crossunder(rsi, Upper_Curve) and Overbought_
- RSI_Lower_Curve_CrossDn_Oversold = crossunder(rsi, Lower_Curve) and Oversold_
- RSI_Lower_Curve_CrossUp_Oversold = crossover(rsi, Lower_Curve) and Oversold_
- alertcondition(RSI_Upper_Curve_CrossUp_Overbought, title="3.0 RSI Overbought & CrossOver Upper Curve", message="RSI Overbought & CrossOver Upper Curve")
- alertcondition(RSI_Upper_Curve_CrossDn_Overbought, title="3.1 RSI Overbought & CrossUnder Upper Curve", message="RSI Overbought & CrossUnder Upper Curve")
- alertcondition(RSI_Lower_Curve_CrossDn_Oversold, title="4.0 RSI Oversold & CrossUnder Lower Curve", message="RSI Oversold & CrossUnder Lower Curve")
- alertcondition(RSI_Lower_Curve_CrossUp_Oversold, title="4.1 RSI Oversold & CrossOver Lower Curve", message="RSI Oversold & CrossOver Lower Curve")
- // Trend Alert
- RSI_Over_Upper_Curve = rsi>Upper_Curve
- RSI_Under_Lower_Curve = rsi<Lower_Curve
- RSI_Shift_up = rsi[2]>rsi[1] and rsi>rsi[1]
- RSI_Shift_dn = rsi[2]<rsi[1] and rsi<rsi[1]
- TrendUp = RSI_Over_Upper_Curve and RSI_Shift_up
- TrendDn = RSI_Under_Lower_Curve and RSI_Shift_dn
- alertcondition(TrendUp and not TrendUp[1], title="5.0 Trend Continuation Up ", message="Trend Continuation Up")
- alertcondition(TrendDn and not TrendDn[1], title="5.1 Trend Continuation Dn", message="Trend Continuation Dn")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement