3#include <algotradelib/core>
41 if (!aligned || !*aligned) {
44 return Price(std::move(
value));
66 return Price(std::move(*moved));
79 std::string
str()
const {
return value_.str(); }
87 friend bool operator==(
const Price& lhs,
const Price& rhs) {
88 return lhs.value_ == rhs.value_;
97 friend bool operator!=(
const Price& lhs,
const Price& rhs) {
return !(lhs == rhs); }
150 size.quantity_ = std::move(amount);
162 size.notional_ = std::move(amount);
190 size.weight_ = std::move(fraction);
231 void require(
SizeKind wanted,
const char* name)
const {
232 if (kind_ != wanted) {
233 throw std::logic_error(std::string(
"order size is not a ") + name);
273 intent.size_ = std::move(
size);
274 intent.limit_ = std::move(
limit);
307 intent.size_ = std::move(
size);
308 intent.limit_ = std::move(
limit);
309 intent.trigger_ = std::move(
trigger);
333 intent.size_ = std::move(
size);
344 OrderIntent copy = *
this;
345 copy.tag_ = std::move(
tag);
359 OrderIntent copy = *
this;
360 copy.client_order_id_ = std::move(key);
398 const std::optional<Price>&
limit() const noexcept {
return limit_; }
404 const std::optional<Price>&
trigger() const noexcept {
return trigger_; }
410 const std::string&
tag() const noexcept {
return tag_; }
417 return client_order_id_;
428 std::optional<Price> limit_;
429 std::optional<Price> trigger_;
431 std::optional<std::string> client_order_id_;
470template <
typename Broker>
An exact signed decimal: an int64 coefficient times 10^-scale.
One order a strategy asks the runner to place.
OrderType type() const noexcept
Returns the order type.
OrderIntent with_tag(std::string tag) const
Returns a copy with a tag.
TimeInForce time_in_force() const noexcept
Returns how long the order stays valid.
const std::string & tag() const noexcept
Returns the tag.
const std::optional< std::string > & client_order_id() const noexcept
Returns the idempotency key.
const SizeSpec & size() const noexcept
Returns how much to trade.
const InstrumentId & instrument() const noexcept
Returns the instrument to trade.
OrderSide side() const noexcept
Returns the side.
OrderIntent with_client_order_id(std::string key) const
Returns a copy with a broker idempotency key.
const std::optional< Price > & limit() const noexcept
Returns the limit price.
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.
const std::optional< Price > & trigger() const noexcept
Returns the trigger price.
static OrderIntent market(InstrumentId instrument, OrderSide side, SizeSpec size, TimeInForce time_in_force=TimeInForce::DAY)
Makes a market order, which carries no price.
static OrderIntent limit(InstrumentId instrument, OrderSide side, SizeSpec size, Price limit, TimeInForce time_in_force=TimeInForce::DAY)
Makes a limit order.
A price known to be on its instrument's tick grid.
friend bool operator!=(const Price &lhs, const Price &rhs)
Compares two prices for inequality.
const Decimal64 & value() const noexcept
Returns the price as a decimal.
friend bool operator==(const Price &lhs, const Price &rhs)
Compares two prices by exact value (scale included; see Decimal64).
static std::optional< Price > checked(Decimal64 value, const PriceRule &rule)
Makes a price from a value that is already on the grid.
std::string str() const
Returns the price as text, for logs.
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.
How much an OrderIntent trades: a quantity, a notional amount, or a target weight.
static SizeSpec notional(Money amount)
Makes a size as an amount of money.
const Decimal64 & as_target_weight() const
Returns the target weight.
static SizeSpec quantity(Decimal64 amount)
Makes a size in shares or contracts.
const Decimal64 & as_quantity() const
Returns the quantity.
SizeKind kind() const noexcept
Returns how the size is expressed.
const Money & as_notional() const
Returns the notional amount.
static SizeSpec target_weight(Decimal64 fraction)
Makes a size as a fraction of portfolio value to hold.
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.
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.
SizeKind
How an order's size is expressed.
@ NOTIONAL
An amount of money; the runner divides it by a price.
@ QUANTITY
A number of shares or contracts.
@ TARGET_WEIGHT
A fraction of portfolio value to hold; the runner trades the difference.
The library: broker-neutral types (core), and the modules below.
OrderType
How an order's price is decided (its price basis).
@ LIMIT
지정가: fills at the stated price or better.
@ STOP_LIMIT
스톱지정가: a limit released when a trigger price is reached.
@ MARKET
시장가: fills at whatever the book offers.
TimeInForce
How long an order stays valid and what happens to an unfilled remainder.
@ DAY
Rests until the session ends; an unfilled remainder is cancelled then.
OrderSide
The side of an order: its economic direction, buy or sell.
Identifies an instrument: its country, venue and symbol.
An exact amount of money in one currency.
A broker's reference to one order, used to amend or cancel it.
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.
OrderRef< Broker > ref_
The order to cancel.
std::string tag_
A label for attribution.
The position a strategy wants to hold in one instrument.
InstrumentId instrument_
The instrument.
std::string tag_
A label carried to the orders and their fills.
Decimal64 target_quantity_
The position to hold, signed: negative is short, and zero means flat.
std::optional< Price > limit_
The limit price for the orders the runner places; without one they are market orders.