algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::mock::MarketFile Class Reference

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...

#include <algotradelib/src/brokers/mock/file.hpp>

Public Member Functions

 MarketFile (std::string path, std::vector< BarSeries > bars, MarketSettings settings, std::vector< Instrument > instruments={})
 Opens or creates the file, replays it into a market built as Market's constructor builds one, and takes its lock.
 ~MarketFile ()=default
 Closes the file, which releases its lock.
 MarketFile (const MarketFile &)=delete
MarketFile & operator= (const MarketFile &)=delete
bool open () const noexcept
 Tests whether the file opened and replayed.
bool failed () const noexcept
 Tests whether a line could not be written or flushed since the file opened.
const std::string & rejection () const noexcept
 Returns why the file did not open.
const std::string & path () const noexcept
 Returns the file's path.
Market & market () noexcept
 Returns the kept market, for MockClient and MarketClock.
const Market & market () const noexcept
 Returns the kept market.

Static Public Attributes

static constexpr std::int64_t FORMAT = 1
 The file format's number, on its first line.
static constexpr std::int64_t SEMANTICS = 1
 The number of the market's rules a file replays under, raised when a fill, cost, ledger, expiry or calendar rule changes.

Detailed Description

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.

Constructor & Destructor Documentation

◆ MarketFile()

algotradelib::mock::MarketFile::MarketFile ( std::string path,
std::vector< BarSeries > bars,
MarketSettings settings,
std::vector< Instrument > instruments = {} )
inline

Opens or creates the file, replays it into a market built as Market's constructor builds one, and takes its lock.

Never throws for the file: check open() and rejection(). A market whose file did not open refuses every placement, cancellation and clock move (state_not_written).

Parameters
pathThe file; created if absent, in a folder that exists.
barsAs Market's constructor takes them.
settingsAs Market's constructor takes them.
instrumentsAs Market's constructor takes them.
Exceptions
std::invalid_argumentas Market's constructor does, before the file is touched.

Definition at line 90 of file file.hpp.

Referenced by ~MarketFile().

Member Function Documentation

◆ failed()

bool algotradelib::mock::MarketFile::failed ( ) const
inlinenoexcept

Tests whether a line could not be written or flushed since the file opened.

Returns
true if one could not; the market then refuses changes until the file is opened again.

Definition at line 122 of file file.hpp.

◆ market() [1/2]

const Market & algotradelib::mock::MarketFile::market ( ) const
inlinenoexcept

Returns the kept market.

Returns
The market.

Definition at line 155 of file file.hpp.

◆ market() [2/2]

Market & algotradelib::mock::MarketFile::market ( )
inlinenoexcept

Returns the kept market, for MockClient and MarketClock.

Returns
The market; copy it to try changes that are not recorded. Assigning to it or moving from it throws std::logic_error.

Definition at line 147 of file file.hpp.

◆ open()

bool algotradelib::mock::MarketFile::open ( ) const
inlinenoexcept

Tests whether the file opened and replayed.

Returns
true if it did; changes are then recorded until one fails (failed()).

Definition at line 114 of file file.hpp.

◆ path()

const std::string & algotradelib::mock::MarketFile::path ( ) const
inlinenoexcept

Returns the file's path.

Returns
The path.

Definition at line 138 of file file.hpp.

Referenced by MarketFile().

◆ rejection()

const std::string & algotradelib::mock::MarketFile::rejection ( ) const
inlinenoexcept

Returns why the file did not open.

Returns
The reason, in fixed words, or empty if it opened.

Definition at line 130 of file file.hpp.

Member Data Documentation

◆ FORMAT

std::int64_t algotradelib::mock::MarketFile::FORMAT = 1
staticconstexpr

The file format's number, on its first line.

Definition at line 70 of file file.hpp.

◆ SEMANTICS

std::int64_t algotradelib::mock::MarketFile::SEMANTICS = 1
staticconstexpr

The number of the market's rules a file replays under, raised when a fill, cost, ledger, expiry or calendar rule changes.

Definition at line 75 of file file.hpp.


The documentation for this class was generated from the following file:
  • algotradelib/src/brokers/mock/file.hpp