From 9a8f678521b20b05a5215e3f0179b1e207e4f69f Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 3 Sep 2024 05:47:34 -0700 Subject: [PATCH] Initial commit of Overnight strategy --- strategies/overnight/OvernightBot.cs | 66 ++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 strategies/overnight/OvernightBot.cs diff --git a/strategies/overnight/OvernightBot.cs b/strategies/overnight/OvernightBot.cs new file mode 100644 index 0000000..20d2b4c --- /dev/null +++ b/strategies/overnight/OvernightBot.cs @@ -0,0 +1,66 @@ +#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; } + } + } +}