algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::alpaca::AlpacaConfig Struct Reference

Alpaca API keys (key id and secret key), paper or live account. More...

#include <algotradelib/src/brokers/alpaca/config.hpp>

Inheritance diagram for algotradelib::alpaca::AlpacaConfig:
Collaboration diagram for algotradelib::alpaca::AlpacaConfig:

Public Member Functions

void from_env_impl ()
 Reads the keys from ALPACA_KEY and ALPACA_SECRET, and the environment from ALPACA_DEMO (see demo_from_env); called by from_env(). Nothing changes if it throws.
void from_json_impl (const std::string &filename)
 Reads the keys from a JSON file; called by from_json(filename). Nothing changes if it throws.
void from_json_text_impl (std::string_view text, const std::string &origin)
 Reads the keys from JSON text in the file's format; called by from_json_text. Nothing changes if it throws.
const char * trading_host () const noexcept
 The trading host for the selected environment.
const char * data_host () const noexcept
 The market-data host, the same for both environments.
const char * port () const noexcept
 The HTTPS port, the same for every host.
Public Member Functions inherited from algotradelib::network::ClientCredentials< AlpacaConfig >
void from_env ()
 Loads the credentials from the broker's environment variables.
void from_json (const std::string &filename)
 Loads the credentials from a JSON file, in the broker's format.
void from_json_text (std::string_view text, const std::string &origin)
 Loads the credentials from JSON text in the broker's format, as from_json reads a file's contents, for credentials kept somewhere other than a file (a system credential store).
const std::string & key () const noexcept
 Returns the app key (client id).
const std::string & secret () const noexcept
 Returns the app secret.
std::string to_jsonbody () const
 Builds a JSON token-request body (grant type, key and secret under the broker's field names).
std::string to_form_urlencoded_body () const
 Builds a form-encoded token-request body (grant_type, client_id, client_secret).

Static Public Member Functions

static bool demo_from_env ()
 Reads ALPACA_DEMO.
static bool demo_from_json (const json::object &doc, const std::string &origin)
 Reads a credential document's "demo".
static constexpr std::string_view key_field () noexcept
 The credential file's field for the key id; Alpaca has no token request behind it.
static constexpr std::string_view secret_field () noexcept
 The credential file's field for the secret key; Alpaca has no token request behind it.

Public Attributes

bool is_demo_ = true
 Whether to use Alpaca's paper trading host; true unless stated otherwise.

Additional Inherited Members

Static Public Attributes inherited from algotradelib::network::ClientCredentials< AlpacaConfig >
static constexpr std::string_view grant_type
 The OAuth grant type sent in a token request.
Protected Attributes inherited from algotradelib::network::ClientCredentials< AlpacaConfig >
std::string key_
 The app key (client id).
std::string secret_
 The app secret.

Detailed Description

Alpaca API keys (key id and secret key), paper or live account.

Load with from_env() (ALPACA_KEY, ALPACA_SECRET, ALPACA_DEMO), from_json(filename) or from_json_text(text, origin). Alpaca has no token: both keys go on every request as headers, so nothing is requested or stored before a call. Paper and live keys differ, and a key sent to the other environment's host is refused (HTTP 401) at its first request, so a forgotten setting fails rather than trades.

Definition at line 30 of file config.hpp.

Member Function Documentation

◆ data_host()

const char * algotradelib::alpaca::AlpacaConfig::data_host ( ) const
inlinenoexcept

The market-data host, the same for both environments.

Returns
"data.alpaca.markets".

Definition at line 172 of file config.hpp.

◆ demo_from_env()

bool algotradelib::alpaca::AlpacaConfig::demo_from_env ( )
inlinestatic

Reads ALPACA_DEMO.

Returns
true (paper) if unset or 1, false (live) if 0.
Exceptions
std::runtime_errorfor any other value.

Definition at line 108 of file config.hpp.

Referenced by from_env_impl().

◆ demo_from_json()

bool algotradelib::alpaca::AlpacaConfig::demo_from_json ( const json::object & doc,
const std::string & origin )
inlinestatic

Reads a credential document's "demo".

Parameters
docThe document.
originWhat the error message calls the source.
Returns
true if absent, otherwise its value.
Exceptions
std::runtime_errorif it is not a boolean.

Definition at line 129 of file config.hpp.

Referenced by from_json_text_impl().

◆ from_env_impl()

void algotradelib::alpaca::AlpacaConfig::from_env_impl ( )
inline

Reads the keys from ALPACA_KEY and ALPACA_SECRET, and the environment from ALPACA_DEMO (see demo_from_env); called by from_env(). Nothing changes if it throws.

Exceptions
std::runtime_errorif a key variable is missing or ALPACA_DEMO is invalid.

Definition at line 45 of file config.hpp.

◆ from_json_impl()

void algotradelib::alpaca::AlpacaConfig::from_json_impl ( const std::string & filename)
inline

Reads the keys from a JSON file; called by from_json(filename). Nothing changes if it throws.

Format: { "key_id": "...", "secret_key": "...", "demo": false }; without "demo" the config is paper.

Parameters
filenameThe file.
Exceptions
std::runtime_errorif the file cannot be read or parsed, a key is missing or not a string, or "demo" is not a boolean.

Definition at line 67 of file config.hpp.

◆ from_json_text_impl()

void algotradelib::alpaca::AlpacaConfig::from_json_text_impl ( std::string_view text,
const std::string & origin )
inline

Reads the keys from JSON text in the file's format; called by from_json_text. Nothing changes if it throws.

Parameters
textThe JSON document.
originWhat error messages call the source ("file creds.json"); not a secret.
Exceptions
std::runtime_errorif the text cannot be parsed, a key is missing or not a string, or "demo" is not a boolean.

Definition at line 84 of file config.hpp.

Referenced by from_json_impl().

◆ key_field()

constexpr std::string_view algotradelib::alpaca::AlpacaConfig::key_field ( )
inlinestaticconstexprnoexcept

The credential file's field for the key id; Alpaca has no token request behind it.

Returns
"key_id".

Definition at line 148 of file config.hpp.

◆ port()

const char * algotradelib::alpaca::AlpacaConfig::port ( ) const
inlinenoexcept

The HTTPS port, the same for every host.

Returns
"443".

Definition at line 180 of file config.hpp.

◆ secret_field()

constexpr std::string_view algotradelib::alpaca::AlpacaConfig::secret_field ( )
inlinestaticconstexprnoexcept

The credential file's field for the secret key; Alpaca has no token request behind it.

Returns
"secret_key".

Definition at line 156 of file config.hpp.

◆ trading_host()

const char * algotradelib::alpaca::AlpacaConfig::trading_host ( ) const
inlinenoexcept

The trading host for the selected environment.

Returns
"paper-api.alpaca.markets" (paper) or "api.alpaca.markets" (live).

Definition at line 164 of file config.hpp.

Member Data Documentation

◆ is_demo_

bool algotradelib::alpaca::AlpacaConfig::is_demo_ = true

Whether to use Alpaca's paper trading host; true unless stated otherwise.

Live trading must be stated: ALPACA_DEMO=0, "demo": false in the credential file, or is_demo_ = false. Live is the same code path as paper and differs only in the trading host; it is supported, but the library's own checks ran on paper only.

Definition at line 38 of file config.hpp.

Referenced by from_env_impl(), from_json_text_impl(), and trading_host().


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