ninjatrader/strategies/rsi-10-6-90-94/RSITenSix.cs

134 lines
4.7 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 RSITenSixBot : Strategy
{
private SMA longTermTrend;
private SMA shortTermTrend;
private RSI rsi;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "RSI 10-6 / 90-94 Bot";
Description = @"Taken from chapter 7 of High Probability ETF Trading (2009) by Larry Connors";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 2;
LongTermTrendPeriod = 200;
ShortTermTrendPeriod = 5;
RSIPeriod = 2;
RSISmoothing = 1;
EnableLongTrades = true;
RSILongEntry1 = 10;
RSILongEntry2 = 6;
EnableShortTrades = true;
RSIShortEntry1 = 90;
RSIShortEntry2 = 94;
}
else if (State == State.DataLoaded)
{
longTermTrend = SMA(LongTermTrendPeriod);
shortTermTrend = SMA(ShortTermTrendPeriod);
rsi = RSI(RSIPeriod, RSISmoothing);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(LongTermTrendPeriod, Math.Max(ShortTermTrendPeriod, RSIPeriod)))
return;
if (EnableLongTrades)
{
if (Position.MarketPosition == MarketPosition.Long && Close[0] > shortTermTrend[0])
ExitLong();
else if (Close[0] > longTermTrend[0])
{
if (rsi[0] < RSILongEntry1 && Position.MarketPosition == MarketPosition.Flat)
EnterLong();
else if (rsi[0] < RSILongEntry2 && Position.MarketPosition == MarketPosition.Long)
EnterLong();
}
}
if (EnableShortTrades)
{
if (Position.MarketPosition == MarketPosition.Short && Close[0] < shortTermTrend[0])
ExitShort();
else if (Close[0] < longTermTrend[0])
{
if (rsi[0] > RSIShortEntry1 && Position.MarketPosition == MarketPosition.Flat)
EnterShort();
else if (rsi[0] > RSIShortEntry2 && Position.MarketPosition == MarketPosition.Short)
EnterShort();
}
}
}
public override string DisplayName
{
get { return Name; }
}
#region Properties
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Long-Term Trend Period", GroupName = "RSI 10-6 / 90-94 Bot", Order = 1)]
public int LongTermTrendPeriod { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Short-Term Trend Period", GroupName = "RSI 10-6 / 90-94 Bot", Order = 2)]
public int ShortTermTrendPeriod { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "RSI Period", GroupName = "RSI 10-6 / 90-94 Bot", Order = 3)]
public int RSIPeriod { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "RSI Smoothing", GroupName = "RSI 10-6 / 90-94 Bot", Order = 4)]
public int RSISmoothing { get; set; }
[NinjaScriptProperty]
[Display(Name = "Enable Long Trades", GroupName = "RSI 10-6 / 90-94 Bot", Order = 5)]
public bool EnableLongTrades { get; set; }
[NinjaScriptProperty]
[Range(1, 100)]
[Display(Name = "RSI Long Entry 1", GroupName = "RSI 10-6 / 90-94 Bot", Order = 6)]
public int RSILongEntry1 { get; set; }
[NinjaScriptProperty]
[Range(1, 100)]
[Display(Name = "RSI Long Entry 2", GroupName = "RSI 10-6 / 90-94 Bot", Order = 7)]
public int RSILongEntry2 { get; set; }
[NinjaScriptProperty]
[Display(Name = "Enable Short Trades", GroupName = "RSI 10-6 / 90-94 Bot", Order = 8)]
public bool EnableShortTrades { get; set; }
[NinjaScriptProperty]
[Range(1, 100)]
[Display(Name = "RSI Short Entry 1", GroupName = "RSI 10-6 / 90-94 Bot", Order = 9)]
public int RSIShortEntry1 { get; set; }
[NinjaScriptProperty]
[Range(1, 100)]
[Display(Name = "RSI Short Entry 2", GroupName = "RSI 10-6 / 90-94 Bot", Order = 10)]
public int RSIShortEntry2 { get; set; }
#endregion
}
}