Files
StockSharp/MatchingEngine/IOrderLifecycleManager.cs
mika@stocksharp.com 1999e27064 Emulator matching: skip per-message work when there is nothing to match
Two early-outs on the per-message matching hot path, which runs millions of times
per backtested month:

- OrderLifecycleManager.GetExpiredOrders/ProcessTime allocated a List on every
  call even when no order had an expiry set. Return an empty enumerable when
  there are no tracked expiry times.
- MatchingEngineAdapter.CheckLimitOrders read BestBid/BestAsk and scanned the
  active orders on every tick/quote. Return immediately when no order is resting,
  skipping those reads and the scan in the overwhelmingly common case.

Both fire only when the respective collection is empty, so whenever orders exist
the original path runs unchanged and backtest results are unaffected.
2026-06-01 23:51:15 +03:00

246 lines
7.0 KiB
C#

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