A mock market that keeps its account in a file: every placement, cancellation and clock move is written before it is made, and opening the file again rebuilds the same market.
More...
A mock market that keeps its account in a file: every placement, cancellation and clock move is written before it is made, and opening the file again rebuilds the same market.
For a program that runs once per command and trades the same account each time. Build it with the bars, settings and reference data the market was first built with: the file's first line holds a digest of them and of the rules the market fills by, and a file whose first line does not match is refused. The digest covers every series in the order given with every bar, the starting cash and instant, the fill model (the market-price rule, the limit price, the whole commission policy, the slippage and the sell tax; not the tariff's name), the session window's times, the market's calendar and each instrument's reference data. It detects a different market; it does not authenticate the file.
Each later line records one change and its instant, never an answer: opening replays them, and the market answers again as it did. A change is made only once its whole line is written and flushed to disk. A line cut short (the process was killed while writing it) is never replayed, and opening removes it. Opening flushes the file and its folder before the market takes a change, and refuses a file it cannot flush. A file refused as another market's, of another format or rules, or damaged is left byte for byte as it was; an incomplete last line or header may be removed before an open that then fails, such as one whose flush fails. When a line cannot be written whole, the change is not made and the call answers as Market::place documents (state_not_written); when it was written and flushing it failed, the change is not made here but reopening may make it (state_unconfirmed). After either, the market refuses every placement, cancellation and clock move until the file is opened again; reads answer as before. A market that halted reopens halted, at the bar it halted on.
Only a market whose settings fully describe it can be kept: a fill model whose market orders are priced by a caller's own function is refused. Refused calls (an order the market refuses, a cancellation of no open order) change nothing and are not recorded. The file is replayed whole each time it is opened, so opening takes longer as its history grows. The client's scripted faults (MockClient::fail_next) are not kept.
On POSIX the file is locked (flock) while it is open, so a second MarketFile on it, in any process, is refused; a process that forks without exec shares the lock with its child. Keep it on a local disk. Windows takes no lock and does not flush the file's folder. A file is opened only where the system clock counts in the unit it was written with.
Not copyable or movable: the market stays at one address while the file is open.
Definition at line 67 of file file.hpp.