stop is set to breakeven. My problem is when two or more positions are open they are all closed when one of the positions hits stop or profit target. How can the others position stay
opened until they met their targets?
This is my code:
/// </summary>
protected override void Initialize()
{
/* There are several ways you can use SetStopLoss and SetProfitTarget. You can have them set to a currency value
or some sort of calculation mode. Calculation modes available are by percent, price, and ticks. SetStopLoss and
SetProfitTarget will submit real working orders unless you decide to simulate the orders. */
SetStopLoss(CalculationMode.Ticks, stoplossticks);
SetProfitTarget(CalculationMode.Ticks, profittargetticks);
CalculateOnBarClose = true;
}
/// <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, stoplossticks);
}
// If a long position is open, allow for stop loss modification to breakeven
else if (Position.MarketPosition == MarketPosition.Long)
{
// Once the price is greater than entry price+50 ticks, set stop loss to breakeven
if (Close[0] > Position.AvgPrice + 40 * TickSize)
{
SetStopLoss(CalculationMode.Price, Position.AvgPrice);
}
}
// Condition set 1
if (CurrentDayOHLC().CurrentOpen[0] > PriorDayOHLC().PriorHigh[0]
&& ToTime(Time[0]) > ToTime(8, 10, 0)
&& ToTime(Time[0]) < ToTime(8, 31, 0))
{
EnterLong(DefaultQuantity, "");
}
}
#region Properties
/// <summary>
/// </summary>
[Description("Numbers of ticks away from entry price for the Stop Loss order")]
[Category("Parameters")]
public int StopLossTicks
{
get { return stoplossticks; }
set { stoplossticks = Math.Max(0, value); }
}
/// <summary>
/// </summary>
[Description("Number of ticks away from entry price for the Profit Target order")]
[Category("Parameters")]
public int ProfitTargetTicks
{
get { return profittargetticks; }
set { profittargetticks = Math.Max(0, value); }
}
#endregion
}
}
Comment