From 751cab995cb085b8c64a8fff60c9971019d53f3c Mon Sep 17 00:00:00 2001 From: moshferatu Date: Sat, 23 Nov 2024 06:19:25 -0800 Subject: [PATCH] Initial commit of VIX Below Moving Average strategy --- .../VIXBelowMovingAverage.cs | 74 +++++++++++++++++++ 1 file changed, 74 insertions(+) create mode 100644 strategies/vix-above-and-below-moving-average/VIXBelowMovingAverage.cs diff --git a/strategies/vix-above-and-below-moving-average/VIXBelowMovingAverage.cs b/strategies/vix-above-and-below-moving-average/VIXBelowMovingAverage.cs new file mode 100644 index 0000000..21655d1 --- /dev/null +++ b/strategies/vix-above-and-below-moving-average/VIXBelowMovingAverage.cs @@ -0,0 +1,74 @@ +#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; } + } +}