Skip to content

Commit fcd584e

Browse files
committed
Update ValidateSufficientBuyingPowerForOrders
1 parent bdb519e commit fcd584e

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

Algorithm.CSharp/InsufficientMarginOrderUpdateRegressionAlgorithm.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,24 +84,29 @@ public override void OnData(Slice data)
8484
public override void OnOrderEvent(OrderEvent orderEvent)
8585
{
8686
// Check if the order is invalid
87-
if (_stopOrderTicket != null && orderEvent.OrderId == _stopOrderTicket.OrderId && _stopOrderTicket.Status != OrderStatus.Invalid)
87+
if (_stopOrderTicket != null && orderEvent.OrderId == _stopOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
8888
{
8989
throw new RegressionTestException($"Order {_stopOrderTicket.OrderId} with symbol {_stopOrderTicket.Symbol} should have been invalid due to insufficient margin after the update, but its current status is {_stopOrderTicket.Status}.");
9090
}
9191

9292
// Check if limit order is invalid due to insufficient margin after update
93-
if (_limitOrderTicket != null && orderEvent.Id == _limitOrderTicket.OrderId && _limitOrderTicket.Status != OrderStatus.Invalid)
93+
if (_limitOrderTicket != null && orderEvent.Id == _limitOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
9494
{
9595
throw new RegressionTestException($"Order {_limitOrderTicket.OrderId} with symbol {_limitOrderTicket.Symbol} should have been invalid due to insufficient margin after the update, but its current status is {_limitOrderTicket.Status}.");
9696
}
9797

9898
// Check if trailing stop order is invalid due to insufficient margin after update
99-
if (_trailingStopOrderTicket != null && orderEvent.Id == _trailingStopOrderTicket.OrderId && _trailingStopOrderTicket.Status != OrderStatus.Invalid)
99+
if (_trailingStopOrderTicket != null && orderEvent.Id == _trailingStopOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
100100
{
101101
throw new RegressionTestException($"Order {_trailingStopOrderTicket.OrderId} with symbol {_trailingStopOrderTicket.Symbol} should have been invalid due to insufficient margin after the update, but its current status is {_trailingStopOrderTicket.Status}.");
102102
}
103103
}
104104

105+
public override void OnEndOfAlgorithm()
106+
{
107+
var orders = Transactions.GetOrders().ToList();
108+
}
109+
105110
/// <summary>
106111
/// Final status of the algorithm
107112
/// </summary>

Engine/TransactionHandlers/BrokerageTransactionHandler.cs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -940,14 +940,6 @@ private OrderResponse HandleUpdateOrderRequest(UpdateOrderRequest request)
940940
return response;
941941
}
942942

943-
// modify the values of the order object
944-
order.ApplyUpdateOrderRequest(request);
945-
946-
// rounds the order prices
947-
RoundOrderPrices(order, security);
948-
949-
ticket.SetOrder(order);
950-
951943
// If the order is not part of a ComboLegLimit update, validate sufficient buying power
952944
if (order.GroupOrderManager == null)
953945
{
@@ -958,6 +950,14 @@ private OrderResponse HandleUpdateOrderRequest(UpdateOrderRequest request)
958950
}
959951
}
960952

953+
// modify the values of the order object
954+
order.ApplyUpdateOrderRequest(request);
955+
956+
// rounds the order prices
957+
RoundOrderPrices(order, security);
958+
959+
ticket.SetOrder(order);
960+
961961
bool orderUpdated;
962962
if (isClosedOrderUpdate)
963963
{
@@ -1075,11 +1075,23 @@ private OrderResponse ValidateSufficientBuyingPowerForOrders(Order order, OrderR
10751075
{
10761076
var errorMessage = securities != null
10771077
? securities.GetErrorMessage(hasSufficientBuyingPowerResult)
1078-
: $"Order Error: id: {order.Id.ToStringInvariant()}, Symbol: {order.Symbol.Value}, Insufficient buying power to complete order, Reason: {hasSufficientBuyingPowerResult.Reason}.";
1078+
: $"Brokerage failed to update order with id: {order.Id.ToStringInvariant()}, Symbol: {order.Symbol.Value}, Insufficient buying power to complete order, Reason: {hasSufficientBuyingPowerResult.Reason}.";
10791079
_algorithm.Error(errorMessage);
1080-
InvalidateOrders(orders ?? [order], errorMessage);
1081-
return OrderResponse.Error(request, OrderResponseErrorCode.InsufficientBuyingPower, errorMessage);
1080+
if (orders == null)
1081+
{
1082+
HandleOrderEvent(new OrderEvent(order,
1083+
_algorithm.UtcTime,
1084+
OrderFee.Zero,
1085+
"Brokerage failed to update order"));
1086+
return OrderResponse.Error(request, OrderResponseErrorCode.BrokerageFailedToUpdateOrder, errorMessage);
1087+
}
1088+
else
1089+
{
1090+
InvalidateOrders(orders, errorMessage);
1091+
return OrderResponse.Error(request, OrderResponseErrorCode.InsufficientBuyingPower, errorMessage);
1092+
}
10821093
}
1094+
10831095
return null;
10841096
}
10851097

0 commit comments

Comments
 (0)