ninjatrader/strategies/trading-new-highs/TradingNewHighs.cs

63 lines
1.9 KiB
C#

#region Using declarations
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class TradingNewHighs : Strategy
{
private const int FiftyTwoWeeksInDays = 252;
private ConnorsRSI connorsRSI;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "Trading New Highs";
Description = @"Taken from chapter 7 of Buy the Fear, Sell the Greed (2018) by Larry Connors";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
MaxDaysSinceHigh = 20;
EntryThreshold = 15;
ExitThreshold = 70;
}
else if (State == State.DataLoaded)
{
connorsRSI = ConnorsRSI(3, 1, 2, 1, 100);
}
}
protected override void OnBarUpdate()
{
if (CurrentBar < FiftyTwoWeeksInDays)
return;
int barsAgo52WeekHigh = HighestBar(Highs[0], FiftyTwoWeeksInDays);
if (barsAgo52WeekHigh <= MaxDaysSinceHigh && connorsRSI[0] < EntryThreshold)
EnterLong();
else if (connorsRSI[0] > ExitThreshold)
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Max Days Since High", GroupName = "Trading New Highs", Order = 1)]
public double MaxDaysSinceHigh { get; set; }
[NinjaScriptProperty]
[Display(Name = "ConnorsRSI Entry Threshold", GroupName = "Trading New Highs", Order = 2)]
public double EntryThreshold { get; set; }
[NinjaScriptProperty]
[Display(Name = "ConnorsRSI Exit Threshold", GroupName = "Trading New Highs", Order = 3)]
public double ExitThreshold { get; set; }
}
}