/*
* 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.Collections.Concurrent;
namespace QuantConnect.Orders
{
///
/// Provides a thread-safe service for caching and managing original orders when they are part of a group.
///
public class GroupOrderCacheManager
{
///
/// A thread-safe dictionary that caches original orders when they are part of a group.
///
///
/// The dictionary uses the order ID as the key and stores the original objects as values.
/// This allows for the modification of the original orders, such as setting the brokerage ID,
/// without retrieving a cloned instance from the order provider.
///
private readonly ConcurrentDictionary _pendingGroupOrders = new();
///
/// Attempts to retrieve all the orders in the combo group from the cache.
///
/// Target order, which can be any of the legs of the combo
/// List of orders in the combo
///
/// true if all the orders in the combo group were successfully retrieved from the cache;
/// otherwise, false. If the retrieval fails, the target order is cached for future retrieval.
///
public bool TryGetGroupCachedOrders(Order order, out List orders)
{
if (!order.TryGetGroupOrders(TryGetOrder, out orders))
{
// some order of the group is missing but cache the new one
CacheOrder(order);
return false;
}
RemoveCachedOrders(orders);
return true;
}
///
/// Attempts to retrieve an original order from the cache using the specified order ID.
///
/// The unique identifier of the order to retrieve.
///
/// The original if found; otherwise, null.
///
private Order TryGetOrder(int orderId)
{
_pendingGroupOrders.TryGetValue(orderId, out var order);
return order;
}
///
/// Caches an original order in the internal dictionary for future retrieval.
///
/// The object to cache.
private void CacheOrder(Order order)
{
_pendingGroupOrders[order.Id] = order;
}
///
/// Removes a list of orders from the internal cache.
///
/// The list of objects to remove from the cache.
private void RemoveCachedOrders(List orders)
{
for (var i = 0; i < orders.Count; i++)
{
_pendingGroupOrders.TryRemove(orders[i].Id, out _);
}
}
}
}