136 lines
4.8 KiB
C#
136 lines
4.8 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 MultipleDaysUpDownBot : Strategy
|
||
|
{
|
||
|
private SMA longTermTrend;
|
||
|
private SMA shortTermTrend;
|
||
|
|
||
|
protected override void OnStateChange()
|
||
|
{
|
||
|
if (State == State.SetDefaults)
|
||
|
{
|
||
|
Name = "Multiple Days Up / Down Bot";
|
||
|
Description = @"Taken from chapter 6 of High Probability ETF Trading (2009) by Larry Connors";
|
||
|
Calculate = Calculate.OnBarClose;
|
||
|
EntriesPerDirection = 2;
|
||
|
|
||
|
LongTermTrendPeriod = 200;
|
||
|
ShortTermTrendPeriod = 5;
|
||
|
MinimumDaysUpDown = 4;
|
||
|
DaysToConsider = 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, DaysToConsider))
|
||
|
return;
|
||
|
|
||
|
if (EnableLongTrades)
|
||
|
{
|
||
|
if (Position.MarketPosition == MarketPosition.Long && Close[0] > shortTermTrend[0])
|
||
|
ExitLong();
|
||
|
else if (Close[0] > longTermTrend[0])
|
||
|
{
|
||
|
if (SufficientLowerCloses(MinimumDaysUpDown, DaysToConsider) && 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 (SufficientHigherCloses(MinimumDaysUpDown, DaysToConsider) && Close[0] > shortTermTrend[0] && Position.MarketPosition == MarketPosition.Flat)
|
||
|
EnterShort();
|
||
|
|
||
|
if (EnableAggressiveEntries && Position.MarketPosition == MarketPosition.Short && Close[0] > Position.AveragePrice)
|
||
|
EnterShort();
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
private bool SufficientLowerCloses(int minDays, int daysToConsider)
|
||
|
{
|
||
|
int lowerCloseCount = 0;
|
||
|
|
||
|
for (int i = 0; i < daysToConsider; i++)
|
||
|
{
|
||
|
if (Close[i] < Close[i + 1])
|
||
|
lowerCloseCount++;
|
||
|
}
|
||
|
|
||
|
return lowerCloseCount >= minDays;
|
||
|
}
|
||
|
|
||
|
private bool SufficientHigherCloses(int minDays, int daysToConsider)
|
||
|
{
|
||
|
int higherCloseCount = 0;
|
||
|
|
||
|
for (int i = 0; i < daysToConsider; i++)
|
||
|
{
|
||
|
if (Close[i] > Close[i + 1])
|
||
|
higherCloseCount++;
|
||
|
}
|
||
|
|
||
|
return higherCloseCount >= minDays;
|
||
|
}
|
||
|
|
||
|
#region Properties
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[Range(1, int.MaxValue)]
|
||
|
[Display(Name = "Long-Term Trend Period", GroupName = "Multiple Days Up / Down Bot", Order = 1)]
|
||
|
public int LongTermTrendPeriod { get; set; }
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[Range(1, int.MaxValue)]
|
||
|
[Display(Name = "Short-Term Trend Period", GroupName = "Multiple Days Up / Down Bot", Order = 2)]
|
||
|
public int ShortTermTrendPeriod { get; set; }
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[Range(1, int.MaxValue)]
|
||
|
[Display(Name = "Minimum Days Up / Down", GroupName = "Multiple Days Up / Down Bot", Order = 3)]
|
||
|
public int MinimumDaysUpDown { get; set; }
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[Range(1, int.MaxValue)]
|
||
|
[Display(Name = "Days to Consider", GroupName = "Multiple Days Up / Down Bot", Order = 4)]
|
||
|
public int DaysToConsider { get; set; }
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[Display(Name = "Enable Long Trades", GroupName = "Multiple Days Up / Down Bot", Order = 5)]
|
||
|
public bool EnableLongTrades { get; set; }
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[Display(Name = "Enable Short Trades", GroupName = "Multiple Days Up / Down Bot", Order = 6)]
|
||
|
public bool EnableShortTrades { get; set; }
|
||
|
|
||
|
[NinjaScriptProperty]
|
||
|
[Display(Name = "Enable Aggressive Entries", GroupName = "Multiple Days Up / Down Bot", Order = 7)]
|
||
|
public bool EnableAggressiveEntries { get; set; }
|
||
|
|
||
|
#endregion
|
||
|
}
|
||
|
}
|