algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
Loading...
Searching...
No Matches
config.hpp
1
#pragma once
2
3
#include "../../network/oauth.hpp"
4
5
#include <boost/json.hpp>
6
7
#include <cstdlib>
8
#include <fstream>
9
#include <iterator>
10
#include <stdexcept>
11
#include <string>
12
#include <string_view>
13
14
namespace
algotradelib
{
15
namespace
alpaca
{
16
17
// https://docs.alpaca.markets/docs/authentication
18
// Trading: https://paper-api.alpaca.markets (paper), https://api.alpaca.markets (live)
19
// Market data: https://data.alpaca.markets (both)
20
30
struct
AlpacaConfig
:
public
network::ClientCredentials
<AlpacaConfig> {
38
bool
is_demo_
=
true
;
39
45
void
from_env_impl
() {
46
const
char
*
key
= std::getenv(
"ALPACA_KEY"
);
47
const
char
*
secret
= std::getenv(
"ALPACA_SECRET"
);
48
if
(!
key
|| !
secret
) {
49
throw
std::runtime_error(
"Add ALPACA_KEY and ALPACA_SECRET environment variables."
);
50
}
51
// Read before anything is assigned, so a refused load leaves this config as it was.
52
const
bool
demo =
demo_from_env
();
53
key_
=
key
;
54
secret_
=
secret
;
55
is_demo_
= demo;
56
}
57
67
void
from_json_impl
(
const
std::string& filename) {
68
std::ifstream ifs(filename);
69
if
(!ifs.is_open()) {
70
throw
std::runtime_error(
"Cannot open Alpaca credential file: "
+ filename);
71
}
72
const
std::string content(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>{});
73
from_json_text_impl
(content,
"file "
+ filename);
74
}
75
84
void
from_json_text_impl
(std::string_view text,
const
std::string& origin) {
85
boost::system::error_code ec;
86
const
auto
jv = json::parse(text, ec);
87
if
(ec || !jv.is_object()) {
88
throw
std::runtime_error(
"Failed to parse Alpaca credential "
+ origin +
": not a JSON object"
);
89
}
90
const
auto
& doc = jv.as_object();
91
const
json::value*
key
= doc.if_contains(
"key_id"
);
92
const
json::value*
secret
= doc.if_contains(
"secret_key"
);
93
if
(!
key
|| !
secret
|| !
key
->is_string() || !
secret
->is_string()) {
94
throw
std::runtime_error(
"Alpaca credential "
+ origin +
" should contain key_id and secret_key as strings"
);
95
}
96
// Everything is read and checked before anything is assigned.
97
const
bool
demo =
demo_from_json
(doc, origin);
98
key_
=
key
->get_string().c_str();
99
secret_
=
secret
->get_string().c_str();
100
is_demo_
= demo;
101
}
102
108
static
bool
demo_from_env
() {
109
const
char
* demo = std::getenv(
"ALPACA_DEMO"
);
110
if
(!demo || std::string_view(demo) ==
"1"
) {
111
return
true
;
112
}
113
if
(std::string_view(demo) ==
"0"
) {
114
return
false
;
115
}
116
throw
std::runtime_error(
117
std::string(
"ALPACA_DEMO is \""
) + demo +
"\"; set it to 1 for paper trading or 0 to trade live, or leave it "
+
118
"unset for paper."
119
);
120
}
121
129
static
bool
demo_from_json
(
const
json::object& doc,
const
std::string& origin) {
130
const
json::value* demo = doc.if_contains(
"demo"
);
131
if
(!demo) {
132
return
true
;
133
}
134
if
(!demo->is_bool()) {
135
throw
std::runtime_error(
136
"Alpaca credential "
+ origin +
137
" has a \"demo\" that is not true or false; write true for paper trading or false to trade live, or leave "
138
"it out for paper."
139
);
140
}
141
return
demo->get_bool();
142
}
143
148
static
constexpr
std::string_view
key_field
() noexcept {
149
return
"key_id"
;
150
}
151
156
static
constexpr
std::string_view
secret_field
() noexcept {
157
return
"secret_key"
;
158
}
159
164
const
char
*
trading_host
() const noexcept {
165
return
is_demo_
?
"paper-api.alpaca.markets"
:
"api.alpaca.markets"
;
166
}
167
172
const
char
*
data_host
() const noexcept {
173
return
"data.alpaca.markets"
;
174
}
175
180
const
char
*
port
() const noexcept {
181
return
"443"
;
182
}
183
};
184
185
}
// namespace alpaca
186
}
// namespace algotradelib
algotradelib::network::ClientCredentials
OAuth client credentials (an app key and secret), the base of each broker's credentials class.
Definition
oauth.hpp:37
algotradelib::network::ClientCredentials< AlpacaConfig >::key
const std::string & key() const noexcept
Definition
oauth.hpp:78
algotradelib::network::ClientCredentials< AlpacaConfig >::key_
std::string key_
Definition
oauth.hpp:113
algotradelib::network::ClientCredentials< AlpacaConfig >::secret_
std::string secret_
Definition
oauth.hpp:115
algotradelib::network::ClientCredentials< AlpacaConfig >::secret
const std::string & secret() const noexcept
Definition
oauth.hpp:86
algotradelib::alpaca
The Alpaca Trading API and Market Data API client: exchange-listed US equities, paper by default.
Definition
client.hpp:33
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
algotradelib::alpaca::AlpacaConfig
Alpaca API keys (key id and secret key), paper or live account.
Definition
config.hpp:30
algotradelib::alpaca::AlpacaConfig::data_host
const char * data_host() const noexcept
The market-data host, the same for both environments.
Definition
config.hpp:172
algotradelib::alpaca::AlpacaConfig::demo_from_json
static bool demo_from_json(const json::object &doc, const std::string &origin)
Reads a credential document's "demo".
Definition
config.hpp:129
algotradelib::alpaca::AlpacaConfig::key_field
static constexpr std::string_view key_field() noexcept
The credential file's field for the key id; Alpaca has no token request behind it.
Definition
config.hpp:148
algotradelib::alpaca::AlpacaConfig::trading_host
const char * trading_host() const noexcept
The trading host for the selected environment.
Definition
config.hpp:164
algotradelib::alpaca::AlpacaConfig::demo_from_env
static bool demo_from_env()
Reads ALPACA_DEMO.
Definition
config.hpp:108
algotradelib::alpaca::AlpacaConfig::from_json_text_impl
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 t...
Definition
config.hpp:84
algotradelib::alpaca::AlpacaConfig::from_json_impl
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.
Definition
config.hpp:67
algotradelib::alpaca::AlpacaConfig::from_env_impl
void from_env_impl()
Reads the keys from ALPACA_KEY and ALPACA_SECRET, and the environment from ALPACA_DEMO (see demo_from...
Definition
config.hpp:45
algotradelib::alpaca::AlpacaConfig::secret_field
static constexpr std::string_view secret_field() noexcept
The credential file's field for the secret key; Alpaca has no token request behind it.
Definition
config.hpp:156
algotradelib::alpaca::AlpacaConfig::is_demo_
bool is_demo_
Whether to use Alpaca's paper trading host; true unless stated otherwise.
Definition
config.hpp:38
algotradelib::alpaca::AlpacaConfig::port
const char * port() const noexcept
The HTTPS port, the same for every host.
Definition
config.hpp:180
algotradelib
src
brokers
alpaca
config.hpp
Generated by
1.18.0