ninjatrader/strategies/connors-rsi/ConnorsRSIBot.cs

93 lines
3.2 KiB
C#
Raw Normal View History

2024-08-27 19:43:08 +00:00
#region Using declarations
using NinjaTrader.Cbi;
using NinjaTrader.NinjaScript.Indicators;
using System;
2024-08-28 19:21:08 +00:00
using System.ComponentModel.DataAnnotations;
2024-08-27 19:43:08 +00:00
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class ConnorsRSIBot : Strategy
{
private ConnorsRSI connorsRSI;
private SMA longTermTrend;
private SMA shortTermTrend;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "Connors RSI Bot";
Description = @"Strategy based on the ConnorsRSI indicator developed by Larry Connors";
2024-08-27 19:43:08 +00:00
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
2024-08-28 19:21:08 +00:00
RSIPeriod = 3;
StreakRSIPeriod = 2;
PercentRankPeriod = 100;
LongEntryThreshold = 15;
ShortEntryThreshold = 85;
2024-08-28 19:21:08 +00:00
LongTermTrendPeriod = 200;
ShortTermTrendPeriod = 5;
2024-08-27 19:43:08 +00:00
}
else if (State == State.DataLoaded)
{
connorsRSI = ConnorsRSI(RSIPeriod, StreakRSIPeriod, PercentRankPeriod);
2024-08-27 19:43:08 +00:00
2024-08-28 19:21:08 +00:00
longTermTrend = SMA(LongTermTrendPeriod);
shortTermTrend = SMA(ShortTermTrendPeriod);
2024-08-27 19:43:08 +00:00
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(LongTermTrendPeriod, PercentRankPeriod))
2024-08-27 19:43:08 +00:00
return;
if (Close[0] > longTermTrend[0] && connorsRSI[0] < LongEntryThreshold)
2024-08-27 19:43:08 +00:00
EnterLong();
else if (Close[0] < longTermTrend[0] && connorsRSI[0] > ShortEntryThreshold)
2024-08-27 19:43:08 +00:00
EnterShort();
if (Position.MarketPosition == MarketPosition.Long && Close[0] > shortTermTrend[0])
ExitLong();
else if (Position.MarketPosition == MarketPosition.Short && Close[0] < shortTermTrend[0])
ExitShort();
}
public override string DisplayName
{
get { return Name; }
}
2024-08-28 19:21:08 +00:00
[NinjaScriptProperty]
[Display(Name = "RSI Period", GroupName = "ConnorsRSI Bot", Order = 1)]
public int RSIPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Streak RSI Period", GroupName = "ConnorsRSI Bot", Order = 2)]
2024-08-28 19:21:08 +00:00
public int StreakRSIPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Percent Rank Period", GroupName = "ConnorsRSI Bot", Order = 3)]
2024-08-28 19:21:08 +00:00
public int PercentRankPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Long Entry Threshold", GroupName = "ConnorsRSI Bot", Order = 4)]
public double LongEntryThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "Short Entry Threshold", GroupName = "ConnorsRSI Bot", Order = 5)]
public double ShortEntryThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "Long-Term Trend Period", GroupName = "ConnorsRSI Bot", Order = 6)]
2024-08-28 19:21:08 +00:00
public int LongTermTrendPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Short-Term Trend Period", GroupName = "ConnorsRSI Bot", Order = 7)]
2024-08-28 19:21:08 +00:00
public int ShortTermTrendPeriod { get; set; }
2024-08-27 19:43:08 +00:00
}
}