Skip to content

Commit

Permalink
feat: use GroupOrderCacheManager
Browse files Browse the repository at this point in the history
  • Loading branch information
Romazes committed Sep 3, 2024
1 parent f339040 commit cb1dd62
Showing 1 changed file with 8 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ public sealed class InteractiveBrokersBrokerage : Brokerage, IDataQueueHandler,

private readonly ConcurrentDictionary<int, StopLimitOrder> _preSubmittedStopLimitOrders = new();

/// <summary>
/// Provides a thread-safe service for caching and managing original orders when they are part of a group.
/// </summary>
private GroupOrderCacheManager _groupOrderCacheManager = new();

// tracks requested order updates, so we can flag Submitted order events as updates
private readonly ConcurrentDictionary<int, int> _orderUpdates = new ConcurrentDictionary<int, int>();

Expand Down Expand Up @@ -1338,13 +1343,13 @@ private void Initialize(
/// <param name="exchange">The exchange to send the order to, defaults to "Smart" to use IB's smart routing</param>
private void IBPlaceOrder(Order order, bool needsNewId, string exchange = null)
{
if (!order.TryGetGroupOrders(TryGetOrder, out var orders))
if (!order.TryGetGroupOrders(_groupOrderCacheManager.TryGetOrder, out var orders))
{
// some order of the group is missing but cache the new one
CacheOrder(order);
_groupOrderCacheManager.CacheOrder(order);
return;
}
RemoveCachedOrders(orders);
_groupOrderCacheManager.RemoveCachedOrders(orders);

// MOO/MOC require directed option orders.
// We resolve non-equity markets in the `CreateContract` method.
Expand Down Expand Up @@ -4975,29 +4980,6 @@ private void HandleManagedAccounts(object sender, IB.ManagedAccountsEventArgs e)
Log.Trace($"InteractiveBrokersBrokerage.HandleManagedAccounts(): Account list: {e.AccountList}");
}

/// <summary>
/// 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
/// </summary>
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<Order> 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<TagValue>
Expand Down

0 comments on commit cb1dd62

Please sign in to comment.