algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::live::SessionSource< Session, Journal, MarketData > Class Template Reference

The source a runner::PollingLoop polls: reads a broker session and feeds bars, quotes, holdings, orders, buying power, price bands and the calendar to a runner. More...

#include <algotradelib/src/live/session_source.hpp>

Classes

struct  ObservedFigure
 One charge figure of one order, and the filled quantity it was recorded at. More...
struct  ObservedCharge
 One order's stated charges, as last recorded. More...

Public Types

using Caps = typename Session::Caps
 The broker's capabilities, from the account session.
using BrokerTag = typename Session::BrokerTag
 The broker tag, from the account session.

Public Member Functions

 SessionSource (Session &session, std::vector< InstrumentId > universe)
 Constructs a source that reads everything from one session.
 SessionSource (Session &session, MarketData &market_data, std::vector< InstrumentId > universe)
 Constructs a source that reads market data from a second session.
void set_journal (Journal &journal)
 Sets the journal each order listing is checked against.
void set_match_window (runner::MatchWindow window)
 Sets the tolerance used to match listed orders to journal entries without a broker reference.
const runner::ReconcileReport & last_reconcile () const noexcept
 Returns what the last check against the journal resolved and left unresolved.
void set_buying_power_currencies (std::vector< Currency > currencies)
 Sets the currencies a buying-power poll asks about (one request per currency).
const std::vector< Currency > & buying_power_currencies () const noexcept
 Returns the buying-power currencies; with RiskLimits::enforce_buying_power_ on, the loop holds placements until each has a usable reading.
std::size_t bars_not_yet_closed () const noexcept
 Returns how many bars were left for a later poll because their period had not ended.
std::size_t bars_fed_as_warmup () const noexcept
 Returns how many bars were fed as history (Runner::feed_warmup), so their decisions were discarded.
std::size_t bars_not_delivered () const noexcept
 Returns how many bars a strategy declaring on_bars was not shown.
void set_bar_query (BarInterval interval, int count)
 Sets the bars each poll requests.
void set_calendar_country (Country country)
 Sets the market whose trading calendar the calendar poll asks for (Country::KR by default).
Country calendar_country () const noexcept
 Returns the market the calendar poll asks about.
const std::optional< BrokerError > & last_error () const noexcept
 Returns the error recorded by the most recent failing read.
template<typename Runner>
runner::PollOutcome poll (runner::PollKind kind, Runner &runner_instance)
 Polls one kind of data and feeds or installs what arrived in the runner.
template<typename Runner>
runner::PollOutcome read_order_list (Runner &runner_instance)
 Reads the broker's order list once, which lifts PollingLoop's startup hold.
const runner::ChargedCosts & charged_costs () const noexcept
 Returns the commission and tax the broker stated on the orders this source has seen.

Static Public Attributes

static constexpr bool provides_working_orders = Caps::has_working_orders
 true if an order-list read installs the broker's working orders in the portfolio.

Detailed Description

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
class algotradelib::live::SessionSource< Session, Journal, MarketData >

The source a runner::PollingLoop polls: reads a broker session and feeds bars, quotes, holdings, orders, buying power, price bands and the calendar to a runner.

Bars: each poll reads every instrument, then feeds the bars not fed before in period-end order across instruments. A bar whose period has not ended by the later of Runner::observed_at() and the series' receipt time is left for a later poll (a daily bar read during the session waits); this check applies to close-stamped bars and to daily and minute bars, and only once either instant is known (open-stamped weekly, monthly and yearly bars are fed unchecked). Of the bars collected, only each instrument's newest (which may be yesterday's, if today's is still forming) is fed live, and only if it ended strictly after every older bar collected in the same poll, of any instrument (a tie is history; instruments' newest bars are not compared with each other). The rest go through Runner::feed_warmup, so a restart does not act on the history it reads. This does not make the newest bar recent.

For a strategy declaring on_bars, the same bars are recorded in the same order and each period's bars are delivered together (Runner::feed or feed_warmup of a strategy::BarSlice, one per interval) before a later period's. A bar arriving after its period was delivered is cached but not delivered; see bars_not_delivered. An exception during such a poll (a bar the cache refuses, a handler that throws) stops the source: its period may be partly recorded and partly delivered, so every later bar poll throws std::logic_error. Restart the program with a fresh runner and source: older periods warm up the strategy, and the newest eligible period may trade.

Requests are tagged by kind, so a client budget with a trading reserve (network::RateLimitBudget::trading_reserve_) keeps capacity for orders. A kind the broker does not offer compiles to no request and returns UNCHANGED. A failed poll leaves the previous calendar, buying power and price bands in place.

With a journal (set_journal), every order listing is also checked against the instructions the journal holds as unresolved, resolving them in the runner's ledger where the broker's rows settle them.

Market data can come from a second session (MarketData): bars, quotes, price bands and the calendar are read from it, everything about the account from Session. For example, a KIS demo account cannot read the calendar, so it is read through a session built from a real account's keys and an empty KisAccount (which refuses every account request and order before any I/O).

Template Parameters
SessionThe broker session for the account (not a market-data-only source).
JournalThe journal type, if one is set.
MarketDataThe session market data is read from (Session by default).

Definition at line 160 of file session_source.hpp.

Member Typedef Documentation

◆ BrokerTag

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
using algotradelib::live::SessionSource< Session, Journal, MarketData >::BrokerTag = typename Session::BrokerTag

The broker tag, from the account session.

Definition at line 183 of file session_source.hpp.

◆ Caps

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
using algotradelib::live::SessionSource< Session, Journal, MarketData >::Caps = typename Session::Caps

The broker's capabilities, from the account session.

Definition at line 181 of file session_source.hpp.

Constructor & Destructor Documentation

◆ SessionSource() [1/2]

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
algotradelib::live::SessionSource< Session, Journal, MarketData >::SessionSource ( Session & session,
std::vector< InstrumentId > universe )
inline

Constructs a source that reads everything from one session.

Parameters
sessionThe session; borrowed, and must outlive the source.
universeThe instruments to poll.

Definition at line 207 of file session_source.hpp.

◆ SessionSource() [2/2]

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
algotradelib::live::SessionSource< Session, Journal, MarketData >::SessionSource ( Session & session,
MarketData & market_data,
std::vector< InstrumentId > universe )
inline

Constructs a source that reads market data from a second session.

Parameters
sessionThe account session; borrowed, and must outlive the source.
market_dataThe session bars, quotes, price bands and the calendar are read from (as its own capabilities allow); borrowed. No order or cancellation is sent through either session.
universeThe instruments to poll.

Definition at line 217 of file session_source.hpp.

Member Function Documentation

◆ bars_fed_as_warmup()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
std::size_t algotradelib::live::SessionSource< Session, Journal, MarketData >::bars_fed_as_warmup ( ) const
inlinenoexcept

Returns how many bars were fed as history (Runner::feed_warmup), so their decisions were discarded.

Returns
The count over the source's life.

Definition at line 276 of file session_source.hpp.

◆ bars_not_delivered()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
std::size_t algotradelib::live::SessionSource< Session, Journal, MarketData >::bars_not_delivered ( ) const
inlinenoexcept

Returns how many bars a strategy declaring on_bars was not shown.

A bar whose period was already delivered (a name that missed its period's poll and reported in a later one) is cached for its own series, without moving the last price or the clock, and not delivered: a second slice for that period, or an older one after a newer, would decide twice or backwards. A second bar of one instrument and interval for one period is skipped. Zero for a strategy with on_bar.

Returns
The count over the source's life (each bar counted once).

Definition at line 289 of file session_source.hpp.

◆ bars_not_yet_closed()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
std::size_t algotradelib::live::SessionSource< Session, Journal, MarketData >::bars_not_yet_closed ( ) const
inlinenoexcept

Returns how many bars were left for a later poll because their period had not ended.

Nonzero is normal for daily bars polled during the session.

Returns
The count over the source's life (each bar counted once).

Definition at line 269 of file session_source.hpp.

◆ buying_power_currencies()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
const std::vector< Currency > & algotradelib::live::SessionSource< Session, Journal, MarketData >::buying_power_currencies ( ) const
inlinenoexcept

Returns the buying-power currencies; with RiskLimits::enforce_buying_power_ on, the loop holds placements until each has a usable reading.

Returns
The currencies.

Definition at line 260 of file session_source.hpp.

◆ calendar_country()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
Country algotradelib::live::SessionSource< Session, Journal, MarketData >::calendar_country ( ) const
inlinenoexcept

Returns the market the calendar poll asks about.

Returns
The market.

Definition at line 315 of file session_source.hpp.

◆ charged_costs()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
const runner::ChargedCosts & algotradelib::live::SessionSource< Session, Journal, MarketData >::charged_costs ( ) const
inlinenoexcept

Returns the commission and tax the broker stated on the orders this source has seen.

A subtotal: a listing cannot show an order that filled between two polls (see runner::ChargedCosts). It accumulates over the source's life; PollingLoop::run reports each run's difference.

Returns
The costs.

Definition at line 443 of file session_source.hpp.

◆ last_error()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
const std::optional< BrokerError > & algotradelib::live::SessionSource< Session, Journal, MarketData >::last_error ( ) const
inlinenoexcept

Returns the error recorded by the most recent failing read.

A diagnostic, not the outcome of the last poll: a poll that does nothing (a kind the broker does not offer, or nothing configured) leaves it as it was, and a working-order poll can return UPDATED while recording a failed journal-reconciliation history read here.

Returns
The error, or empty if cleared by a later successful read.

Definition at line 326 of file session_source.hpp.

◆ last_reconcile()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
const runner::ReconcileReport & algotradelib::live::SessionSource< Session, Journal, MarketData >::last_reconcile ( ) const
inlinenoexcept

Returns what the last check against the journal resolved and left unresolved.

Returns
The report.

Definition at line 242 of file session_source.hpp.

◆ poll()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
template<typename Runner>
runner::PollOutcome algotradelib::live::SessionSource< Session, Journal, MarketData >::poll ( runner::PollKind kind,
Runner & runner_instance )
inline

Polls one kind of data and feeds or installs what arrived in the runner.

A poll is not atomic. Buying power and price bands ask for every currency or instrument even after one fails: the answers that arrived are installed, and the poll returns the failure (a failure outranks a rate-limit refusal). A quote poll stops at the first failure, with the quotes before it already fed. A bar poll that fails feeds nothing. A working-order poll installs the listing, reconciles, then feeds order updates, before it reads any history the runner needs; if that read fails it returns the failure with those effects kept. UPDATED does not mean journal reconciliation succeeded (see last_reconcile() and last_error()).

Template Parameters
RunnerThe runner type.
Parameters
kindThe kind to poll.
runner_instanceThe runner.
Returns
UPDATED if a read succeeded and fed or installed something (holdings and working orders: every successful read), UNCHANGED if nothing arrived (or the broker does not offer the kind), FAILED or RATE_LIMITED on an error (see last_error()).

Definition at line 347 of file session_source.hpp.

◆ read_order_list()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
template<typename Runner>
runner::PollOutcome algotradelib::live::SessionSource< Session, Journal, MarketData >::read_order_list ( Runner & runner_instance)
inline

Reads the broker's order list once, which lifts PollingLoop's startup hold.

Reads the working orders where the broker offers that inquiry; otherwise the whole order history (all pages, or the read fails). A broker with neither does not compile. The listing is adopted as the starting state: orders already resting produce no order updates, and one that fills or is cancelled before the first working-order poll produces none either.

Template Parameters
RunnerThe runner type.
Parameters
runner_instanceThe runner.
Returns
UPDATED on success, otherwise FAILED or RATE_LIMITED.

Definition at line 380 of file session_source.hpp.

◆ set_bar_query()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
void algotradelib::live::SessionSource< Session, Journal, MarketData >::set_bar_query ( BarInterval interval,
int count )
inline

Sets the bars each poll requests.

Parameters
intervalThe bar interval.
countHow many bars to request per instrument.

Definition at line 296 of file session_source.hpp.

◆ set_buying_power_currencies()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
void algotradelib::live::SessionSource< Session, Journal, MarketData >::set_buying_power_currencies ( std::vector< Currency > currencies)
inline

Sets the currencies a buying-power poll asks about (one request per currency).

Empty by default: a buying-power poll then does nothing.

Parameters
currenciesThe currencies.

Definition at line 251 of file session_source.hpp.

◆ set_calendar_country()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
void algotradelib::live::SessionSource< Session, Journal, MarketData >::set_calendar_country ( Country country)
inline

Sets the market whose trading calendar the calendar poll asks for (Country::KR by default).

A broker that does not publish that market's calendar refuses the request before sending it.

Parameters
countryThe market.

Definition at line 309 of file session_source.hpp.

◆ set_journal()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
void algotradelib::live::SessionSource< Session, Journal, MarketData >::set_journal ( Journal & journal)
inline

Sets the journal each order listing is checked against.

Use the journal the runner writes to (bind_journaled). Listings arrive with the startup order-list read and every working-order poll; a broker without a working-order inquiry is checked only at startup.

Parameters
journalThe journal; borrowed, and must outlive the source.

Definition at line 229 of file session_source.hpp.

◆ set_match_window()

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
void algotradelib::live::SessionSource< Session, Journal, MarketData >::set_match_window ( runner::MatchWindow window)
inline

Sets the tolerance used to match listed orders to journal entries without a broker reference.

Parameters
windowThe window.

Definition at line 236 of file session_source.hpp.

Member Data Documentation

◆ provides_working_orders

template<typename Session, typename Journal = runner::MemoryJournal, typename MarketData = Session>
bool algotradelib::live::SessionSource< Session, Journal, MarketData >::provides_working_orders = Caps::has_working_orders
staticconstexpr

true if an order-list read installs the broker's working orders in the portfolio.

false at a broker without a working-order inquiry: the list is read from history, which lifts the loop's startup gate but lists nothing to cancel, so a closing pass cannot know what rests.

Definition at line 200 of file session_source.hpp.


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