algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
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
37
namespace
kiwoom_credentials {
38
44
inline
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
63
inline
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
90
inline
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
110
inline
algotradelib::network::HttpClientOptions
paced_options
() {
111
algotradelib::network::HttpClientOptions
options;
112
options.
rate_limit_
=
algotradelib::network::RateLimitBudget
{1, 1, 0};
113
return
options;
114
}
115
126
inline
void
load
(
127
const
std::optional<std::filesystem::path>& directory,
128
const
std::optional<std::string>& label,
129
algotradelib::kiwoom::KiwoomConfig
& config,
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"
);
144
const
bool
demo =
algotradelib::kiwoom::KiwoomConfig::demo_from_env
();
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
algotradelib::network::ClientCredentials::from_env
void from_env()
Loads the credentials from the broker's environment variables.
Definition
oauth.hpp:46
algotradelib::network::ClientCredentials::from_json
void from_json(const std::string &filename)
Loads the credentials from a JSON file, in the broker's format.
Definition
oauth.hpp:54
kis_credentials.hpp
The examples' KIS credentials: one JSON file per account, or the environment.
kiwoom_credentials::refuse_unknown_flags
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...
Definition
kiwoom_credentials.hpp:90
kiwoom_credentials::paced_options
algotradelib::network::HttpClientOptions paced_options()
Returns transport options that pace a Kiwoom client at one request a second.
Definition
kiwoom_credentials.hpp:110
kiwoom_credentials::valid_label
bool valid_label(std::string_view label)
Tests whether a label can name a file in the credentials directory.
Definition
kiwoom_credentials.hpp:44
kiwoom_credentials::take_label
std::optional< std::string > take_label(int &argc, char **argv)
Removes --kiwoom-account LABEL from the command line and returns the label.
Definition
kiwoom_credentials.hpp:63
kiwoom_credentials::load
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...
Definition
kiwoom_credentials.hpp:126
algotradelib::kiwoom::KiwoomConfig
Kiwoom credentials (app key and secret key), demo or real account, the market-data tape,...
Definition
config.hpp:154
algotradelib::kiwoom::KiwoomConfig::is_demo_
bool is_demo_
Whether to use Kiwoom's demo (모의투자) endpoints; true unless stated otherwise.
Definition
config.hpp:162
algotradelib::kiwoom::KiwoomConfig::demo_from_env
static bool demo_from_env()
Reads KIWOOM_DEMO.
Definition
config.hpp:268
algotradelib::network::HttpClientOptions
Deadlines, size limits and pacing for an HTTP client.
Definition
http_client.hpp:77
algotradelib::network::HttpClientOptions::rate_limit_
std::optional< RateLimitBudget > rate_limit_
The rate-limit budget that paces this connection's requests, or empty (the default) for no pacing (As...
Definition
http_client.hpp:95
algotradelib::network::RateLimitBudget
The request rate a connection may use.
Definition
rate_limit.hpp:119
examples
kiwoom_credentials.hpp
Generated by
1.18.0