Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use try out pattern
Browse files Browse the repository at this point in the history
JosueNina committed Jan 30, 2025
1 parent 475bb24 commit 88d48ff
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions Engine/TransactionHandlers/BrokerageTransactionHandler.cs
Original file line number Diff line number Diff line change
@@ -844,8 +844,7 @@ private OrderResponse HandleSubmitOrderRequest(SubmitOrderRequest request)
}

// check to see if we have enough money to place the order
var validationResult = ValidateSufficientBuyingPowerForOrders(order, request, orders, securities);
if (validationResult != null)
if (!HasSufficientBuyingPowerForOrders(order, request, out var validationResult, orders, securities))
{
return validationResult;
}
@@ -945,8 +944,7 @@ private OrderResponse HandleUpdateOrderRequest(UpdateOrderRequest request)
{
var updatedOrder = order.Clone();
updatedOrder.ApplyUpdateOrderRequest(request);
var validationResult = ValidateSufficientBuyingPowerForOrders(updatedOrder, request);
if (validationResult != null)
if (!HasSufficientBuyingPowerForOrders(updatedOrder, request, out var validationResult))
{
return validationResult;
}
@@ -1055,8 +1053,9 @@ private OrderResponse HandleCancelOrderRequest(CancelOrderRequest request)
/// Returns an error response if validation fails or an exception occurs.
/// Returns null if validation passes.
/// </summary>
private OrderResponse ValidateSufficientBuyingPowerForOrders(Order order, OrderRequest request, List<Order> orders = null, Dictionary<Order, Security> securities = null)
private bool HasSufficientBuyingPowerForOrders(Order order, OrderRequest request, out OrderResponse response, List<Order> orders = null, Dictionary<Order, Security> securities = null)
{
response = null;
HasSufficientBuyingPowerForOrderResult hasSufficientBuyingPowerResult;
try
{
@@ -1066,35 +1065,34 @@ private OrderResponse ValidateSufficientBuyingPowerForOrders(Order order, OrderR
{
Log.Error(err);
_algorithm.Error($"Order Error: id: {order.Id.ToStringInvariant()}, Error executing margin models: {err.Message}");
HandleOrderEvent(new OrderEvent(order,
_algorithm.UtcTime,
OrderFee.Zero,
"Error executing margin models"));
return OrderResponse.Error(request, OrderResponseErrorCode.ProcessingError, "An error occurred while checking sufficient buying power for the orders.");
HandleOrderEvent(new OrderEvent(order, _algorithm.UtcTime, OrderFee.Zero, "Error executing margin models"));

response = OrderResponse.Error(request, OrderResponseErrorCode.ProcessingError, "An error occurred while checking sufficient buying power for the orders.");
return false;
}

if (!hasSufficientBuyingPowerResult.IsSufficient)
{
var errorMessage = securities != null
? securities.GetErrorMessage(hasSufficientBuyingPowerResult)
: $"Brokerage failed to update order with id: {order.Id.ToStringInvariant()}, Symbol: {order.Symbol.Value}, Insufficient buying power to complete order, Reason: {hasSufficientBuyingPowerResult.Reason}.";

_algorithm.Error(errorMessage);

if (request is UpdateOrderRequest)
{
HandleOrderEvent(new OrderEvent(order,
_algorithm.UtcTime,
OrderFee.Zero,
"Brokerage failed to update order"));
return OrderResponse.Error(request, OrderResponseErrorCode.BrokerageFailedToUpdateOrder, errorMessage);
HandleOrderEvent(new OrderEvent(order, _algorithm.UtcTime, OrderFee.Zero, "Brokerage failed to update order"));
response = OrderResponse.Error(request, OrderResponseErrorCode.BrokerageFailedToUpdateOrder, errorMessage);
}
else
{
InvalidateOrders(orders, errorMessage);
return OrderResponse.Error(request, OrderResponseErrorCode.InsufficientBuyingPower, errorMessage);
response = OrderResponse.Error(request, OrderResponseErrorCode.InsufficientBuyingPower, errorMessage);
}
return false;
}

return null;
return true;
}

private void HandleOrderEvents(List<OrderEvent> orderEvents)

0 comments on commit 88d48ff

Please sign in to comment.