Why a breakout needs a time budget

A breakout can remain above its entry price without developing into a sustained trend. A time stop limits exposure to a stalled position independently of its profit or loss. In NinjaTrader 8, counting bars since an actual entry execution rather than since the entry signal avoids an off-by-one error when historical orders fill on the next bar.

Strategy rules

On a completed primary-series bar, enter one contract long if the strategy is flat and the close exceeds the highest high of the preceding 20 bars. Exclude the current bar from the channel. Once long, submit a market exit when five bars have elapsed since the most recent named entry execution. Do not evaluate another entry on that same exit decision. Configure Calculate.OnBarClose, EntriesPerDirection = 1 and a single primary series. This is a research example, not a production-ready order manager.

Execution-Based Time Stops for Breakout Strategies

Hypothetical closes: entry bar 100, then 101, 102, 101.5, 101.8 and 101.2. At bar 5 the time exit is requested even though price remains above the assumed entry fill. The plotted exit signal is not a guaranteed fill.

NinjaScript implementation

// NinjaScript Strategy members
protected override void OnBarUpdate()
{
    if (BarsInProgress != 0 || CurrentBar < 21)
        return;

    if (Position.MarketPosition == MarketPosition.Long)
    {
        int elapsed = BarsSinceEntryExecution(0, "Breakout", 0);
        if (elapsed >= 5)
            ExitLong("TimeExit", "Breakout");
        return;
    }

    if (Position.MarketPosition == MarketPosition.Flat
        && Close[0] > MAX(High, 20)[1])
        EnterLong(1, "Breakout");
}

Insert the method into a NinjaScript Strategy template and retain its standard namespaces. Set Calculate = Calculate.OnBarClose in State.SetDefaults. The BarsSinceEntryExecution method returns the bar count since the specified execution, or -1 if none exists. The entry name must match exactly. MAX(High, 20)[1] references the prior completed rolling maximum. In historical standard fill processing a market order generated at bar close normally fills at the next bar open; real-time execution depends on market conditions.

Risk controls and edge cases

Add a protective stop before live use, for example a separately tested fixed tick distance with SetStopLoss tied to the entry signal. A time stop does not cap monetary loss. Track working orders and rejection events explicitly, particularly if adapting the example for partial fills or multiple entries. The most recent execution may reset the count in a multi-fill design; if the desired clock starts with the first fill, store that bar index in OnExecutionUpdate instead. Decide whether session-close liquidation takes precedence and whether positions can survive overnight.

Testing protocol

Compare holding limits of 3, 5 and 10 bars with an otherwise identical baseline without a time stop. Use the same commissions, slippage and protective stop. Measure average holding time, maximum adverse excursion, net returns and drawdown. Test gaps, session transitions and missing bars. A five-bar limit represents 25 minutes on a continuous five-minute chart but not necessarily 25 minutes across a session break. Reserve an out-of-sample period and avoid selecting a limit solely on historical profit. This code has not been compiled or backtested here. Futures and leveraged products can incur substantial losses.

For execution-aware time exits and custom risk controls, see Custom NinjaTrader 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.