#region Using declarations using NinjaTrader.Cbi; using NinjaTrader.NinjaScript.Indicators; using System; using System.ComponentModel.DataAnnotations; #endregion namespace NinjaTrader.NinjaScript.Strategies { public class R3Bot : Strategy { private SMA longTermTrend; private RSI rsi; protected override void OnStateChange() { if (State == State.SetDefaults) { Description = @"Taken from chapter 4 of High Probability ETF Trading (2009) by Larry Connors"; Name = "R3 Bot"; Calculate = Calculate.OnBarClose; EntriesPerDirection = 2; LongTermTrendPeriod = 200; RSIPeriod = 2; RSISmoothing = 1; ConsecutiveDays = 3; EnableLongTrades = true; LongFirstDayRSI = 60.0; LongRSIEntry = 10.0; LongRSIExit = 70.0; EnableShortTrades = true; ShortFirstDayRSI = 40.0; ShortRSIEntry = 90.0; ShortRSIExit = 30.0; EnableAggressiveEntries = true; } else if (State == State.DataLoaded) { longTermTrend = SMA(LongTermTrendPeriod); rsi = RSI(RSIPeriod, RSISmoothing); AddChartIndicator(longTermTrend); AddChartIndicator(rsi); } } protected override void OnBarUpdate() { if (CurrentBar < Math.Max(LongTermTrendPeriod, ConsecutiveDays)) return; if (EnableLongTrades) { if (Position.MarketPosition == MarketPosition.Long && rsi[0] > LongRSIExit) ExitLong(); else if (Close[0] > longTermTrend[0]) { if (ConsecutiveRSIDrop(ConsecutiveDays) && rsi[ConsecutiveDays - 1] < LongFirstDayRSI) { if (rsi[0] < LongRSIEntry) { if (Position.MarketPosition == MarketPosition.Flat) EnterLong(); } } if (EnableAggressiveEntries && Position.MarketPosition == MarketPosition.Long && Close[0] < Position.AveragePrice) EnterLong(); } } if (EnableShortTrades) { if (Position.MarketPosition == MarketPosition.Short && rsi[0] < ShortRSIExit) ExitShort(); else if (Close[0] < longTermTrend[0]) { if (ConsecutiveRSIRise(ConsecutiveDays) && rsi[ConsecutiveDays - 1] > ShortFirstDayRSI) { if (rsi[0] > ShortRSIEntry) { if (Position.MarketPosition == MarketPosition.Flat) EnterShort(); } } if (EnableAggressiveEntries && Position.MarketPosition == MarketPosition.Short && Close[0] > Position.AveragePrice) EnterShort(); } } } private bool ConsecutiveRSIDrop(int days) { for (int i = 0; i < days - 1; i++) { if (rsi[i] >= rsi[i + 1]) return false; } return true; } private bool ConsecutiveRSIRise(int days) { for (int i = 0; i < days - 1; i++) { if (rsi[i] <= rsi[i + 1]) return false; } return true; } #region Properties [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "Long-Term Trend Period", GroupName = "R3 Bot", Order = 1)] public int LongTermTrendPeriod { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "RSI Period", GroupName = "R3 Bot", Order = 2)] public int RSIPeriod { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "RSI Smoothing", GroupName = "R3 Bot", Order = 3)] public int RSISmoothing { get; set; } [NinjaScriptProperty] [Range(1, int.MaxValue)] [Display(Name = "Consecutive Days", GroupName = "R3 Bot", Order = 4)] public int ConsecutiveDays { get; set; } [NinjaScriptProperty] [Display(Name = "Enable Long Trades", GroupName = "R3 Bot", Order = 5)] public bool EnableLongTrades { get; set; } [NinjaScriptProperty] [Range(0.0, 100.0)] [Display(Name = "Long First Day RSI", GroupName = "R3 Bot", Order = 6)] public double LongFirstDayRSI { get; set; } [NinjaScriptProperty] [Range(0.0, 100.0)] [Display(Name = "Long RSI Entry", GroupName = "R3 Bot", Order = 7)] public double LongRSIEntry { get; set; } [NinjaScriptProperty] [Range(0.0, 100.0)] [Display(Name = "Long RSI Exit", GroupName = "R3 Bot", Order = 8)] public double LongRSIExit { get; set; } [NinjaScriptProperty] [Display(Name = "Enable Short Trades", GroupName = "R3 Bot", Order = 9)] public bool EnableShortTrades { get; set; } [NinjaScriptProperty] [Range(0.0, 100.0)] [Display(Name = "Short First Day RSI", GroupName = "R3 Bot", Order = 10)] public double ShortFirstDayRSI { get; set; } [NinjaScriptProperty] [Range(0.0, 100.0)] [Display(Name = "Short RSI Entry", GroupName = "R3 Bot", Order = 11)] public double ShortRSIEntry { get; set; } [NinjaScriptProperty] [Range(0.0, 100.0)] [Display(Name = "Short RSI Exit", GroupName = "R3 Bot", Order = 12)] public double ShortRSIExit { get; set; } [NinjaScriptProperty] [Display(Name = "Enable Aggressive Entries", GroupName = "R3 Bot", Order = 13)] public bool EnableAggressiveEntries { get; set; } #endregion } }