ninjatrader/strategies/vol-panics/VolPanicsBot.cs

83 lines
2.9 KiB
C#

#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class VolPanicsBot : Strategy
{
private SMA movingAverage;
private RSI rsi;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Description = @"Taken from chapter 5 of Buy the Fear, Sell the Greed (2018) by Larry Connors";
Name = "Vol Panics 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;
IsInstantiatedOnEachOptimizationIteration = true;
MovingAveragePeriod = 5;
RSIPeriod = 4;
RSISmoothing = 1;
EntryThreshold = 70;
}
else if (State == State.DataLoaded)
{
movingAverage = SMA(MovingAveragePeriod);
rsi = RSI(RSIPeriod, RSISmoothing);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(MovingAveragePeriod, RSIPeriod))
return;
if (Close[0] > movingAverage[0] && rsi[0] > EntryThreshold)
EnterShort();
if (Position.MarketPosition == MarketPosition.Short && Close[0] < movingAverage[0])
ExitShort();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Moving Average Period", GroupName = "Vol Panics Bot", Order = 1)]
public int MovingAveragePeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Period", GroupName = "Vol Panics Bot", Order = 2)]
public int RSIPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Smoothing", GroupName = "Vol Panics Bot", Order = 3)]
public int RSISmoothing { get; set; }
[NinjaScriptProperty]
[Display(Name = "RSI Entry Threshold", GroupName = "Vol Panics Bot", Order = 4)]
public int EntryThreshold { get; set; }
}
}