Syntax Rules For ThinkOrSwim

TickTockTony

New member
VIP
This statment gets, Invalid statment :RSI

rsi = RSI(close, n);

isDivergenceUp = (rsi > Over_Bought) and (close < high[n]);
isDivergenceDown = (rsi < Over_Sold) and (close > high[n]);

Error here: rsi.AssignValueColor(if rsi > Over_Bought then Color.Red else if rsi < Over_Sold then Color.Green
else if (isDivergenceUp) then
else Color.Yellow);
 
a couple of syntax issues:

what you wrote:
rsi = RSI(close, n);
correct syntax:
def xrsi = RSI("length" = n, "over bought" = Over_Bought, "over sold" = Over_Sold, "price" = CLOSE)."RSI";
you can use shortcuts, like you attempted above. you don't need to define every variable if you are using defaults. But they must be in the correct order. length is defined before price.
you will commonly see:
def xrsi = RSI("length" = n, "price"=CLOSE);

Not sure of the syntax for a ToS study?
Here is a cool hack for determining the syntax when referencing ToS studies
https://usethinkscript.com/threads/reference-syntax-and-indicator-settings-in-thinkorswim.5022/

Other syntax issues like:
if (isDivergenceUp) then
else Color.Yellow);
fixed:
if isDivergenceUp then color.violet
else Color.Yellow);

Overall ToS rules:
all the variables that you are using need to be defined before application with either def or input statements.
Ruby:
## ########################################################
# RSI Diverence Indicator
# @TickTockTony 12/2023
## ########################################################
# definitions & logic
input n = 8 ;
input Over_Bought = 30 ;
input Over_Sold = 70 ;
def xrsi = RSI("length" = n, "over bought" = Over_Bought, "over sold" = Over_Sold, "price" = CLOSE);
def isDivergenceUp = (xrsi > Over_Bought) and (close < high[n]);
def isDivergenceDown = (xrsi < Over_Sold) and (close > high[n]);

## ########################################################
# charting & formatting
declare lower;
declare real_size;

plot prsi = xrsi;
prsi.AssignValueColor(
if xrsi > Over_Bought then Color.Red else
if xrsi < Over_Sold then Color.Green else
if isDivergenceUp then color.violet else Color.Yellow);

plot fifty = 50 ;
fifty.SetPaintingStrategy(PaintingStrategy.DASHES);
fifty.SetDefaultColor(color.gray) ;
fifty.SetLineWeight(1);
fifty.HideBubble() ;  fifty.HideTitle() ;

Addcloud(Over_Bought,10,color.gray, color.gray);
Addcloud(Over_Sold,90,color.gray, color.gray);

w9x3F2f.png
 
Last edited:
I've corrected some of the references inside the PLOT conditional statements ... but the basic IF/ELSE ... ie, pre conditioning if the PLOT logic should execute at all remains.

#If (simMovAvg < simMovAvg[5]) {
plot simpleUp = Crosses(simMovAvg , simMovAvg[1] , crossingType == crossingType.above);
simpleUp.AssignValueColor(Color.WHITE);
simpleUp.SetLineWeight(5);
simpleUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
#} else {
#If (simMovAvg > simMovAvg[5]) {
plot simpleDown = Crosses(simMovAvg , simMovAvg[1], crossingType == crossingType.below);
simpleDown.AssignValueColor(Color.PINK);
simpleDown.SetLineWeight(5);
simpleDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
#} else {
# def nop = true;#
#}
 
All variables need to be defined before application with either def or input statements. And you had some syntax issues with your plot statements.

Here is a cool hack for determining the syntax when referencing ToS studies
https://usethinkscript.com/threads/reference-syntax-and-indicator-settings-in-thinkorswim.5022/

You can use the above hack to walk through the wizard to create your plot statements, too!
o2zdYUd.png

Ruby:
## ########################################################
# MovingAverage Momo Indicator
# @Dave Blue 12/2023
## ########################################################
# definitions & logic
input ma_length = 9 ;
def simMovAvg = SimpleMovingAvg("price" = CLOSE, "length" = ma_length);

## ########################################################
# charting & formatting

#If (simMovAvg < simMovAvg[5]) {
plot simpleUp = Crosses(simMovAvg , simMovAvg[5] ,CrossingDirection.ABOVE);
     simpleUp.AssignValueColor(Color.WHITE);
     simpleUp.SetLineWeight(5);
     simpleUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

#If (simMovAvg > simMovAvg[5]) {
plot simpleDown = Crosses(simMovAvg , simMovAvg[5], CrossingDirection.BELOW);
     simpleDown.AssignValueColor(Color.PINK);
     simpleDown.SetLineWeight(5);
     simpleDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Hope this helps
 
Last edited:

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
322 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