Add optional trend filter to Overnight strategy

This commit is contained in:
moshferatu 2024-09-03 06:10:17 -07:00
parent 9808dea300
commit 46938e91b0

View File

@ -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
@ -13,6 +15,8 @@ namespace NinjaTrader.NinjaScript.Strategies
private DateTime sessionClose;
private SMA longTermTrend;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
@ -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; }
}
}