NinjaTrader opening range breakout

Opening range breakout bot for NinjaTrader 8

An opening range breakout bot is only as specific as its session window, range definition, trigger, quantity, and protection rules. Pineify can generate that logic as one editable managed NinjaScript Strategy for review and platform testing.

Content checked on

Opening range breakout managed Strategy

A single-file example that freezes a daily opening range, checks close breakouts, and attaches fixed managed protection.

using System;
using System.ComponentModel.DataAnnotations;
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript;

namespace NinjaTrader.NinjaScript.Strategies
{
    public class OpeningRangeBreakoutStrategy : Strategy
    {
        private DateTime rangeDate = DateTime.MinValue;
        private double rangeHigh;
        private double rangeLow;
        private bool rangeReady;

        [NinjaScriptProperty]
        [Range(0, 235959)]
        [Display(Name = "Range start", GroupName = "Session", Order = 0)]
        public int RangeStart { get; set; }

        [NinjaScriptProperty]
        [Range(0, 235959)]
        [Display(Name = "Range end", GroupName = "Session", Order = 1)]
        public int RangeEnd { get; set; }

        [NinjaScriptProperty]
        [Range(1, 200)]
        [Display(Name = "Stop ticks", GroupName = "Risk", Order = 2)]
        public int StopTicks { get; set; }

        [NinjaScriptProperty]
        [Range(1, 400)]
        [Display(Name = "Target ticks", GroupName = "Risk", Order = 3)]
        public int TargetTicks { get; set; }

        protected override void OnStateChange()
        {
            if (State == State.SetDefaults)
            {
                Name = "OpeningRangeBreakoutStrategy";
                Calculate = Calculate.OnBarClose;
                BarsRequiredToTrade = 20;
                RangeStart = 93000;
                RangeEnd = 100000;
                StopTicks = 12;
                TargetTicks = 24;
            }
            else if (State == State.Configure)
            {
                SetStopLoss("LongEntry", CalculationMode.Ticks, StopTicks, false);
                SetProfitTarget("LongEntry", CalculationMode.Ticks, TargetTicks);
                SetStopLoss("ShortEntry", CalculationMode.Ticks, StopTicks, false);
                SetProfitTarget("ShortEntry", CalculationMode.Ticks, TargetTicks);
            }
        }

        protected override void OnBarUpdate()
        {
            if (CurrentBar < BarsRequiredToTrade)
                return;

            if (rangeDate != Time[0].Date)
            {
                rangeDate = Time[0].Date;
                rangeHigh = double.MinValue;
                rangeLow = double.MaxValue;
                rangeReady = false;
            }

            int currentTime = Time[0].Hour * 10000
                + Time[0].Minute * 100
                + Time[0].Second;

            if (currentTime >= RangeStart && currentTime <= RangeEnd)
            {
                rangeHigh = Math.Max(rangeHigh, High[0]);
                rangeLow = Math.Min(rangeLow, Low[0]);
                return;
            }

            if (!rangeReady && currentTime > RangeEnd && rangeHigh > double.MinValue)
                rangeReady = true;

            if (!rangeReady || Position.MarketPosition != MarketPosition.Flat)
                return;

            if (Close[0] > rangeHigh)
                EnterLong(1, "LongEntry");
            else if (Close[0] < rangeLow)
                EnterShort(1, "ShortEntry");
        }
    }
}

Direct answer

A NinjaTrader opening range breakout Strategy records a defined high and low during an opening window, then evaluates a breakout after that window closes. The result depends on the instrument, Trading Hours template, bar timing, range window, and order rules. Pineify can generate a single-file NinjaScript Strategy for those stated parameters and return bounded static diagnostics, but it does not run NinjaTrader, supply futures data, place orders, or guarantee a breakout or return.

What to know first

  • Define the opening window in the chart or session time context and document how daylight-saving changes are handled.
  • Record the range only while the window is open, then block new range updates after the window closes.
  • Specify whether a trigger uses a close beyond the range, an intrabar touch, or a different condition.
  • Attach quantity, stop, target, and one-position rules to named managed entry signals.
  • Treat the example as a testable artifact, not a claim that opening range breakouts are profitable.

Supported scope

What this NinjaScript workflow can cover

  • Single-file NinjaScript opening range Strategy
  • Configurable session start and end parameters
  • Frozen range high and low state
  • Close-confirmed breakout example
  • Signal-specific managed stop and target rules
  • External static/API diagnostics and platform test checklist

Define the opening range and session context

Start with a precise window such as the bars between a stated start and end time. State which timestamp source the rule uses, which Trading Hours template is active, and whether the opening range crosses midnight. RTH and ETH are not interchangeable labels, and a date-based reset alone may not represent every exchange session.

Record the high and low only during the opening window. After the window ends, freeze those values for the breakout test until the next defined session reset. If the instrument has holidays or an overnight schedule, test the range against the official session calendar rather than assuming every calendar day is a session.

  • Write the start and end times in the chart time context.
  • Choose a reset rule for a new session and a missing-session day.
  • Record the source bar type and update frequency.

Choose a breakout trigger that can be audited

A close above the frozen range high is different from an intrabar trade through that level. The first depends on bar-close timing and available OHLC data, while the second depends on tick-level behavior, Calculate settings, and the data series. Put the trigger in plain language before generating code.

Add a direction rule, position rule, and duplicate-signal rule. For example, the Strategy may allow one LongEntry or ShortEntry position per session and ignore a second trigger while a position is open. These choices affect test results and should not be hidden inside a vague "breakout bot" label.

  • Specify close confirmation or intrabar trigger behavior.
  • Name the long and short signals and allowed quantity.
  • State what happens after a failed breakout or a second break.

Add protection without implying an execution guarantee

A managed Strategy can set signal-specific stop loss and profit target values before the entry. Choose ticks, price, currency, or another supported CalculationMode intentionally. A trailing stop is a different choice and cannot be combined with SetStopLoss for the same entry signal.

Protective methods describe instructions that NinjaTrader may submit after an execution. They do not guarantee a fill, a price, or a broker response. Test gaps, slippage, partial fills, rejected orders, and session transitions in the platform workflow.

  • Use the same fromEntrySignal on each associated protection method.
  • Keep stop and target values configurable but bounded.
  • Inspect order and execution callbacks when state matters.

Review the NinjaScript artifact and static boundary

The artifact below uses ordinary DateTime fields and a fixed daily window to make the range state visible. It is intentionally a compact managed Strategy example. A production design may need a SessionIterator, a documented Trading Hours template, or a multi-series readiness guard, which should be requested and tested explicitly.

Pineify can check one complete source file with external syntax and supported API diagnostics. That result does not render the range on a NinjaTrader chart, compile generated wrappers, run Strategy Analyzer, or access your selected market data.

  • Review range reset and lookback guards line by line.
  • Compare code times with the chart data box and template.
  • Compile the complete source in NinjaTrader Editor after static checking.

Test the opening range on more than one market condition

Keep the instrument, Bars type, session template, date range, costs, and parameters fixed when comparing code versions. Inspect the individual range, trigger, exit, and no-trade decisions. An opening range can be narrow, wide, missing, or crossed in both directions, and each case needs a defined outcome.

Use a separate validation period and a simulation account after the historical review. A strategy that looks clean on one selected sample can behave differently under another session schedule, data feed, or execution condition.

  • Save the range values and trigger decisions with each test.
  • Include narrow, wide, and no-data opening windows.
  • Treat historical, simulated, and live behavior as separate evidence.

Sources and verification

Last verified:

Pineify provides information and code-generation assistance, not investment advice. Generated code, static checks, backtests, simulations, and historical examples cannot predict prices or guarantee returns. Review all code and test it in NinjaTrader Editor, Strategy Analyzer, and a simulation account before considering live use.

Frequently asked questions

Turn your opening range rules into NinjaScript

Describe the session window, range reset, breakout trigger, quantity, and protection. Pineify will generate one editable Strategy source file for review.