ninjatrader/strategies/vol-panics/VolPanicsBot.cs

96 lines
3.4 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;
private double entryPrice;
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 = 2;
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;
EnableAggressiveEntries = true;
}
else if (State == State.DataLoaded)
{
movingAverage = SMA(MovingAveragePeriod);
rsi = RSI(RSIPeriod, RSISmoothing);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(MovingAveragePeriod, RSIPeriod))
return;
if (Position.MarketPosition == MarketPosition.Flat && Close[0] > movingAverage[0] && rsi[0] > EntryThreshold)
{
EnterShort();
entryPrice = Close[0];
}
if (Position.MarketPosition == MarketPosition.Short && EnableAggressiveEntries && Close[0] > entryPrice)
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; }
[NinjaScriptProperty]
[Display(Name = "Enable Aggressive Entries", GroupName = "Vol Panics Bot", Order = 5)]
public bool EnableAggressiveEntries { get; set; }
}
}