algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::runner::Context< BrokerTag, CapsType > Class Template Reference

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>

Inheritance diagram for algotradelib::runner::Context< BrokerTag, CapsType >:

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.

Detailed Description

template<typename BrokerTag, typename CapsType>
class algotradelib::runner::Context< BrokerTag, CapsType >

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.

Template Parameters
BrokerTagThe broker tag of order references (a strategy reads them with auto).
CapsTypeThe broker capabilities, exposed as caps for if constexpr.

Definition at line 81 of file context.hpp.

Member Typedef Documentation

◆ Broker

template<typename BrokerTag, typename CapsType>
using algotradelib::runner::Context< BrokerTag, CapsType >::Broker = BrokerTag

The broker tag of the order references.

Definition at line 84 of file context.hpp.

◆ caps

template<typename BrokerTag, typename CapsType>
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.

Constructor & Destructor Documentation

◆ Context() [1/2]

template<typename BrokerTag, typename CapsType>
algotradelib::runner::Context< BrokerTag, CapsType >::Context ( const Context< BrokerTag, CapsType > & )
delete

Deleted: a context cannot be copied, so a handler cannot take it by value.

Referenced by Context(), Context(), operator=(), and operator=().

◆ Context() [2/2]

template<typename BrokerTag, typename CapsType>
algotradelib::runner::Context< BrokerTag, CapsType >::Context ( std::size_t series_capacity = 512)
inlineexplicit

Constructs a context.

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

Member Function Documentation

◆ calendar()

template<typename BrokerTag, typename CapsType>
const TradingCalendar & algotradelib::runner::Context< BrokerTag, CapsType >::calendar ( ) const
inlinenoexcept

Returns the trading calendar the runner last read.

Returns
The calendar; empty if none was read.

Definition at line 164 of file context.hpp.

Referenced by set_calendar().

◆ cancel()

template<typename BrokerTag, typename CapsType>
void algotradelib::runner::Context< BrokerTag, CapsType >::cancel ( const OrderRef< BrokerTag > & ref,
std::string tag = {} )
inline

Queues a cancellation of a working order.

Parameters
refThe order, from portfolio().working_.
tagA label for the cancellation.

Definition at line 247 of file context.hpp.

◆ drain()

template<typename BrokerTag, typename CapsType>
std::vector< Instruction< BrokerTag > > algotradelib::runner::Context< BrokerTag, CapsType >::drain ( )
inline

Removes and returns the queued requests (for runners).

Returns
The requests, in order.

Definition at line 263 of file context.hpp.

◆ instrument()

template<typename BrokerTag, typename CapsType>
std::optional< Instrument > algotradelib::runner::Context< BrokerTag, CapsType >::instrument ( const InstrumentId & id) const
inline

Returns an instrument's reference data, including its price grid.

Parameters
idThe instrument.
Returns
The reference data, or std::nullopt if the runner has not loaded it.

Definition at line 177 of file context.hpp.

◆ last_price()

template<typename BrokerTag, typename CapsType>
std::optional< Decimal64 > algotradelib::runner::Context< BrokerTag, CapsType >::last_price ( const InstrumentId & id) const
inline

Returns the latest price seen for an instrument (a quote's last price or a bar's close).

Parameters
idThe instrument.
Returns
The price, or std::nullopt if none has been seen.

Definition at line 190 of file context.hpp.

◆ log()

template<typename BrokerTag, typename CapsType>
Log & algotradelib::runner::Context< BrokerTag, CapsType >::log ( )
inlinenoexcept

Returns the log.

Returns
The log.

Definition at line 226 of file context.hpp.

◆ now()

template<typename BrokerTag, typename CapsType>
std::chrono::system_clock::time_point algotradelib::runner::Context< BrokerTag, CapsType >::now ( ) const
inlinenoexcept

Returns the instant the runner is acting at.

Returns
The current instant (a replay's clock in a backtest).

Definition at line 152 of file context.hpp.

◆ operator=() [1/2]

template<typename BrokerTag, typename CapsType>
Context & algotradelib::runner::Context< BrokerTag, CapsType >::operator= ( const Context< BrokerTag, CapsType > & )
delete

Deleted: a context cannot be copied.

Returns
Not callable.

◆ operator=() [2/2]

template<typename BrokerTag, typename CapsType>
Context & algotradelib::runner::Context< BrokerTag, CapsType >::operator= ( Context< BrokerTag, CapsType > && )
default

Move assignment: takes the other context's state.

Returns
*this.

◆ phase()

template<typename BrokerTag, typename CapsType>
MarketPhase algotradelib::runner::Context< BrokerTag, CapsType >::phase ( ) const
inlinenoexcept

Returns the market phase.

Returns
The phase.

Definition at line 158 of file context.hpp.

Referenced by set_phase().

◆ portfolio()

template<typename BrokerTag, typename CapsType>
const Portfolio< BrokerTag > & algotradelib::runner::Context< BrokerTag, CapsType >::portfolio ( ) const
inlinenoexcept

Returns the portfolio: positions, buying power and the orders still working.

Returns
The portfolio.

Definition at line 170 of file context.hpp.

Referenced by set_portfolio().

◆ queued()

template<typename BrokerTag, typename CapsType>
const std::vector< Instruction< BrokerTag > > & algotradelib::runner::Context< BrokerTag, CapsType >::queued ( ) const
inlinenoexcept

Returns the queued requests without removing them (for runners).

Returns
The requests, in order.

Definition at line 257 of file context.hpp.

◆ set_calendar()

template<typename BrokerTag, typename CapsType>
void algotradelib::runner::Context< BrokerTag, CapsType >::set_calendar ( TradingCalendar calendar)
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.

Parameters
calendarThe calendar.

Definition at line 290 of file context.hpp.

Referenced by algotradelib::runner::ManualSource< Runner >::poll().

◆ set_instrument()

template<typename BrokerTag, typename CapsType>
void algotradelib::runner::Context< BrokerTag, CapsType >::set_instrument ( Instrument reference)
inline

Adds or replaces an instrument's reference data.

Parameters
referenceThe reference data.

Definition at line 302 of file context.hpp.

◆ set_last_price()

template<typename BrokerTag, typename CapsType>
void algotradelib::runner::Context< BrokerTag, CapsType >::set_last_price ( const InstrumentId & id,
Decimal64 price )
inline

Records an instrument's latest price, used to value market orders and size notionals.

Parameters
idThe instrument.
priceThe price.

Definition at line 312 of file context.hpp.

◆ set_now()

template<typename BrokerTag, typename CapsType>
void algotradelib::runner::Context< BrokerTag, CapsType >::set_now ( std::chrono::system_clock::time_point when)
inlinenoexcept

Sets the current instant (for runners).

Parameters
whenThe instant.

Definition at line 273 of file context.hpp.

◆ set_phase()

template<typename BrokerTag, typename CapsType>
void algotradelib::runner::Context< BrokerTag, CapsType >::set_phase ( MarketPhase phase)
inlinenoexcept

Sets the market phase (for runners).

Parameters
phaseThe phase.

Definition at line 279 of file context.hpp.

◆ set_portfolio()

template<typename BrokerTag, typename CapsType>
void algotradelib::runner::Context< BrokerTag, CapsType >::set_portfolio ( Portfolio< BrokerTag > portfolio)
inline

Replaces the portfolio (for runners).

Parameters
portfolioThe portfolio.

Definition at line 296 of file context.hpp.

◆ submit() [1/2]

template<typename BrokerTag, typename CapsType>
void algotradelib::runner::Context< BrokerTag, CapsType >::submit ( OrderIntent intent)
inline

Queues an order; the runner checks and sends it after the handler returns.

Parameters
intentThe order.

Definition at line 234 of file context.hpp.

◆ submit() [2/2]

template<typename BrokerTag, typename CapsType>
void algotradelib::runner::Context< BrokerTag, CapsType >::submit ( TargetPosition target)
inline

Queues a target position; the runner trades the difference from the portfolio.

Parameters
targetThe target.

Definition at line 240 of file context.hpp.


The documentation for this class was generated from the following file: