Skip to content

Commit

Permalink
Refactor: use GroupOrderCacheManager (#123)
Browse files Browse the repository at this point in the history
* feat: use GroupOrderCacheManager

* refactor: get GroupCached Order use Lean extension

* test:refactor: update option Expiration date
  • Loading branch information
Romazes committed Sep 6, 2024
1 parent 4e5685f commit 2c188c7
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 28 deletions.
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

0 comments on commit 2c188c7

Please sign in to comment.