algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
Loading...
Searching...
No Matches
example-kis-auth.cpp
1
#include "
kis_credentials.hpp
"
2
3
#include <algotradelib/kis>
4
#include <iostream>
5
#include <string>
6
7
namespace
beast = boost::beast;
8
namespace
http = boost::beast::http;
9
namespace
net = boost::asio;
10
namespace
ssl = net::ssl;
11
12
int
main(
int
argc,
char
* argv[]) {
13
// Credentials from `--credentials DIR` or ATL_CREDENTIALS_DIR (kis_credentials.hpp), else from
14
// environment variables:
15
// export KIS_APP_KEY=PSxxxxxxxxxxxxxxxx
16
// export KIS_APP_SECRET=xxxxxxxx...
17
// export KIS_DEMO=0 (live; unset or 1 = paper trading)
18
algotradelib::kis::KisConfig
cfg;
19
try
{
20
kis_credentials::load
(
kis_credentials::take_directory
(argc, argv), cfg,
nullptr
, std::cout);
21
}
catch
(
const
std::exception& e) {
22
std::cerr <<
"[error] "
<< e.what() <<
"\n"
;
23
return
1;
24
}
25
26
std::cout <<
"env : "
<< (cfg.
is_demo_
?
"demo (paper)"
:
"real"
) <<
"\n"
;
27
std::cout <<
"REST : "
<< cfg.
rest_host
() <<
":"
<< cfg.
rest_port
() <<
"\n"
;
28
std::cout <<
"WS : "
<< cfg.
ws_host
() <<
":"
<< cfg.
ws_port
() <<
"\n\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::kis::KisAuth
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
std::cout <<
"Fetching WS approval key ..."
;
46
try
{
47
(void)auth.accessWebsocket();
48
std::cout <<
" OK"
<<
"\n\n"
;
49
}
catch
(
const
std::exception& e) {
50
std::cerr <<
" FAIL: "
<< e.what() <<
"\n\n"
;
51
return
1;
52
}
53
54
return
0;
55
}
algotradelib::kis::KisAuth
Obtains and caches KIS access tokens and WebSocket approval keys with blocking requests; thread-safe.
Definition
auth.hpp:19
kis_credentials.hpp
The examples' KIS credentials: one JSON file per account, or the environment.
kis_credentials::load
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...
Definition
kis_credentials.hpp:129
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
algotradelib::kis::KisConfig
KIS credentials (app key and secret), demo or real account, and the market-data tape.
Definition
config.hpp:90
algotradelib::kis::KisConfig::is_demo_
bool is_demo_
Whether to use KIS's demo (모의투자) endpoints; true unless stated otherwise.
Definition
config.hpp:101
algotradelib::kis::KisConfig::ws_port
const char * ws_port() const noexcept
Returns the WebSocket port for the environment.
Definition
config.hpp:250
algotradelib::kis::KisConfig::rest_port
const char * rest_port() const noexcept
Returns the REST port for the environment.
Definition
config.hpp:238
algotradelib::kis::KisConfig::rest_host
const char * rest_host() const noexcept
Returns the REST host for the environment.
Definition
config.hpp:230
algotradelib::kis::KisConfig::ws_host
const char * ws_host() const noexcept
Returns the WebSocket host (the same for both environments).
Definition
config.hpp:244
examples
example-kis-auth.cpp
Generated by
1.18.0