#region Using declarations using NinjaTrader.Cbi; using NinjaTrader.Data; using NinjaTrader.NinjaScript.Indicators; using System; using System.ComponentModel.DataAnnotations; #endregion namespace NinjaTrader.NinjaScript.Strategies { public class OvernightBot : Strategy { private const int PrimaryBars = 0; private const int MinuteBars = 1; private DateTime sessionClose; private SMA longTermTrend; 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; UseTrendFilter = false; LongTermTrendPeriod = 200; } 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); longTermTrend = SMA(LongTermTrendPeriod); } } protected override void OnBarUpdate() { if (BarsInProgress != MinuteBars) return; if (UseTrendFilter && Close[0] < longTermTrend[0]) 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; } } [NinjaScriptProperty] [Display(Name = "Use Trend Filter", GroupName = "Overnight Bot", Order = 1)] public bool UseTrendFilter { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "Long Term Trend Period", GroupName = "Overnight Bot", Order = 2)] public int LongTermTrendPeriod { get; set; } } }