Use the mother bar as the breakout reference

An inside bar has a smaller range contained inside the previous mother bar. This long-only thinkScript strategy requires a third bar to close above the mother bar's high. It does not enter merely because the inside bar's high is exceeded, and it does not assume a confirmed pattern predicts a profitable move.

Three-bar setup

The mother bar has high 104 and low 96; the next bar has high 103 and low 98. Strict inequalities define an inside bar. A third bar closing at 105 qualifies; a bar touching 106 but closing at 103 does not. The simulated entry occurs at the following bar's open. Exit on a completed close below a ten-bar simple moving average, with the simulated exit at the following open.

Inside-Bar Breakout Strategy with a Moving Average Exit

Two hypothetical mother-bar setups. Both touch above 104, but only the close at 105 confirms.

thinkScript example

input exitLength = 10;
def avg = Average(close, exitLength);
def inside = high[1] < high[2] and
             low[1] > low[2];
def enter = inside and close > high[2];
def leave = close < avg;

def held = CompoundValue(1,
    if held[1] == 0 and enter then 1
    else if held[1] == 1 and leave then 0
    else held[1], 0);

AddOrder(OrderType.BUY_TO_OPEN,
    held[1] == 0 and held == 1,
    open[-1], 1, Color.GREEN, Color.GREEN,
    "Inside breakout");
AddOrder(OrderType.SELL_TO_CLOSE,
    held[1] == 1 and held == 0,
    open[-1], 1, Color.RED, Color.RED,
    "MA exit");

The recursive held variable represents intended simulated position state, not brokerage holdings. It alternates entry and exit signals and blocks pyramiding in this simplified long-only model. On a flat-state bar, entry takes priority even if the exit condition is also true. The official AddOrder reference describes the next-bar order convention; open[-1] supplies the next opening price for the simulation, never for the current signal decision.

Testing and risk

Compare strict inside bars with a version that allows equal boundaries, and compare mother-bar breaks with inside-bar breaks. Keep exit logic constant; assess false breakouts, holding duration, adverse excursion and drawdown after realistic costs. This demonstration has no hard intrabar protective stop: a moving-average exit cannot cap loss. For a separate version, model stop execution conservatively, including gaps and uncertain intrabar ordering. AddOrder generates hypothetical strategy trades, not unattended live orders. The code has not been compiled or backtested here. Actual trades can incur substantial losses.

For custom execution and strategy rules, see Custom Thinkorswim strategy Development Services.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.