algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-kiwoom-auth.cpp
2
3#include <algotradelib/kiwoom>
4#include <iostream>
5
6namespace net = boost::asio;
7namespace ssl = net::ssl;
8
9int 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.
17 try {
18 const auto directory = kis_credentials::take_directory(argc, argv);
19 const auto label = kiwoom_credentials::take_label(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}
Obtains and caches a Kiwoom access token with blocking requests; thread-safe.
Definition auth.hpp:21
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.
The examples' Kiwoom credentials: one JSON file per key, 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...
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
const char * rest_host() const noexcept
The REST host for the selected environment.
Definition config.hpp:319