algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
intent.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algotradelib/core>
4
5#include <optional>
6#include <stdexcept>
7#include <string>
8#include <utility>
9
10namespace algotradelib {
11namespace strategy {
12
22
29class Price {
30public:
39 static std::optional<Price> checked(Decimal64 value, const PriceRule& rule) {
40 const auto aligned = rule.on_tick(value);
41 if (!aligned || !*aligned) {
42 return std::nullopt;
43 }
44 return Price(std::move(value));
45 }
46
57 static std::optional<Price> rounded(const Decimal64& value, const PriceRule& rule, OrderSide side) {
58 auto moved = rule.round_to_tick(value, side);
59 if (!moved) {
60 return std::nullopt;
61 }
62 return Price(std::move(*moved));
63 }
64
69 const Decimal64& value() const noexcept {
70 return value_;
71 }
72
77 std::string str() const {
78 return value_.str();
79 }
80
87 friend bool operator==(const Price& lhs, const Price& rhs) {
88 return lhs.value_ == rhs.value_;
89 }
90
97 friend bool operator!=(const Price& lhs, const Price& rhs) {
98 return !(lhs == rhs);
99 }
100
101private:
102 explicit Price(Decimal64 value) : value_(std::move(value)) {}
103
104 Decimal64 value_;
105};
106
116
122inline constexpr bool size_kind_is_target(SizeKind kind) noexcept {
123 switch (kind) {
125 return true;
128 return false;
129 }
130 return false;
131}
132
139class SizeSpec {
140public:
142 SizeSpec() = default;
143
149 static SizeSpec quantity(Decimal64 amount) {
150 SizeSpec size;
151 size.kind_ = SizeKind::QUANTITY;
152 size.quantity_ = std::move(amount);
153 return size;
154 }
155
161 static SizeSpec notional(Money amount) {
162 SizeSpec size;
163 size.kind_ = SizeKind::NOTIONAL;
164 size.notional_ = std::move(amount);
165 return size;
166 }
167
190 SizeSpec size;
191 size.kind_ = SizeKind::TARGET_WEIGHT;
192 size.weight_ = std::move(fraction);
193 return size;
194 }
195
200 SizeKind kind() const noexcept {
201 return kind_;
202 }
203
209 const Decimal64& as_quantity() const {
210 require(SizeKind::QUANTITY, "quantity");
211 return quantity_;
212 }
213
219 const Money& as_notional() const {
220 require(SizeKind::NOTIONAL, "notional");
221 return notional_;
222 }
223
230 require(SizeKind::TARGET_WEIGHT, "target weight");
231 return weight_;
232 }
233
234private:
235 void require(SizeKind wanted, const char* name) const {
236 if (kind_ != wanted) {
237 throw std::logic_error(std::string("order size is not a ") + name);
238 }
239 }
240
242 Decimal64 quantity_;
243 Money notional_;
244 Decimal64 weight_;
245};
246
253class OrderIntent {
254public:
265 static OrderIntent limit(
267 ) {
268 OrderIntent intent;
269 intent.instrument_ = std::move(instrument);
270 intent.side_ = side;
271 intent.type_ = OrderType::LIMIT;
272 intent.time_in_force_ = time_in_force;
273 intent.size_ = std::move(size);
274 intent.limit_ = std::move(limit);
275 return intent;
276 }
277
294 static OrderIntent stop_limit(
297 ) {
298 OrderIntent intent;
299 intent.instrument_ = std::move(instrument);
300 intent.side_ = side;
301 intent.type_ = OrderType::STOP_LIMIT;
302 intent.time_in_force_ = time_in_force;
303 intent.size_ = std::move(size);
304 intent.limit_ = std::move(limit);
305 intent.trigger_ = std::move(trigger);
306 return intent;
307 }
308
318 static OrderIntent market(
320 ) {
321 OrderIntent intent;
322 intent.instrument_ = std::move(instrument);
323 intent.side_ = side;
324 intent.type_ = OrderType::MARKET;
325 intent.time_in_force_ = time_in_force;
326 intent.size_ = std::move(size);
327 return intent;
328 }
329
336 OrderIntent with_tag(std::string tag) const {
337 OrderIntent copy = *this;
338 copy.tag_ = std::move(tag);
339 return copy;
340 }
341
351 OrderIntent with_client_order_id(std::string key) const {
352 OrderIntent copy = *this;
353 copy.client_order_id_ = std::move(key);
354 return copy;
355 }
356
361 const InstrumentId& instrument() const noexcept {
362 return instrument_;
363 }
364
369 OrderSide side() const noexcept {
370 return side_;
371 }
372
377 OrderType type() const noexcept {
378 return type_;
379 }
380
385 TimeInForce time_in_force() const noexcept {
386 return time_in_force_;
387 }
388
393 const SizeSpec& size() const noexcept {
394 return size_;
395 }
396
401 const std::optional<Price>& limit() const noexcept {
402 return limit_;
403 }
404
409 const std::optional<Price>& trigger() const noexcept {
410 return trigger_;
411 }
412
417 const std::string& tag() const noexcept {
418 return tag_;
419 }
420
425 const std::optional<std::string>& client_order_id() const noexcept {
426 return client_order_id_;
427 }
428
429private:
430 OrderIntent() = default;
431
432 InstrumentId instrument_;
433 OrderSide side_{OrderSide::BUY};
435 TimeInForce time_in_force_{TimeInForce::DAY};
436 SizeSpec size_;
437 std::optional<Price> limit_;
438 std::optional<Price> trigger_;
439 std::string tag_;
440 std::optional<std::string> client_order_id_;
441};
442
461
479template <typename Broker>
484 std::string tag_;
485};
486
487} // namespace strategy
488} // namespace algotradelib
An exact signed decimal: an int64 coefficient times 10^-scale.
Definition types.hpp:443
One order a strategy asks the runner to place.
Definition intent.hpp:253
OrderType type() const noexcept
Returns the order type.
Definition intent.hpp:377
OrderIntent with_tag(std::string tag) const
Returns a copy with a tag.
Definition intent.hpp:336
TimeInForce time_in_force() const noexcept
Returns how long the order stays valid.
Definition intent.hpp:385
const std::string & tag() const noexcept
Returns the tag.
Definition intent.hpp:417
const std::optional< std::string > & client_order_id() const noexcept
Returns the idempotency key.
Definition intent.hpp:425
const SizeSpec & size() const noexcept
Returns how much to trade.
Definition intent.hpp:393
const InstrumentId & instrument() const noexcept
Returns the instrument to trade.
Definition intent.hpp:361
OrderSide side() const noexcept
Returns the side.
Definition intent.hpp:369
OrderIntent with_client_order_id(std::string key) const
Returns a copy with a broker idempotency key.
Definition intent.hpp:351
const std::optional< Price > & limit() const noexcept
Returns the limit price.
Definition intent.hpp:401
static OrderIntent stop_limit(InstrumentId instrument, OrderSide side, SizeSpec size, Price trigger, Price limit, TimeInForce time_in_force=TimeInForce::DAY)
Makes a stop-limit order: a limit order the broker releases when the trigger price is reached.
Definition intent.hpp:294
const std::optional< Price > & trigger() const noexcept
Returns the trigger price.
Definition intent.hpp:409
static OrderIntent market(InstrumentId instrument, OrderSide side, SizeSpec size, TimeInForce time_in_force=TimeInForce::DAY)
Makes a market order, which carries no price.
Definition intent.hpp:318
static OrderIntent limit(InstrumentId instrument, OrderSide side, SizeSpec size, Price limit, TimeInForce time_in_force=TimeInForce::DAY)
Makes a limit order.
Definition intent.hpp:265
A price known to be on its instrument's tick grid.
Definition intent.hpp:29
friend bool operator!=(const Price &lhs, const Price &rhs)
Compares two prices for inequality.
Definition intent.hpp:97
const Decimal64 & value() const noexcept
Returns the price as a decimal.
Definition intent.hpp:69
friend bool operator==(const Price &lhs, const Price &rhs)
Compares two prices by exact value (scale included; see Decimal64).
Definition intent.hpp:87
static std::optional< Price > checked(Decimal64 value, const PriceRule &rule)
Makes a price from a value that is already on the grid.
Definition intent.hpp:39
std::string str() const
Returns the price as text, for logs.
Definition intent.hpp:77
static std::optional< Price > rounded(const Decimal64 &value, const PriceRule &rule, OrderSide side)
Makes a price by rounding a value onto the grid, in the order's favour.
Definition intent.hpp:57
How much an OrderIntent trades: a quantity, a notional amount, or a target weight.
Definition intent.hpp:139
static SizeSpec notional(Money amount)
Makes a size as an amount of money.
Definition intent.hpp:161
const Decimal64 & as_target_weight() const
Returns the target weight.
Definition intent.hpp:229
static SizeSpec quantity(Decimal64 amount)
Makes a size in shares or contracts.
Definition intent.hpp:149
const Decimal64 & as_quantity() const
Returns the quantity.
Definition intent.hpp:209
SizeKind kind() const noexcept
Returns how the size is expressed.
Definition intent.hpp:200
const Money & as_notional() const
Returns the notional amount.
Definition intent.hpp:219
static SizeSpec target_weight(Decimal64 fraction)
Makes a size as a fraction of portfolio value to hold.
Definition intent.hpp:189
SizeSpec()=default
Constructs a quantity of zero (reconciling it reports QUANTITY_ROUNDS_TO_ZERO).
What a strategy sees and emits: events, intents, the portfolio, reconciliation and risk limits.
Definition concept.hpp:16
constexpr bool size_kind_is_target(SizeKind kind) noexcept
Tests whether a size states a holding to reach (a target), rather than an amount to trade.
Definition intent.hpp:122
SizeKind
How an order's size is expressed.
Definition intent.hpp:108
@ NOTIONAL
An amount of money; the runner divides it by a price.
Definition intent.hpp:112
@ QUANTITY
A number of shares or contracts.
Definition intent.hpp:110
@ TARGET_WEIGHT
A fraction of portfolio value to hold; the runner trades the difference.
Definition intent.hpp:114
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
@ STOP_LIMIT
스톱지정가: a limit released when a trigger price is reached.
Definition types.hpp:2013
@ MARKET
시장가: fills at whatever the book offers.
Definition types.hpp:2003
TimeInForce
How long an order stays valid and what happens to an unfilled remainder.
Definition types.hpp:2088
@ DAY
Rests until the session ends; an unfilled remainder is cancelled then.
Definition types.hpp:2091
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
An exact amount of money in one currency.
Definition types.hpp:1168
A broker's reference to one order, used to amend or cancel it.
Definition types.hpp:2402
The price grid (tick sizes) an instrument's order prices must sit on.
std::optional< bool > on_tick(const Decimal64 &price) const
Tests whether a price is on the grid.
std::optional< Decimal64 > round_to_tick(const Decimal64 &price, OrderSide side) const
Rounds a price to its band's tick in the order's favour: a buy rounds down, a sell up.
A request to cancel one working order.
Definition intent.hpp:480
OrderRef< Broker > ref_
The order to cancel.
Definition intent.hpp:482
std::string tag_
A label for attribution.
Definition intent.hpp:484
The position a strategy wants to hold in one instrument.
Definition intent.hpp:451
InstrumentId instrument_
The instrument.
Definition intent.hpp:453
std::string tag_
A label carried to the orders and their fills.
Definition intent.hpp:459
Decimal64 target_quantity_
The position to hold, signed: negative is short, and zero means flat.
Definition intent.hpp:455
std::optional< Price > limit_
The limit price for the orders the runner places; without one they are market orders.
Definition intent.hpp:457