algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
kiwoom_credentials.hpp
Go to the documentation of this file.
1#pragma once
2
25
26#include "kis_credentials.hpp"
27
28#include <algotradelib/kiwoom>
29
30#include <filesystem>
31#include <optional>
32#include <ostream>
33#include <stdexcept>
34#include <string>
35#include <string_view>
36
37namespace kiwoom_credentials {
38
44inline bool valid_label(std::string_view label) {
45 if (label.empty() || label.front() == '-') {
46 return false;
47 }
48 for (const char c : label) {
49 if (!((c >= 'a' && c <= 'z') || (c >= '0' && c <= '9') || c == '-')) {
50 return false;
51 }
52 }
53 return true;
54}
55
63inline std::optional<std::string> take_label(int& argc, char** argv) {
64 std::optional<std::string> label;
65 int kept = 1;
66 for (int i = 1; i < argc; ++i) {
67 if (std::string_view(argv[i]) == "--kiwoom-account") {
68 if (i + 1 >= argc || std::string_view(argv[i + 1]).rfind("--", 0) == 0) {
69 throw std::runtime_error("--kiwoom-account needs a label, such as kr");
70 }
71 label = argv[++i];
72 if (!valid_label(*label)) {
73 throw std::runtime_error("--kiwoom-account takes lowercase letters, digits and '-', not \"" + *label + "\"");
74 }
75 } else {
76 argv[kept++] = argv[i];
77 }
78 }
79 argc = kept;
80 return label;
81}
82
90inline void refuse_unknown_flags(int argc, char** argv) {
91 for (int i = 1; i < argc; ++i) {
92 if (std::string_view(argv[i]).rfind("--", 0) == 0) {
93 throw std::runtime_error(std::string("unknown option ") + argv[i]);
94 }
95 }
96}
97
115
126inline void load(
127 const std::optional<std::filesystem::path>& directory,
128 const std::optional<std::string>& label,
130 std::ostream& out
131) {
132 if (!directory) {
133 if (label) {
134 throw std::runtime_error(
135 "--kiwoom-account selects a file in a credentials directory; give --credentials DIR or set "
136 "ATL_CREDENTIALS_DIR"
137 );
138 }
139 config.from_env();
140 out << "credentials: environment variables\n";
141 return;
142 }
143 const std::string name = label.value_or("kr");
145 const auto path = *directory / "kiwoom" / ((demo ? "demo-" : "real-") + name + ".json");
146 if (!std::filesystem::exists(path)) {
147 throw std::runtime_error(
148 "no " + path.string() + "; the credentials directory is given, so the environment is not read"
149 );
150 }
151 config.from_json(path.string());
152 if (config.is_demo_ != demo) {
153 throw std::runtime_error(
154 path.string() + " says \"demo\": " + (config.is_demo_ ? "true" : "false") +
155 ", which contradicts its name"
156 );
157 }
158 out << "credentials: " << path.string() << '\n';
159 if (name != "kr") {
160 out << "note: the Kiwoom adapter calls domestic-stock endpoints only\n";
161 }
162}
163
164} // namespace kiwoom_credentials
void from_env()
Loads the credentials from the broker's environment variables.
Definition oauth.hpp:46
void from_json(const std::string &filename)
Loads the credentials from a JSON file, in the broker's format.
Definition oauth.hpp:54
The examples' KIS credentials: one JSON file per account, or the environment.
void refuse_unknown_flags(int argc, char **argv)
Refuses any flag left on the command line after the credential flags were taken, so a mistyped --kiwo...
algotradelib::network::HttpClientOptions paced_options()
Returns transport options that pace a Kiwoom client at one request a second.
bool valid_label(std::string_view label)
Tests whether a label can name a file in the credentials directory.
std::optional< std::string > take_label(int &argc, char **argv)
Removes --kiwoom-account LABEL from the command line and returns the label.
void load(const std::optional< std::filesystem::path > &directory, const std::optional< std::string > &label, algotradelib::kiwoom::KiwoomConfig &config, std::ostream &out)
Loads the credentials from DIR/kiwoom/{demo,real}-LABEL.json (as KIWOOM_DEMO chooses; label kr by def...
Kiwoom credentials (app key and secret key), demo or real account, the market-data tape,...
Definition config.hpp:154
bool is_demo_
Whether to use Kiwoom's demo (모의투자) endpoints; true unless stated otherwise.
Definition config.hpp:162
static bool demo_from_env()
Reads KIWOOM_DEMO.
Definition config.hpp:268
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.