![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
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. | |
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).
| Session | The broker session for the account (not a market-data-only source). |
| Journal | The journal type, if one is set. |
| MarketData | The session market data is read from (Session by default). |
Definition at line 160 of file session_source.hpp.
| 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.
| 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.
|
inline |
Constructs a source that reads everything from one session.
| session | The session; borrowed, and must outlive the source. |
| universe | The instruments to poll. |
Definition at line 207 of file session_source.hpp.
|
inline |
Constructs a source that reads market data from a second session.
| session | The account session; borrowed, and must outlive the source. |
| market_data | The 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. |
| universe | The instruments to poll. |
Definition at line 217 of file session_source.hpp.
|
inlinenoexcept |
Returns how many bars were fed as history (Runner::feed_warmup), so their decisions were discarded.
Definition at line 276 of file session_source.hpp.
|
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.
Definition at line 289 of file session_source.hpp.
|
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.
Definition at line 269 of file session_source.hpp.
|
inlinenoexcept |
Returns the buying-power currencies; with RiskLimits::enforce_buying_power_ on, the loop holds placements until each has a usable reading.
Definition at line 260 of file session_source.hpp.
|
inlinenoexcept |
Returns the market the calendar poll asks about.
Definition at line 315 of file session_source.hpp.
|
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.
Definition at line 443 of file session_source.hpp.
|
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.
Definition at line 326 of file session_source.hpp.
|
inlinenoexcept |
Returns what the last check against the journal resolved and left unresolved.
Definition at line 242 of file session_source.hpp.
|
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()).
| Runner | The runner type. |
| kind | The kind to poll. |
| runner_instance | The runner. |
Definition at line 347 of file session_source.hpp.
|
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.
| Runner | The runner type. |
| runner_instance | The runner. |
Definition at line 380 of file session_source.hpp.
|
inline |
Sets the bars each poll requests.
| interval | The bar interval. |
| count | How many bars to request per instrument. |
Definition at line 296 of file session_source.hpp.
|
inline |
Sets the currencies a buying-power poll asks about (one request per currency).
Empty by default: a buying-power poll then does nothing.
| currencies | The currencies. |
Definition at line 251 of file session_source.hpp.
|
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.
| country | The market. |
Definition at line 309 of file session_source.hpp.
|
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.
| journal | The journal; borrowed, and must outlive the source. |
Definition at line 229 of file session_source.hpp.
|
inline |
Sets the tolerance used to match listed orders to journal entries without a broker reference.
| window | The window. |
Definition at line 236 of file session_source.hpp.
|
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.