From 2c188c7c48f95132933476017954867511f8bcc2 Mon Sep 17 00:00:00 2001 From: Roman Yavnikov <45608740+Romazes@users.noreply.github.com> Date: Sat, 7 Sep 2024 00:01:37 +0300 Subject: [PATCH] Refactor: use GroupOrderCacheManager (#123) * feat: use GroupOrderCacheManager * refactor: get GroupCached Order use Lean extension * test:refactor: update option Expiration date --- ...eractiveBrokersBrokerageAdditionalTests.cs | 2 +- .../InteractiveBrokersBrokerage.cs | 33 ++++--------------- 2 files changed, 7 insertions(+), 28 deletions(-) diff --git a/QuantConnect.InteractiveBrokersBrokerage.Tests/InteractiveBrokersBrokerageAdditionalTests.cs b/QuantConnect.InteractiveBrokersBrokerage.Tests/InteractiveBrokersBrokerageAdditionalTests.cs index 13e8f7c..67cb0a4 100644 --- a/QuantConnect.InteractiveBrokersBrokerage.Tests/InteractiveBrokersBrokerageAdditionalTests.cs +++ b/QuantConnect.InteractiveBrokersBrokerage.Tests/InteractiveBrokersBrokerageAdditionalTests.cs @@ -176,7 +176,7 @@ public void SendComboOrderWithUnderlying(OrderType orderType, decimal comboLimit brokerage.CancelOrder(order); } - var optionsExpiration = new DateTime(2023, 7, 21); + var optionsExpiration = new DateTime(2024, 9, 6); var orderProperties = new InteractiveBrokersOrderProperties(); var group = new GroupOrderManager(1, legCount: orderType != OrderType.ComboLegLimit ? 3 : 2, quantity: comboDirection == OrderDirection.Buy ? 2 : -2); diff --git a/QuantConnect.InteractiveBrokersBrokerage/InteractiveBrokersBrokerage.cs b/QuantConnect.InteractiveBrokersBrokerage/InteractiveBrokersBrokerage.cs index 288b984..72ae609 100644 --- a/QuantConnect.InteractiveBrokersBrokerage/InteractiveBrokersBrokerage.cs +++ b/QuantConnect.InteractiveBrokersBrokerage/InteractiveBrokersBrokerage.cs @@ -147,6 +147,11 @@ public sealed class InteractiveBrokersBrokerage : Brokerage, IDataQueueHandler, private readonly ConcurrentDictionary _preSubmittedStopLimitOrders = new(); + /// + /// Provides a thread-safe service for caching and managing original orders when they are part of a group. + /// + private GroupOrderCacheManager _groupOrderCacheManager = new(); + // tracks requested order updates, so we can flag Submitted order events as updates private readonly ConcurrentDictionary _orderUpdates = new ConcurrentDictionary(); @@ -1338,13 +1343,10 @@ private void Initialize( /// The exchange to send the order to, defaults to "Smart" to use IB's smart routing private void IBPlaceOrder(Order order, bool needsNewId, string exchange = null) { - if (!order.TryGetGroupOrders(TryGetOrder, out var orders)) + if (!_groupOrderCacheManager.TryGetGroupCachedOrders(order, out var orders)) { - // some order of the group is missing but cache the new one - CacheOrder(order); return; } - RemoveCachedOrders(orders); // MOO/MOC require directed option orders. // We resolve non-equity markets in the `CreateContract` method. @@ -4975,29 +4977,6 @@ private void HandleManagedAccounts(object sender, IB.ManagedAccountsEventArgs e) Log.Trace($"InteractiveBrokersBrokerage.HandleManagedAccounts(): Account list: {e.AccountList}"); } - /// - /// We cache the original orders when they are part of a group. We don't ask the order provider because we would get a clone - /// and we want to be able to modify the original setting the brokerage id - /// - private Order TryGetOrder(int orderId) - { - _pendingGroupOrders.TryGetValue(orderId, out var order); - return order; - } - - private void CacheOrder(Order order) - { - _pendingGroupOrders[order.Id] = order; - } - - private void RemoveCachedOrders(List orders) - { - for (var i = 0; i < orders.Count; i++) - { - _pendingGroupOrders.TryRemove(orders[i].Id, out _); - } - } - private void AddGuaranteedTag(IBApi.Order ibOrder, bool nonGuaranteed) { ibOrder.SmartComboRoutingParams = new List