algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-tossinvest-quote.cpp
1#include <algotradelib/tossinvest>
2
3#include <boost/asio/io_context.hpp>
4#include <boost/asio/ssl/context.hpp>
5
6#include <exception>
7#include <iostream>
8#include <memory>
9#include <string>
10#include <utility>
11
12namespace net = boost::asio;
13namespace ssl = net::ssl;
14
15int main(int argc, char* argv[]) {
16 const std::string symbol = argc > 1 ? argv[1] : "005930";
17
18 try {
20 config.from_env();
21
22 std::cout << "broker: Toss Securities" << "\n";
23 std::cout << "symbol: " << symbol << '\n';
24
25 net::io_context ioc;
26 ssl::context tls(ssl::context::tls_client);
27 tls.set_default_verify_paths();
28 tls.set_verify_mode(ssl::verify_peer);
29
30 auto client = std::make_shared<algotradelib::tossinvest::TossClient>(
31 ioc.get_executor(), tls, std::move(config)
32 );
33 int exit_code = 1;
34 client->async_quote(
35 {algotradelib::Country::KR, algotradelib::Venue::COMBINED, symbol},
37 if (!result) {
38 const auto& error = result.error();
39 std::cerr << "quote failed";
40 if (!error.broker_code_.empty()) {
41 std::cerr << " [" << error.broker_code_ << ']';
42 }
43 if (!error.request_id_.empty()) {
44 std::cerr << " request=" << error.request_id_;
45 }
46 if (error.http_status_ != 0) {
47 std::cerr << " HTTP " << error.http_status_;
48 }
49 if (!error.message_.empty()) {
50 std::cerr << ": " << error.message_;
51 }
52 std::cerr << '\n';
53 return;
54 }
55
56 const auto& quote = result.value();
57 std::cout << quote.instrument_.symbol_ << ' ' << quote.last_price_.str() << ' '
58 << quote.currency_.str() << '\n';
59 exit_code = 0;
60 }
61 );
62 ioc.run();
63 return exit_code;
64 } catch (const std::exception& exception) {
65 std::cerr << "example failed: " << exception.what() << '\n';
66 return 1;
67 }
68}
Either a value or a BrokerError.
Definition types.hpp:797
void from_env()
Loads the credentials from the broker's environment variables.
Definition oauth.hpp:46
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
Toss Securities credentials (client id and secret). Toss has no demo environment.
Definition config.hpp:24