algotradelib
0.0.0.9000
C++ header-only library for algorithmic trading
Toggle main menu visibility
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
11
namespace
atl
=
algotradelib
;
12
13
namespace
{
14
16
atl::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
27
atl::OrderType
parse_order_type(std::string_view type) {
28
if
(type ==
"LIMIT"
) {
29
return
atl::OrderType::LIMIT
;
30
}
31
if
(type ==
"MARKET"
) {
32
return
atl::OrderType::MARKET
;
33
}
34
throw
std::invalid_argument(
"type must be LIMIT or MARKET"
);
35
}
36
38
void
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
46
int
main(
int
argc,
char
* argv[]) {
47
try
{
48
if
(argc > 6) {
49
throw
std::invalid_argument(
"too many arguments"
);
50
}
51
52
atl::OrderRequest
order;
53
order.
instrument_
=
atl::InstrumentId
{
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
}
algotradelib::Decimal64::str
std::string str() const
Formats the value as a decimal string.
Definition
types.hpp:411
algotradelib::Decimal64::parse
static Decimal64 parse(std::string_view input)
Parses a decimal string exactly (no binary floating point).
Definition
types.hpp:323
algotradelib
The library: broker-neutral types (core), and the modules below.
Definition
conversion.hpp:13
algotradelib::OrderType
OrderType
How an order's price is decided (its price basis).
Definition
types.hpp:1707
algotradelib::OrderType::LIMIT
@ LIMIT
지정가: fills at the stated price or better.
Definition
types.hpp:1710
algotradelib::OrderType::MARKET
@ MARKET
시장가: fills at whatever the book offers.
Definition
types.hpp:1712
algotradelib::OrderSide
OrderSide
The side of an order: its economic direction, buy or sell.
Definition
types.hpp:1683
algotradelib::InstrumentId
Identifies an instrument: its country, venue and symbol.
Definition
types.hpp:78
algotradelib::InstrumentId::symbol_
std::string symbol_
The symbol as the venue spells it, such as "005930".
Definition
types.hpp:88
algotradelib::OrderRequest
An order to place, in broker-neutral form.
Definition
types.hpp:1925
algotradelib::OrderRequest::quantity_
Decimal64 quantity_
The quantity, in the instrument's units (shares).
Definition
types.hpp:1950
algotradelib::OrderRequest::type_
OrderType type_
The price basis: limit, market, best-limit, and so on.
Definition
types.hpp:1931
algotradelib::OrderRequest::instrument_
InstrumentId instrument_
The instrument; its venue_ is identity, and routing is this struct's venue_.
Definition
types.hpp:1927
algotradelib::OrderRequest::price_
std::optional< Decimal64 > price_
The price; required when carries_caller_price(type_), refused otherwise.
Definition
types.hpp:1952
algotradelib::OrderRequest::side_
OrderSide side_
Buy or sell.
Definition
types.hpp:1929
examples
example-kis-place-order.cpp
Generated by
1.18.0