Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating Orders should fail if it causes insufficient margin #8553

198 changes: 198 additions & 0 deletions Algorithm.CSharp/InsufficientMarginOrderUpdateRegressionAlgorithm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

using System.Collections.Generic;
using System.Linq;
using QuantConnect.Data;
using QuantConnect.Interfaces;
using QuantConnect.Orders;

namespace QuantConnect.Algorithm.CSharp
{
/// <summary>
/// This algorithm tests order updates with margin constraints to ensure that orders become invalid when exceeding margin requirements.
/// </summary>
public class InsufficientMarginOrderUpdateRegressionAlgorithm : QCAlgorithm, IRegressionAlgorithmDefinition
{
private OrderTicket _stopOrderTicket;
private OrderTicket _limitOrderTicket;
private OrderTicket _trailingStopOrderTicket;
private bool _updatesReady;
private bool _updatesInProgress;
private int _updateEventsCount;

public override void Initialize()
{
SetStartDate(2018, 4, 3);
SetEndDate(2018, 4, 4);
AddForex("EURUSD", Resolution.Minute);
_updatesInProgress = true;
_updateEventsCount = 0;
}

public override void OnData(Slice data)
{

if (!Portfolio.Invested)
{
var qty = CalculateOrderQuantity("EURUSD", 50m);

MarketOrder("EURUSD", qty);

// Place stop market, limit, and trailing stop orders with half the quantity
_stopOrderTicket = StopMarketOrder("EURUSD", -qty / 2, Securities["EURUSD"].Price - 0.003m);
_limitOrderTicket = LimitOrder("EURUSD", -qty / 2, Securities["EURUSD"].Price - 0.003m);
_trailingStopOrderTicket = TrailingStopOrder("EURUSD", -qty / 2, Securities["EURUSD"].Price - 0.003m, 0.01m, true);

// Update the stop order
var updateStopOrderSettings = new UpdateOrderFields
{
// Attempt to increase the order quantity significantly
Quantity = -qty * 100,
StopPrice = Securities["EURUSD"].Price - 0.003m
};
_stopOrderTicket.Update(updateStopOrderSettings);

// Update limit order
var updateLimitOrderSettings = new UpdateOrderFields
{
// Attempt to increase the order quantity significantly
Quantity = -qty * 100,
LimitPrice = Securities["EURUSD"].Price - 0.003m
};
_limitOrderTicket.Update(updateLimitOrderSettings);

// Update trailing stop order
var updateTrailingStopOrderSettings = new UpdateOrderFields
{
// Attempt to increase the order quantity significantly
Quantity = -qty * 100,
StopPrice = Securities["EURUSD"].Price - 0.003m,
TrailingAmount = 0.01m,
};
_trailingStopOrderTicket.Update(updateTrailingStopOrderSettings);
_updatesReady = true;
}
}

public override void OnOrderEvent(OrderEvent orderEvent)
{
if (_updatesReady && _updatesInProgress)
{
if (orderEvent.Status != OrderStatus.Submitted)
{
throw new RegressionTestException($"Unexpected order event status {orderEvent.Status} received. Expected Submitted.");
}
// All updates have been enqueued and should be rejected one by one
if (orderEvent.OrderId == _stopOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
{
throw new RegressionTestException($"The stop order update should have been rejected due to insufficient margin");
}

if (orderEvent.Id == _limitOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
{
throw new RegressionTestException($"The limit order update should have been rejected due to insufficient margin");
}

if (orderEvent.Id == _trailingStopOrderTicket.OrderId && !orderEvent.Message.Contains("Brokerage failed to update order"))
{
throw new RegressionTestException($"The trailing stop order update should have been rejected due to insufficient margin");
}
_updateEventsCount++;
}
if (_updateEventsCount >= 3)
{
_updatesInProgress = false;
}

}

public override void OnEndOfAlgorithm()
{
// Updates were rejected, so all orders should be in Filled status
var orders = Transactions.GetOrders().ToList();
foreach (var order in orders)
{
if (order.Status != OrderStatus.Filled)
{
throw new RegressionTestException($"Order {order.Id} with symbol {order.Symbol} should have been filled, but its current status is {order.Status}.");
}
}
if (!_updatesReady)
{
throw new RegressionTestException("Update Orders should be ready!");
}
}

/// <summary>
/// Final status of the algorithm
/// </summary>
public AlgorithmStatus AlgorithmStatus => AlgorithmStatus.Completed;

/// <summary>
/// This is used by the regression test system to indicate if the open source Lean repository has the required data to run this algorithm.
/// </summary>
public bool CanRunLocally { get; } = true;

/// <summary>
/// This is used by the regression test system to indicate which languages this algorithm is written in.
/// </summary>
public virtual List<Language> Languages { get; } = new() { Language.CSharp };

/// <summary>
/// Data Points count of all timeslices of algorithm
/// </summary>
public long DataPoints => 2893;

/// <summary>
/// Data Points count of the algorithm history
/// </summary>
public int AlgorithmHistoryDataPoints => 60;

/// <summary>
/// This is used by the regression test system to indicate what the expected statistics are from running the algorithm
/// </summary>
public Dictionary<string, string> ExpectedStatistics => new Dictionary<string, string>
{
{"Total Orders", "4"},
{"Average Win", "0%"},
{"Average Loss", "0%"},
{"Compounding Annual Return", "0%"},
{"Drawdown", "0%"},
{"Expectancy", "0"},
{"Start Equity", "100000.00"},
{"End Equity", "90809.64"},
{"Net Profit", "0%"},
{"Sharpe Ratio", "0"},
{"Sortino Ratio", "0"},
{"Probabilistic Sharpe Ratio", "0%"},
{"Loss Rate", "0%"},
{"Win Rate", "0%"},
{"Profit-Loss Ratio", "0"},
{"Alpha", "0"},
{"Beta", "0"},
{"Annual Standard Deviation", "0"},
{"Annual Variance", "0"},
{"Information Ratio", "0"},
{"Tracking Error", "0"},
{"Treynor Ratio", "0"},
{"Total Fees", "$0.00"},
{"Estimated Strategy Capacity", "$99000.00"},
{"Lowest Capacity Asset", "EURUSD 8G"},
{"Portfolio Turnover", "6777.62%"},
{"OrderListHash", "505feaf1ae70ead2d7ab78ea257d7342"}
};
}
}
82 changes: 60 additions & 22 deletions Engine/TransactionHandlers/BrokerageTransactionHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -844,29 +844,9 @@ private OrderResponse HandleSubmitOrderRequest(SubmitOrderRequest request)
}

// check to see if we have enough money to place the order
HasSufficientBuyingPowerForOrderResult hasSufficientBuyingPowerResult;
try
if (!HasSufficientBuyingPowerForOrders(order, request, out var validationResult, orders, securities))
{
hasSufficientBuyingPowerResult = _algorithm.Portfolio.HasSufficientBuyingPowerForOrder(orders);
}
catch (Exception err)
{
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, "Error in GetSufficientCapitalForOrder");
}

if (!hasSufficientBuyingPowerResult.IsSufficient)
{
var errorMessage = securities.GetErrorMessage(hasSufficientBuyingPowerResult);
_algorithm.Error(errorMessage);

InvalidateOrders(orders, errorMessage);
return OrderResponse.Error(request, OrderResponseErrorCode.InsufficientBuyingPower, errorMessage);
return validationResult;
}

// verify that our current brokerage can actually take the order
Expand Down Expand Up @@ -959,6 +939,17 @@ private OrderResponse HandleUpdateOrderRequest(UpdateOrderRequest request)
return response;
}

// If the order is not part of a ComboLegLimit update, validate sufficient buying power
if (order.GroupOrderManager == null)
{
var updatedOrder = order.Clone();
updatedOrder.ApplyUpdateOrderRequest(request);
if (!HasSufficientBuyingPowerForOrders(updatedOrder, request, out var validationResult))
{
return validationResult;
}
}

// modify the values of the order object
order.ApplyUpdateOrderRequest(request);

Expand Down Expand Up @@ -1057,6 +1048,53 @@ private OrderResponse HandleCancelOrderRequest(CancelOrderRequest request)
return OrderResponse.Success(request);
}

/// <summary>
/// Validates if there is sufficient buying power for the given order(s).
/// Returns an error response if validation fails or an exception occurs.
/// Returns null if validation passes.
/// </summary>
private bool HasSufficientBuyingPowerForOrders(Order order, OrderRequest request, out OrderResponse response, List<Order> orders = null, Dictionary<Order, Security> securities = null)
{
response = null;
HasSufficientBuyingPowerForOrderResult hasSufficientBuyingPowerResult;
try
{
hasSufficientBuyingPowerResult = _algorithm.Portfolio.HasSufficientBuyingPowerForOrder(orders ?? [order]);
}
catch (Exception err)
{
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"));

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, errorMessage));
response = OrderResponse.Error(request, OrderResponseErrorCode.BrokerageFailedToUpdateOrder, errorMessage);
}
else
{
InvalidateOrders(orders, errorMessage);
response = OrderResponse.Error(request, OrderResponseErrorCode.InsufficientBuyingPower, errorMessage);
}
return false;
}

return true;
}

private void HandleOrderEvents(List<OrderEvent> orderEvents)
{
lock (_lockHandleOrderEvent)
Expand Down