ninjatrader/strategies/vix-stretches/VIXStretchesBot.cs

118 lines
4.0 KiB
C#
Raw Normal View History

#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]
2024-09-19 12:30:14 +00:00
[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; }
}
}