protected override void OnExecutionUpdate(Execution execution, string executionId, double price, int quantity, MarketPosition marketPosition, string orderId, DateTime time)
{
// Phase 1: Cancel Existing Orders
if (execution.Order == longScaleInOrder)
{
Print($"Scale-in executed for long position at {price}. Initiating cancellation phase.");
// Cancel any existing long exit orders
if (longLimitOrder != null)
{
CancelOrder(longLimitOrder);
longLimitOrder = null;
Print("Cancelled existing long limit order.");
}
if (longStopOrder != null)
{
CancelOrder(longStopOrder);
longStopOrder = null;
Print("Cancelled existing long stop order.");
}
// Set a flag indicating new orders need to be placed
isLongOrdersPending = true;
}
else if (execution.Order == shortScaleInOrder)
{
Print($"Scale-in executed for short position at {price}. Initiating cancellation phase.");
// Cancel any existing short exit orders
if (shortLimitOrder != null)
{
CancelOrder(shortLimitOrder);
shortLimitOrder = null;
Print("Cancelled existing short limit order.");
}
if (shortStopOrder != null)
{
CancelOrder(shortStopOrder);
shortStopOrder = null;
Print("Cancelled existing short stop order.");
}
// Set a flag indicating new orders need to be placed
isShortOrdersPending = true;
}
// Phase 2: Place New Orders
if (isLongOrdersPending)
{
Print($"Placing new long exit orders for position size: {Position.Quantity}");
longLimitOrder = ExitLongLimit(0, true, Position.Quantity, originalEntryPrice, "NewExitLongLimit", "ScaleInLong");
Print($"New long limit order placed at {originalEntryPrice} with quantity {Position.Quantity}");
longStopOrder = ExitLongStopMarket(0, true, Position.Quantity, stopPrice, "NewExitLongStop", "ScaleInLong");
Print($"New long stop order placed at {stopPrice} with quantity {Position.Quantity}");
isLongOrdersPending = false; // Reset the flag
}
else if (isShortOrdersPending)
{
Print($"Placing new short exit orders for position size: {Position.Quantity}");
shortLimitOrder = ExitShortLimit(0, true, Position.Quantity, originalEntryPrice, "NewExitShortLimit", "ScaleInShort");
Print($"New short limit order placed at {originalEntryPrice} with quantity {Position.Quantity}");
shortStopOrder = ExitShortStopMarket(0, true, Position.Quantity, stopPrice, "NewExitShortStop", "ScaleInShort");
Print($"New short stop order placed at {stopPrice} with quantity {Position.Quantity}");
isShortOrdersPending = false; // Reset the flag
}
// Original entrys and stop and take profit placement
if (execution.Name == "BullishGapEntry")
{
// Set stop loss and profit target for long position with isLiveUntilCancelled
longStopOrder = ExitLongStopMarket(0, true, quantity, stopPrice, "ExitLongStop", "BullishGapEntry");
longLimitOrder = ExitLongLimit(0, true, quantity, targetPrice, "ExitLongLimit", "BullishGapEntry");
longScaleInOrder = EnterLongLimit(0, true, 1, High[2], "ScaleInLong");
Print($"longScaleInOrder: {scaleInPriceLong}");
}
else if (execution.Name == "BearishGapEntry")
{
// Set stop loss and profit target for short position with isLiveUntilCancelled
shortStopOrder = ExitShortStopMarket(0, true, quantity, stopPrice, "ExitShortStop", "BearishGapEntry");
shortLimitOrder = ExitShortLimit(0, true, quantity, targetPrice, "ExitShortLimit", "BearishGapEntry");
Print($" ExitShortLimit: {targetPrice}, ExitShortLimit");
shortScaleInOrder = EnterShortLimit(0, true, 1, Low[2], "ScaleInShort");
Print($" shortScaleInOrder price: {scaleInPriceShort}");
}
}

Comment