ninjatrader/strategies/vix-rsi/VIXRSIBot.cs

110 lines
3.7 KiB
C#
Raw Permalink Normal View History

2024-09-19 12:05:47 +00:00
#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class VIXRSIBot : Strategy
{
private const int VIXBars = 1;
private RSI rsi;
private RSI vixRsi;
private SMA longTermTrend;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Taken from chapter 12 of Short Term Trading Strategies That Work";
Name = "VIX RSI 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;
RSIPeriod = 2;
RSISmoothing = 1;
RSIEntryThreshold = 30;
RSIExitThreshold = 65;
VIXRSIEntryThreshold = 90;
}
else if (State == State.Configure)
{
AddDataSeries("^VIX");
}
else if (State == State.DataLoaded)
{
rsi = RSI(RSIPeriod, RSISmoothing);
vixRsi = RSI(BarsArray[VIXBars], RSIPeriod, RSISmoothing);
longTermTrend = SMA(LongTermTrendPeriod);
}
}
protected override void OnBarUpdate()
{
if (BarsInProgress != 0)
return;
if (CurrentBars[VIXBars] < 0)
return;
if (CurrentBar < BarsRequiredToTrade)
return;
if (Close[0] > longTermTrend[0] &&
vixRsi[0] > VIXRSIEntryThreshold &&
Opens[VIXBars][0] > Closes[VIXBars][1] &&
rsi[0] < RSIEntryThreshold)
EnterLong();
else if (rsi[0] > RSIExitThreshold)
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
2024-09-19 12:05:47 +00:00
[NinjaScriptProperty]
2024-09-19 12:29:46 +00:00
[Display(Name = "Long-Term Trend Period", GroupName = "VIX RSI Bot", Order = 1)]
2024-09-19 12:05:47 +00:00
public int LongTermTrendPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Period", GroupName = "VIX RSI Bot", Order = 2)]
public int RSIPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Smoothing", GroupName = "VIX RSI Bot", Order = 3)]
public int RSISmoothing { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Entry Threshold", GroupName = "VIX RSI Bot", Order = 4)]
public int RSIEntryThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Exit Threshold", GroupName = "VIX RSI Bot", Order = 5)]
public int RSIExitThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "VIX RSI Entry Threshold", GroupName = "VIX RSI Bot", Order = 6)]
public int VIXRSIEntryThreshold { get; set; }
}
}