ninjatrader/strategies/vix-above-and-below-moving-average/VIXBelowMovingAverage.cs

75 lines
2.4 KiB
C#

#region Using declarations
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class VIXBelowMovingAverage : Strategy
{
private const int PrimaryBars = 0;
private const int VIXBars = 1;
private SMA ShortTermTrend;
private SMA LongTermTrend;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "VIX Below Moving Average";
Description = @"Based on chatper 10 of How Markets Really Work (2012) by Larry Connors";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
MovingAveragePeriod = 10;
LongTermTrendPeriod = 200;
PercentAbove = 10;
DaysToExit = 5;
}
else if (State == State.Configure)
{
AddDataSeries("^VIX");
}
else if (State == State.DataLoaded)
{
ShortTermTrend = SMA(Closes[VIXBars], MovingAveragePeriod);
LongTermTrend = SMA(LongTermTrendPeriod);
}
}
protected override void OnBarUpdate()
{
if (PrimaryBars != BarsInProgress || CurrentBar < MovingAveragePeriod)
return;
if (Close[0] > LongTermTrend[0] && Closes[VIXBars][0] < ShortTermTrend[0] * (1 + PercentAbove / 100))
EnterLong();
if (BarsSinceEntryExecution(PrimaryBars, "", 0) >= DaysToExit)
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Moving Average Period", GroupName = "VIX Below Moving Average", Order = 1)]
public int MovingAveragePeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Long-Term Trend Period", GroupName = "VIX Below Moving Average", Order = 2)]
public int LongTermTrendPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Percent Above", GroupName = "VIX Below Moving Average", Order = 3)]
public double PercentAbove { get; set; }
[NinjaScriptProperty]
[Display(Name = "Days to Exit", GroupName = "VIX Below Moving Average", Order = 4)]
public int DaysToExit { get; set; }
}
}