ninjatrader/strategies/trin-thrusts/TRINThrusts.cs

71 lines
2.1 KiB
C#

#region Using declarations
using NinjaTrader.NinjaScript.Indicators;
using System.ComponentModel.DataAnnotations;
#endregion
namespace NinjaTrader.NinjaScript.Strategies
{
public class TRINThrusts : Strategy
{
private const int PrimaryBars = 0;
private const int TRINBars = 1;
private SMA LongTermTrend;
protected override void OnStateChange()
{
if (State == State.SetDefaults)
{
Name = "TRIN Thrusts";
Description = @"Take from Chapter 3 of Connors on Advanced Trading Strategies (1998) by Larry Connors";
Calculate = Calculate.OnBarClose;
EntriesPerDirection = 1;
LongTermTrendPeriod = 200;
PercentBelow = 40.0;
DaysToExit = 2;
}
else if (State == State.Configure)
{
AddDataSeries("^TRIN");
}
else if (State == State.DataLoaded)
{
LongTermTrend = SMA(LongTermTrendPeriod);
}
}
protected override void OnBarUpdate()
{
if (PrimaryBars != BarsInProgress)
return;
if (CurrentBar < LongTermTrendPeriod || CurrentBars[TRINBars] < 1)
return;
if (Close[0] > LongTermTrend[0] && Closes[TRINBars][0] < Closes[TRINBars][1] * (1 - PercentBelow / 100))
EnterLong();
if (BarsSinceEntryExecution(PrimaryBars, "", 0) >= DaysToExit)
ExitLong();
}
public override string DisplayName
{
get { return Name; }
}
[NinjaScriptProperty]
[Display(Name = "Long-Term Trend Period", GroupName = "TRIN Thrusts", Order = 1)]
public int LongTermTrendPeriod { get; set; }
[NinjaScriptProperty]
[Display(Name = "Percent Below", GroupName = "TRIN Thrusts", Order = 2)]
public double PercentBelow { get; set; }
[NinjaScriptProperty]
[Display(Name = "Days to Exit", GroupName = "TRIN Thrusts", Order = 3)]
public int DaysToExit { get; set; }
}
}