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_) << "'\n";
35
36 net::io_context ioc;
37 ssl::context tls(ssl::context::tls_client);
38 tls.set_default_verify_paths();
39 tls.set_verify_mode(ssl::verify_peer);
40
41 auto client = std::make_shared<algotradelib::kiwoom::KiwoomClient>(ioc.get_executor(), tls, std::move(config));
42 int exit_code = 1;
43 client->async_quote(
44 {algotradelib::Country::KR, algotradelib::Venue::COMBINED, symbol},
46 if (!result) {
47 const auto& error = result.error();
48 std::cerr << "quote failed";
49 if (!error.broker_code_.empty()) {
50 std::cerr << " [" << error.broker_code_ << ']';
51 }
52 if (error.http_status_ != 0) {
53 std::cerr << " HTTP " << error.http_status_;
54 }
55 if (!error.message_.empty()) {
56 std::cerr << ": " << error.message_;
57 }
58 std::cerr << '\n';
59 return;
60 }
61
62 const auto& quote = result.value();
63 std::cout
64 << quote.instrument_.symbol_ << ' ' << quote.last_price_.str() << ' ' << quote.currency_.str() << '\n';
65 if (quote.volume_) {
66 std::cout << "volume: " << quote.volume_->str() << '\n';
67 }
68 if (quote.top_of_book_) {
69 const auto& top = *quote.top_of_book_;
70 std::cout
71 << "bid: " << (top.bid_price_ ? top.bid_price_->str() : "-") << " x "
72 << (top.bid_quantity_ ? top.bid_quantity_->str() : "-") << '\n';
73 std::cout
74 << "ask: " << (top.ask_price_ ? top.ask_price_->str() : "-") << " x "
75 << (top.ask_quantity_ ? top.ask_quantity_->str() : "-") << '\n';
76 }
77 exit_code = 0;
78 }
79 );
80 ioc.run();
81 return exit_code;
82 } catch (const std::exception& exception) {
83 std::cerr << "example failed: " << exception.what() << '\n';
84 return 1;
85 }
86}
Either a value or a BrokerError.
Definition types.hpp:1058
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:48
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:331
Kiwoom credentials (app key and secret key), demo or real account, the market-data tape,...
Definition config.hpp:155
bool is_demo_
Whether to use Kiwoom's demo (모의투자) endpoints; true unless stated otherwise.
Definition config.hpp:163
MarketTape market_tape_
Which venue's prices every Kiwoom market-data request reads (quotes, depth, trades,...
Definition config.hpp:171