algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-kis-quote.cpp
1#include "kis_credentials.hpp"
2
3#include <algotradelib/kis>
4
5#include <boost/asio/io_context.hpp>
6#include <boost/asio/ssl/context.hpp>
7
8#include <exception>
9#include <iostream>
10#include <memory>
11#include <string>
12#include <utility>
13
14namespace net = boost::asio;
15namespace ssl = net::ssl;
16
17int main(int argc, char* argv[]) {
18 try {
19 const auto directory = kis_credentials::take_directory(argc, argv);
20 const std::string symbol = argc > 1 ? argv[1] : "005930";
22 kis_credentials::load(directory, config, nullptr, std::cout);
23
24 std::cout << "environment: " << (config.is_demo_ ? "demo" : "live") << '\n';
25 std::cout << "symbol: " << symbol << '\n';
26
27 net::io_context ioc;
28 ssl::context tls(ssl::context::tls_client);
29 tls.set_default_verify_paths();
30 tls.set_verify_mode(ssl::verify_peer);
31
32 auto client = std::make_shared<algotradelib::kis::KisClient>(
33 ioc.get_executor(), tls, std::move(config), kis_credentials::paced_options()
34 );
35 int exit_code = 1;
36 client->async_quote(
37 {algotradelib::Country::KR, algotradelib::Venue::COMBINED, symbol},
39 if (!result) {
40 const auto& error = result.error();
41 std::cerr << "quote failed";
42 if (!error.broker_code_.empty()) {
43 std::cerr << " [" << error.broker_code_ << ']';
44 }
45 if (error.http_status_ != 0) {
46 std::cerr << " HTTP " << error.http_status_;
47 }
48 if (!error.message_.empty()) {
49 std::cerr << ": " << error.message_;
50 }
51 std::cerr << '\n';
52 return;
53 }
54
55 const auto& quote = result.value();
56 std::cout << quote.instrument_.symbol_ << ' ' << quote.last_price_.str() << ' '
57 << quote.currency_.str() << '\n';
58 exit_code = 0;
59 }
60 );
61 ioc.run();
62 return exit_code;
63 } catch (const std::exception& exception) {
64 std::cerr << "example failed: " << exception.what() << '\n';
65 return 1;
66 }
67}
Either a value or a BrokerError.
Definition types.hpp:797
The examples' KIS credentials: one JSON file per account, or the environment.
algotradelib::network::HttpClientOptions paced_options()
Returns transport options that pace a KIS client at one request a second.
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...
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.
void quote(S &strategy, Ctx &context, const QuoteEvent &event)
Calls strategy.on_quote(context, event) if the strategy declares on_quote; otherwise does nothing.
Definition concept.hpp:240
KIS credentials (app key and secret), demo or real account, and the market-data tape.
Definition config.hpp:90
bool is_demo_
Whether to use KIS's demo (모의투자) endpoints; true unless stated otherwise.
Definition config.hpp:101