I am having trouble with the trailing stop feature when backtesting the attached strategy on the ES 60 minute chart.
The strategy is supposed to set a trailing stop if, after X amount of days after entering the position, the position is winning. If the position is down after that X amount of days, then the position exits.
The problem that happens is that when running the backtest, the trail stop never executes. The position never closes once opened, even if i set the trail stop for 1 tick on a 4 year backtest. Can anyone help?
Thanks
// This namespace holds all strategies and is required. Do not change it.
namespace NinjaTrader.Strategy
{
/// <summary>
/// Enter the description of your strategy here
/// </summary>
[Description("Enter the description of your strategy here")]
public class ADXstrategy : Strategy
{
#region Variables
private int adxLevel = 35;
private int daysToHold = 8;
private bool trailStopOn = true;
private double trailStopAmount = .01;
private bool trailstopset = false;
#endregion
/// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
Add(ADX(14));
Enabled = true;
TraceOrders = true;
CalculateOnBarClose = true;
SetTrailStop(CalculationMode.Percent,1000000);
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
//According to book best results when ADX above 35 and held for 8 days
{
if (Position.MarketPosition == MarketPosition.Flat)
{
trailstopset = false;
SetTrailStop(CalculationMode.Percent,1000000);
if(ADX(14)[0] >= adxLevel)
{
EnterLong();
}
}
if (Position.MarketPosition == MarketPosition.Long && BarsSinceEntry() >= daysToHold)
{
if (trailStopOn && Close [0] > Close [daysToHold] && !trailstopset)
{
SetTrailStop(CalculationMode.Percent, trailStopAmount);
trailstopset = true;
}
else if (!trailstopset)
ExitLong();
}
}
#region Properties
[Description("")]
[GridCategory("Parameters")]
public int AdxLevel
{
get { return adxLevel; }
set { adxLevel = value; }
}
[Description("")]
[GridCategory("Parameters")]
public int DaysToHold
{
get { return daysToHold; }
set { daysToHold = value; }
}
[Description("")]
[GridCategory("Parameters")]
public bool TrailStopOn
{
get { return trailStopOn; }
set { trailStopOn = value; }
}
[Description("")]
[GridCategory("Parameters")]
public double TrailStopAmount
{
get { return trailStopAmount; }
set { trailStopAmount = value; }
}
#endregion
}
}

Comment