algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
example-kis-place-order.cpp
1// Constructs and prints one representative KIS order without creating a
2// client or performing network I/O. This example is always a dry-run.
3#include <algotradelib/kis>
4
5#include <exception>
6#include <iostream>
7#include <stdexcept>
8#include <string>
9#include <string_view>
10
11namespace atl = algotradelib;
12
13namespace {
14
16atl::OrderSide parse_order_side(std::string_view side) {
17 if (side == "BUY") {
18 return atl::OrderSide::BUY;
19 }
20 if (side == "SELL") {
21 return atl::OrderSide::SELL;
22 }
23 throw std::invalid_argument("side must be BUY or SELL");
24}
25
27atl::OrderType parse_order_type(std::string_view type) {
28 if (type == "LIMIT") {
30 }
31 if (type == "MARKET") {
33 }
34 throw std::invalid_argument("type must be LIMIT or MARKET");
35}
36
38void print_usage(const char* program) {
39 std::cerr
40 << "usage: " << program << " [symbol] [quantity] [BUY|SELL] "
41 << "[LIMIT|MARKET] [limit-price]\n";
42}
43
44} // namespace
45
47int main(int argc, char* argv[]) {
48 try {
49 if (argc > 6) {
50 throw std::invalid_argument("too many arguments");
51 }
52
55 atl::Country::KR,
56 atl::Venue::COMBINED,
57 argc > 1 ? argv[1] : "005930",
58 };
59 order.quantity_ = atl::Decimal64::parse(argc > 2 ? argv[2] : "1");
60 order.side_ = parse_order_side(argc > 3 ? argv[3] : "BUY");
61 order.type_ = parse_order_type(argc > 4 ? argv[4] : "LIMIT");
62 if (order.type_ == atl::OrderType::LIMIT) {
63 order.price_ = atl::Decimal64::parse(argc > 5 ? argv[5] : "70000");
64 } else if (argc > 5) {
65 throw std::invalid_argument("a MARKET dry-run must not include a limit price");
66 }
67
68 std::cout << "dry-run: this example never sends an order\n";
69 std::cout
70 << (order.side_ == atl::OrderSide::BUY ? "BUY " : "SELL ") << order.quantity_.str() << ' '
71 << order.instrument_.symbol_;
72 if (order.price_) {
73 std::cout << " @ " << order.price_->str();
74 } else {
75 std::cout << " at market";
76 }
77 std::cout << '\n';
78 return 0;
79 } catch (const std::exception& exception) {
80 std::cerr << "example failed: " << exception.what() << '\n';
81 print_usage(argv[0]);
82 return 1;
83 }
84}
std::string str() const
Formats the value as a decimal string.
Definition types.hpp:557
static Decimal64 parse(std::string_view input)
Parses a decimal string exactly (no binary floating point).
Definition types.hpp:468
The library: broker-neutral types (core), and the modules below.
OrderType
How an order's price is decided (its price basis).
Definition types.hpp:1998
@ LIMIT
지정가: fills at the stated price or better.
Definition types.hpp:2001
@ MARKET
시장가: fills at whatever the book offers.
Definition types.hpp:2003
OrderSide
The side of an order: its economic direction, buy or sell.
Definition types.hpp:1974
Identifies an instrument: its country, venue and symbol.
Definition types.hpp:85
std::string symbol_
The symbol as the venue spells it, such as "005930".
Definition types.hpp:95
An order to place, in broker-neutral form.
Definition types.hpp:2216
Decimal64 quantity_
The quantity, in the instrument's units (shares).
Definition types.hpp:2241
OrderType type_
The price basis: limit, market, best-limit, and so on.
Definition types.hpp:2222
InstrumentId instrument_
The instrument; its venue_ is identity, and routing is this struct's venue_.
Definition types.hpp:2218
std::optional< Decimal64 > price_
The price; required when carries_caller_price(type_), refused otherwise.
Definition types.hpp:2243
OrderSide side_
Buy or sell.
Definition types.hpp:2220