3#include <algotradelib/core>
8#include <unordered_map>
24 case OrderStatus::UNKNOWN:
25 case OrderStatus::PENDING:
26 case OrderStatus::PENDING_CANCEL:
27 case OrderStatus::PENDING_REPLACE:
28 case OrderStatus::PARTIAL_FILLED:
30 case OrderStatus::FILLED:
31 case OrderStatus::CANCELED:
32 case OrderStatus::REJECTED:
33 case OrderStatus::CANCEL_REJECTED:
34 case OrderStatus::REPLACE_REJECTED:
35 case OrderStatus::REPLACED:
41namespace portfolio_detail {
55template <
typename Working>
56void add_working(Decimal64& total,
const Working& order,
OrderSide side) {
57 if (order.order_.side_ != side || !
order_is_open(order.order_.status_)) {
60 const auto remaining =
decimal_subtract(order.order_.quantity_, order.order_.execution_.filled_quantity_);
64 if (
const auto sum =
decimal_add(total, *remaining)) {
81template <
typename Broker>
116 std::chrono::system_clock::time_point
as_of_{};
134 const std::chrono::system_clock::time_point epoch{};
175 if (position.
instrument_ == instrument && !portfolio_detail::add_net(total, position)) {
194 for (
const auto& order :
working_) {
195 if (order.order_.instrument_ == instrument) {
196 portfolio_detail::add_working(total, order, side);
266 for (
const auto& amount :
cash_) {
267 if (amount.currency_ == currency) {
268 return amount.amount_;
285 if (amount.cash_.currency_ == currency) {
286 return amount.cash_.amount_;
318 for (
const auto& order :
working_) {
319 if (order.ref_ == ref) {
349 template <
typename Broker>
351 for (std::size_t at = 0; at < portfolio.
positions_.size(); ++at) {
352 rows_[portfolio.
positions_[at].instrument_].positions_.push_back(at);
354 for (std::size_t at = 0; at < portfolio.
working_.size(); ++at) {
355 rows_[portfolio.
working_[at].order_.instrument_].working_.push_back(at);
365 const auto found = rows_.find(instrument);
366 return found == rows_.end() ? nullptr : &found->second;
370 std::unordered_map<InstrumentId, Rows, id_detail::InstrumentIdHash> rows_;
A three-letter currency code, such as KRW or USD (ISO 4217 style).
An exact signed decimal: an int64 coefficient times 10^-scale.
Decimal64()=default
Constructs zero.
PortfolioRows(const Portfolio< Broker > &portfolio)
Indexes a portfolio.
const Rows * of(const InstrumentId &instrument) const
Returns where an instrument's rows are.
What a strategy sees and emits: events, intents, the portfolio, reconciliation and risk limits.
bool order_is_open(OrderStatus status)
Tests whether an order in a given status may still trade.
The library: broker-neutral types (core), and the modules below.
OrderStatus
Where an order stands in its lifecycle.
std::optional< Decimal64 > decimal_add(const Decimal64 &lhs, const Decimal64 &rhs)
Adds two decimals exactly.
std::optional< Decimal64 > decimal_subtract(const Decimal64 &lhs, const Decimal64 &rhs)
Subtracts two decimals exactly.
bool decimal_less(const Decimal64 &lhs, const Decimal64 &rhs)
Compares two decimals numerically, whatever their scales.
OrderSide
The side of an order: its economic direction, buy or sell.
One position row in a holdings snapshot.
std::optional< Decimal64 > sellable_quantity_
How much the broker says may be sold now.
Decimal64 quantity_
The quantity in this row.
InstrumentId instrument_
The instrument held (brokers report holdings without a trading venue).
std::optional< SellableBasis > sellable_basis_
What this row's sellable_quantity_ covers, when the figure came from a per-instrument inquiry rather ...
Identifies an instrument: its country, venue and symbol.
A broker's reference to one order, used to amend or cancel it.
What a sellable quantity covers, and how RiskGuard must treat resting sells against it.
RestingTreatment resting_
How resting sells are treated; unestablished by default.
Coverage coverage_
What the figure covers; unestablished by default.
An order still open to modification or cancellation.
A sellable quantity together with what it covers, read from the same row.
Where one instrument's rows are.
std::vector< std::size_t > positions_
Places in positions_, ascending.
std::vector< std::size_t > working_
Places in working_, ascending.
A sellable quantity together with what it covers, read from the same row.
SellableBasis basis_
What the figure covers; unestablished if the rows disagree.
std::optional< Decimal64 > quantity_
The figure, or std::nullopt if no row of the instrument reported one.
What the account holds and which orders are still working, as a strategy sees it.
std::vector< WorkingOrder< Broker > > working_
Orders still outstanding at the broker.
SellableBasis sellable_basis_
What the sellable_quantity_ figures on positions_ cover.
std::vector< Money > cash_
Cash per currency, in a backtest only.
std::optional< Decimal64 > buying_power_in(const Currency ¤cy) const
Returns the buying power in a currency.
std::optional< Decimal64 > net_quantity_of(const InstrumentId &instrument) const
Returns the net quantity held of an instrument, summed over every row.
Decimal64 working_quantity(const InstrumentId &instrument, OrderSide side) const
Returns the quantity still to fill on open orders for an instrument and side.
std::chrono::system_clock::time_point working_as_of_
When the working-order listing was received, on the same clock as as_of_.
std::optional< Decimal64 > quantity_of(const InstrumentId &instrument) const
Returns the quantity of the first position row for an instrument.
const WorkingOrder< Broker > * find_working(const OrderRef< Broker > &ref) const
Finds a working order by its reference.
std::vector< BuyingPower > buying_power_
What may be spent, per currency.
std::optional< Decimal64 > cash_in(const Currency ¤cy) const
Returns the cash held in a currency (backtest only; see cash_).
std::chrono::system_clock::time_point as_of_
When the holdings were received, on this machine's clock.
std::optional< Decimal64 > sellable_quantity_of(const InstrumentId &instrument) const
Returns the sellable quantity the broker reported for an instrument (first row).
SellableReading sellable_reading_for(const InstrumentId &instrument) const
Returns an instrument's sellable quantity and its basis, taken from the same row.
std::vector< HoldingPosition > positions_
Every position row the broker reported.
bool listing_newer_than_holdings() const noexcept
Tests whether the working-order listing is newer than the holdings.