mirror of
https://github.com/StockSharp/StockSharp.git
synced 2026-12-04 08:32:23 +00:00
The engine's job is to reconcile real orders against a real book. It was also making the book up, and what it made up was wrong in ways that cost money. A quote states where the market is. Each one was written in as a level and the ones before it kept, so a market walking one way left every price it had ever quoted resting in the book while the touch stayed where the walk began: three falling quotes and the book held a bid above its own ask. Past the trim depth the stale side froze there for good while the other tracked away without bound. A print says a trade happened. It was written in as a resting level, on a side guessed when the feed did not say, at a volume of one when the print carried none; on an empty book a single trade conjured both sides of a market with the far side a made-up spread away; and a print at or through the touch was written opposite at the price of the print, which crosses the book on purpose. Then it trimmed - and the trim could not tell an invented level from one the venue had stated, sparing only levels holding a client's own order, so every print cut the venue's real book to five levels a side. The matcher subscribes to ticks, so that is what the live book has been doing. An order asking for more than the whole opposite side got the book extended past its worst level, a step apart, doubling volume each time, and traded through it: five lots offered, fifty asked for, fifty filled. That is replay, and the emulation asks for it by name; the engine had it on by default and the venue never turned it off. A resting order the market moved through was filled for its whole balance with no level consumed and no counterparty. An instrument whose venue never states a price step has one guessed from the first price quoted. The guess was written into the security definition the venue sent - kept by reference, so in the venue it is the feeder's own object - and it was written whether or not the venue had stated a step, because the gate was "have we guessed yet" rather than "were we told". All of it goes. What a quote still tells the engine is whether the instrument may be traded; what a print still does is check the stops waiting on that price; extending a book happens only when asked. Building a book out of quotes and prints was already the emulation's own work, done correctly there - it moves the level instead of leaving the old one behind - and filling a resting order without a counterparty joins it. The knobs that bounded and spaced what the engine invented go with it, along with a trim that had no caller. What stays is matching: folding an incremental feed into the book the engine holds, keeping client orders across a venue snapshot, stops, margin, and refusing an order the book cannot price - which after this is the only thing separating "there is no market" from "this order is free". The two tests standing watch over the emulated book asked only that it stay under a level count. It always did: a book pinned to the first price it ever saw is one level deep. They ask now that the touch be the last thing quoted and that the sides have not crossed.
221 lines
6.0 KiB
C#
221 lines
6.0 KiB
C#
namespace StockSharp.MatchingEngine;
|
|
|
|
/// <summary>
|
|
/// Represents a price level in the order book.
|
|
/// </summary>
|
|
/// <param name="Price">The price of the level.</param>
|
|
/// <param name="Volume">Total volume at this level.</param>
|
|
/// <param name="Orders">Orders at this level.</param>
|
|
public record OrderBookLevel(decimal Price, decimal Volume, IReadOnlyList<EmulatorOrder> Orders);
|
|
|
|
/// <summary>
|
|
/// Order stored in the emulator order book.
|
|
/// </summary>
|
|
public class EmulatorOrder
|
|
{
|
|
/// <summary>
|
|
/// Transaction ID of the order.
|
|
/// </summary>
|
|
public long TransactionId { get; init; }
|
|
|
|
/// <summary>
|
|
/// Order ID assigned by exchange/emulator.
|
|
/// </summary>
|
|
public long? OrderId { get; set; }
|
|
|
|
/// <summary>
|
|
/// Order side.
|
|
/// </summary>
|
|
public Sides Side { get; init; }
|
|
|
|
/// <summary>
|
|
/// Order price.
|
|
/// </summary>
|
|
public decimal Price { get; init; }
|
|
|
|
/// <summary>
|
|
/// Remaining order volume.
|
|
/// </summary>
|
|
public decimal Balance { get; set; }
|
|
|
|
/// <summary>
|
|
/// Original order volume.
|
|
/// </summary>
|
|
public decimal Volume { get; init; }
|
|
|
|
/// <summary>
|
|
/// Portfolio name. Null for market quotes.
|
|
/// </summary>
|
|
public string PortfolioName { get; init; }
|
|
|
|
/// <summary>
|
|
/// Time in force.
|
|
/// </summary>
|
|
public TimeInForce? TimeInForce { get; init; }
|
|
|
|
/// <summary>
|
|
/// Order type.
|
|
/// </summary>
|
|
public OrderTypes? OrderType { get; init; }
|
|
|
|
/// <summary>
|
|
/// Post-only flag.
|
|
/// </summary>
|
|
public bool PostOnly { get; init; }
|
|
|
|
/// <summary>
|
|
/// Expiry date.
|
|
/// </summary>
|
|
public DateTime? ExpiryDate { get; init; }
|
|
|
|
/// <summary>
|
|
/// Server time when order was registered.
|
|
/// </summary>
|
|
public DateTime ServerTime { get; init; }
|
|
|
|
/// <summary>
|
|
/// Local time when order was registered.
|
|
/// </summary>
|
|
public DateTime LocalTime { get; init; }
|
|
|
|
/// <summary>
|
|
/// Margin price used for blocking funds (may differ from order price).
|
|
/// </summary>
|
|
public decimal MarginPrice { get; init; }
|
|
|
|
/// <summary>
|
|
/// Indicates if this is a user order (has portfolio) or market quote.
|
|
/// </summary>
|
|
public bool IsUserOrder => PortfolioName is not null;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Interface for order book management.
|
|
/// </summary>
|
|
public interface IOrderBook
|
|
{
|
|
/// <summary>
|
|
/// Security ID.
|
|
/// </summary>
|
|
SecurityId SecurityId { get; }
|
|
|
|
/// <summary>
|
|
/// Get best bid price and volume.
|
|
/// </summary>
|
|
(decimal price, decimal volume)? BestBid { get; }
|
|
|
|
/// <summary>
|
|
/// Get best ask price and volume.
|
|
/// </summary>
|
|
(decimal price, decimal volume)? BestAsk { get; }
|
|
|
|
/// <summary>
|
|
/// Total volume of all bids.
|
|
/// </summary>
|
|
decimal TotalBidVolume { get; }
|
|
|
|
/// <summary>
|
|
/// Total volume of all asks.
|
|
/// </summary>
|
|
decimal TotalAskVolume { get; }
|
|
|
|
/// <summary>
|
|
/// Number of bid levels.
|
|
/// </summary>
|
|
int BidLevels { get; }
|
|
|
|
/// <summary>
|
|
/// Number of ask levels.
|
|
/// </summary>
|
|
int AskLevels { get; }
|
|
|
|
/// <summary>
|
|
/// Add a quote to the order book.
|
|
/// </summary>
|
|
/// <param name="order">Order or market quote to add. If <see cref="EmulatorOrder.TransactionId"/> is default, it is treated as a market quote.</param>
|
|
void AddQuote(EmulatorOrder order);
|
|
|
|
/// <summary>
|
|
/// Remove a quote from the order book.
|
|
/// </summary>
|
|
/// <param name="transactionId">Transaction id of the order to remove.</param>
|
|
/// <param name="side">Side (buy/sell) where the order is placed.</param>
|
|
/// <param name="price">Price level of the order.</param>
|
|
/// <returns>True if removed, false if not found.</returns>
|
|
bool RemoveQuote(long transactionId, Sides side, decimal price);
|
|
|
|
/// <summary>
|
|
/// Update volume at a price level (for market quotes).
|
|
/// </summary>
|
|
/// <param name="side">Side to update.</param>
|
|
/// <param name="price">Price level to update.</param>
|
|
/// <param name="volume">New market volume at the level.</param>
|
|
void UpdateLevel(Sides side, decimal price, decimal volume);
|
|
|
|
/// <summary>
|
|
/// Remove entire level at price.
|
|
/// </summary>
|
|
/// <param name="side">Side to remove level from.</param>
|
|
/// <param name="price">Price level to remove.</param>
|
|
/// <returns>Removed orders that were present at that level.</returns>
|
|
IEnumerable<EmulatorOrder> RemoveLevel(Sides side, decimal price);
|
|
|
|
/// <summary>
|
|
/// Get quotes for a side, ordered by price (best first).
|
|
/// </summary>
|
|
/// <param name="side">Side to retrieve levels for.</param>
|
|
IEnumerable<OrderBookLevel> GetLevels(Sides side);
|
|
|
|
/// <summary>
|
|
/// Get volume at a specific price level.
|
|
/// </summary>
|
|
/// <param name="side">Side to query.</param>
|
|
/// <param name="price">Price level to query.</param>
|
|
decimal GetVolumeAtPrice(Sides side, decimal price);
|
|
|
|
/// <summary>
|
|
/// Get all user orders at a specific price level.
|
|
/// </summary>
|
|
/// <param name="side">Side to query.</param>
|
|
/// <param name="price">Price level to query.</param>
|
|
IEnumerable<EmulatorOrder> GetOrdersAtPrice(Sides side, decimal price);
|
|
|
|
/// <summary>
|
|
/// Check if there's a quote at the specified price.
|
|
/// </summary>
|
|
/// <param name="side">Side to check.</param>
|
|
/// <param name="price">Price level to check.</param>
|
|
bool HasLevel(Sides side, decimal price);
|
|
|
|
/// <summary>
|
|
/// Clear all quotes.
|
|
/// </summary>
|
|
void Clear();
|
|
|
|
/// <summary>
|
|
/// Clear all quotes for a specific side.
|
|
/// </summary>
|
|
/// <param name="side">Side to clear.</param>
|
|
void Clear(Sides side);
|
|
|
|
/// <summary>
|
|
/// Set the order book from a snapshot (replaces all non-user quotes).
|
|
/// </summary>
|
|
/// <param name="bids">Sequence of bid quote changes to set.</param>
|
|
/// <param name="asks">Sequence of ask quote changes to set.</param>
|
|
void SetSnapshot(IEnumerable<QuoteChange> bids, IEnumerable<QuoteChange> asks);
|
|
|
|
/// <summary>
|
|
/// Create a QuoteChangeMessage from current state.
|
|
/// </summary>
|
|
/// <param name="localTime">Local time to set on the generated message.</param>
|
|
/// <param name="serverTime">Server time to set on the generated message.</param>
|
|
QuoteChangeMessage ToMessage(DateTime localTime, DateTime serverTime);
|
|
|
|
/// <summary>
|
|
/// Get total volume for a side.
|
|
/// </summary>
|
|
/// <param name="side">Side to get total volume for.</param>
|
|
decimal GetTotalVolume(Sides side);
|
|
}
|