2024-09-13 18:34:19 +00:00
|
|
|
#region Using declarations
|
|
|
|
using NinjaTrader.Cbi;
|
|
|
|
using NinjaTrader.NinjaScript.Indicators;
|
|
|
|
using System;
|
|
|
|
using System.ComponentModel.DataAnnotations;
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
namespace NinjaTrader.NinjaScript.Strategies
|
|
|
|
{
|
|
|
|
public class ThreeDayHighLowBot : Strategy
|
|
|
|
{
|
|
|
|
private SMA longTermTrend;
|
|
|
|
private SMA shortTermTrend;
|
|
|
|
|
|
|
|
protected override void OnStateChange()
|
|
|
|
{
|
|
|
|
if (State == State.SetDefaults)
|
|
|
|
{
|
|
|
|
Name = "3-Day High / Low Bot";
|
|
|
|
Description = @"Taken from chapter 2 of High Probability ETF Trading (2009) by Larry Connors";
|
|
|
|
Calculate = Calculate.OnBarClose;
|
|
|
|
EntriesPerDirection = 2;
|
|
|
|
IsInstantiatedOnEachOptimizationIteration = false;
|
|
|
|
|
|
|
|
ConsecutiveDays = 3;
|
|
|
|
LongTermTrendPeriod = 200;
|
|
|
|
ShortTermTrendPeriod = 5;
|
|
|
|
EnableLongTrades = true;
|
|
|
|
EnableShortTrades = true;
|
2024-09-13 19:22:27 +00:00
|
|
|
EnableAggressiveEntries = true;
|
2024-09-13 18:34:19 +00:00
|
|
|
}
|
|
|
|
else if (State == State.DataLoaded)
|
|
|
|
{
|
|
|
|
longTermTrend = SMA(LongTermTrendPeriod);
|
|
|
|
shortTermTrend = SMA(ShortTermTrendPeriod);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnBarUpdate()
|
|
|
|
{
|
|
|
|
if (CurrentBar < Math.Max(LongTermTrendPeriod, ConsecutiveDays + 1))
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (EnableLongTrades)
|
|
|
|
{
|
|
|
|
if (Close[0] > longTermTrend[0] && Close[0] < shortTermTrend[0])
|
|
|
|
{
|
|
|
|
if (IsConsecutiveLowerHighsAndLows(ConsecutiveDays))
|
|
|
|
{
|
|
|
|
if (Position.MarketPosition == MarketPosition.Flat)
|
|
|
|
EnterLong();
|
|
|
|
else if (Position.MarketPosition == MarketPosition.Long && Close[0] < Position.AveragePrice
|
|
|
|
&& EnableAggressiveEntries)
|
|
|
|
EnterLong();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Position.MarketPosition == MarketPosition.Long && Close[0] > shortTermTrend[0])
|
|
|
|
ExitLong();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (EnableShortTrades)
|
|
|
|
{
|
|
|
|
if (Close[0] < longTermTrend[0] && Close[0] > shortTermTrend[0])
|
|
|
|
{
|
|
|
|
if (IsConsecutiveHigherHighsAndLows(ConsecutiveDays))
|
|
|
|
{
|
|
|
|
if (Position.MarketPosition == MarketPosition.Flat)
|
|
|
|
EnterShort();
|
|
|
|
else if (Position.MarketPosition == MarketPosition.Short && Close[0] > Position.AveragePrice
|
|
|
|
&& EnableAggressiveEntries)
|
|
|
|
EnterShort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Position.MarketPosition == MarketPosition.Short && Close[0] < shortTermTrend[0])
|
|
|
|
ExitShort();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsConsecutiveLowerHighsAndLows(int days)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < days; i++)
|
|
|
|
{
|
|
|
|
if (High[i] >= High[i + 1] || Low[i] >= Low[i + 1])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool IsConsecutiveHigherHighsAndLows(int days)
|
|
|
|
{
|
|
|
|
for (int i = 0; i < days; i++)
|
|
|
|
{
|
|
|
|
if (High[i] <= High[i + 1] || Low[i] <= Low[i + 1])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-09-25 14:06:34 +00:00
|
|
|
public override string DisplayName
|
|
|
|
{
|
|
|
|
get { return Name; }
|
|
|
|
}
|
|
|
|
|
2024-09-13 18:34:19 +00:00
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(1, int.MaxValue)]
|
|
|
|
[Display(Name = "Consecutive Days", GroupName = "3-Day High / Low Bot", Order = 1)]
|
|
|
|
public int ConsecutiveDays { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(1, int.MaxValue)]
|
|
|
|
[Display(Name = "Long-Term Trend Period", GroupName = "3-Day High / Low Bot", Order = 2)]
|
|
|
|
public int LongTermTrendPeriod { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Range(1, int.MaxValue)]
|
|
|
|
[Display(Name = "Short-Term Trend Period", GroupName = "3-Day High / Low Bot", Order = 3)]
|
|
|
|
public int ShortTermTrendPeriod { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Enable Long Trades", GroupName = "3-Day High / Low Bot", Order = 4)]
|
|
|
|
public bool EnableLongTrades { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Enable Short Trades", GroupName = "3-Day High / Low Bot", Order = 5)]
|
|
|
|
public bool EnableShortTrades { get; set; }
|
|
|
|
|
|
|
|
[NinjaScriptProperty]
|
|
|
|
[Display(Name = "Enable Aggressive Entries", GroupName = "3-Day High / Low Bot", Order = 6)]
|
|
|
|
public bool EnableAggressiveEntries { get; set; }
|
|
|
|
}
|
|
|
|
}
|