3 Commits
Author SHA1 Message Date
mika@stocksharp.com 47fcbdf055 Match orders against the book, and leave inventing one to emulation
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.
2026-08-25 03:33:59 +03:00
mika@stocksharp.com 748006938f Emulation: bound the synthesized matching-engine order book to MaxDepth
The decomposed matching engine synthesizes an order book from ticks/Level1 when there is no real order-book
history (MatchingEngineAdapter.ProcessTick/ProcessLevel1 -> OrderBook.UpdateLevel). It added a new level per
distinct price and never removed stale ones, so replaying a long varied price stream - as a tick backtest
does - grew the book without bound (hundreds of thousands of OrderBookLevelImpl plus the downstream
ExecutionMessages they drive), accumulating into multi-GB heap growth that eventually exhausted memory and
crashed the test host near the end of the full suite.

Restore the MaxDepth cap the monolith MarketEmulatorOld already applied: OrderBook.TrimSynthesizedDepth drops
the farthest synthetic levels beyond MaxDepth after each synthesized UpdateLevel, and never removes levels
that hold real registered user orders - only their synthetic MarketVolume is cleared. MatchingEngineSettings
gets a MaxDepth (default 5) mirroring MarketEmulatorSettings, and MarketEmulator syncs it into the engine.

Add tests asserting the tick- and Level1-synthesized book stays within MaxDepth; the fill-price tests (fills
near the current price, not a stale extreme) stay green.
2026-06-21 23:05:20 +03:00
mika@stocksharp.com 7d805c7e26 Add MatchingEngineAdapter with SecurityState and Settings
Message-based matching engine adapter implementing IMessageTransport.
Contains pure matching logic without emulation dependencies:
- SecurityState: per-security state (OrderBook, OrderManager, steps)
- MatchingEngineSettings: minimal settings for matching
- MatchingEngineAdapter: order matching, portfolio management, stop orders,
  L1/tick->orderbook conversion, IncreaseDepthVolume
2026-03-05 19:56:13 +03:00