algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
kis_credentials.hpp
Go to the documentation of this file.
1#pragma once
2
16
17#include <algotradelib/kis>
18
19#include <cstdlib>
20#include <filesystem>
21#include <optional>
22#include <ostream>
23#include <stdexcept>
24#include <string>
25#include <string_view>
26
27namespace kis_credentials {
28
34inline std::optional<std::filesystem::path> environment_directory() {
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");
38 }
39 return variable != nullptr ? std::optional<std::filesystem::path>(variable) : std::nullopt;
40}
41
52inline std::optional<std::filesystem::path> take_directory(int& argc, char** argv) {
53 std::optional<std::filesystem::path> directory;
54 int kept = 1;
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() || std::string_view(argv[i + 1]).rfind("--", 0) == 0) {
58 throw std::runtime_error("--credentials needs a directory");
59 }
60 directory = argv[++i];
61 } else {
62 argv[kept++] = argv[i];
63 }
64 }
65 argc = kept;
66 return directory ? directory : environment_directory();
67}
68
84
92inline std::filesystem::path required_file(const std::filesystem::path& directory, const char* name) {
93 const auto path = directory / "kis" / name;
94 if (!std::filesystem::exists(path)) {
95 throw std::runtime_error(
96 "no " + path.string() + "; the credentials directory is given, so the environment is not read"
97 );
98 }
99 return path;
100}
101
109inline void load_config(algotradelib::kis::KisConfig& config, const std::filesystem::path& path, bool demo) {
110 config.from_json(path.string());
111 if (config.is_demo_ != demo) {
112 throw std::runtime_error(
113 path.string() + " says \"demo\": " + (config.is_demo_ ? "true" : "false") + ", which contradicts its name"
114 );
115 }
116}
117
127inline void load(
128 const std::optional<std::filesystem::path>& directory, algotradelib::kis::KisConfig& config,
129 algotradelib::kis::KisAccount* account, std::ostream& out
130) {
131 if (!directory) {
132 config.from_env();
133 if (account != nullptr) {
134 account->from_env();
135 }
136 out << "credentials: environment variables\n";
137 return;
138 }
140 const auto path = required_file(*directory, demo ? "demo.json" : "real.json");
141 load_config(config, path, demo);
142 if (account != nullptr) {
143 account->from_json(path.string());
144 }
145 out << "credentials: " << path.string() << '\n';
146}
147
157inline std::optional<algotradelib::kis::KisConfig> market_data_config(
158 const std::optional<std::filesystem::path>& directory
159) {
160 if (!directory || !std::filesystem::exists(*directory / "kis" / "real.json")) {
161 return std::nullopt;
162 }
164 load_config(config, *directory / "kis" / "real.json", false);
165 return config;
166}
167
168} // namespace kis_credentials
void from_env()
Loads the credentials from the broker's environment variables.
Definition oauth.hpp:47
void from_json(const std::string &filename)
Loads the credentials from a JSON file, in the broker's format.
Definition oauth.hpp:57
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.
Definition client.hpp:31
void from_env()
Loads the account from the KIS_CANO and (optional, default "01") KIS_ACNT_PRDT_CD environment variabl...
Definition client.hpp:42
void from_json(const std::string &filename)
Loads the account from a credential file's account_number (eight digits) and account_product_code (tw...
Definition client.hpp:63
KIS credentials (app key and secret), demo or real account, and the market-data tape.
Definition config.hpp:83
bool is_demo_
Whether to use KIS's demo (모의투자) endpoints; true unless stated otherwise.
Definition config.hpp:94
static bool demo_from_env()
Reads KIS_DEMO.
Definition config.hpp:178
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.