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

Refactor: use GroupOrderCacheManager #123

Merged
merged 3 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
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,10 @@ 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 (!_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.
Expand Down Expand Up @@ -4975,29 +4977,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
Loading