namespace StockSharp.MatchingEngine;
///
/// Manages order lifecycle (active orders, expiry, etc.).
///
public interface IOrderLifecycleManager
{
///
/// Register a new active order.
///
/// Order to register.
/// Current time for expiry calculation.
/// True if registered, false if already exists.
bool RegisterOrder(EmulatorOrder order, DateTime currentTime);
///
/// Get an active order by transaction ID.
///
/// Transaction id of the order to retrieve.
/// The matching or null if not found.
EmulatorOrder GetOrder(long transactionId);
///
/// Try to get an active order by transaction ID.
///
/// Transaction id to look up.
/// When method returns true, contains the found order.
/// True if order was found, otherwise false.
bool TryGetOrder(long transactionId, out EmulatorOrder order);
///
/// Remove an order from active orders.
///
/// Transaction id of the order to remove.
/// True if removed, false if not found.
bool RemoveOrder(long transactionId);
///
/// Remove and return an order.
///
/// Transaction id of the order to remove.
/// When method returns true, contains the removed order.
/// True if the order was removed, otherwise false.
bool TryRemoveOrder(long transactionId, out EmulatorOrder order);
///
/// Get all active orders.
///
/// Enumeration of all active orders.
IEnumerable GetActiveOrders();
///
/// Get all active orders for a portfolio.
///
/// Portfolio name to filter active orders.
/// Active orders for the specified portfolio.
IEnumerable GetActiveOrders(string portfolioName);
///
/// Get all active orders for a security.
///
/// Security identifier to filter active orders.
/// Active orders for the specified security.
IEnumerable GetActiveOrders(SecurityId securityId);
///
/// Get all active orders matching filter.
///
/// Optional portfolio name filter.
/// Optional security id filter.
/// Optional side filter.
/// Active orders matching the specified filters.
IEnumerable GetActiveOrders(string portfolioName, SecurityId? securityId, Sides? side);
///
/// Get orders that have expired as of the given time.
///
/// Time to check expirations against.
/// Expired orders at the specified time.
IEnumerable GetExpiredOrders(DateTime currentTime);
///
/// Process time passage and return expired orders.
///
/// Current time to process expiries.
/// Orders that expired during processing.
IEnumerable ProcessTime(DateTime currentTime);
///
/// Clear all orders.
///
void Clear();
///
/// Count of active orders.
///
int Count { get; }
}
///
/// Order lifecycle manager implementation.
///
public class OrderLifecycleManager : IOrderLifecycleManager
{
private readonly Dictionary _activeOrders = [];
private readonly Dictionary _expiryTimes = [];
///
public int Count => _activeOrders.Count;
///
public bool RegisterOrder(EmulatorOrder order, DateTime currentTime)
{
if (order is null)
throw new ArgumentNullException(nameof(order));
if (_activeOrders.ContainsKey(order.TransactionId))
return false;
_activeOrders[order.TransactionId] = order;
if (order.ExpiryDate.HasValue)
{
var expiry = order.ExpiryDate.Value;
if (expiry > currentTime)
{
_expiryTimes[order.TransactionId] = expiry;
}
}
return true;
}
///
public EmulatorOrder GetOrder(long transactionId)
{
return _activeOrders.TryGetValue(transactionId, out var order) ? order : null;
}
///
public bool TryGetOrder(long transactionId, out EmulatorOrder order)
{
return _activeOrders.TryGetValue(transactionId, out order);
}
///
public bool RemoveOrder(long transactionId)
{
_expiryTimes.Remove(transactionId);
return _activeOrders.Remove(transactionId);
}
///
public bool TryRemoveOrder(long transactionId, out EmulatorOrder order)
{
if (_activeOrders.TryGetValue(transactionId, out order))
{
_activeOrders.Remove(transactionId);
_expiryTimes.Remove(transactionId);
return true;
}
return false;
}
///
public IEnumerable GetActiveOrders()
{
return _activeOrders.Values;
}
///
public IEnumerable GetActiveOrders(string portfolioName)
{
return _activeOrders.Values.Where(o =>
string.Equals(o.PortfolioName, portfolioName, StringComparison.OrdinalIgnoreCase));
}
///
public IEnumerable GetActiveOrders(SecurityId securityId)
{
// Note: Orders don't store SecurityId in current design
// This would need to be added if needed
return _activeOrders.Values;
}
///
public IEnumerable GetActiveOrders(string portfolioName, SecurityId? securityId, Sides? side)
{
var query = _activeOrders.Values.AsEnumerable();
if (!portfolioName.IsEmpty())
query = query.Where(o => string.Equals(o.PortfolioName, portfolioName, StringComparison.OrdinalIgnoreCase));
if (side.HasValue)
query = query.Where(o => o.Side == side.Value);
return query;
}
///
public IEnumerable GetExpiredOrders(DateTime currentTime)
{
// ProcessTime runs on every message; most have no order pending expiry, so avoid
// allocating a list in the common empty case.
if (_expiryTimes.Count == 0)
return [];
var expired = new List();
foreach (var kvp in _expiryTimes)
{
if (kvp.Value <= currentTime && _activeOrders.TryGetValue(kvp.Key, out var order))
{
expired.Add(order);
}
}
return expired;
}
///
public IEnumerable ProcessTime(DateTime currentTime)
{
if (_expiryTimes.Count == 0)
return [];
var expired = GetExpiredOrders(currentTime).ToList();
foreach (var order in expired)
{
_activeOrders.Remove(order.TransactionId);
_expiryTimes.Remove(order.TransactionId);
}
return expired;
}
///
public void Clear()
{
_activeOrders.Clear();
_expiryTimes.Clear();
}
}