4#include "./portfolio.hpp"
6#include <algotradelib/core>
104 return "no_reference_data";
106 return "unknown_price_grid";
108 return "no_price_available";
110 return "no_portfolio_value";
112 return "quantity_rounds_to_zero";
114 return "not_representable";
116 return "no_permitted_price";
118 return "broker_refused";
120 return "binding_refused";
122 return "trigger_off_grid";
124 return "stop_order_resting";
126 return "unsupported_weight";
128 return "negative_size";
200template <
typename Broker>
293namespace reconcile_detail {
297template <
typename Broker>
300 WorkingRowsOf(
const Portfolio<Broker>& portfolio,
const PortfolioRows* rows,
const InstrumentId& instrument)
301 : working_(&portfolio.working_), instrument_(&instrument) {
302 if (rows !=
nullptr) {
303 const auto* found = rows->of(instrument);
304 places_ = found !=
nullptr ? &found->working_ : &none_;
310 const WorkingOrder<Broker>& operator*()
const {
311 return (*owner_->working_)[at_];
313 iterator& operator++() {
318 friend bool operator!=(
const iterator& lhs,
const iterator& rhs) {
319 return lhs.step_ != rhs.step_;
323 friend class WorkingRowsOf;
324 iterator(
const WorkingRowsOf* owner, std::size_t step) : owner_(owner), step_(step) {
329 if (owner_->places_ !=
nullptr) {
330 if (step_ < owner_->places_->size()) {
331 at_ = (*owner_->places_)[step_];
336 step_ < owner_->working_->size() && !((*owner_->working_)[step_].order_.instrument_ == *owner_->instrument_)
342 const WorkingRowsOf* owner_;
347 iterator begin()
const {
348 return iterator(
this, 0);
350 iterator end()
const {
351 return iterator(
this, places_ !=
nullptr ? places_->size() : working_->size());
355 Decimal64 working_quantity(
OrderSide side)
const {
357 for (
const auto& order : *
this) {
358 portfolio_detail::add_working(total, order, side);
364 const std::vector<WorkingOrder<Broker>>* working_;
365 const InstrumentId* instrument_;
366 const std::vector<std::size_t>* places_{
nullptr};
367 static inline const std::vector<std::size_t> none_{};
371template <
typename Broker>
372std::optional<Decimal64> net_quantity(
373 const Portfolio<Broker>& portfolio,
const PortfolioRows* rows,
const InstrumentId& instrument
375 if (rows ==
nullptr) {
376 return portfolio.net_quantity_of(instrument);
379 if (
const auto* found = rows->of(instrument)) {
380 for (
const auto at : found->positions_) {
381 if (!portfolio_detail::add_net(total, portfolio.positions_[at])) {
390inline std::optional<Decimal64> lot_of(
const Instrument& instrument) {
391 if (!instrument.lot_size_) {
394 if (instrument.lot_size_->coefficient() <= 0) {
397 return instrument.lot_size_;
412inline std::optional<Decimal64> round_to_lot(
413 const Decimal64& quantity,
const std::optional<Decimal64>& lot,
bool& adjusted
423 adjusted = !(*rounded == quantity);
438inline std::optional<Decimal64> clamp_to_limits(
439 const Decimal64& price,
OrderSide side,
const std::optional<PriceLimits>& limits,
bool& adjusted
445 if (side == OrderSide::BUY && limits->upper_ &&
decimal_less(*limits->upper_, price)) {
447 return limits->upper_;
449 if (side == OrderSide::SELL && limits->lower_ &&
decimal_less(price, *limits->lower_)) {
451 return limits->lower_;
464inline std::optional<Decimal64> prepare_price(
465 const Decimal64& wanted,
OrderSide side,
const PriceRule& rule,
const std::optional<PriceLimits>& limits,
466 bool& clamped,
bool& rounded
468 const auto within = clamp_to_limits(wanted, side, limits, clamped);
472 const auto on_grid = rule.round_to_tick(*within, side);
476 rounded = !(*on_grid == *within);
487inline bool rounded_out_of_band(
488 const Decimal64& wanted,
const Decimal64& priced,
OrderSide side,
const std::optional<PriceLimits>& limits
493 if (side == OrderSide::BUY) {
506inline bool below_tolerance(
const Decimal64& drift,
const Decimal64& already_working,
const ReconcileInputs& inputs) {
507 const auto& tolerance = inputs.tolerance_;
509 if (tolerance.minimum_quantity_ &&
decimal_less(drift, *tolerance.minimum_quantity_)) {
513 if (tolerance.minimum_notional_ && inputs.reference_price_) {
515 if (value &&
decimal_less(*value, *tolerance.minimum_notional_)) {
522 if (tolerance.minimum_fraction_of_working_ && already_working.coefficient() > 0) {
525 if (fraction &&
decimal_less(*fraction, *tolerance.minimum_fraction_of_working_)) {
572template <
typename Broker>
590 const auto net = reconcile_detail::net_quantity(portfolio, inputs.
rows_, target.
instrument_);
592 const reconcile_detail::WorkingRowsOf<Broker> rows(portfolio, inputs.
rows_, target.
instrument_);
598 const auto held = *net;
608 const auto magnitude = buying ? std::optional<Decimal64>(*needed) :
decimal_subtract(zero, *needed);
619 bool stop_resting =
false;
620 for (
const auto& working : rows) {
623 is_stop(working.order_)
629 const bool met = !buying && !selling;
630 for (
const auto& working : rows) {
634 if (is_stop(working.order_)) {
638 plan.
cancel_.push_back(working.ref_);
642 plan.
cancel_.push_back(working.ref_);
656 for (
const auto& working : rows) {
660 const bool opposes = (buying && working.order_.side_ == OrderSide::SELL) ||
661 (selling && working.order_.side_ == OrderSide::BUY) || (!buying && !selling);
663 plan.
cancel_.push_back(working.ref_);
666 if (!buying && !selling) {
670 const OrderSide side = buying ? OrderSide::BUY : OrderSide::SELL;
671 const auto already = rows.working_quantity(side);
694 if (drift && reconcile_detail::below_tolerance(*drift, already, inputs)) {
706 for (
const auto& working : rows) {
708 working.order_.instrument_ == target.
instrument_ && working.order_.side_ == side &&
711 plan.
cancel_.push_back(working.ref_);
714 const auto left =
decimal_subtract(working.order_.quantity_, working.order_.execution_.filled_quantity_);
736 if (remaining->coefficient() == 0) {
746 const auto lot = reconcile_detail::lot_of(*inputs.
instrument_);
747 const auto sized = reconcile_detail::round_to_lot(*remaining, lot, order.
quantity_rounded_);
751 if (sized->coefficient() == 0) {
759 const auto priced = reconcile_detail::prepare_price(
766 if (reconcile_detail::rounded_out_of_band(target.
limit_->value(), *priced, side, inputs.
limits_)) {
775 plan.
place_.push_back(std::move(order));
795template <
typename Broker>
810 const auto sizing_price =
826 if (!sizing_price || sizing_price->coefficient() <= 0) {
845 if (!sizing_price || sizing_price->coefficient() <= 0) {
852 const auto shares =
decimal_divide(*allocation, *sizing_price, 0);
868 const auto lot = reconcile_detail::lot_of(*inputs.
instrument_);
869 const auto sized = reconcile_detail::round_to_lot(wanted, lot, order.
quantity_rounded_);
873 if (sized->coefficient() == 0) {
878 if (intent.
limit()) {
879 const auto priced = reconcile_detail::prepare_price(
886 if (reconcile_detail::rounded_out_of_band(intent.
limit()->value(), *priced, intent.
side(), inputs.
limits_)) {
909 plan.
place_.push_back(std::move(order));
962 if (!price || price->coefficient() <= 0) {
966 const auto shares = allocation ?
decimal_divide(*allocation, *price, 0) : std::nullopt;
967 bool adjusted =
false;
968 const auto wanted = shares ?
969 reconcile_detail::round_to_lot(*shares, reconcile_detail::lot_of(*inputs.
instrument_), adjusted) :
979 out.
target_ = std::move(target);
An exact signed decimal: an int64 coefficient times 10^-scale.
std::int64_t coefficient() const noexcept
Returns the coefficient.
Decimal64()=default
Constructs zero.
static Decimal64 parse(std::string_view input)
Parses a decimal string exactly (no binary floating point).
One order a strategy asks the runner to place.
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.
const std::optional< Price > & trigger() const noexcept
Returns the trigger price.
static OrderIntent limit(InstrumentId instrument, OrderSide side, SizeSpec size, Price limit, TimeInForce time_in_force=TimeInForce::DAY)
Makes a limit order.
Where each instrument's rows are in one portfolio: its position rows and its working rows,...
const Decimal64 & as_target_weight() const
Returns the target weight.
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.
What a strategy asks the runner for: orders (OrderIntent), target positions (TargetPosition) and canc...
What a strategy sees and emits: events, intents, the portfolio, reconciliation and risk limits.
constexpr std::string_view reject_reason_name(RejectReason reason) noexcept
Returns a lowercase name for a reason, such as "no_reference_data", for logs.
bool order_is_open(OrderStatus status)
Tests whether an order in a given status may still trade.
WeightTarget weight_target(const OrderIntent &intent, const ReconcileInputs &inputs)
Converts a weight-sized intent into the target position it states.
RejectReason
Why an instruction was not placed.
@ BROKER_REFUSED
The broker refused the instruction (a request was sent).
@ QUANTITY_ROUNDS_TO_ZERO
The quantity rounded down to zero on the lot.
@ NO_REFERENCE_DATA
No reference data for the instrument, so neither the tick grid nor the lot is known.
@ NOT_REPRESENTABLE
The decimal arithmetic did not fit, or the portfolio's rows did not add up.
@ STOP_ORDER_RESTING
A target needs a trade while a stop order rests on the instrument; the stop is cancelled first and no...
@ TRIGGER_OFF_GRID
A stop-limit order's trigger price is not on the instrument's tick grid.
@ UNSUPPORTED_WEIGHT
A weight-sized intent with something a holding cannot express: a sell, a negative weight,...
@ NO_PRICE_AVAILABLE
A price was needed to size the order and none was available.
@ BINDING_REFUSED
The binding refused to send the instruction, so no request was made.
@ NEGATIVE_SIZE
An intent's size is negative: a quantity or a notional amount below zero.
@ NO_PERMITTED_PRICE
Rounding the limit onto the grid, in the cautious direction, would move it out of the day's band: no ...
@ UNKNOWN_PRICE_GRID
The instrument's tick grid is unknown, so a limit price cannot be made valid.
@ NO_PORTFOLIO_VALUE
A weight needed the portfolio's value, and none (or a non-positive one, or one in another currency) w...
bool is_stop_order(const OrderSnapshot &order)
Tests whether a working order is a stop order: a trigger-price type, or one carrying a trigger price.
@ 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.
Plan< Broker > reconcile(const TargetPosition &target, const Portfolio< Broker > &portfolio, const ReconcileInputs &inputs)
Plans the cancellations and the order that move a holding to a target position.
The library: broker-neutral types (core), and the modules below.
std::optional< Decimal64 > decimal_floor_to_multiple(const Decimal64 &value, const Decimal64 &step)
Rounds down to a multiple of a step (toward negative infinity).
std::optional< Decimal64 > decimal_add(const Decimal64 &lhs, const Decimal64 &rhs)
Adds two decimals exactly.
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.
constexpr bool carries_trigger_price(OrderType type) noexcept
Tests whether an order type takes a trigger price.
constexpr bool order_status_cancel_pending(OrderStatus status) noexcept
Tests whether a cancellation of an order in this status is already on its way, so asking again can on...
OrderSession
The trading session an order is submitted into.
@ REGULAR
The continuous regular session.
std::optional< Decimal64 > decimal_subtract(const Decimal64 &lhs, const Decimal64 &rhs)
Subtracts two decimals exactly.
std::optional< Decimal64 > decimal_divide(const Decimal64 &lhs, const Decimal64 &rhs, std::uint8_t scale)
Divides two decimals, truncating toward zero at a given scale.
Venue
A trading venue: an exchange, or the combined view across exchanges.
bool decimal_less(const Decimal64 &lhs, const Decimal64 &rhs)
Compares two decimals numerically, whatever their scales.
std::optional< Decimal64 > decimal_multiply(const Decimal64 &lhs, const Decimal64 &rhs)
Multiplies two decimals exactly; the scales add.
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.
Reference data for one instrument.
Currency currency_
The currency it trades in.
PriceRule price_rule_
The price grid, set by the adapter or the caller; kind UNKNOWN where it has no table.
Decimal64 amount_
The amount.
The state of one order, as a broker listing reported it.
OrderType type_
The price basis; UNKNOWN if the broker's code was not recognized.
std::optional< Decimal64 > trigger_price_
The trigger price, whenever the broker reports a positive one.
std::optional< bool > on_tick(const Decimal64 &price) const
Tests whether a price is on the grid.
What to send: first every cancellation, then every placement.
bool empty() const noexcept
Tests whether the plan is empty.
std::vector< PlannedOrder > place_
The orders to place, after the cancellations.
std::vector< Rejection > rejected_
The instructions not placed, with reasons.
std::vector< OrderRef< Broker > > cancel_
The orders to cancel, sent first.
One order the runner is to place.
TimeInForce time_in_force_
How long the order stays valid.
bool price_clamped_
Whether the price was clamped to the day's band (an aggressive limit beyond it).
OrderSession session_
The trading session.
std::optional< std::string > client_order_id_
The idempotency key, if the intent carried one.
Venue venue_
Where to route the order; see OrderRequest::venue_.
bool price_would_have_been_clamped_
Whether a price band was known but not applied (too old), and would have clamped this price.
std::string tag_
The tag of the intent or target the order came from.
bool quantity_rounded_
Whether the quantity was rounded down onto the lot.
bool price_rounded_
Whether the price was rounded onto the tick grid.
std::optional< Decimal64 > trigger_price_
The trigger price of a STOP_LIMIT order, exactly as stated; empty otherwise.
OrderType type_
The order type.
InstrumentId instrument_
The instrument to trade.
std::optional< Decimal64 > price_
The limit price, after clamping and rounding; empty for a market order.
Decimal64 quantity_
The quantity, after rounding onto the lot.
What the account holds and which orders are still working, as a strategy sees it.
One instruction that was not placed, and why.
InstrumentId instrument_
The instrument it was for.
RejectReason reason_
Why it was not placed.
std::string tag_
The tag of the intent or target it came from.
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.
How small a difference from a target is not worth an order.
std::optional< Decimal64 > minimum_notional_
The smallest drift worth trading, valued at the reference price.
bool stated() const noexcept
Tests whether any floor is set.
std::optional< Decimal64 > minimum_quantity_
The smallest drift worth trading, in shares.
std::optional< Decimal64 > minimum_fraction_of_working_
The smallest drift worth trading, as a fraction of the quantity already working (0....
The result of weight_target: a target position or a refusal, exactly one of them.
std::optional< TargetPosition > target_
The target position, if the weight could be sized.
std::optional< RejectReason > refused_
Why it could not be, otherwise.