algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-kiwoom-quote.cpp
1// Reads one Kiwoom quote with its volume and the top of its book.
2//
3// Both come from one `ka10007` response, so this costs one request rather than two.
4
6
7#include <algotradelib/kiwoom>
8
9#include <boost/asio/io_context.hpp>
10#include <boost/asio/ssl/context.hpp>
11
12#include <exception>
13#include <iostream>
14#include <memory>
15#include <string>
16#include <utility>
17
18namespace net = boost::asio;
19namespace ssl = net::ssl;
20
21int main(int argc, char* argv[]) {
22 try {
23 // `--credentials DIR [--kiwoom-account LABEL]`, else the environment (kiwoom_credentials.hpp).
24 const auto directory = kis_credentials::take_directory(argc, argv);
25 const auto label = kiwoom_credentials::take_label(argc, argv);
27 const std::string symbol = argc > 1 ? argv[1] : "005930";
29 kiwoom_credentials::load(directory, label, config, std::cout);
30
31 std::cout << "environment: " << (config.is_demo_ ? "demo" : "live") << '\n';
32 std::cout << "symbol: " << symbol << '\n';
33 // One setting for every market-data verb; the consolidated book by default.
34 std::cout << "tape suffix: '" << algotradelib::kiwoom::tape_suffix(config.market_tape_)
35 << "'\n";
36
37 net::io_context ioc;
38 ssl::context tls(ssl::context::tls_client);
39 tls.set_default_verify_paths();
40 tls.set_verify_mode(ssl::verify_peer);
41
42 auto client = std::make_shared<algotradelib::kiwoom::KiwoomClient>(
43 ioc.get_executor(), tls, std::move(config)
44 );
45 int exit_code = 1;
46 client->async_quote(
47 {algotradelib::Country::KR, algotradelib::Venue::COMBINED, symbol},
49 if (!result) {
50 const auto& error = result.error();
51 std::cerr << "quote failed";
52 if (!error.broker_code_.empty()) {
53 std::cerr << " [" << error.broker_code_ << ']';
54 }
55 if (error.http_status_ != 0) {
56 std::cerr << " HTTP " << error.http_status_;
57 }
58 if (!error.message_.empty()) {
59 std::cerr << ": " << error.message_;
60 }
61 std::cerr << '\n';
62 return;
63 }
64
65 const auto& quote = result.value();
66 std::cout << quote.instrument_.symbol_ << ' ' << quote.last_price_.str() << ' '
67 << quote.currency_.str() << '\n';
68 if (quote.volume_) {
69 std::cout << "volume: " << quote.volume_->str() << '\n';
70 }
71 if (quote.top_of_book_) {
72 const auto& top = *quote.top_of_book_;
73 std::cout << "bid: " << (top.bid_price_ ? top.bid_price_->str() : "-") << " x "
74 << (top.bid_quantity_ ? top.bid_quantity_->str() : "-") << '\n';
75 std::cout << "ask: " << (top.ask_price_ ? top.ask_price_->str() : "-") << " x "
76 << (top.ask_quantity_ ? top.ask_quantity_->str() : "-") << '\n';
77 }
78 exit_code = 0;
79 }
80 );
81 ioc.run();
82 return exit_code;
83 } catch (const std::exception& exception) {
84 std::cerr << "example failed: " << exception.what() << '\n';
85 return 1;
86 }
87}
Either a value or a BrokerError.
Definition types.hpp:797
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...
constexpr const char * tape_suffix(MarketTape tape) noexcept
Returns the symbol suffix for a tape.
Definition config.hpp:47
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
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
MarketTape market_tape_
Which venue's prices every Kiwoom market-data request reads (quotes, depth, trades,...
Definition config.hpp:170