Add VIX 5% Bot, taken from Chapter 5 of Short Term Trading Strategies That Work

This commit is contained in:
moshferatu 2024-07-21 05:53:36 -07:00
parent bdac441f1e
commit fb5a073eac

View File

@ -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; }
}
}