From 46938e91b08b290277386747e40813a1bb94c2df Mon Sep 17 00:00:00 2001 From: moshferatu Date: Tue, 3 Sep 2024 06:10:17 -0700 Subject: [PATCH] Add optional trend filter to Overnight strategy --- strategies/overnight/OvernightBot.cs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/strategies/overnight/OvernightBot.cs b/strategies/overnight/OvernightBot.cs index 20d2b4c..e9c0e00 100644 --- a/strategies/overnight/OvernightBot.cs +++ b/strategies/overnight/OvernightBot.cs @@ -1,7 +1,9 @@ #region Using declarations -using System; using NinjaTrader.Cbi; using NinjaTrader.Data; +using NinjaTrader.NinjaScript.Indicators; +using System; +using System.ComponentModel.DataAnnotations; #endregion namespace NinjaTrader.NinjaScript.Strategies @@ -12,6 +14,8 @@ namespace NinjaTrader.NinjaScript.Strategies private const int MinuteBars = 1; private DateTime sessionClose; + + private SMA longTermTrend; protected override void OnStateChange() { @@ -34,6 +38,9 @@ namespace NinjaTrader.NinjaScript.Strategies RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; StopTargetHandling = StopTargetHandling.PerEntryExecution; IsInstantiatedOnEachOptimizationIteration = true; + + UseTrendFilter = false; + LongTermTrendPeriod = 200; } else if (State == State.Configure) { @@ -43,6 +50,8 @@ namespace NinjaTrader.NinjaScript.Strategies { SessionIterator chartSession = new SessionIterator(BarsArray[PrimaryBars]); sessionClose = chartSession.GetTradingDayEndLocal(chartSession.ActualTradingDayExchange); + + longTermTrend = SMA(LongTermTrendPeriod); } } @@ -51,6 +60,9 @@ namespace NinjaTrader.NinjaScript.Strategies 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(); @@ -62,5 +74,14 @@ namespace NinjaTrader.NinjaScript.Strategies { 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; } } }