![]() |
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 SessionWindow & | session () const noexcept |
| Returns the session window the runner derives phases from, for strategy::SessionEdges. | |
| 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. | |
| const Instrument * | find_instrument (const InstrumentId &id) const |
| Returns an instrument's reference data without copying it. | |
| 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_session (SessionWindow session) |
| Sets the session window (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 257 of file context.hpp.
| using algotradelib::runner::Context< BrokerTag, CapsType >::Broker = BrokerTag |
The broker tag of the order references.
Definition at line 260 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 262 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 322 of file context.hpp.
|
inlinenoexcept |
Returns the trading calendar the runner last read.
Definition at line 358 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 469 of file context.hpp.
|
inline |
Removes and returns the queued requests (for runners).
Definition at line 487 of file context.hpp.
|
inline |
Returns an instrument's reference data without copying it.
Valid until the instrument's reference data is replaced (set_instrument) or the context is destroyed; read it at once rather than keeping it.
| id | The instrument. |
Definition at line 392 of file context.hpp.
|
inline |
Returns an instrument's reference data, including its price grid.
| id | The instrument. |
Definition at line 375 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 402 of file context.hpp.
|
inlinenoexcept |
|
inlinenoexcept |
Returns the instant the runner is acting at.
Definition at line 330 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 338 of file context.hpp.
Referenced by set_phase().
|
inlinenoexcept |
Returns the portfolio: positions, buying power and the orders still working.
Definition at line 366 of file context.hpp.
Referenced by set_portfolio().
|
inlinenoexcept |
Returns the queued requests without removing them (for runners).
Definition at line 479 of file context.hpp.
|
inlinenoexcept |
Returns the session window the runner derives phases from, for strategy::SessionEdges.
A live loop over a source that states calendar sessions takes the phase from the calendar's hours instead, and this stays the configured window.
Definition at line 350 of file context.hpp.
Referenced by set_session().
|
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 526 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 543 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 554 of file context.hpp.
|
inlinenoexcept |
Sets the current instant (for runners).
| when | The instant. |
Definition at line 497 of file context.hpp.
|
inlinenoexcept |
Sets the market phase (for runners).
| phase | The phase. |
Definition at line 505 of file context.hpp.
|
inline |
Replaces the portfolio (for runners).
| portfolio | The portfolio. |
Definition at line 534 of file context.hpp.
|
inline |
Sets the session window (for runners).
| session | The window. |
Definition at line 513 of file context.hpp.
|
inline |
Queues an order; the runner checks and sends it after the handler returns.
| intent | The order. |
Definition at line 452 of file context.hpp.
|
inline |
Queues a target position; the runner trades the difference from the portfolio.
| target | The target. |
Definition at line 460 of file context.hpp.