thinkScript strategies

AddOrder and Strategies

AddOrder adds simulated orders to a thinkorswim chart strategy for backtesting. Use OrderType constants to control entry and exit behavior, then review the strategy report for hypothetical performance.

EmaCrossoverStrategyexcerpt
AddOrder(
    OrderType.BUY_TO_OPEN,
    enterLong,
    open[-1],
    tradeSize,
    Color.GREEN,
    Color.GREEN,
    "Long"
);
OrderType
condition
simulated
Function
AddOrder
Order types
6 OrderType constants
Execution
Simulated, not live

Direct answer

What AddOrder does in a thinkScript strategy

AddOrder is the function that turns a study into a strategy. When the condition is true, it adds a simulated order at the specified price and size. The order appears as a signal arrow on the chart and feeds into the strategy report, which calculates hypothetical trades and performance metrics.

The first argument is an OrderType constant that controls whether the order opens or closes a position. The default price is open[-1], the open of the next bar, so the order executes after the signal bar closes.

Complete source

EMA crossover strategy code

This strategy buys when the fast EMA crosses above the slow EMA and sells when it crosses below. It uses BUY_TO_OPEN and SELL_TO_CLOSE for explicit position control.

EmaCrossoverStrategy
Review in the thinkScript Editor
# EMA crossover strategy with AddOrder
input fastLength = 9;
input slowLength = 21;
input tradeSize = 1;

def fastEMA = ExpAverage(close, fastLength);
def slowEMA = ExpAverage(close, slowLength);
def enterLong = Crosses(fastEMA, slowEMA, CrossingDirection.ABOVE);
def exitLong = Crosses(fastEMA, slowEMA, CrossingDirection.BELOW);

AddOrder(
    OrderType.BUY_TO_OPEN,
    enterLong,
    open[-1],
    tradeSize,
    Color.GREEN,
    Color.GREEN,
    "Long"
);

AddOrder(
    OrderType.SELL_TO_CLOSE,
    exitLong,
    open[-1],
    tradeSize,
    Color.RED,
    Color.RED,
    "Exit"
);

plot Fast = fastEMA;
plot Slow = slowEMA;
Fast.SetDefaultColor(Color.CYAN);
Slow.SetDefaultColor(Color.MAGENTA);
The default price is open[-1], the open of the next bar. This means the order executes one bar after the signal fires, which matches how thinkorswim strategies evaluate conditions on closed bars.

OrderType reference

All OrderType constants

The AUTO constants handle both opening and closing in one call. The explicit constants give you separate control over entry and exit.

ConstantAction
OrderType.BUY_AUTOOpens a long or closes a short position automatically
OrderType.SELL_AUTOOpens a short or closes a long position automatically
OrderType.BUY_TO_OPENOpens a new long position
OrderType.SELL_TO_CLOSECloses an existing long position
OrderType.SELL_TO_OPENOpens a new short position
OrderType.BUY_TO_CLOSECloses an existing short position

Platform steps

Add a strategy to thinkorswim

  1. 1

    Open the Charts tab, click Studies, select Edit Studies, then switch to the Strategies tab.

  2. 2

    Click Create, remove the default code, and paste the complete strategy source from this page.

  3. 3

    Let the thinkScript Editor parse the source. Resolve any messages before saving.

  4. 4

    Add the strategy to a chart. Signal arrows appear at entry and exit points.

  5. 5

    Right-click the strategy on the chart and select Show Strategy Report to review simulated trades and metrics.

Frequently asked questions

Build a backtesting strategy with AI

Describe your entry and exit rules in plain English. The Pineify thinkorswim AI Coding Agent generates a complete thinkScript strategy file with AddOrder calls and the correct OrderType constants. Review the source, then test it in the Editor and evaluate the strategy report.

Open the thinkScript AI agent

This page is for educational and informational purposes. It does not provide investment advice, predict future prices, or guarantee trading results. Simulated strategy performance does not guarantee future results.