I have Strategy with multi MA logic and SL and PT. SL and PT a can choose in the Settings but they have a static positions on the chart.
I modified my strategy for SL and PT and now I can move SL and PT on the chart, so my SL and PT are free to move. I used OnOrderUpdate and OnExecutionUpdate.
Now I have problem when I see on the chart a few crossovers on one candle( a few signals for opening a few positions) - strategy placing just only one SL and PT (only for one position) and I have (for example) 3 Open Orders and just one SL and PT for one Order.
I understand, that I need to unique name for each order and I added to the each name number of condition, but it not helps..
Could you explain me how I must be implement order processing so that all executed orders have their own SL and PT?
My part of the code with OnOrderUpdate and OnExecutionUpdate:
protected override void OnOrderUpdate(Order order, double limitPrice, double stopPrice, int quantity, int filled, double averageFillPrice, OrderState orderState, DateTime time, ErrorCode error, string nativeError)
{
if (order.Name == "Long" + II + CurrentBar) // Long orders
{
entryOrderL = order;
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
entryOrderL = null;
sumFilled = 0;
}
}
else if (order.Name == "Short" + II + CurrentBar) // Short orders
{
entryOrderS = order;
if (order.OrderState == OrderState.Cancelled && order.Filled == 0)
{
entryOrderS = null;
sumFilled = 0;
}
}
}
protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
if (entryOrderL != null && entryOrderL == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
sumFilled += execution.Quantity;
if (stop[numberPairTemp] != 0)
{
if (execution.Order.OrderState == OrderState.PartFilled)
{
stopOrderL = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - stop[numberPairTemp] * TickSize, "StopL" + II + executionBarsL[0], "Long" + II + CurrentBar);
targetOrderL = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + profit[numberPairTemp] * TickSize, "TargetL" + II + executionBarsL[0], "Long" + II + CurrentBar);
}
else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled)
{
stopOrderL = ExitLongStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - stop[numberPairTemp] * TickSize, "StopL" + II + executionBarsL[0], "Long" + II + CurrentBar);
targetOrderL = ExitLongLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + profit[numberPairTemp] * TickSize, "TargetL" + II + executionBarsL[0], "Long" + II + CurrentBar);
}
}
if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
{
entryOrderL = null;
sumFilled = 0;
numberPairTemp = 0;
}
}
}
else if (entryOrderS != null && entryOrderS == execution.Order)
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled || (execution.Order.OrderState == OrderState.Cancelled && execution.Order.Filled > 0))
{
sumFilled += execution.Quantity;
if (profit[numberPairTemp] != 0)
{
if (execution.Order.OrderState == OrderState.PartFilled)
{
stopOrderS = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + stop[numberPairTemp] * TickSize, "StopS" + II + executionBarsL[0], "Short" + II + CurrentBar);
targetOrderS = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - profit[numberPairTemp] * TickSize, "TargetS" + II + executionBarsL[0], "Short" + II + CurrentBar);
}
else if (execution.Order.OrderState == OrderState.Filled && sumFilled == execution.Order.Filled) // Update exit order (for Short) quantities once orderstate turns to filled
{
// Stop-Loss order for OrderState.Filled
stopOrderS = ExitShortStopMarket(0, true, execution.Order.Filled, execution.Order.AverageFillPrice + stop[numberPairTemp] * TickSize, "StopS" + II + executionBarsL[0], "Short" + II + CurrentBar);
targetOrderS = ExitShortLimit(0, true, execution.Order.Filled, execution.Order.AverageFillPrice - profit[numberPairTemp] * TickSize, "TargetS" + II + executionBarsL[0], "Short" + II + CurrentBar);
}
}
if (execution.Order.OrderState != OrderState.PartFilled && sumFilled == execution.Order.Filled)
{
entryOrderS = null;
sumFilled = 0;
numberPairTemp = 0;
}
}
}
if ((stopOrderL != null && stopOrderL == execution.Order) || (targetOrderL != null && targetOrderL == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopOrderL = null;
targetOrderL = null;
}
}
if ((stopOrderS != null && stopOrderS == execution.Order) || (targetOrderS != null && targetOrderS == execution.Order))
{
if (execution.Order.OrderState == OrderState.Filled || execution.Order.OrderState == OrderState.PartFilled)
{
stopOrderS = null;
targetOrderS = null;
}
}
}
Thanks!


Comment