ninjatrader/strategies/connors-rsi/ConnorsRSIBot.cs

93 lines
3.2 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 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";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
RSIPeriod = 3;
StreakRSIPeriod = 2;
PercentRankPeriod = 100;
LongEntryThreshold = 15;
ShortEntryThreshold = 85;
LongTermTrendPeriod = 200;
ShortTermTrendPeriod = 5;
}
else if (State == State.DataLoaded)
{
connorsRSI = ConnorsRSI(RSIPeriod, StreakRSIPeriod, PercentRankPeriod);
longTermTrend = SMA(LongTermTrendPeriod);
shortTermTrend = SMA(ShortTermTrendPeriod);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(LongTermTrendPeriod, PercentRankPeriod))
return;
if (Close[0] > longTermTrend[0] && connorsRSI[0] < LongEntryThreshold)
EnterLong();
else if (Close[0] < longTermTrend[0] && connorsRSI[0] > ShortEntryThreshold)
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; }
}
[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)]
public int StreakRSIPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Percent Rank Period", GroupName = "ConnorsRSI Bot", Order = 3)]
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)]
public int LongTermTrendPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Short-Term Trend Period", GroupName = "ConnorsRSI Bot", Order = 7)]
public int ShortTermTrendPeriod { get; set; }
}
}