From 9bcd75ca9670ab33664163ef7a1d95d0cbfc9d3c Mon Sep 17 00:00:00 2001 From: moshferatu Date: Fri, 30 Aug 2024 12:12:13 -0700 Subject: [PATCH] Initial commit of VIX 5% strategy --- strategies/vix-5-percent/VIXFivePercentBot.cs | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 strategies/vix-5-percent/VIXFivePercentBot.cs diff --git a/strategies/vix-5-percent/VIXFivePercentBot.cs b/strategies/vix-5-percent/VIXFivePercentBot.cs new file mode 100644 index 0000000..120393d --- /dev/null +++ b/strategies/vix-5-percent/VIXFivePercentBot.cs @@ -0,0 +1,82 @@ +#region Using declarations +using NinjaTrader.Cbi; +using NinjaTrader.NinjaScript.Indicators; +using System.ComponentModel.DataAnnotations; +#endregion + +namespace NinjaTrader.NinjaScript.Strategies +{ + public class VIXFivePercentBot : Strategy + { + private const int VIXBars = 1; + + private SMA movingAverage; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Taken from chapter 5 of Short Term Trading Strategies That Work"; + Name = "VIX 5% Bot"; + Calculate = Calculate.OnBarClose; + EntriesPerDirection = 1; + EntryHandling = EntryHandling.AllEntries; + IsExitOnSessionCloseStrategy = true; + ExitOnSessionCloseSeconds = 30; + IsFillLimitOnTouch = false; + MaximumBarsLookBack = MaximumBarsLookBack.TwoHundredFiftySix; + OrderFillResolution = OrderFillResolution.Standard; + Slippage = 0; + StartBehavior = StartBehavior.WaitUntilFlat; + TimeInForce = TimeInForce.Gtc; + TraceOrders = false; + RealtimeErrorHandling = RealtimeErrorHandling.StopCancelClose; + StopTargetHandling = StopTargetHandling.PerEntryExecution; + BarsRequiredToTrade = 20; + IsInstantiatedOnEachOptimizationIteration = true; + + MovingAveragePeriod = 10; + } + else if (State == State.Configure) + { + AddDataSeries("^VIX"); + } + else if (State == State.DataLoaded) + { + movingAverage = SMA(BarsArray[VIXBars], MovingAveragePeriod); + } + } + + protected override void OnBarUpdate() + { + if (BarsInProgress != 0) + return; + + if (CurrentBars[VIXBars] < 0) + return; + + if (CurrentBar < BarsRequiredToTrade) + return; + + double upperThreshold = movingAverage[0] * 1.05; + double lowerThreshold = movingAverage[0] * 0.95; + + // Entry condition: If VIX is 5% or more above its 10-day moving average. + if (Closes[VIXBars][0] >= upperThreshold) + EnterLong(); + + // Exit condition: If VIX is 5% or more below its 10-day moving average. + if (Position.MarketPosition == MarketPosition.Long && Closes[VIXBars][0] <= lowerThreshold) + ExitLong(); + } + + public override string DisplayName + { + get { return Name; } + } + + [NinjaScriptProperty] + [Display(Name = "Moving Average Period", GroupName = "VIX 5% Bot", Order = 1)] + public int MovingAveragePeriod { get; set; } + } +}