algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
errors.hpp
1#pragma once
2
3#include "../../core/types.hpp"
4
5#include <boost/json.hpp>
6
7#include <optional>
8#include <string>
9#include <string_view>
10
11namespace algotradelib::kiwoom {
12
27
33inline KiwoomRateLimitLayer kiwoom_rate_limit_layer(std::string_view code) noexcept {
34 if (code == "1700") {
36 }
37 if (code == "1701") {
39 }
40 if (code == "1702") {
42 }
44}
45
62inline ErrorCategory kiwoom_error_category(std::string_view code) noexcept {
63 // Header-level authentication faults: the authorization field is missing or malformed.
64 if (code == "1512" || code == "1513" || code == "1514" || code == "1515" || code == "1516") {
66 }
67 // Availability, not authentication. Checked before the 80xx sweep below, which it sits inside.
68 if (code == "8104" || code == "8200") {
70 }
71 // The key, the token, the terminal, or the registered IP.
72 if (code.size() == 4 && code.front() == '8') {
74 }
77 }
78 // 재귀 호출이 발생하여 API 호출을 제한합니다: a throttle, stated as one.
79 if (code == "1687") {
81 }
82 if (
83 code == "1501" || code == "1504" || code == "1505" || code == "1511" || code == "1517" || code == "1901" ||
84 code == "1902" || code == "1903"
85 ) {
87 }
89}
90
99inline std::optional<BrokerError> kiwoom_declared_error(const boost::json::object& document) {
100 const auto code = document.if_contains("return_code");
101 if (!code) {
102 return std::nullopt;
103 }
104 std::string text;
105 if (code->is_int64()) {
106 text = std::to_string(code->as_int64());
107 } else if (code->is_uint64()) {
108 text = std::to_string(code->as_uint64());
109 } else if (code->is_string()) {
110 text = std::string(code->as_string());
111 } else {
112 BrokerError error;
114 error.broker_code_ = "invalid_return_code";
115 error.message_ = "return_code is neither a number nor a string";
116 return error;
117 }
118 if (text == "0") {
119 return std::nullopt;
120 }
121
122 BrokerError error;
123 error.broker_code_ = text;
124 error.reached_broker_ = true;
125 if (const auto message = document.if_contains("return_msg"); message && message->is_string()) {
126 error.message_ = std::string(message->as_string());
127 } else {
128 error.message_ = "Kiwoom declared return_code " + text;
129 }
130 error.category_ = kiwoom_error_category(text);
131 return error;
132}
133
150
162inline std::string_view kiwoom_refusal_code(std::string_view message) noexcept {
163 constexpr std::string_view prefix = "[2000](";
164 if (message.size() < prefix.size() || message.substr(0, prefix.size()) != prefix || message.back() != ')') {
165 return {};
166 }
167 const auto inner = message.substr(prefix.size(), message.size() - prefix.size() - 1);
168 const auto colon = inner.find(':');
169 if (colon == std::string_view::npos || colon == 0 || colon + 1 == inner.size()) {
170 return {};
171 }
172 const auto code = inner.substr(0, colon);
173 for (const char character : code) {
174 if (!((character >= 'A' && character <= 'Z') || (character >= '0' && character <= '9'))) {
175 return {};
176 }
177 }
178 return code;
179}
180
206inline std::optional<BrokerError> kiwoom_recognized_refusal(
207 int http_status, const boost::json::object& document, KiwoomOrderOperation operation, bool demo
208) {
209 if (http_status != 200) {
210 return std::nullopt;
211 }
212 const auto code = document.if_contains("return_code");
213 const bool generic = code &&
214 ((code->is_int64() && code->as_int64() == 20) || (code->is_uint64() && code->as_uint64() == 20) ||
215 (code->is_string() && code->as_string() == "20"));
216 const auto message = document.if_contains("return_msg");
217 if (!generic || !message || !message->is_string()) {
218 return std::nullopt;
219 }
220 const std::string stated(kiwoom_refusal_code(std::string_view(message->as_string())));
221 const char* name = "";
222 const bool placement = operation == KiwoomOrderOperation::PLACE &&
223 (demo ? (stated == "800033" || stated == "RC4057" || stated == "RC4058") : stated == "571445");
224 const bool cancellation =
225 operation == KiwoomOrderOperation::CANCEL && (demo ? stated == "RC4033" : stated == "506550");
226 if (placement) {
227 name = "placement";
228 } else if (cancellation) {
229 name = "cancellation";
230 } else {
231 return std::nullopt;
232 }
233 if (
234 const auto number = document.if_contains("ord_no"); number && !(number->is_string() && number->as_string().empty())
235 ) {
236 return std::nullopt;
237 }
238 for (const char* field : {"cncl_qty", "mdfy_qty"}) {
239 const auto quantity = document.if_contains(field);
240 if (!quantity) {
241 continue;
242 }
243 if (
244 !quantity->is_string() || std::string_view(quantity->as_string()).find_first_not_of('0') != std::string_view::npos
245 ) {
246 return std::nullopt;
247 }
248 }
249 BrokerError error;
251 error.reached_broker_ = true;
252 error.http_status_ = http_status;
253 error.broker_code_ = stated;
254 error.message_ = std::string(message->as_string()) + " (return_code 20; recognized refusal: Kiwoom " +
255 (demo ? "demo" : "real") + ", cash " + name + ", " + stated + ")";
256 return error;
257}
258
259} // namespace algotradelib::kiwoom
The Kiwoom Securities REST API client.
Definition auth.hpp:12
KiwoomRateLimitLayer kiwoom_rate_limit_layer(std::string_view code) noexcept
Classifies a Kiwoom return code as a rate-limit refusal.
Definition errors.hpp:33
std::optional< BrokerError > kiwoom_declared_error(const boost::json::object &document)
Reads the failure a Kiwoom response declares in its body (Kiwoom reports business failures with HTTP ...
Definition errors.hpp:99
KiwoomRateLimitLayer
Which of Kiwoom's three rate limits a refusal reports (per API, account-wide, or per API group); back...
Definition errors.hpp:17
@ ENDPOINT
1700: this API's own allowance.
Definition errors.hpp:21
@ TOTAL
1701: the total allowance across every API.
Definition errors.hpp:23
@ GROUP
1702: the allowance of the API group this endpoint belongs to.
Definition errors.hpp:25
KiwoomOrderOperation
Which order instruction a reply answers.
Definition errors.hpp:140
@ CANCEL
A cancellation (kt10003).
Definition errors.hpp:148
@ UNCLASSIFIED
Not an order request, or one this client does not classify.
Definition errors.hpp:142
@ PLACE
A placement (kt10000, kt10001).
Definition errors.hpp:144
ErrorCategory kiwoom_error_category(std::string_view code) noexcept
Classifies a Kiwoom return_code.
Definition errors.hpp:62
std::string_view kiwoom_refusal_code(std::string_view message) noexcept
Reads the code inside an order refusal's return_msg, which Kiwoom writes as [2000](CODE:text) under t...
Definition errors.hpp:162
std::optional< BrokerError > kiwoom_recognized_refusal(int http_status, const boost::json::object &document, KiwoomOrderOperation operation, bool demo)
Reads the refusal a cash-order reply states, for the replies this client recognizes as one.
Definition errors.hpp:206
ErrorCategory
The kind of failure a BrokerError reports.
Definition types.hpp:948
@ RATE_LIMIT
The broker refused the request for its rate limit.
Definition types.hpp:958
@ PROTOCOL
The reply could not be read, or the broker declared a failure this client does not classify further: ...
Definition types.hpp:965
@ AUTHENTICATION
The credentials or token were refused.
Definition types.hpp:956
@ INVALID_ARGUMENT
The request was invalid, refused by the client before sending or by the broker.
Definition types.hpp:974
The error a broker or transport operation returns.
Definition types.hpp:1003
int http_status_
The HTTP status of the reply: set for an HTTP error and for a reply whose body is not a JSON object....
Definition types.hpp:1020
std::string message_
A human-readable description, for logs; do not parse it.
Definition types.hpp:1033
ErrorCategory category_
The kind of failure; the field to branch on.
Definition types.hpp:1005
std::string broker_code_
The broker's own error code, verbatim (for example KIS's EGW00201).
Definition types.hpp:1022
bool reached_broker_
Whether a request was sent before this error.
Definition types.hpp:1014