#region Using declarations using NinjaTrader.Cbi; using NinjaTrader.NinjaScript.Indicators; using System; using System.ComponentModel.DataAnnotations; #endregion namespace NinjaTrader.NinjaScript.Strategies { public class PercentBBot : Strategy { private SMA longTermTrend; private PercentB percentB; protected override void OnStateChange() { if (State == State.SetDefaults) { Name = "%B Bot"; Description = @"Taken from chapter 5 of High Probability ETF Trading (2009) by Larry Connors"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 2; LongTermTrendPeriod = 200; PercentBPeriod = 5; PercentBStandardDeviations = 1.0; ConsecutiveDays = 3; EnableLongTrades = true; LongEntry = 0.2; LongExit = 0.8; EnableShortTrades = true; ShortEntry = 0.8; ShortExit = 0.2; EnableAggressiveEntries = true; } else if (State == State.DataLoaded) { longTermTrend = SMA(LongTermTrendPeriod); percentB = PercentB(PercentBPeriod, PercentBStandardDeviations); AddChartIndicator(longTermTrend); AddChartIndicator(percentB); } } protected override void OnBarUpdate() { if (CurrentBar < Math.Max(LongTermTrendPeriod, PercentBPeriod) + ConsecutiveDays) return; if (EnableLongTrades) { if (Position.MarketPosition == MarketPosition.Long && percentB[0] > LongExit) ExitLong(); else if (Close[0] > longTermTrend[0]) { if (ConsecutiveBelow(LongEntry, ConsecutiveDays) && Position.MarketPosition == MarketPosition.Flat) EnterLong(); if (EnableAggressiveEntries && Position.MarketPosition == MarketPosition.Long && percentB[0] < LongEntry) EnterLong(); } } if (EnableShortTrades) { if (Position.MarketPosition == MarketPosition.Short && percentB[0] < ShortExit) ExitShort(); else if (Close[0] < longTermTrend[0]) { if (ConsecutiveAbove(ShortEntry, ConsecutiveDays) && Position.MarketPosition == MarketPosition.Flat) EnterShort(); if (EnableAggressiveEntries && Position.MarketPosition == MarketPosition.Short && percentB[0] > ShortEntry) EnterShort(); } } } private bool ConsecutiveBelow(double threshold, int days) { for (int i = 0; i < days; i++) { if (percentB[i] >= threshold) return false; } return true; } private bool ConsecutiveAbove(double threshold, int days) { for (int i = 0; i < days; i++) { if (percentB[i] <= threshold) return false; } return true; } public override string DisplayName { get { return Name; } } #region Properties [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "Long-Term Trend Period", GroupName = "%B Bot", Order = 1)] public int LongTermTrendPeriod { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "%B Period", GroupName = "%B Bot", Order = 2)] public int PercentBPeriod { get; set; } [NinjaScriptProperty] [Range(0.1, double.MaxValue)] [Display(Name = "%B Standard Deviations", GroupName = "%B Bot", Order = 3)] public double PercentBStandardDeviations { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "Consecutive Days", GroupName = "%B Bot", Order = 4)] public int ConsecutiveDays { get; set; } [NinjaScriptProperty] [Display(Name = "Enable Long Trades", GroupName = "%B Bot", Order = 5)] public bool EnableLongTrades { get; set; } [NinjaScriptProperty] [Range(0.0, 1.0)] [Display(Name = "Long %B Entry", GroupName = "%B Bot", Order = 6)] public double LongEntry { get; set; } [NinjaScriptProperty] [Range(0.0, 1.0)] [Display(Name = "Long %B Exit", GroupName = "%B Bot", Order = 7)] public double LongExit { get; set; } [NinjaScriptProperty] [Display(Name = "Enable Short Trades", GroupName = "%B Bot", Order = 8)] public bool EnableShortTrades { get; set; } [NinjaScriptProperty] [Range(0.0, 1.0)] [Display(Name = "Short %B Entry", GroupName = "%B Bot", Order = 9)] public double ShortEntry { get; set; } [NinjaScriptProperty] [Range(0.0, 1.0)] [Display(Name = "Short %B Exit", GroupName = "%B Bot", Order = 10)] public double ShortExit { get; set; } [NinjaScriptProperty] [Display(Name = "Enable Aggressive Entries", GroupName = "%B Bot", Order = 11)] public bool EnableAggressiveEntries { get; set; } #endregion } }