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.
AddOrder(
OrderType.BUY_TO_OPEN,
enterLong,
open[-1],
tradeSize,
Color.GREEN,
Color.GREEN,
"Long"
);- 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.
# 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);open[-1], the open of the next bar. AddOrder places a hypothetical order on that next bar. Keep future price offsets out of entry conditions; the next-bar price convention is not permission to use future information in a signal.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.
| Constant | Action |
|---|---|
| OrderType.BUY_AUTO | Opens a long or closes a short position automatically |
| OrderType.SELL_AUTO | Opens a short or closes a long position automatically |
| OrderType.BUY_TO_OPEN | Opens a new long position |
| OrderType.SELL_TO_CLOSE | Closes an existing long position |
| OrderType.SELL_TO_OPEN | Opens a new short position |
| OrderType.BUY_TO_CLOSE | Closes an existing short position |
Platform steps
Add a strategy to thinkorswim
- 1
Open the Charts tab, click Studies, select Edit Studies, then switch to the Strategies tab.
- 2
Click Create, remove the default code, and paste the complete strategy source from this page.
- 3
Let the thinkScript Editor parse the source. Resolve any messages before saving.
- 4
Add the strategy to a chart. Signal arrows appear at entry and exit points.
- 5
Right-click a strategy signal and choose Show report to inspect simulated trades and metrics.
Primary sources
Official thinkScript references used here
Related topics
Explore more thinkScript guides and tools
Continue building thinkorswim studies or convert your strategy logic across platforms.
- thinkScript TutorialStart with variables, plots, and functions in thinkorswim.
- AddLabel and AsTextDisplay formatted text labels and custom status on charts.
- thinkScript ColorsUse Color constants, CreateColor, and dynamic plot colors.
- thinkScript to Pine Script ConverterConvert thinkorswim study and strategy code to Pine Script.
Frequently asked questions
What does AddOrder do in thinkScript?
AddOrder adds a simulated order to a thinkorswim strategy when a condition is true. It is used for backtesting. The order appears as a signal arrow on the chart and contributes to the strategy report. AddOrder does not send real orders to a brokerage.
Does AddOrder place live trades in thinkorswim?
No. AddOrder is a simulation function for chart strategies. It marks entry and exit points on the chart and calculates a hypothetical PnL in the strategy report. Chart strategies do not submit live brokerage orders.
What OrderType constants are available in thinkScript?
thinkScript provides six OrderType constants: BUY_AUTO and SELL_AUTO for automatic open or close, BUY_TO_OPEN and SELL_TO_OPEN for opening positions, and BUY_TO_CLOSE and SELL_TO_CLOSE for closing positions. The AUTO constants handle both opening and closing in one call.
What price does AddOrder use by default?
The default price is open[-1], which is the open price of the next bar after the condition is true. A different third argument changes the assumed simulation price; it does not create a pending limit order or prove that a real fill was available.
How do I create a thinkScript strategy?
A strategy is a thinkScript script that contains at least one AddOrder call. Open the Editor from the Strategies tab in Edit Studies, write your conditions and AddOrder calls, then add the strategy to a chart. The strategy report shows simulated trades and performance metrics.
Can Pineify generate a thinkScript strategy with AddOrder?
Yes. Describe the entry and exit rules in plain English. The Pineify thinkorswim AI Coding Agent can draft a strategy source file. It does not compile or execute thinkScript in thinkorswim. Review the calls, parse the code in the platform Editor, and inspect the hypothetical strategy report.
Build a backtesting strategy with AI
Describe your entry and exit rules in plain English. The Pineify thinkorswim AI Coding Agent can draft the source. Review its conditions and simulation assumptions, then compile and evaluate it in thinkorswim. No platform execution or performance result is established by generation.
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.