RSI Algo For ThinkOrSwim

If anyone could help me with this I'd appreciate it.
https://www.tradingview.com/script/jz39ymWe-RSI-Algo-with-alert/

I was able to get much of the behavior, but the buy and sell toggles arent working properly for me.

Thank you!

Code:
study("RSI Algo", overlay=true)
myPeriod = input(defval=14, type=integer, title="Period")
myThresholdUp = input(defval=70, type=float, title="Upper Threshold")
myThresholdDn = input(defval=30, type=float, title="Lower Threshold")
myAlgoFlipToggle = input(defval=false, type=bool, title="Imverse Algorthim")
myLineToggle = input(defval=true, type=bool, title="Show Lines")
myLabelToggle = input(defval=true, type=bool, title="Show Labels")
myRSI=rsi(close, myPeriod)
buy = myAlgoFlipToggle ? falling(myRSI,1) and cross(myRSI, myThresholdDn) : rising(myRSI, 1) and cross(myRSI,myThresholdUp)
sell = myAlgoFlipToggle ? rising(myRSI, 1) and cross(myRSI,myThresholdUp) : falling(myRSI,1) and cross(myRSI, myThresholdDn)
myPosition = buy==1 ? 0 : sell==1 or myPosition[1]==1 ? 1 : 0
trendColor = buy ? red : sell ? green : na
plot(myLineToggle ? buy and myPosition[1]==1 ? low - 0.004: sell and myPosition[1]==0 ? high + 0.004 : na : na, color=trendColor, style=line, linewidth=4, editable=false)
plotshape(myLabelToggle ? buy and myPosition[1]==1 ? low - 0.005 : na : na, style=shape.labelup, location=location.absolute, text="Buy", transp=0, textcolor = white, color=black, editable=false)
plotshape(myLabelToggle ? sell and myPosition[1]==0 ? high + 0.005 : na : na, style=shape.labeldown, location=location.absolute, text="Sell", transp=0, textcolor = white, color=black, editable=false)
alertcondition(buy, title='Buy Trend', message='Buy')
alertcondition(sell, title='Sell Trend', message='Sell')
alertcondition(myPosition, title='Trend Change', message='Trend Change')
check the below

CSS:
# @legalMeerkat56102
#study("RSI Algo", overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023
input alerts   = yes;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input rsiPeriod   = 14;        # "Period"
input ThresholdUp = 70;        # "Upper Threshold"
input ThresholdDn = 30;        # "Lower Threshold"
input myAlgoFlipToggle = no;     # "Imverse Algorthim"
input myLineToggle  = yes;       # "Show Lines"
input myLabelToggle = yes;       # "Show Labels"

def na = Double.NaN;
def srcDiff = close - close[1];

def NetChgAvg = MovingAverage(AverageType.WILDERS, srcDiff, rsiPeriod);
def TotChgAvg = MovingAverage(AverageType.WILDERS, AbsValue(srcDiff), rsiPeriod);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def nRSI = 50 * (ChgRatio + 1);
def myRSI = nRSI;

def crossUp = (myRSI crosses ThresholdUp);
def crossDn = (myRSI crosses ThresholdDn);
def buy =  if  myAlgoFlipToggle then (myRSI < myRSI[1]) and crossDn else
           if !myAlgoFlipToggle then (myRSI > myRSI[1]) and crossUp else buy[1];
def sell = if  myAlgoFlipToggle then (myRSI > myRSI[1]) and crossUp else
           if !myAlgoFlipToggle then (myRSI < myRSI[1]) and crossDn else sell[1];
def myPosition = if buy == 1 then 0 else if sell == 1 or myPosition[1] == 1 then 1 else 0;
def trendColor = if buy then 1 else if sell then -1 else trendColor[1];
def rsiAlgo = if buy  and myPosition[1] == 1 then low  - 0.004 else
              if sell and myPosition[1] == 0 then high + 0.004 else na;
plot RSIAlgoLine = if myLineToggle then rsiAlgo else na;
RSIAlgoLine.SetLineWeight(2);
RSIAlgoLine.EnableApproximation();
RSIAlgoLine.AssignValueColor(if trendColor[1] > 0 then Color.UPTICK else if trendColor[1] < 0 then Color.DOWNTICK else Color.GRAY);

#--- Bubbles
AddChartBubble(myLabelToggle and buy and myPosition[1] == 1, low - 0.005, "Buy", Color.GREEN, no);
AddChartBubble(myLabelToggle and sell and myPosition[1] == 0, high + 0.005, "Sell", Color.RED, yes);

#---- Alerts
Alert(alerts and buy, "Buy Trend", Alert.BAR, sound);
Alert(alerts and sell, "Sell Trend", Alert.BAR, sound);
Alert(alerts and myPosition, "Trend Change", Alert.BAR, sound);

#--- END CODE
 
check the below

CSS:
# @legalMeerkat56102
#study("RSI Algo", overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023
input alerts   = yes;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input rsiPeriod   = 14;        # "Period"
input ThresholdUp = 70;        # "Upper Threshold"
input ThresholdDn = 30;        # "Lower Threshold"
input myAlgoFlipToggle = no;     # "Imverse Algorthim"
input myLineToggle  = yes;       # "Show Lines"
input myLabelToggle = yes;       # "Show Labels"

def na = Double.NaN;
def srcDiff = close - close[1];

def NetChgAvg = MovingAverage(AverageType.WILDERS, srcDiff, rsiPeriod);
def TotChgAvg = MovingAverage(AverageType.WILDERS, AbsValue(srcDiff), rsiPeriod);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def nRSI = 50 * (ChgRatio + 1);
def myRSI = nRSI;

def crossUp = (myRSI crosses ThresholdUp);
def crossDn = (myRSI crosses ThresholdDn);
def buy =  if  myAlgoFlipToggle then (myRSI < myRSI[1]) and crossDn else
           if !myAlgoFlipToggle then (myRSI > myRSI[1]) and crossUp else buy[1];
def sell = if  myAlgoFlipToggle then (myRSI > myRSI[1]) and crossUp else
           if !myAlgoFlipToggle then (myRSI < myRSI[1]) and crossDn else sell[1];
def myPosition = if buy == 1 then 0 else if sell == 1 or myPosition[1] == 1 then 1 else 0;
def trendColor = if buy then 1 else if sell then -1 else trendColor[1];
def rsiAlgo = if buy  and myPosition[1] == 1 then low  - 0.004 else
              if sell and myPosition[1] == 0 then high + 0.004 else na;
plot RSIAlgoLine = if myLineToggle then rsiAlgo else na;
RSIAlgoLine.SetLineWeight(2);
RSIAlgoLine.EnableApproximation();
RSIAlgoLine.AssignValueColor(if trendColor[1] > 0 then Color.UPTICK else if trendColor[1] < 0 then Color.DOWNTICK else Color.GRAY);

#--- Bubbles
AddChartBubble(myLabelToggle and buy and myPosition[1] == 1, low - 0.005, "Buy", Color.GREEN, no);
AddChartBubble(myLabelToggle and sell and myPosition[1] == 0, high + 0.005, "Sell", Color.RED, yes);

#---- Alerts
Alert(alerts and buy, "Buy Trend", Alert.BAR, sound);
Alert(alerts and sell, "Sell Trend", Alert.BAR, sound);
Alert(alerts and myPosition, "Trend Change", Alert.BAR, sound);

#--- END CODE
Curious if this is a repainting indicator?
I could be off base here but it reminded me of a zig zag style indicator when I loaded it up. I'll chk it out on Tuesday and see how it reacts.
 
Last edited by a moderator:
Curious if this is a repainting indicator?
I could be off base here but it reminded me of a zig zag style indicator when I loaded it up. I'll chk it out on Tuesday and see how it reacts.
There is nothing in this code that presents as an obvious repainter.
Please update us with your findings. Thanks (y)
 
There is nothing in this code that presents as an obvious repainter.
Please update us with your findings. Thanks (y)
Put it up on a bunch of different charts. Definitely NOT a repainter. First chart I put it up on it looked too perfect... just a winning streak. My apologies for the false alarm!
 
@samer800 Thank you for sharing the TOS script !
I have externalized following parameters, tuning to values

NOTE: This setup still requires further testing :)
Happy to hear your feedback.

input rsiPeriod = 13;
input NetChgAvgType = AverageType.WILDERS;
input TotChgAvgType = AverageType.HULL;


See 1Y/1D chart image link below:
RSI Algo TOS

See updated script below:

Code:
# @legalMeerkat56102
#study("RSI Algo", overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023
input alerts   = yes;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input rsiPeriod   = 14;        # "Period"
input ThresholdUp = 70;        # "Upper Threshold"
input ThresholdDn = 30;        # "Lower Threshold"
input myAlgoFlipToggle = no;     # "Imverse Algorthim"
input myLineToggle  = yes;       # "Show Lines"
input myLabelToggle = yes;       # "Show Labels"
input NetChgAvgType = AverageType.WILDERS;
input TotChgAvgType = AverageType.WILDERS;


def na = Double.NaN;
def srcDiff = close - close[1];

def NetChgAvg = MovingAverage(NetChgAvgType, srcDiff, rsiPeriod);
def TotChgAvg = MovingAverage(TotChgAvgType, AbsValue(srcDiff), rsiPeriod);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def nRSI = 50 * (ChgRatio + 1);
def myRSI = nRSI;

def crossUp = (myRSI crosses ThresholdUp);
def crossDn = (myRSI crosses ThresholdDn);
def buy =  if  myAlgoFlipToggle then (myRSI < myRSI[1]) and crossDn else
           if !myAlgoFlipToggle then (myRSI > myRSI[1]) and crossUp else buy[1];
def sell = if  myAlgoFlipToggle then (myRSI > myRSI[1]) and crossUp else
           if !myAlgoFlipToggle then (myRSI < myRSI[1]) and crossDn else sell[1];
def myPosition = if buy == 1 then 0 else if sell == 1 or myPosition[1] == 1 then 1 else 0;
def trendColor = if buy then 1 else if sell then -1 else trendColor[1];
def rsiAlgo = if buy  and myPosition[1] == 1 then low  - 0.004 else
              if sell and myPosition[1] == 0 then high + 0.004 else na;
plot RSIAlgoLine = if myLineToggle then rsiAlgo else na;
RSIAlgoLine.SetLineWeight(2);
RSIAlgoLine.EnableApproximation();
RSIAlgoLine.AssignValueColor(if trendColor[1] > 0 then Color.UPTICK else if trendColor[1] < 0 then Color.DOWNTICK else Color.GRAY);

#--- Bubbles
AddChartBubble(myLabelToggle and buy and myPosition[1] == 1, low - 0.005, "Buy", Color.GREEN, no);
AddChartBubble(myLabelToggle and sell and myPosition[1] == 0, high + 0.005, "Sell", Color.RED, yes);

#---- Alerts
Alert(alerts and buy, "Buy Trend", Alert.BAR, sound);
Alert(alerts and sell, "Sell Trend", Alert.BAR, sound);
Alert(alerts and myPosition, "Trend Change", Alert.BAR, sound);

#--- END CODE
 
Last edited by a moderator:
@samer800 Thank you for sharing the TOS script !
I have externalized following parameters, tuning to values

NOTE: This setup still requires further testing :)
Happy to hear your feedback.

input rsiPeriod = 13;
input NetChgAvgType = AverageType.WILDERS;
input TotChgAvgType = AverageType.HULL;


See 1Y/1D chart image link below:
RSI Algo TOS

See updated script below:

Code:
# @legalMeerkat56102
#study("RSI Algo", overlay=true)
# Converted by Sam4Cok@Samer800 - 01/2023
input alerts   = yes;
input sound    = {default "NoSound", "Ding", "Bell", "Chimes", "Ring"};
input rsiPeriod   = 14;        # "Period"
input ThresholdUp = 70;        # "Upper Threshold"
input ThresholdDn = 30;        # "Lower Threshold"
input myAlgoFlipToggle = no;     # "Imverse Algorthim"
input myLineToggle  = yes;       # "Show Lines"
input myLabelToggle = yes;       # "Show Labels"
input NetChgAvgType = AverageType.WILDERS;
input TotChgAvgType = AverageType.WILDERS;


def na = Double.NaN;
def srcDiff = close - close[1];

def NetChgAvg = MovingAverage(NetChgAvgType, srcDiff, rsiPeriod);
def TotChgAvg = MovingAverage(TotChgAvgType, AbsValue(srcDiff), rsiPeriod);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def nRSI = 50 * (ChgRatio + 1);
def myRSI = nRSI;

def crossUp = (myRSI crosses ThresholdUp);
def crossDn = (myRSI crosses ThresholdDn);
def buy =  if  myAlgoFlipToggle then (myRSI < myRSI[1]) and crossDn else
           if !myAlgoFlipToggle then (myRSI > myRSI[1]) and crossUp else buy[1];
def sell = if  myAlgoFlipToggle then (myRSI > myRSI[1]) and crossUp else
           if !myAlgoFlipToggle then (myRSI < myRSI[1]) and crossDn else sell[1];
def myPosition = if buy == 1 then 0 else if sell == 1 or myPosition[1] == 1 then 1 else 0;
def trendColor = if buy then 1 else if sell then -1 else trendColor[1];
def rsiAlgo = if buy  and myPosition[1] == 1 then low  - 0.004 else
              if sell and myPosition[1] == 0 then high + 0.004 else na;
plot RSIAlgoLine = if myLineToggle then rsiAlgo else na;
RSIAlgoLine.SetLineWeight(2);
RSIAlgoLine.EnableApproximation();
RSIAlgoLine.AssignValueColor(if trendColor[1] > 0 then Color.UPTICK else if trendColor[1] < 0 then Color.DOWNTICK else Color.GRAY);

#--- Bubbles
AddChartBubble(myLabelToggle and buy and myPosition[1] == 1, low - 0.005, "Buy", Color.GREEN, no);
AddChartBubble(myLabelToggle and sell and myPosition[1] == 0, high + 0.005, "Sell", Color.RED, yes);

#---- Alerts
Alert(alerts and buy, "Buy Trend", Alert.BAR, sound);
Alert(alerts and sell, "Sell Trend", Alert.BAR, sound);
Alert(alerts and myPosition, "Trend Change", Alert.BAR, sound);

#--- END CODE
Do you have the link to your RSI ALGO Chart with the squeeze and the Momentum indicator
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
555 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top