Resource icon

thinkScript Conditions: if, then, else statement

An if-then condition statement is employed when you want an indicator to act differently in specific situations. In ThinkorSwim, the if-then statement allows for advanced behavior and give your thinkScript code the flexibility to make decisions.


def pattern = if condition then 1 else 0;


This lower study will plot 1 for every red candle.

declare lower;

def red_bar = close < open;
plot spike = if red_bar then 1 else 0;

The example above is more so of an if-expression.

Here is another example where we use the if statement. When you have multiple conditions in your script, you can deploy the if - then - else statement multiple times.

The script below highlights bullish and bearish Engulfing patterns on your chart.
  • If the Bullish condition returns true, then the code will highlight your candle in green.
  • If the Bearish condition returns true, then the code highlights your candle in red.
  • For any candles without the Engulfing pattern, the script assigns the color white to each candle.
# TD Ameritrade IP Company, Inc. (c) 2011-2020

input length = 20;
input trendSetup = 3;

def BodyMax = Max(open, close);
def BodyMin = Min(open, close);
def IsEngulfing = BodyMax > BodyMax[1] and
    BodyMin < BodyMin[1];
def IsWhite = open < close;
def IsBlack = open > close;
def IsPrevDoji = IsDoji(length)[1];

def Bearish = IsAscending(close, trendSetup)[1] and
    (IsWhite[1] or IsPrevDoji) and
    IsBlack and

def Bullish = IsDescending(close, trendSetup)[1] and
    (IsBlack[1] or IsPrevDoji) and
    IsWhite and

AssignPriceColor(if Bullish then else if Bearish then else color.white);

Additional scripts related to if, then, else

  • Like
Reactions: JerryA
First release
Last update
5.00 star(s) 1 ratings

More resources from BenTen

Latest reviews

Nice and clear example.
Thank you for the lovely first review ❤️

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.