Using IF function with AddOrder() in ThinkorSwim


New member
I have a question about thinkcript AddOrder() function. Maybe you can help with this. Just learning coding in TOS. I have a signal in a strategy as follows:

input barexit = 2;
def LongExt= LongEnt from barexit bars ago;
AddOrder(OrderType.BUY_TO_OPEN, LongEnt, open, TradeSize);

AddOrder(OrderType.SELL_TO_CLOSE, LongExt, open, TradeSize);

where LongEnt is defined. The above is working properly. However, I am looking to actually only have a signal if have the entry of the next bar takes out the high of the current bar.

I am trying an if statement but can’t get it to work. This is what I tried.

AddOrder(If(high[-1] > high, (OrderType.BUY_TO_OPEN, LongEnt, high, TradeSize), null);

Any help would be great as I am trying to learn.
Rather than using an "IF" statement in the AddOrder, I would use an "AND" statement when you define your LongEnt variable.

For example, since you don't define your LongEnt condition above, I'm just going to say that the close must be above the 8 period exponential moving average AND the next open must be above the current high.

input barexit = 2;
input TradeSize = 1000;
def EMA = ExpAverage(close, 8);
def LongEnt = close > EMA and open[-1] > high;
def LongExt = LongEnt from barexit bars ago;
AddOrder(OrderType.BUY_TO_OPEN, LongEnt, open, TradeSize);
AddOrder(OrderType.SELL_TO_CLOSE, LongExt, open, TradeSize);

You will see in the image below that the green bar is above the EMA8 (condition1) AND the next bar's open is above the green bar's high (condition2), so the order was opened.



  • I0mFqL4.png
    16.9 KB · Views: 137
That worked great. Do you know of an easy way to export strategy results to excel in column format. When I hit the export button it saves a csv will all data in the same column.
Is it possible to use strategy to make buy and sell automatically once we have script on any stock without manual effort to buy and sell based on signals?

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
384 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.