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

HTTP and WebSocket transport, OAuth tokens, and request pacing. More...

Classes

class  AsyncHttpClient
 An asynchronous HTTP/1.1 client over TLS that keeps one connection to one host, used by the broker clients. More...
struct  AuthBody
 An OAuth access token and its expiry, the base of each broker's token-response class. More...
class  BasicRateLimitBucket
 A rate limiter (the generic cell rate algorithm) that computes when a request may go; it neither waits nor reads the clock itself. More...
class  CalendarCache
 Where blocking broker clients share a market's trading calendar, across threads and processes. More...
class  ClientCredentials
 OAuth client credentials (an app key and secret), the base of each broker's credentials class. More...
struct  ClientStores
 What the clients of a run share, across processes too; each takes those its constructor accepts. More...
class  FileCalendarCache
 A CalendarCache in a private folder: one file per name. POSIX only; not provided on Windows. More...
class  FileTokenStore
 A TokenStore in a private folder: one token file and one lock file per identity. POSIX only; not provided on Windows. More...
class  HttpClient
 A blocking HTTP/1.1 client over TLS that keeps one connection to one host. More...
struct  HttpClientOptions
 Deadlines, size limits and pacing for an HTTP client. More...
struct  HttpRequest
 An HTTP request for AsyncHttpClient. More...
struct  RateLimitBudget
 The request rate a connection may use. More...
class  RateLimitController
 The rate limiter of one connection (one app key): a RateLimitBucket on the steady clock, plus counters. More...
struct  RateLimitGroupStats
 A rate limiter's counters for one traffic class. More...
struct  RateLimitStats
 A rate limiter's counters per traffic class, its current budget, and the broker's refusals (from RateLimitController::stats). More...
struct  StoredToken
 An access token as a TokenStore keeps it. More...
class  TokenStore
 Where blocking broker clients on one key share its access token, across threads and processes. More...
class  WebSocketSessionBase
 The base of the WebSocket clients WsSession and WssSession: connecting, sending, receiving, closing and reconnecting. More...
struct  WebSocketSessionOptions
 Options for a WebSocket session (WsSession, WssSession). More...
class  WsSession
 A WebSocket client over plain TCP (no TLS); derive from it and override on_message. More...
class  WssSession
 A WebSocket client over TLS; derive from it and override on_message. More...

Typedefs

using HttpResponse = http::response<http::string_body>
 The HTTP response type the transport clients return.
using RateLimitBucket = BasicRateLimitBucket<>
 A rate-limit bucket on the steady clock.
using WssSessionOptions = WebSocketSessionOptions
 Options for a WssSession.

Enumerations

enum class  RateLimitGroup {
  TRADING , AUTH , ACCOUNT , MARKET_DATA ,
  REFERENCE
}
 The traffic class of a request, for rate limiting and queue priority. The broker adapter that builds a request sets it. More...

Functions

BrokerError calendar_cache_error (std::string_view reason)
 Returns a calendar cache's error: TRANSPORT, not sent to the broker, code calendar_cache.
template<typename Client, typename Config>
std::shared_ptr< Client > make_client (ssl::context &context, Config config, HttpClientOptions options, const ClientStores &stores)
 Constructs a blocking client with the run's stores; sends nothing.
BrokerError transport_error (std::string stage, beast::error_code ec)
 Converts a transport error into a BrokerError.
std::string make_host_header (const std::string &host, const std::string &port)
 Builds an HTTP Host header value.
void shutdown_ssl_stream (beast::ssl_stream< beast::tcp_stream > &stream, std::chrono::seconds timeout) noexcept
 Shuts a TLS stream down (best effort) and closes its socket.
void close_ssl_stream (beast::ssl_stream< beast::tcp_stream > &stream) noexcept
 Cancels a TLS stream's operations and closes its socket, without waiting for the TLS shutdown.
constexpr int rate_limit_priority (RateLimitGroup group) noexcept
 Returns a traffic class's queue priority.
constexpr std::string_view rate_limit_group_name (RateLimitGroup group) noexcept
 Returns a lowercase name for a traffic class, such as "market_data", for logs.
constexpr std::size_t rate_limit_group_index (RateLimitGroup group) noexcept
 Returns a traffic class's index, for per-class arrays.
constexpr int usable_burst (const RateLimitBudget &budget, RateLimitGroup group) noexcept
 Returns how much of the burst a traffic class may use.
BrokerError token_store_error (std::string_view reason)
 Returns a store's error: AUTHENTICATION, not sent to the broker, code token_store.
BrokerError token_unusable_error ()
 Returns the error of a call whose token stopped being usable while it ran on a client with a store: AUTHENTICATION, not sent to the broker, code token_unusable. The next call prepares a token.
std::string token_identity (std::string_view broker, std::string_view environment, std::string_view key)
 Names what a token may not outlive: the broker, the environment and the app key.

Variables

constexpr std::array< RateLimitGroup, 5 > all_rate_limit_groups
 Every RateLimitGroup, in declaration order.
constexpr std::size_t rate_limit_group_count = all_rate_limit_groups.size()
 The number of traffic classes.

Detailed Description

HTTP and WebSocket transport, OAuth tokens, and request pacing.

Typedef Documentation

◆ HttpResponse

using algotradelib::network::HttpResponse = http::response<http::string_body>

The HTTP response type the transport clients return.

Definition at line 48 of file http_client.hpp.

◆ RateLimitBucket

A rate-limit bucket on the steady clock.

Definition at line 357 of file rate_limit.hpp.

◆ WssSessionOptions

Enumeration Type Documentation

◆ RateLimitGroup

The traffic class of a request, for rate limiting and queue priority. The broker adapter that builds a request sets it.

Enumerator
TRADING 

Order placement, amendment and cancellation. HttpClientOptions::fresh_connection_for_orders_ applies to these requests.

AUTH 

Access-token issue and refresh.

ACCOUNT 

Holdings, working orders, order history and buying power.

MARKET_DATA 

Quotes, bars, order book and trades.

REFERENCE 

Instrument reference data and trading calendars.

Definition at line 26 of file rate_limit.hpp.

Function Documentation

◆ calendar_cache_error()

BrokerError algotradelib::network::calendar_cache_error ( std::string_view reason)
inline

Returns a calendar cache's error: TRANSPORT, not sent to the broker, code calendar_cache.

Parameters
reasonFixed text.
Returns
The error.

Definition at line 48 of file calendar_cache.hpp.

Referenced by calendar_cache_error(), algotradelib::network::FileCalendarCache::load(), and algotradelib::network::FileCalendarCache::publish().

◆ close_ssl_stream()

void algotradelib::network::close_ssl_stream ( beast::ssl_stream< beast::tcp_stream > & stream)
inlinenoexcept

Cancels a TLS stream's operations and closes its socket, without waiting for the TLS shutdown.

Parameters
streamThe stream.

Definition at line 171 of file http_client.hpp.

Referenced by close_ssl_stream().

◆ make_client()

template<typename Client, typename Config>
std::shared_ptr< Client > algotradelib::network::make_client ( ssl::context & context,
Config config,
HttpClientOptions options,
const ClientStores & stores )

Constructs a blocking client with the run's stores; sends nothing.

The client gets the token store always, and the calendar cache when its constructor takes one as a fifth argument. A client of another shape, or whose constructor is a variadic forwarding template (it counts as taking five arguments and then fails in its own body), is constructed by hand. The stores are passed as const lvalues, so a constructor taking std::shared_ptr<TokenStore>&& fails inside std::make_shared.

Template Parameters
ClientA client constructible as Client(ssl::context&, Config, HttpClientOptions, std::shared_ptr<TokenStore>), optionally with a trailing std::shared_ptr<CalendarCache>.
ConfigThe client's configuration type, BrokerTraits<Client>::Config.
Parameters
contextThe TLS context; borrowed, and must outlive the client.
configThe credentials and environment.
optionsHTTP options, pacing included; nothing is added here, and a client may choose its own pacing when none is set (the Alpaca client does).
storesThe run's stores.
Returns
The client.

Definition at line 41 of file client_stores.hpp.

Referenced by make_client().

◆ make_host_header()

std::string algotradelib::network::make_host_header ( const std::string & host,
const std::string & port )
inline

Builds an HTTP Host header value.

Parameters
hostThe host name.
portThe port.
Returns
host, with :port appended unless the port is 443.

Definition at line 145 of file http_client.hpp.

Referenced by make_host_header().

◆ rate_limit_group_index()

std::size_t algotradelib::network::rate_limit_group_index ( RateLimitGroup group)
inlineconstexprnoexcept

Returns a traffic class's index, for per-class arrays.

Parameters
groupThe class.
Returns
An index below rate_limit_group_count.

Definition at line 115 of file rate_limit.hpp.

Referenced by algotradelib::network::RateLimitStats::group(), algotradelib::network::RateLimitStats::group(), and rate_limit_group_index().

◆ rate_limit_group_name()

std::string_view algotradelib::network::rate_limit_group_name ( RateLimitGroup group)
inlineconstexprnoexcept

Returns a lowercase name for a traffic class, such as "market_data", for logs.

Parameters
groupThe class.
Returns
The name; empty for a value that is not a RateLimitGroup.

Definition at line 81 of file rate_limit.hpp.

Referenced by rate_limit_group_name().

◆ rate_limit_priority()

int algotradelib::network::rate_limit_priority ( RateLimitGroup group)
inlineconstexprnoexcept

Returns a traffic class's queue priority.

From highest: AUTH (every other request waits for a token), TRADING, ACCOUNT, MARKET_DATA, REFERENCE.

Parameters
groupThe class.
Returns
The priority; larger is served first.

Definition at line 60 of file rate_limit.hpp.

Referenced by rate_limit_priority().

◆ shutdown_ssl_stream()

void algotradelib::network::shutdown_ssl_stream ( beast::ssl_stream< beast::tcp_stream > & stream,
std::chrono::seconds timeout )
inlinenoexcept

Shuts a TLS stream down (best effort) and closes its socket.

The TLS shutdown is a blocking call that timeout does not bound, so it can wait for an unresponsive peer.

Parameters
streamThe stream.
timeoutSet as the stream's expiry, which does not bound the blocking shutdown.

Definition at line 158 of file http_client.hpp.

Referenced by shutdown_ssl_stream().

◆ token_identity()

std::string algotradelib::network::token_identity ( std::string_view broker,
std::string_view environment,
std::string_view key )
inline

Names what a token may not outlive: the broker, the environment and the app key.

Parameters
brokerThe broker, such as kis.
environmentThe environment, such as demo or real.
keyThe app key; only its SHA-256 digest appears.
Returns
<broker>-<environment>-<64 hexadecimal digits>, or empty if the digest fails (no store accepts it).

Definition at line 123 of file token_store.hpp.

Referenced by algotradelib::kis::KisConfig::token_identity(), algotradelib::kiwoom::KiwoomConfig::token_identity(), token_identity(), and algotradelib::tossinvest::TossConfig::token_identity().

◆ token_store_error()

BrokerError algotradelib::network::token_store_error ( std::string_view reason)
inline

Returns a store's error: AUTHENTICATION, not sent to the broker, code token_store.

Parameters
reasonFixed text; never a token, a file's contents or another error's message.
Returns
The error.

Definition at line 93 of file token_store.hpp.

Referenced by algotradelib::network::FileTokenStore::lock(), and token_store_error().

◆ token_unusable_error()

BrokerError algotradelib::network::token_unusable_error ( )
inline

Returns the error of a call whose token stopped being usable while it ran on a client with a store: AUTHENTICATION, not sent to the broker, code token_unusable. The next call prepares a token.

Returns
The error.

Definition at line 107 of file token_store.hpp.

Referenced by token_unusable_error().

◆ transport_error()

BrokerError algotradelib::network::transport_error ( std::string stage,
beast::error_code ec )
inline

Converts a transport error into a BrokerError.

Parameters
stageWhere it failed (such as "connect"), stored as the error code.
ecThe error.
Returns
A CANCELLED error for an aborted operation, otherwise TRANSPORT. reached_broker_ is left at its default; the caller sets it.

Definition at line 131 of file http_client.hpp.

Referenced by transport_error().

◆ usable_burst()

int algotradelib::network::usable_burst ( const RateLimitBudget & budget,
RateLimitGroup group )
inlineconstexprnoexcept

Returns how much of the burst a traffic class may use.

Parameters
budgetThe budget.
groupThe class.
Returns
burst_ for TRADING and AUTH; otherwise burst_ - trading_reserve_, at least 1.

Definition at line 146 of file rate_limit.hpp.

Referenced by algotradelib::network::RateLimitController::acquire(), algotradelib::network::RateLimitController::delay_until(), and usable_burst().

Variable Documentation

◆ all_rate_limit_groups

std::array<RateLimitGroup, 5> algotradelib::network::all_rate_limit_groups
inlineconstexpr
Initial value:
{
}
@ MARKET_DATA
Quotes, bars, order book and trades.
@ ACCOUNT
Holdings, working orders, order history and buying power.
@ TRADING
Order placement, amendment and cancellation. HttpClientOptions::fresh_connection_for_orders_ applies ...
@ REFERENCE
Instrument reference data and trading calendars.
@ AUTH
Access-token issue and refresh.

Every RateLimitGroup, in declaration order.

Definition at line 43 of file rate_limit.hpp.

◆ rate_limit_group_count

std::size_t algotradelib::network::rate_limit_group_count = all_rate_limit_groups.size()
inlineconstexpr

The number of traffic classes.

Definition at line 108 of file rate_limit.hpp.