#region Using declarations using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.NinjaScript.Indicators; #endregion //This namespace holds Strategies in this folder and is required. Do not change it. namespace NinjaTrader.NinjaScript.Strategies { public class OpeningRangeBot : Strategy { private const int PrimaryBars = 0; private OpeningRange OR; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Strategy based on opening range breakouts"; Name = "Opening Range Bot"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = true; ExitOnSessionCloseSeconds = 30; IsFillLimitOnTouch = false; MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; OrderFillResolution = OrderFillResolution.Standard; Slippage = 0; StartBehavior = StartBehavior.WaitUntilFlat; TimeInForce = TimeInForce.Gtc; TraceOrders = false; RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; BarsRequiredToTrade = 20; IsInstantiatedOnEachOptimizationIteration = true; } else if (State == State.Configure) { // Data series required by the opening range indicator, must be loaded here. AddDataSeries(BarsPeriodType.Minute, 1); AddDataSeries(Instrument.FullName, BarsPeriod, "US Equities RTH"); } else if (State == State.DataLoaded) { OR = OpeningRange(30, OpeningRangeBarType.Minutes); } } protected override void OnBarUpdate() { if (PrimaryBars != BarsInProgress) return; // TODO: Make this timezone-agnostic. if (ToTime(Time[0]) <= 70000) return; if (Position.MarketPosition != MarketPosition.Flat) return; if (Open[0] < OR.ORH[0] && Close[0] > OR.ORH[0]) { SetStopLoss(CalculationMode.Price, Low[0]); EnterLong(); } else if (Open[0] > OR.ORL[0] && Close[0] < OR.ORL[0]) { SetStopLoss(CalculationMode.Price, High[0]); EnterShort(); } } } }