![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
A market in memory: bars the caller gives, a clock the caller moves, one account's cash, holdings and orders. More...
#include <algotradelib/src/brokers/mock/market.hpp>
Public Member Functions | |
| Market (std::vector< BarSeries > bars, MarketSettings settings, std::vector< Instrument > instruments={}) | |
| Builds a market at settings.start_. | |
| std::chrono::system_clock::time_point | now () const noexcept |
| Returns the market's clock. | |
| const std::optional< std::string > & | halted () const noexcept |
| Returns why the market halted, if it did. | |
| void | advance_to (std::chrono::system_clock::time_point to) |
| Moves the clock to an instant, applying every bar whose period end it reaches, in order of period end across instruments (ties in the order the series were given). | |
| Result< BarSeries > | bars (const BarQuery &query) const |
| Answers a bar request: bars whose period has ended by now(), never a forming one. | |
| Result< QuoteSnapshot > | quote (const InstrumentId &instrument) const |
| Answers a quote: the close of the instrument's last bar that has ended by now(). | |
| Result< Instrument > | instrument (const InstrumentId &instrument) const |
| Answers a reference-data request. | |
| Result< TradingCalendar > | calendar (Country country) const |
| Answers a calendar request. | |
| Result< HoldingsSnapshot > | holdings () const |
| Answers a holdings request. | |
| Money | cash () const |
| Returns the account's cash, before what open buys reserve. | |
| Result< BuyingPower > | buying_power (const Currency ¤cy) const |
| Answers a buying-power request: cash less what open buys reserve. | |
| Result< PlacedOrder< Broker > > | place (const OrderRequest &order) |
| Places an order. | |
| Result< PlacedOrder< Broker > > | cancel (const std::string &order_id) |
| Cancels the whole remainder of an open order. | |
| WorkingOrders< Broker > | working_orders () const |
| Answers a working-order request. | |
| OrderPage | order_history (const OrderHistoryQuery &query) const |
| Answers an order-history request: every order, oldest first, in one page. | |
Friends | |
| class | MarketFile |
A market in memory: bars the caller gives, a clock the caller moves, one account's cash, holdings and orders.
A bar is applied when the clock passes its period end. Orders fill by the backtest's rule (backtest::NextBarFillModel): an order trades against the first bar of its instrument whose period began at or after the instant the market accepted it, a market order at that bar's open (or at the price of the fill model's market-price rule, given the series' bar before it), a limit order if the bar's range reaches the limit, never partly; the fill is stamped at the bar's period end. A DAY order lives in the session of that first bar, and expires as a backtest's does: after a daily bar of its session, after an intraday bar ending at or after the session's close, or before a bar of a later date (a session whose closing bar is missing still ended). An immediate-or-cancel or fill-or-kill order expires after its first bar. Expired orders are CANCELED.
Unlike a backtest, the market never fills an order against a bar that began before the order was accepted, even where a backtest cannot tell and allows it (ReplayResult::unchecked_fill_causality_).
Not modelled: trading hours (an order is accepted at any instant; the calendar and the session window only date the market), price bands, partial fills, volume, settlement, short selling, interest. Cash is kept in one currency, and may go below zero when a market buy fills above the price it reserved.
If a fill cannot be costed (the fill model's commission has no rate for its date) or recorded (its amount does not fit an exact decimal), or a position cannot be valued at a bar's close, the market halts: advance_to throws std::runtime_error and keeps throwing, placements are refused (market_halted), and the clock stays at the period end of the bar it halted on. What was done before the failure stands, including fills of earlier orders on that bar; the order that failed stays PENDING. Reads answer that state.
Not thread-safe. Nothing is persisted unless the market is kept in a state file (MarketFile): a copy of a kept market is an ordinary market, which writes nothing, and assigning to a kept market or moving from it throws std::logic_error, leaving it as it was.
Definition at line 143 of file market.hpp.
|
inline |
Builds a market at settings.start_.
| bars | One series per instrument, each strictly ascending, of a minute to daily interval; series may differ in interval, stamp and currency. |
| settings | The starting cash, the first instant, the fill rule and the session window. |
| instruments | Reference data for some or all of the instruments, as instrument answers it and as a backtest is given it (backtest::ReplayConfig::instruments_). An instrument without one gets an active stock in its series' currency, a lot of one share and no price grid (PriceRule::unconstrained). |
| std::invalid_argument | for two series of one instrument, a series not strictly ascending, a weekly, monthly or yearly series, a negative starting cash, or reference data for an instrument without bars. |
Definition at line 158 of file market.hpp.
|
inline |
Moves the clock to an instant, applying every bar whose period end it reaches, in order of period end across instruments (ties in the order the series were given).
| to | The instant; the clock never moves backwards. |
| std::invalid_argument | if to is before now(). |
| std::runtime_error | if the market halts on this call (the clock then stays at the period end of the bar it halted on; see the class) or had halted. |
| MarketFileError | if the market is kept in a file that could not record the move. |
Definition at line 226 of file market.hpp.
Answers a bar request: bars whose period has ended by now(), never a forming one.
| query | The instrument, the interval (the series' own), the count and the newest timestamp. |
Definition at line 266 of file market.hpp.
Referenced by Market().
|
inline |
Answers a buying-power request: cash less what open buys reserve.
An open limit buy reserves its quantity at its limit, an open market buy its quantity at the last close when it was placed. The figure can be negative after a market buy filled above that.
| currency | The currency. |
Definition at line 423 of file market.hpp.
|
inline |
Answers a calendar request.
| country | The market. |
Definition at line 357 of file market.hpp.
|
inline |
Cancels the whole remainder of an open order.
| order_id | The order id. |
Definition at line 553 of file market.hpp.
|
inline |
Returns the account's cash, before what open buys reserve.
Definition at line 409 of file market.hpp.
Referenced by buying_power(), and place().
|
inlinenoexcept |
Returns why the market halted, if it did.
Definition at line 213 of file market.hpp.
|
inline |
Answers a holdings request.
Definition at line 373 of file market.hpp.
|
inline |
Answers a reference-data request.
| instrument | The instrument. |
Definition at line 343 of file market.hpp.
Referenced by instrument(), and quote().
|
inlinenoexcept |
Returns the market's clock.
Definition at line 205 of file market.hpp.
|
inline |
Answers an order-history request: every order, oldest first, in one page.
| query | from_ and to_ keep orders accepted on those local dates (inclusive); the cursor, symbol and limit are not read. |
Definition at line 602 of file market.hpp.
|
inline |
Places an order.
Checked in this order, each refusal INVALID_ARGUMENT with reached_broker_ set: market_halted; unknown_instrument; unsupported_order_type (market and limit only); unsupported_time_in_force (DAY, immediate-or-cancel, fill-or-kill); invalid_quantity (not a positive whole number); invalid_price (a limit without a positive price, or a market order with one); no_price (a market buy before any bar of its instrument ended); insufficient_cash (a buy reserving more than the buying power, or in a currency without cash); insufficient_holdings (a sell of more than is held less open sells).
A kept market whose file could not record the order answers TRANSPORT: state_not_written without reached_broker_, or state_unconfirmed with it (reopening the file may accept the order).
| order | The order; its venue is not used. |
Definition at line 459 of file market.hpp.
|
inline |
Answers a quote: the close of the instrument's last bar that has ended by now().
| instrument | The instrument. |
Definition at line 315 of file market.hpp.
Referenced by quote().
|
inline |
Answers a working-order request.
Definition at line 580 of file market.hpp.
|
friend |
Definition at line 616 of file market.hpp.