algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::live Namespace Reference

Connects a runner to a broker session for live trading. More...

Classes

class  SessionExecutor
 The executor that sends a runner's orders and cancellations through a broker session. More...
class  SessionSource
 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...

Typedefs

template<typename Strategy, typename Session>
using BoundRunner
 The runner type bind returns.
template<typename Strategy, typename Session>
using ObservingRunner
 The runner type bind_observer returns: the same capabilities and tag, sending nowhere.
template<typename Strategy, typename Session, typename Journal, typename Clock = runner::SteadyClock>
using JournaledRunner
 The runner type bind_journaled returns: the same pipeline, journaling before it sends.

Functions

template<typename Strategy, typename Session>
BoundRunner< Strategy, Session > bind (Session &session, Strategy strategy, strategy::RiskLimits limits={}, std::size_t series_capacity=default_series_capacity, Venue default_route=Venue::UNSPECIFIED)
 Binds a strategy to a broker session, so its instructions are sent to the broker.
template<typename Strategy, typename Session, typename Journal, typename Clock = runner::SteadyClock>
JournaledRunner< Strategy, Session, Journal, Clock > bind_journaled (Session &session, Journal &journal, Strategy strategy, strategy::RiskLimits limits={}, std::size_t series_capacity=default_series_capacity, Venue default_route=Venue::UNSPECIFIED, Clock clock={})
 Binds a strategy to a broker session, recording every instruction in a journal before it is sent.
template<typename Strategy, typename Session>
ObservingRunner< Strategy, Session > bind_observer (Session &session, Strategy strategy, strategy::RiskLimits limits={}, std::size_t series_capacity=default_series_capacity)
 Binds a strategy for observation: the session's capabilities and broker tag, and nothing to send through.
runner::PollOutcome poll_outcome_of (const BrokerError &error) noexcept
 Classifies a failed inquiry for a poll.
BrokerError incomplete_history (std::string code, std::string message)
 Makes the error an order-history read reports when the history is incomplete (a page said there is more, and it was not read).
BrokerError history_not_continued ()
 Makes the incomplete-history error for a broker whose adapter cannot read a further page (has_history_cursor is false).
BrokerError history_page_limit (int pages)
 Makes the incomplete-history error for a history longer than the page limit.

Variables

constexpr std::size_t default_series_capacity = 512
 The number of trailing bars per instrument a bound runner keeps by default.

Detailed Description

Connects a runner to a broker session for live trading.

Typedef Documentation

◆ BoundRunner

template<typename Strategy, typename Session>
using algotradelib::live::BoundRunner
Initial value:
Strategy,
typename Session::Caps,
typename Session::BrokerTag,
>
The executor that sends a runner's orders and cancellations through a broker session.
Runs one strategy: feeds it events, reconciles what it asks for, checks the risk limits,...
Definition runner.hpp:297

The runner type bind returns.

Template Parameters
StrategyThe strategy type.
SessionThe broker session type.

Definition at line 32 of file bind.hpp.

◆ JournaledRunner

template<typename Strategy, typename Session, typename Journal, typename Clock = runner::SteadyClock>
using algotradelib::live::JournaledRunner
Initial value:
Strategy,
typename Session::Caps,
typename Session::BrokerTag,
>
An executor wrapper that records each instruction in a journal before sending it.
Definition journal.hpp:695

The runner type bind_journaled returns: the same pipeline, journaling before it sends.

Template Parameters
StrategyThe strategy type.
SessionThe broker session type.
JournalThe journal type (such as runner::FileJournal).
ClockThe clock journal lines are stamped with.

Definition at line 100 of file bind.hpp.

◆ ObservingRunner

template<typename Strategy, typename Session>
using algotradelib::live::ObservingRunner
Initial value:
Strategy,
typename Session::Caps,
typename Session::BrokerTag,
>
An executor that sends nothing and records what it was given (the dry run's default).

The runner type bind_observer returns: the same capabilities and tag, sending nowhere.

Template Parameters
StrategyThe strategy type.
SessionThe broker session type.

Definition at line 45 of file bind.hpp.

Function Documentation

◆ bind()

template<typename Strategy, typename Session>
BoundRunner< Strategy, Session > algotradelib::live::bind ( Session & session,
Strategy strategy,
strategy::RiskLimits limits = {},
std::size_t series_capacity = default_series_capacity,
Venue default_route = Venue::UNSPECIFIED )

Binds a strategy to a broker session, so its instructions are sent to the broker.

This can place orders. bind_observer is the same pipeline with nowhere to send. The runner requires holdings to have been read before it places (see Runner::allow_absent_portfolio for a run that reads none).

Template Parameters
StrategyThe strategy type.
SessionThe broker session type.
Parameters
sessionThe session; borrowed, and must outlive the runner.
strategyThe strategy.
limitsThe risk limits.
series_capacityHow many trailing bars each cached series keeps.
default_routeThe venue for an order that names none, which is every order a strategy plans. UNSPECIFIED takes the broker's own default; Kiwoom has none, so a Kiwoom runner places nothing until a route is given here. See SessionExecutor::default_route.
Returns
The runner.

Definition at line 71 of file bind.hpp.

Referenced by bind().

◆ bind_journaled()

template<typename Strategy, typename Session, typename Journal, typename Clock = runner::SteadyClock>
JournaledRunner< Strategy, Session, Journal, Clock > algotradelib::live::bind_journaled ( Session & session,
Journal & journal,
Strategy strategy,
strategy::RiskLimits limits = {},
std::size_t series_capacity = default_series_capacity,
Venue default_route = Venue::UNSPECIFIED,
Clock clock = {} )

Binds a strategy to a broker session, recording every instruction in a journal before it is sent.

Use this rather than building a runner::JournalingExecutor yourself: bind makes its own executor, so a journaling executor built beside it would never be used, and nothing would report it.

The runner is serialized (runner::Runner::serialized): one order per instrument until holdings account for it. Call runner::recover_runner with the journal before trading, on every start: it restores what a previous process left owed, and the runner places nothing until it has run. To keep several orders on one instrument outstanding instead, call allow_overlapping_orders first.

Template Parameters
StrategyThe strategy type.
SessionThe broker session type.
JournalThe journal type.
ClockThe clock journal lines are stamped with.
Parameters
sessionThe session; borrowed, and must outlive the runner.
journalThe journal; borrowed, and must outlive the runner.
strategyThe strategy.
limitsThe risk limits.
series_capacityHow many trailing bars each cached series keeps.
default_routeAs for bind; the journal records the route the order is sent with (SessionExecutor::effective_route, such as KRX for an unspecified KIS order).
clockThe clock journal lines are stamped with, and recovery's history sweep later asks the broker about. Give the loop's clock when it is not the wall clock (a paper broker over stored history, a test): through a runner::ClockReference unless copies of it read the same time.
Returns
The runner.

Definition at line 137 of file bind.hpp.

Referenced by bind_journaled().

◆ bind_observer()

template<typename Strategy, typename Session>
ObservingRunner< Strategy, Session > algotradelib::live::bind_observer ( Session & session,
Strategy strategy,
strategy::RiskLimits limits = {},
std::size_t series_capacity = default_series_capacity )

Binds a strategy for observation: the session's capabilities and broker tag, and nothing to send through.

A capability-gated strategy compiles the same branch as it would live. Nothing can be sent because the runner has no executor that reaches a broker; its reports list what would have been sent.

Template Parameters
StrategyThe strategy type.
SessionThe broker session type.
Parameters
sessionThe session; only its type is used.
strategyThe strategy.
limitsThe risk limits.
series_capacityHow many trailing bars each cached series keeps.
Returns
The runner.

Definition at line 175 of file bind.hpp.

Referenced by bind_observer().

◆ history_not_continued()

BrokerError algotradelib::live::history_not_continued ( )
inline

Makes the incomplete-history error for a broker whose adapter cannot read a further page (has_history_cursor is false).

Returns
The error (code history_not_continued).

Definition at line 65 of file session_source.hpp.

Referenced by history_not_continued(), and algotradelib::live::SessionSource< Session, Journal, MarketData >::read_order_list().

◆ history_page_limit()

BrokerError algotradelib::live::history_page_limit ( int pages)
inline

Makes the incomplete-history error for a history longer than the page limit.

Parameters
pagesThe page limit.
Returns
The error (code history_page_limit).

Definition at line 78 of file session_source.hpp.

Referenced by history_page_limit(), and algotradelib::live::SessionSource< Session, Journal, MarketData >::read_order_list().

◆ incomplete_history()

BrokerError algotradelib::live::incomplete_history ( std::string code,
std::string message )
inline

Makes the error an order-history read reports when the history is incomplete (a page said there is more, and it was not read).

Its category is PROTOCOL, with reached_broker_ set. Callers treat it as a failed read and conclude nothing from the pages that were read.

Parameters
codeThe error code.
messageThe message.
Returns
The error.

Definition at line 51 of file session_source.hpp.

Referenced by history_not_continued(), history_page_limit(), and incomplete_history().

◆ poll_outcome_of()

runner::PollOutcome algotradelib::live::poll_outcome_of ( const BrokerError & error)
inlinenoexcept

Classifies a failed inquiry for a poll.

A rate-limit refusal is not a failure: it does not count toward runner::FailurePolicy's budget. No order instruction was sent, so no failed read needs reconciling.

Parameters
errorThe error.
Returns
RATE_LIMITED for ErrorCategory::RATE_LIMIT, otherwise FAILED.

Definition at line 34 of file session_source.hpp.

Referenced by poll_outcome_of(), and algotradelib::live::SessionSource< Session, Journal, MarketData >::read_order_list().

Variable Documentation

◆ default_series_capacity

std::size_t algotradelib::live::default_series_capacity = 512
inlineconstexpr

The number of trailing bars per instrument a bound runner keeps by default.

Definition at line 24 of file bind.hpp.