ninjatrader/strategies/triple-rsi-drop/TripleRSIDrop.cs

106 lines
3.1 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 TripleRSIDrop : Strategy
{
private SMA longTermTrend;
private RSI rsi;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "Triple RSI Drop";
Description = @"Similar to R3, credit to Larry Connors";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
LongTermTrendPeriod = 200;
RSIPeriod = 5;
ConsecutiveDays = 3;
FirstDayRSI = 60.0;
RSIEntry = 30.0;
RSIExit = 50.0;
}
else if (State == State.DataLoaded)
{
longTermTrend = SMA(LongTermTrendPeriod);
rsi = RSI(RSIPeriod, 1);
AddChartIndicator(longTermTrend);
AddChartIndicator(rsi);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < Math.Max(LongTermTrendPeriod, ConsecutiveDays))
return;
if (Close[0] > longTermTrend[0])
{
if (RSIDrop(ConsecutiveDays) && rsi[ConsecutiveDays - 1] < FirstDayRSI && rsi[0] < RSIEntry)
EnterLong();
}
if (Position.MarketPosition == MarketPosition.Long && rsi[0] > RSIExit)
ExitLong();
}
private bool RSIDrop(int days)
{
for (int i = 0; i < days - 1; i++)
{
if (rsi[i] >= rsi[i + 1])
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 = "Triple RSI Bot", Order = 1)]
public int LongTermTrendPeriod { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "RSI Period", GroupName = "Triple RSI Bot", Order = 2)]
public int RSIPeriod { get; set; }
[NinjaScriptProperty]
[Range(1, int.MaxValue)]
[Display(Name = "Consecutive Days", GroupName = "Triple RSI Bot", Order = 3)]
public int ConsecutiveDays { get; set; }
[NinjaScriptProperty]
[Range(0.0, 100.0)]
[Display(Name = "First Day RSI", GroupName = "Triple RSI Bot", Order = 4)]
public double FirstDayRSI { get; set; }
[NinjaScriptProperty]
[Range(0.0, 100.0)]
[Display(Name = "RSI Entry", GroupName = "Triple RSI Bot", Order = 5)]
public double RSIEntry { get; set; }
[NinjaScriptProperty]
[Range(0.0, 100.0)]
[Display(Name = "RSI Exit", GroupName = "Triple RSI Bot", Order = 6)]
public double RSIExit { get; set; }
#endregion
}
}