[INDENT] /// <summary>
/// This method is used to configure the strategy and is called once before any strategy method is called.
/// </summary>
protected override void Initialize()
{
SetStopLoss("", CalculationMode.Ticks, Stop, false);
SetProfitTarget("",CalculationMode.Ticks, finalTarget);
TraceOrders = true;
CalculateOnBarClose = false;
EntriesPerDirection = 1;
}
/// <summary>
/// Called on each bar update event (incoming tick)
/// </summary>
protected override void OnBarUpdate()
{
// Resets the stop loss to the original value when all positions are closed
if (Position.MarketPosition == MarketPosition.Flat)
{
SetStopLoss(CalculationMode.Ticks, stop);
}
// If a long position is open, allow for stop loss modification
else if (Position.MarketPosition == MarketPosition.Long)
{
// BREAKEVEN Once the price is greater than entry pric + breakEvenStop set stop loss to breakeven
if (Close[0] >= Position.AvgPrice + breakEvenStop * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
// 1stTarget Once the price is greater than entry price + firstTarget, set stop loss to 1 times above B E
if (Close[0] >= Position.AvgPrice + firstTarget * TickSize)
{
SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * TickSize));
}
// 2ndTarget Once the price is greater than entry price + secondTarget, set stop loss to 2 times above B E
if (Close[0] >= Position.AvgPrice + secondTarget * TickSize)
{
SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 2 * TickSize));
}
// 3rdTarget Once the price is greater than entry price + thirdTarget, set stop loss to 3 times above B E
if (Close[0] >= Position.AvgPrice + thirdTarget * TickSize)
{
SetStopLoss(CalculationMode.Price, (Position.AvgPrice + profitStop * 3 * TickSize));
}
}
// If a short position is open, allow for stop loss modification
else if (Position.MarketPosition == MarketPosition.Short)
{
// BreakEven Once the price is less than entry price - breakEvenStop set stop loss to breakeven
if (Close[0] <= Position.AvgPrice - breakEvenStop * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
// 1stTarget Once the price is less than entry price - firstTarget, set stop loss to 1 times above B E
if (Close[0] <= Position.AvgPrice - firstTarget * TickSize)
{
SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * TickSize));
}
// 2ndTarget Once the price is less than entry price - secondTarget, set stop loss to 2 times above B E
if (Close[0] <= Position.AvgPrice - secondTarget * TickSize)
{
SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 2 * TickSize));
}
// 3rdTarget Once the price is less than entry price - thirdTarget, set stop loss to 3 times above B E
if (Close[0] <= Position.AvgPrice - thirdTarget * TickSize)
{
SetStopLoss(CalculationMode.Price, (Position.AvgPrice - profitStop * 3 * TickSize));
}
}
[/INDENT]
EDIT: The latter was demonstrated back testing and market replay.

Comment