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 << "usage: " << program << " [symbol] [quantity] [BUY|SELL] "
40 << "[LIMIT|MARKET] [limit-price]\n";
41}
42
43} // namespace
44
46int main(int argc, char* argv[]) {
47 try {
48 if (argc > 6) {
49 throw std::invalid_argument("too many arguments");
50 }
51
54 atl::Country::KR,
55 atl::Venue::COMBINED,
56 argc > 1 ? argv[1] : "005930",
57 };
58 order.quantity_ = atl::Decimal64::parse(argc > 2 ? argv[2] : "1");
59 order.side_ = parse_order_side(argc > 3 ? argv[3] : "BUY");
60 order.type_ = parse_order_type(argc > 4 ? argv[4] : "LIMIT");
61 if (order.type_ == atl::OrderType::LIMIT) {
62 order.price_ = atl::Decimal64::parse(argc > 5 ? argv[5] : "70000");
63 } else if (argc > 5) {
64 throw std::invalid_argument("a MARKET dry-run must not include a limit price");
65 }
66
67 std::cout << "dry-run: this example never sends an order\n";
68 std::cout << (order.side_ == atl::OrderSide::BUY ? "BUY " : "SELL ")
69 << order.quantity_.str() << ' ' << order.instrument_.symbol_;
70 if (order.price_) {
71 std::cout << " @ " << order.price_->str();
72 } else {
73 std::cout << " at market";
74 }
75 std::cout << '\n';
76 return 0;
77 } catch (const std::exception& exception) {
78 std::cerr << "example failed: " << exception.what() << '\n';
79 print_usage(argv[0]);
80 return 1;
81 }
82}
std::string str() const
Formats the value as a decimal string.
Definition types.hpp:411
static Decimal64 parse(std::string_view input)
Parses a decimal string exactly (no binary floating point).
Definition types.hpp:323
The library: broker-neutral types (core), and the modules below.
OrderType
How an order's price is decided (its price basis).
Definition types.hpp:1707
@ LIMIT
지정가: fills at the stated price or better.
Definition types.hpp:1710
@ MARKET
시장가: fills at whatever the book offers.
Definition types.hpp:1712
OrderSide
The side of an order: its economic direction, buy or sell.
Definition types.hpp:1683
Identifies an instrument: its country, venue and symbol.
Definition types.hpp:78
std::string symbol_
The symbol as the venue spells it, such as "005930".
Definition types.hpp:88
An order to place, in broker-neutral form.
Definition types.hpp:1925
Decimal64 quantity_
The quantity, in the instrument's units (shares).
Definition types.hpp:1950
OrderType type_
The price basis: limit, market, best-limit, and so on.
Definition types.hpp:1931
InstrumentId instrument_
The instrument; its venue_ is identity, and routing is this struct's venue_.
Definition types.hpp:1927
std::optional< Decimal64 > price_
The price; required when carries_caller_price(type_), refused otherwise.
Definition types.hpp:1952
OrderSide side_
Buy or sell.
Definition types.hpp:1929