#region Using declarations using System; using NinjaTrader.Cbi; using NinjaTrader.Data; #endregion namespace NinjaTrader.NinjaScript.Strategies { public class OvernightBot : Strategy { private const int PrimaryBars = 0; private const int MinuteBars = 1; private DateTime sessionClose; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Taken from Chapter 7 of Short Term Trading Strategies That Work"; Name = "Overnight Bot"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 1; EntryHandling = EntryHandling.AllEntries; IsExitOnSessionCloseStrategy = false; 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; IsInstantiatedOnEachOptimizationIteration = true; } else if (State == State.Configure) { AddDataSeries(BarsPeriodType.Minute, 1); } else if (State == State.DataLoaded) { SessionIterator chartSession = new SessionIterator(BarsArray[PrimaryBars]); sessionClose = chartSession.GetTradingDayEndLocal(chartSession.ActualTradingDayExchange); } } protected override void OnBarUpdate() { if (BarsInProgress != MinuteBars) return; // It's not possible to enter trades on the close of a bar, so we enter on the open of the minute bar prior to session close. if (ToTime(Time[0]) == ToTime(sessionClose.AddMinutes(-1))) EnterLong(); else if (Bars.IsLastBarOfSession) // Technically exits at the open of the next session since we're using OnBarClose. ExitLong(); } public override string DisplayName { get { return Name; } } } }