mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 08:32:23 +00:00
A market order names no price, so only the book can give it one. Sent against a side holding nothing - an empty book, or a book quoting only the order's own side - it produced an Active row and then a Done row with the balance still equal to the volume and no error at all, which reads exactly like an order that filled as far as it asked to. A strategy could not tell that it had not traded, let alone why. The refusal was already written, and switched off: the check that an order with no price cannot be paid for sits behind an early return taken when money is not being checked, and money is not checked by default. Whether a market exists is not a question about money. Two neighbours stay as they are, and are now pinned: an order the book fills only part of did find a market, so it ends Done with what it bought; a limit order carries its own price and rests in an empty book. Gone with it: a setting naming this behaviour that nothing ever read, and a variable in the matcher that computed whether anything traded and did nothing with the answer.
135 lines
3.7 KiB
C#
135 lines
3.7 KiB
C#
namespace StockSharp.MatchingEngine;
|
|
|
|
/// <summary>
|
|
/// Result of order matching.
|
|
/// </summary>
|
|
public class MatchResult
|
|
{
|
|
/// <summary>
|
|
/// Order that was matched.
|
|
/// </summary>
|
|
public EmulatorOrder Order { get; init; }
|
|
|
|
/// <summary>
|
|
/// Whether order was fully matched.
|
|
/// </summary>
|
|
public bool IsFullyMatched => RemainingVolume <= 0;
|
|
|
|
/// <summary>
|
|
/// Whether any trades were executed.
|
|
/// </summary>
|
|
public bool HasTrades => Trades.Count > 0;
|
|
|
|
/// <summary>
|
|
/// Remaining volume after matching.
|
|
/// </summary>
|
|
public decimal RemainingVolume { get; init; }
|
|
|
|
/// <summary>
|
|
/// List of trades executed.
|
|
/// </summary>
|
|
public IReadOnlyList<MatchTrade> Trades { get; init; } = [];
|
|
|
|
/// <summary>
|
|
/// Whether order should be placed in order book.
|
|
/// </summary>
|
|
public bool ShouldPlaceInBook { get; init; }
|
|
|
|
/// <summary>
|
|
/// Whether order was rejected (e.g., post-only crossing).
|
|
/// </summary>
|
|
public bool IsRejected { get; init; }
|
|
|
|
/// <summary>
|
|
/// Rejection reason if rejected.
|
|
/// </summary>
|
|
public string RejectionReason { get; init; }
|
|
|
|
/// <summary>
|
|
/// Final order state.
|
|
/// </summary>
|
|
public OrderStates FinalState { get; init; }
|
|
|
|
/// <summary>
|
|
/// Orders that were matched against (for notification).
|
|
/// </summary>
|
|
public IReadOnlyList<EmulatorOrder> MatchedOrders { get; init; } = [];
|
|
}
|
|
|
|
/// <summary>
|
|
/// Trade resulting from order matching.
|
|
/// </summary>
|
|
/// <param name="Price">Price the trade was done at.</param>
|
|
/// <param name="Volume">Traded volume.</param>
|
|
/// <param name="InitiatorSide">Side of the order that initiated the trade.</param>
|
|
/// <param name="CounterFills">Volume this trade took from each resting order on the other side.</param>
|
|
public record MatchTrade(
|
|
decimal Price,
|
|
decimal Volume,
|
|
Sides InitiatorSide,
|
|
IReadOnlyList<MatchFill> CounterFills
|
|
);
|
|
|
|
/// <summary>
|
|
/// Settings for order matching.
|
|
/// </summary>
|
|
public class MatchingSettings
|
|
{
|
|
/// <summary>
|
|
/// Price step of the security.
|
|
/// </summary>
|
|
public decimal PriceStep { get; init; } = 0.01m;
|
|
|
|
/// <summary>
|
|
/// Volume step of the security.
|
|
/// </summary>
|
|
public decimal VolumeStep { get; init; } = 1m;
|
|
|
|
/// <summary>
|
|
/// Spread size in price steps.
|
|
/// </summary>
|
|
public int SpreadSize { get; init; } = 1;
|
|
|
|
/// <summary>
|
|
/// Maximum depth of the order book.
|
|
/// </summary>
|
|
public int MaxDepth { get; init; } = 10;
|
|
|
|
/// <summary>
|
|
/// When true, use order price instead of market price for limit order trades.
|
|
/// This is used for candle-based matching.
|
|
/// </summary>
|
|
public bool UseOrderPriceForLimitTrades { get; init; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Interface for order matching logic.
|
|
/// </summary>
|
|
public interface IOrderMatcher
|
|
{
|
|
/// <summary>
|
|
/// Try to match an order against the order book.
|
|
/// </summary>
|
|
/// <param name="order">Order to match.</param>
|
|
/// <param name="book">Order book to match against.</param>
|
|
/// <param name="settings">Matching settings.</param>
|
|
/// <returns>Match result.</returns>
|
|
MatchResult Match(EmulatorOrder order, IOrderBook book, MatchingSettings settings);
|
|
|
|
/// <summary>
|
|
/// Check if order would cross the book (for post-only validation).
|
|
/// </summary>
|
|
/// <param name="order">Order to check.</param>
|
|
/// <param name="book">Order book to check against.</param>
|
|
/// <returns>True if the order would cross the book, otherwise false.</returns>
|
|
bool WouldCross(EmulatorOrder order, IOrderBook book);
|
|
|
|
/// <summary>
|
|
/// Get the best execution price for a market order.
|
|
/// </summary>
|
|
/// <param name="side">Side of the market order (buy/sell).</param>
|
|
/// <param name="book">Order book to query.</param>
|
|
/// <returns>Best available price for the market order, or null if book is empty.</returns>
|
|
decimal? GetMarketPrice(Sides side, IOrderBook book);
|
|
}
|