17#include <algotradelib/kis>
27namespace kis_credentials {
35 const char* variable = std::getenv(
"ATL_CREDENTIALS_DIR");
36 if (variable !=
nullptr && *variable ==
'\0') {
37 throw std::runtime_error(
"ATL_CREDENTIALS_DIR is set but empty");
39 return variable !=
nullptr ? std::optional<std::filesystem::path>(variable) : std::nullopt;
52inline std::optional<std::filesystem::path>
take_directory(
int& argc,
char** argv) {
53 std::optional<std::filesystem::path> directory;
55 for (
int i = 1; i < argc; ++i) {
56 if (std::string_view(argv[i]) ==
"--credentials") {
57 if (i + 1 >= argc || std::string_view(argv[i + 1]).empty() ||
58 std::string_view(argv[i + 1]).rfind(
"--", 0) == 0) {
59 throw std::runtime_error(
"--credentials needs a directory");
61 directory = argv[++i];
63 argv[kept++] = argv[i];
93inline std::filesystem::path
required_file(
const std::filesystem::path& directory,
const char* name) {
94 const auto path = directory /
"kis" / name;
95 if (!std::filesystem::exists(path)) {
96 throw std::runtime_error(
97 "no " + path.string() +
"; the credentials directory is given, so the environment is not read"
113 throw std::runtime_error(
114 path.string() +
" says \"demo\": " + (config.
is_demo_ ?
"true" :
"false") +
115 ", which contradicts its name"
130 const std::optional<std::filesystem::path>& directory,
137 if (account !=
nullptr) {
140 out <<
"credentials: environment variables\n";
144 const auto path =
required_file(*directory, demo ?
"demo.json" :
"real.json");
146 if (account !=
nullptr) {
149 out <<
"credentials: " << path.string() <<
'\n';
162 const std::optional<std::filesystem::path>& directory
164 if (!directory || !std::filesystem::exists(*directory /
"kis" /
"real.json")) {
168 load_config(config, *directory /
"kis" /
"real.json",
false);
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.
algotradelib::network::HttpClientOptions paced_options()
Returns transport options that pace a KIS client at one request a second.
std::filesystem::path required_file(const std::filesystem::path &directory, const char *name)
Returns the path of a credential file in the directory.
std::optional< std::filesystem::path > environment_directory()
Returns ATL_CREDENTIALS_DIR, for an example that parses no arguments of its own.
std::optional< algotradelib::kis::KisConfig > market_data_config(const std::optional< std::filesystem::path > &directory)
Returns a configuration for market data alone, from DIR/kis/real.json.
void load(const std::optional< std::filesystem::path > &directory, algotradelib::kis::KisConfig &config, algotradelib::kis::KisAccount *account, std::ostream &out)
Loads the credentials (and account) KIS_DEMO chooses, and prints where from (a path or "environment v...
void load_config(algotradelib::kis::KisConfig &config, const std::filesystem::path &path, bool demo)
Loads a configuration from a file and refuses one whose "demo" contradicts its name.
std::optional< std::filesystem::path > take_directory(int &argc, char **argv)
Removes --credentials DIR from the command line and returns the directory, else ATL_CREDENTIALS_DIR.
A KIS account: the account number and product code every account request names.
void from_env()
Loads the account from the KIS_CANO and (optional, default "01") KIS_ACNT_PRDT_CD environment variabl...
void from_json(const std::string &filename)
Loads the account from a credential file's account_number (eight digits) and account_product_code (tw...
KIS credentials (app key and secret), demo or real account, and the market-data tape.
bool is_demo_
Whether to use KIS's demo (모의투자) endpoints; true unless stated otherwise.
static bool demo_from_env()
Reads KIS_DEMO.
Deadlines, size limits and pacing for an HTTP client.
std::optional< RateLimitBudget > rate_limit_
The rate-limit budget that paces this connection's requests, or empty (the default) for no pacing (As...
The request rate a connection may use.