ninjatrader/strategies/3-day-high-low/ThreeDayHighLowBot.cs

126 lines
4.5 KiB
C#

#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;
EnableAggressiveEntries = true;
}
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 (Position.MarketPosition == MarketPosition.Long && Close[0] > shortTermTrend[0])
ExitLong();
else if (Close[0] > longTermTrend[0])
{
if (IsConsecutiveLowerHighsAndLows(ConsecutiveDays) && Close[0] < shortTermTrend[0] && Position.MarketPosition == MarketPosition.Flat)
EnterLong();
if (EnableAggressiveEntries && Position.MarketPosition == MarketPosition.Long && Close[0] < Position.AveragePrice)
EnterLong();
}
}
if (EnableShortTrades)
{
if (Position.MarketPosition == MarketPosition.Short && Close[0] < shortTermTrend[0])
ExitShort();
else if (Close[0] < longTermTrend[0])
{
if (IsConsecutiveHigherHighsAndLows(ConsecutiveDays) && Close[0] > shortTermTrend[0] && Position.MarketPosition == MarketPosition.Flat)
EnterShort();
if (EnableAggressiveEntries && Position.MarketPosition == MarketPosition.Short && Close[0] > Position.AveragePrice)
EnterShort();
}
}
}
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;
}
public override string DisplayName
{
get { return Name; }
}
[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; }
}
}