3#include "../../network/oauth.hpp"
4#include "../../network/token_store.hpp"
6#include "../../core/types.hpp"
8#include <boost/json.hpp>
67 if (text.size() != 14) {
70 for (
const char digit : text) {
71 if (digit <
'0' || digit >
'9') {
75 const auto number = [text](std::size_t offset, std::size_t length) {
77 for (std::size_t index = offset; index < offset + length; ++index) {
78 value = value * 10 + (text[index] -
'0');
83 parts.tm_year = number(0, 4) - 1900;
84 parts.tm_mon = number(4, 2) - 1;
85 parts.tm_mday = number(6, 2);
86 parts.tm_hour = number(8, 2);
87 parts.tm_min = number(10, 2);
88 parts.tm_sec = number(12, 2);
91 const std::time_t utc = _mkgmtime(&parts) - 9 * 3600;
93 const std::time_t utc = timegm(&parts) - 9 * 3600;
95 return std::chrono::system_clock::from_time_t(utc);
211 const char*
key = std::getenv(
"KIWOOM_APP_KEY");
212 const char*
secret = std::getenv(
"KIWOOM_SECRET_KEY");
214 throw std::runtime_error(
"Add KIWOOM_APP_KEY and KIWOOM_SECRET_KEY environment variables.");
235 std::ifstream ifs(filename);
236 if (!ifs.is_open()) {
237 throw std::runtime_error(
"Cannot open Kiwoom credential file: " + filename);
239 const std::string content(std::istreambuf_iterator<char>(ifs), std::istreambuf_iterator<char>{});
252 boost::system::error_code ec;
253 const auto jv = json::parse(text, ec);
254 if (ec || !jv.is_object()) {
255 throw std::runtime_error(
256 "Failed to parse Kiwoom credentials " + origin +
257 (ec ?
" — " + ec.message() : std::string(
" — not a JSON object"))
260 const auto& doc = jv.as_object();
261 const json::value*
key = doc.if_contains(
"appkey");
262 const json::value*
secret = doc.if_contains(
"secretkey");
264 throw std::runtime_error(
"Kiwoom credential " + origin +
" should contain appkey and secretkey as strings");
269 key_ =
key->get_string().c_str();
280 const char* demo = std::getenv(
"KIWOOM_DEMO");
281 if (!demo || std::string_view(demo) ==
"1") {
284 if (std::string_view(demo) ==
"0") {
287 throw std::runtime_error(
288 std::string(
"KIWOOM_DEMO is \"") + demo +
289 "\"; set it to 1 for the demo endpoints or 0 to trade "
290 "live, or leave it unset for demo."
302 const json::value* demo = doc.if_contains(
"demo");
306 if (!demo->is_bool()) {
307 throw std::runtime_error(
308 "Kiwoom credential " + origin +
309 " has a \"demo\" that is not true or false; write "
310 "true for the demo endpoints or false to trade live, or leave it out for demo."
313 return demo->get_bool();
320 static constexpr std::string_view
key_field() noexcept {
345 return is_demo_ ?
"mockapi.kiwoom.com" :
"api.kiwoom.com";
361 return is_demo_ ?
"mockapi.kiwoom.com" :
"api.kiwoom.com";
401 if (
const auto expiry = doc.if_contains(
"expires_dt"); expiry && expiry->is_string()) {
404 if (
const auto type = doc.if_contains(
"token_type"); type && type->is_string()) {
OAuth client credentials (an app key and secret), the base of each broker's credentials class.
const std::string & key() const noexcept
const std::string & secret() const noexcept
The Kiwoom Securities REST API client.
constexpr const char * tape_suffix(MarketTape tape) noexcept
Returns the symbol suffix for a tape.
MarketTape
Which venue's prices Kiwoom market-data requests read (see KiwoomConfig::market_tape_).
@ CONSOLIDATED
KRX and NXT combined, the SOR book (_AL).
@ KRX
KRX alone (no suffix).
KiwoomSellableScope
What a Kiwoom holdings row's tradable quantity (trde_able_qty) covers; undocumented,...
@ UNSPECIFIED
Not stated: the sellable basis is unestablished, and a sell checked against it is refused.
@ WHOLE_HOLDING
Asserted: it covers every share held of the instrument, on any venue.
std::chrono::system_clock::time_point parse_compact_expiry(std::string_view text)
Reads Kiwoom's token expiry, yyyyMMddHHmmss in Korean time.
constexpr bool kiwoom_position_scope_asserted(KiwoomPositionScope scope) noexcept
Tests whether a holdings scope states what Kiwoom's holdings figure covers, so holdings may be read a...
KiwoomPositionScope
What the venue named on a Kiwoom holdings request (kt00018's dmst_stex_tp) means for the positions it...
@ UNSPECIFIED
Not stated: every new order and amendment is refused (cancellations are not), and the session's holdi...
@ VALUATION_ONLY
Asserted: the venue only selects which venue's price values each position, and every position is list...
std::string token_identity(std::string_view broker, std::string_view environment, std::string_view key)
Names what a token may not outlive: the broker, the environment and the app key.
The library: broker-neutral types (core), and the modules below.
Venue
A trading venue: an exchange, or the combined view across exchanges.
Kiwoom credentials (app key and secret key), demo or real account, the market-data tape,...
void from_json_impl(const std::string &filename)
Reads the credentials from a JSON file; called by from_json(filename). Nothing changes if it throws.
static bool demo_from_json(const json::object &doc, const std::string &origin)
Reads a credential document's "demo".
static constexpr std::string_view secret_field() noexcept
The JSON field Kiwoom expects the application secret under.
const char * rest_port() const noexcept
The REST port, which is the same for both environments.
const char * ws_host() const noexcept
The WebSocket host for the selected environment.
KiwoomPositionScope holdings_position_scope_
Your assertion of what holdings_venue_ means for the positions listed. No default: until it is set,...
bool is_demo_
Whether to use Kiwoom's demo (모의투자) endpoints; true unless stated otherwise.
static constexpr std::string_view key_field() noexcept
The JSON field Kiwoom expects the application key under.
MarketTape market_tape_
Which venue's prices every Kiwoom market-data request reads (quotes, depth, trades,...
std::string tape_symbol(std::string_view symbol) const
Returns a symbol with this configuration's tape suffix, for a market-data request.
KiwoomSellableScope holdings_sellable_scope_
Your assertion of what a holdings row's tradable quantity covers; separate from the position scope,...
const char * ws_port() const noexcept
The WebSocket port, which is the same for both environments.
std::string token_identity() const
Names what a token of these credentials may not outlive, for a network::TokenStore.
void from_env_impl()
Reads the credentials from KIWOOM_APP_KEY and KIWOOM_SECRET_KEY, and the environment from KIWOOM_DEMO...
void from_json_text_impl(std::string_view text, const std::string &origin)
Reads the credentials from JSON text in the file's format; called by from_json_text....
static bool demo_from_env()
Reads KIWOOM_DEMO.
const char * rest_host() const noexcept
The REST host for the selected environment.
Venue holdings_venue_
The venue holdings requests (kt00018) name: Venue::KRX or Venue::NXT. No default: holdings requests a...
A Kiwoom REST access token (au10001's token) and its expiry (expires_dt).
std::string token_type_
Kiwoom's token_type (bearer in its example).
static constexpr std::string_view token_field() noexcept
The field Kiwoom returns the bearer token under.
void from_json_impl(const json::object &doc)
Reads the expiry (expires_dt, the only one Kiwoom states) and token_type; called by from_json....
An OAuth access token and its expiry, the base of each broker's token-response class.
std::chrono::system_clock::time_point expires_at_