protected override void OnExecutionUpdate(Cbi.Execution execution, string executionId, double price, int quantity,
Cbi.MarketPosition marketPosition, string orderId, DateTime time)
{
/// Handle actions for this execution being an entry order
if (execution.IsEntryStrategy)
{
/// Place SL and (multiple) TP levels
if (marketPosition == MarketPosition.Long)
{
if (StopLoss != 0)
slExitOrder = ExitLongStopMarket(0, true, Position.Quantity, price - StopLoss*TickSize, "Stop Loss", "Long");
/// Seems what we need is to delete old TP and enter new one for all lots based on (execution.AverageFillPrice + TakeProfit*TickSize)
if (TakeProfit > 0)
tpExitOrder = ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + TakeProfit*TickSize, "Take Profit", "Long");
}
else
{
if (StopLoss != 0)
slExitOrder = ExitShortStopMarket(0, true, Position.Quantity, price + StopLoss*TickSize, "Stop Loss", "Short");
/// Seems what we need is to delete old TP and enter new one for all lots based on (execution.AverageFillPrice + TakeProfit*TickSize)
if (TakeProfit > 0)
tpExitOrder = ExitShortLimit(0, true, Position.Quantity, Position.AveragePrice - TakeProfit*TickSize, "Take Profit", "Short");
}
/// Save the bar index of this entry; CurrentBar zero index on first (leftmost) bar
if (firstEntryBarIdx == -1)
firstEntryBarIdx = CurrentBar;
}
/// Handle actions for this execution being an exit order ('exit strategy')
else if (execution.Order.OrderAction == OrderAction.Sell || execution.Order.OrderAction == OrderAction.BuyToCover)
{
if (Position.Quantity > 0)
{
if (slExitOrder != null)
{
Log($"Trade Closed; attempting to adjust SL order quantity to {Position.Quantity}...");
/// Verify slExitOrder state before updating
if (slExitOrder.OrderState == OrderState.Working || slExitOrder.OrderState == OrderState.Accepted)
{
/// Turns out the marketPosition passed in may not be the same as our actual position direction,
/// perhaps because this is called in reference to the stop postion(??). So get correct one
var currentMarketPosition = Position.MarketPosition;
if (currentMarketPosition == MarketPosition.Long)
slExitOrder = ExitLongStopMarket(0, true, Position.Quantity, slExitOrder.StopPrice, "Stop Loss", "Long");
else if (currentMarketPosition == MarketPosition.Short)
slExitOrder = ExitShortStopMarket(0, true, Position.Quantity, slExitOrder.StopPrice, "Stop Loss", "Short");
else
Log($"...but currentMarketPosition is not Long OR Short!!!!!!");
}
else
Log($"...but Stop Order not in a modifiable state; Current state = {slExitOrder.OrderState.ToString()}");
}
else
Log($"...but Stop Order == NULL, so...No stop order to modify??");
if (tpExitOrder != null)
{
Log($"Trade Closed; attempting to adjust TP order quantity to {Position.Quantity}...");
/// Verify slExitOrder state before updating
if (tpExitOrder.OrderState == OrderState.Working || tpExitOrder.OrderState == OrderState.Accepted)
{
var currentMarketPosition = Position.MarketPosition;
if (currentMarketPosition == MarketPosition.Long)
tpExitOrder = ExitLongLimit(0, true, Position.Quantity, Position.AveragePrice + TakeProfit*TickSize, "Take Profit", "Long");
else if (currentMarketPosition == MarketPosition.Short)
tpExitOrder = ExitShortLimit(0, true, Position.Quantity, Position.AveragePrice - TakeProfit*TickSize, "Take Profit", "Short");
else
Log($"...but currentMarketPosition is not Long OR Short!!!!!!");
}
else
Log($"...but TP Order not in a modifiable state; Current state = {tpExitOrder.OrderState.ToString()}");
}
else
Log($"...but TP Order == NULL, so...No stop order to modify??");
}
/// Reset some vars if flat
else if (Position.Quantity == 0)
{
beTriggered1Price = beTriggered2Price = beTriggered3Price = beTriggered4Price = 0;
trailTriggered = false;
firstEntryBarIdx = -1;
}
}
}
Trade Closed; attempting to adjust SL order quantity to 2...
...but Stop Order not in a modifiable state; Current state = CancelSubmitted
Trade Closed; attempting to adjust TP order quantity to 2...
...but Stop Order not in a modifiable state; Current state = CancelSubmitted
So if the state of my order is not working or accepted... I assume this CancelSubmitted was temporary, so... what would be the appropriate way to revisit this and get that SL & TP quantity updated?

Comment