#region Variables
private double ticks = 15;
#endregion
/// <summary>
/// This method is used to configure the indicator and is called once before any bar data is loaded.
/// </summary>
protected override void Initialize()
{
Add(new Plot(Color.Red, PlotStyle.Line, "TrailingStop"));
CalculateOnBarClose = true;
Overlay = true;
BarsRequired = 1;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
if (CurrentBar < 1)
return;
// Trailing stop
double trail;
double loss = Ticks*TickSize ;
if (Close[0] >= Value[1] && Close[1] >= Value[1])
trail = Math.Max(Value[1], Close[0] - loss);
else if (Close[0] < Value[1] && Close[1] < Value[1])
trail = Math.Min(Value[1], Close[0] + loss);
else if (Close[0] >= Value[1])
{
trail = Close[0] - loss;
}
else
{
trail = Close[0] + loss;
}
Value.Set(trail);
}


Comment