#region Using declarations using NinjaTrader.Cbi; using NinjaTrader.NinjaScript.Indicators; using System; using System.ComponentModel.DataAnnotations; #endregion namespace NinjaTrader.NinjaScript.Strategies { public class VolPanicsBot235 : Strategy { private SMA movingAverage; private RSI rsi; private int entryCount; private double latestEntryPrice; protected override void OnStateChange() { if (State == State.SetDefaults) { Name = "Vol Panics Bot (2-3-5 Scale-In)"; Description = @"Taken from chapter 5 of Buy the Fear, Sell the Greed (2018) by Larry Connors"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 3; MovingAveragePeriod = 5; RSIPeriod = 4; RSISmoothing = 1; EntryThreshold = 70; TotalPositionSize = 1000; } 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) { entryCount = 1; latestEntryPrice = Close[0]; EnterShort(CalculateQuantity(20)); } if (Position.MarketPosition == MarketPosition.Short && Close[0] > latestEntryPrice) { entryCount += 1; latestEntryPrice = Close[0]; if (entryCount == 2) EnterShort(CalculateQuantity(30)); else if (entryCount == 3) EnterShort(CalculateQuantity(50)); } if (Position.MarketPosition == MarketPosition.Short && Close[0] < movingAverage[0]) ExitShort(); } private int CalculateQuantity(double percent) { double dollarAmount = TotalPositionSize * (percent / 100.0); int quantity = Convert.ToInt32(Math.Floor(dollarAmount / Close[0])); return quantity > 0 ? quantity : 1; } public override string DisplayName { get { return Name; } } [NinjaScriptProperty] [Display(Name = "Moving Average Period", GroupName = "Vol Panics Bot (2-3-5 Scale-In)", Order = 1)] public int MovingAveragePeriod { get; set; } [NinjaScriptProperty] [Display(Name = "RSI Period", GroupName = "Vol Panics Bot (2-3-5 Scale-In)", Order = 2)] public int RSIPeriod { get; set; } [NinjaScriptProperty] [Display(Name = "RSI Smoothing", GroupName = "Vol Panics Bot (2-3-5 Scale-In)", Order = 3)] public int RSISmoothing { get; set; } [NinjaScriptProperty] [Display(Name = "RSI Entry Threshold", GroupName = "Vol Panics Bot (2-3-5 Scale-In)", Order = 4)] public int EntryThreshold { get; set; } [NinjaScriptProperty] [Display(Name = "Total Position Size", GroupName = "Vol Panics Bot (2-3-5 Scale-In)", Order = 5)] public int TotalPositionSize { get; set; } } }