algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
Loading...
Searching...
No Matches
example-kiwoom-auth.cpp
1
#include "
kiwoom_credentials.hpp
"
2
3
#include <algotradelib/kiwoom>
4
#include <iostream>
5
6
namespace
net = boost::asio;
7
namespace
ssl = net::ssl;
8
9
int
main(
int
argc,
char
** argv) {
10
// Credentials from one file per key (kiwoom_credentials.hpp):
11
// example-kiwoom-auth --credentials DIR [--kiwoom-account kr] reads DIR/kiwoom/demo-kr.json
12
// or, without a directory, from environment variables:
13
// export KIWOOM_APP_KEY=AxserEsdcredca.....
14
// export KIWOOM_SECRET_KEY=SEefdcwcforehDre2fdvc....
15
// KIWOOM_DEMO=0 selects live (real-<label>.json); unset or 1 is paper trading.
16
algotradelib::kiwoom::KiwoomConfig
cfg;
17
try
{
18
const
auto
directory =
kis_credentials::take_directory
(argc, argv);
19
const
auto
label =
kiwoom_credentials::take_label
(argc, argv);
20
kiwoom_credentials::refuse_unknown_flags
(argc, argv);
21
kiwoom_credentials::load
(directory, label, cfg, std::cout);
22
}
catch
(
const
std::exception& e) {
23
std::cerr <<
"[error] "
<< e.what() <<
"\n"
;
24
return
1;
25
}
26
27
std::cout <<
"env : "
<< (cfg.
is_demo_
?
"demo (paper)"
:
"real"
) <<
"\n"
;
28
std::cout <<
"REST : "
<< cfg.
rest_host
() <<
"\n"
;
29
30
net::io_context ioc;
31
ssl::context ctx(ssl::context::tlsv12_client);
// TLS 1.2 or 1.3
32
ctx.set_default_verify_paths();
33
ctx.set_verify_mode(ssl::verify_peer);
34
algotradelib::kiwoom::KiwoomAuth
auth(ioc, ctx, cfg);
35
36
std::cout <<
"Fetching access token ..."
;
37
try
{
38
(void)auth.accessToken();
39
std::cout <<
" OK"
<<
"\n\n"
;
40
}
catch
(
const
std::exception& e) {
41
std::cerr <<
" FAIL: "
<< e.what() <<
"\n\n"
;
42
return
1;
43
}
44
45
return
0;
46
}
algotradelib::kiwoom::KiwoomAuth
Obtains and caches a Kiwoom access token with blocking requests; thread-safe.
Definition
auth.hpp:21
kis_credentials::take_directory
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.
Definition
kis_credentials.hpp:52
kiwoom_credentials.hpp
The examples' Kiwoom credentials: one JSON file per key, 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::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::rest_host
const char * rest_host() const noexcept
The REST host for the selected environment.
Definition
config.hpp:319
examples
example-kiwoom-auth.cpp
Generated by
1.18.0