94 lines
3.1 KiB
C#
94 lines
3.1 KiB
C#
#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;
|
|
EntryPercent = 5;
|
|
ExitPercent = 5;
|
|
}
|
|
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 entryThreshold = movingAverage[0] * (1 + EntryPercent / 100.0);
|
|
double exitThreshold = movingAverage[0] * (1 - ExitPercent / 100.0);
|
|
|
|
if (Closes[VIXBars][0] >= entryThreshold)
|
|
EnterLong();
|
|
|
|
if (Position.MarketPosition == MarketPosition.Long && Closes[VIXBars][0] <= exitThreshold)
|
|
ExitLong();
|
|
}
|
|
|
|
public override string DisplayName
|
|
{
|
|
get { return Name; }
|
|
}
|
|
|
|
[NinjaScriptProperty]
|
|
[Range(0.1, 100)]
|
|
[Display(Name = "Entry Percent", GroupName = "VIX 5% Bot", Order = 1)]
|
|
public double EntryPercent { get; set; }
|
|
|
|
[NinjaScriptProperty]
|
|
[Range(0.1, 100)]
|
|
[Display(Name = "Exit Percent", GroupName = "VIX 5% Bot", Order = 2)]
|
|
public double ExitPercent { get; set; }
|
|
|
|
[NinjaScriptProperty]
|
|
[Range(1, int.MaxValue)]
|
|
[Display(Name = "Moving Average Period", GroupName = "VIX 5% Bot", Order = 3)]
|
|
public int MovingAveragePeriod { get; set; }
|
|
}
|
|
}
|