From b73dcd1cf14ca357b4bc20d1c5b3323df13f039e Mon Sep 17 00:00:00 2001 From: moshferatu Date: Wed, 18 Sep 2024 07:21:38 -0700 Subject: [PATCH] Initial commit of VIX stretches strategy --- strategies/vix-stretches/VIXStretchesBot.cs | 117 ++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 strategies/vix-stretches/VIXStretchesBot.cs diff --git a/strategies/vix-stretches/VIXStretchesBot.cs b/strategies/vix-stretches/VIXStretchesBot.cs new file mode 100644 index 0000000..05ab780 --- /dev/null +++ b/strategies/vix-stretches/VIXStretchesBot.cs @@ -0,0 +1,117 @@ +#region Using declarations +using NinjaTrader.Cbi; +using NinjaTrader.NinjaScript.Indicators; +using System.ComponentModel.DataAnnotations; +#endregion + +namespace NinjaTrader.NinjaScript.Strategies +{ + public class VIXStretchesBot : Strategy + { + private const int VIXBars = 1; + + private SMA longTermTrend; + private SMA vixMovingAverage; + private RSI rsi; + + private int vixStretchDays; + + protected override void OnStateChange() + { + if (State == State.SetDefaults) + { + Description = @"Taken from chapter 12 of Short Term Trading Strategies That Work"; + Name = "VIX Stretches 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; + + LongTermTrendPeriod = 200; + VIXMovingAveragePeriod = 10; + VIXStretchDaysThreshold = 3; + RSIPeriod = 2; + RSISmoothing = 1; + RSIExitThreshold = 65; + } + else if (State == State.Configure) + { + AddDataSeries("^VIX"); + + vixStretchDays = 0; + } + else if (State == State.DataLoaded) + { + longTermTrend = SMA(LongTermTrendPeriod); + vixMovingAverage = SMA(BarsArray[VIXBars], VIXMovingAveragePeriod); + rsi = RSI(RSIPeriod, RSISmoothing); + } + } + + protected override void OnBarUpdate() + { + if (BarsInProgress != 0) + return; + + if (CurrentBars[VIXBars] < 0) + return; + + if (CurrentBar < BarsRequiredToTrade) + return; + + double upperThreshold = vixMovingAverage[0] * 1.05; + + if (Closes[VIXBars][0] >= upperThreshold) + vixStretchDays++; + else + vixStretchDays = 0; + + if (Close[0] > longTermTrend[0] && vixStretchDays >= VIXStretchDaysThreshold) + EnterLong(); + + if (Position.MarketPosition == MarketPosition.Long && rsi[0] >= RSIExitThreshold) + ExitLong(); + } + + public override string DisplayName + { + get { return Name; } + } + + [NinjaScriptProperty] + [Display(Name = "Long Term Trend Period", GroupName = "VIX Stretches Bot", Order = 1)] + public int LongTermTrendPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "VIX Moving Average Period", GroupName = "VIX Stretches Bot", Order = 2)] + public int VIXMovingAveragePeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "VIX Stretch Days", GroupName = "VIX Stretches Bot", Order = 3)] + public int VIXStretchDaysThreshold { get; set; } + + [NinjaScriptProperty] + [Display(Name = "RSI Period", GroupName = "VIX Stretches Bot", Order = 4)] + public int RSIPeriod { get; set; } + + [NinjaScriptProperty] + [Display(Name = "RSI Smoothing", GroupName = "VIX Stretches Bot", Order = 5)] + public int RSISmoothing { get; set; } + + [NinjaScriptProperty] + [Display(Name = "RSI Exit Threshold", GroupName = "VIX Stretches Bot", Order = 6)] + public int RSIExitThreshold { get; set; } + } +}