![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
What a strategy's handlers receive as ctx: everything the strategy may read, and where it queues what it wants. More...
#include <algotradelib/src/runner/context.hpp>
Classes | |
| class | Log |
| A log that keeps lines in memory for the runner to emit or a test to read. More... | |
Public Types | |
| using | Broker = BrokerTag |
| The broker tag of the order references. | |
| using | caps = CapsType |
| The broker capabilities, for if constexpr (Ctx::caps::has_depth) and the like. | |
Public Member Functions | |
| Context (const Context &)=delete | |
| Deleted: a context cannot be copied, so a handler cannot take it by value. | |
| Context & | operator= (const Context &)=delete |
| Deleted: a context cannot be copied. | |
| Context (Context &&)=default | |
| Move constructor: takes the state of the context passed in, which is left valid but unspecified. | |
| Context & | operator= (Context &&)=default |
| Move assignment: takes the other context's state. | |
| Context (std::size_t series_capacity=512) | |
| Constructs a context. | |
| std::chrono::system_clock::time_point | now () const noexcept |
| Returns the instant the runner is acting at. | |
| MarketPhase | phase () const noexcept |
| Returns the market phase. | |
| const TradingCalendar & | calendar () const noexcept |
| Returns the trading calendar the runner last read. | |
| const Portfolio< BrokerTag > & | portfolio () const noexcept |
| Returns the portfolio: positions, buying power and the orders still working. | |
| std::optional< Instrument > | instrument (const InstrumentId &id) const |
| Returns an instrument's reference data, including its price grid. | |
| std::optional< Decimal64 > | last_price (const InstrumentId &id) const |
| Returns the latest price seen for an instrument (a quote's last price or a bar's close). | |
| Log & | log () noexcept |
| Returns the log. | |
| void | submit (OrderIntent intent) |
| Queues an order; the runner checks and sends it after the handler returns. | |
| void | submit (TargetPosition target) |
| Queues a target position; the runner trades the difference from the portfolio. | |
| void | cancel (const OrderRef< BrokerTag > &ref, std::string tag={}) |
| Queues a cancellation of a working order. | |
| const std::vector< Instruction< BrokerTag > > & | queued () const noexcept |
| Returns the queued requests without removing them (for runners). | |
| std::vector< Instruction< BrokerTag > > | drain () |
| Removes and returns the queued requests (for runners). | |
| void | set_now (std::chrono::system_clock::time_point when) noexcept |
| Sets the current instant (for runners). | |
| void | set_phase (MarketPhase phase) noexcept |
| Sets the market phase (for runners). | |
| void | set_calendar (TradingCalendar calendar) |
| Replaces the trading calendar (for runners and sources, not strategies). | |
| void | set_portfolio (Portfolio< BrokerTag > portfolio) |
| Replaces the portfolio (for runners). | |
| void | set_instrument (Instrument reference) |
| Adds or replaces an instrument's reference data. | |
| void | set_last_price (const InstrumentId &id, Decimal64 price) |
| Records an instrument's latest price, used to value market orders and size notionals. | |
What a strategy's handlers receive as ctx: everything the strategy may read, and where it queues what it wants.
The same type in a dry run, a live session and a backtest. Nothing here performs I/O: every read comes from caches the runner filled, and submit and cancel only queue requests, which the runner checks and sends after the handler returns. The members under "What the runner uses" are for runners, not strategies.
| BrokerTag | The broker tag of order references (a strategy reads them with auto). |
| CapsType | The broker capabilities, exposed as caps for if constexpr. |
Definition at line 81 of file context.hpp.
| using algotradelib::runner::Context< BrokerTag, CapsType >::Broker = BrokerTag |
The broker tag of the order references.
Definition at line 84 of file context.hpp.
| using algotradelib::runner::Context< BrokerTag, CapsType >::caps = CapsType |
The broker capabilities, for if constexpr (Ctx::caps::has_depth) and the like.
Definition at line 86 of file context.hpp.
|
delete |
Deleted: a context cannot be copied, so a handler cannot take it by value.
Referenced by Context(), Context(), operator=(), and operator=().
|
inlineexplicit |
Constructs a context.
| series_capacity | How many trailing bars each cached series keeps; must be positive with a linear algebra backend, or the first record_bar throws. |
Definition at line 144 of file context.hpp.
|
inlinenoexcept |
Returns the trading calendar the runner last read.
Definition at line 164 of file context.hpp.
Referenced by set_calendar().
|
inline |
Queues a cancellation of a working order.
| ref | The order, from portfolio().working_. |
| tag | A label for the cancellation. |
Definition at line 247 of file context.hpp.
|
inline |
Removes and returns the queued requests (for runners).
Definition at line 263 of file context.hpp.
|
inline |
Returns an instrument's reference data, including its price grid.
| id | The instrument. |
Definition at line 177 of file context.hpp.
|
inline |
Returns the latest price seen for an instrument (a quote's last price or a bar's close).
| id | The instrument. |
Definition at line 190 of file context.hpp.
|
inlinenoexcept |
|
inlinenoexcept |
Returns the instant the runner is acting at.
Definition at line 152 of file context.hpp.
|
delete |
Deleted: a context cannot be copied.
|
default |
Move assignment: takes the other context's state.
|
inlinenoexcept |
Returns the market phase.
Definition at line 158 of file context.hpp.
Referenced by set_phase().
|
inlinenoexcept |
Returns the portfolio: positions, buying power and the orders still working.
Definition at line 170 of file context.hpp.
Referenced by set_portfolio().
|
inlinenoexcept |
Returns the queued requests without removing them (for runners).
Definition at line 257 of file context.hpp.
|
inline |
Replaces the trading calendar (for runners and sources, not strategies).
A strategy should only read calendar(): an order submitted in the same handler call that changed the calendar is checked against the phase from before the change. runner::LoopReport::sends_under_a_revised_phase_ counts such sends.
| calendar | The calendar. |
Definition at line 290 of file context.hpp.
Referenced by algotradelib::runner::ManualSource< Runner >::poll().
|
inline |
Adds or replaces an instrument's reference data.
| reference | The reference data. |
Definition at line 302 of file context.hpp.
|
inline |
Records an instrument's latest price, used to value market orders and size notionals.
| id | The instrument. |
| price | The price. |
Definition at line 312 of file context.hpp.
|
inlinenoexcept |
Sets the current instant (for runners).
| when | The instant. |
Definition at line 273 of file context.hpp.
|
inlinenoexcept |
Sets the market phase (for runners).
| phase | The phase. |
Definition at line 279 of file context.hpp.
|
inline |
Replaces the portfolio (for runners).
| portfolio | The portfolio. |
Definition at line 296 of file context.hpp.
|
inline |
Queues an order; the runner checks and sends it after the handler returns.
| intent | The order. |
Definition at line 234 of file context.hpp.
|
inline |
Queues a target position; the runner trades the difference from the portfolio.
| target | The target. |
Definition at line 240 of file context.hpp.