![]() |
algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
|
What the account holds and which orders are still working, as a strategy sees it. More...
#include <algotradelib/src/strategy/portfolio.hpp>
Classes | |
| struct | SellableReading |
| A sellable quantity together with what it covers, read from the same row. More... | |
Public Member Functions | |
| bool | listing_newer_than_holdings () const noexcept |
| Tests whether the working-order listing is newer than the holdings. | |
| std::optional< Decimal64 > | quantity_of (const InstrumentId &instrument) const |
| Returns the quantity of the first position row for an instrument. | |
| 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. | |
| SellableReading | sellable_reading_for (const InstrumentId &instrument) const |
| Returns an instrument's sellable quantity and its basis, taken from the same row. | |
| std::optional< Decimal64 > | cash_in (const Currency ¤cy) const |
| Returns the cash held in a currency (backtest only; see cash_). | |
| std::optional< Decimal64 > | buying_power_in (const Currency ¤cy) const |
| Returns the buying power in a currency. | |
| std::optional< Decimal64 > | sellable_quantity_of (const InstrumentId &instrument) const |
| Returns the sellable quantity the broker reported for an instrument (first row). | |
| const WorkingOrder< Broker > * | find_working (const OrderRef< Broker > &ref) const |
| Finds a working order by its reference. | |
Public Attributes | |
| std::vector< HoldingPosition > | positions_ |
| Every position row the broker reported. | |
| std::vector< Money > | cash_ |
| Cash per currency, in a backtest only. | |
| std::vector< BuyingPower > | buying_power_ |
| What may be spent, per currency. | |
| std::vector< WorkingOrder< Broker > > | working_ |
| Orders still outstanding at the broker. | |
| std::chrono::system_clock::time_point | as_of_ {} |
| When the holdings were received, on this machine's clock. | |
| std::chrono::system_clock::time_point | working_as_of_ {} |
| When the working-order listing was received, on the same clock as as_of_. | |
| SellableBasis | sellable_basis_ |
| What the sellable_quantity_ figures on positions_ cover. | |
What the account holds and which orders are still working, as a strategy sees it.
Read it with ctx.portfolio(). Size orders against positions and working orders (net_quantity_of, working_quantity), or emit a TargetPosition and let the runner do it; sizing from positions alone buys twice after a partial fill.
| Broker | The broker tag of the working orders' references; read the portfolio with auto so a strategy never names it. |
Definition at line 50 of file portfolio.hpp.
|
inline |
Returns the buying power in a currency.
Live, the broker's figure; in a backtest, the ledger's modelled cash.
| currency | The currency. |
Definition at line 270 of file portfolio.hpp.
Referenced by algotradelib::strategy::RiskGuard::evaluate().
|
inline |
Returns the cash held in a currency (backtest only; see cash_).
| currency | The currency. |
Definition at line 252 of file portfolio.hpp.
|
inline |
Finds a working order by its reference.
| ref | The order reference. |
Definition at line 304 of file portfolio.hpp.
|
inlinenoexcept |
Tests whether the working-order listing is newer than the holdings.
Then a resting buy that filled between the two reads appears in neither, and the portfolio under-counts the position. The runner withholds a plan sized against such a portfolio (WithheldPlanReason::PORTFOLIO_SUPERSEDED).
Definition at line 101 of file portfolio.hpp.
|
inline |
Returns the net quantity held of an instrument, summed over every row.
Use this to size an order: a holding reported as two rows of 60 and 40 is 100.
| instrument | The instrument. |
Definition at line 140 of file portfolio.hpp.
Referenced by algotradelib::strategy::closes_toward_flat(), algotradelib::strategy::RiskGuard::evaluate(), and algotradelib::strategy::reconcile().
|
inline |
Returns the quantity of the first position row for an instrument.
| instrument | The instrument. |
Definition at line 122 of file portfolio.hpp.
|
inline |
Returns the sellable quantity the broker reported for an instrument (first row).
Sellable can be less than held (unsettled, pledged or locked shares). A broker that does not publish it leaves this absent; do not substitute the held quantity. To check a sell, prefer sellable_reading_for, which also says what the figure covers.
| instrument | The instrument. |
Definition at line 290 of file portfolio.hpp.
|
inline |
Returns an instrument's sellable quantity and its basis, taken from the same row.
A row's own basis is used if it has one, otherwise the portfolio's sellable_basis_. When an instrument has several rows, figures are not summed; if the rows' bases differ, or their figures differ under the same basis, the reading's basis is unestablished and RiskGuard refuses a checked sell.
| instrument | The instrument. |
Definition at line 209 of file portfolio.hpp.
Referenced by algotradelib::strategy::RiskGuard::evaluate(), and algotradelib::strategy::SellableConventionProbe< Broker >::observe().
|
inline |
Returns the quantity still to fill on open orders for an instrument and side.
Sums ordered minus filled over the orders order_is_open accepts. An order reporting more filled than ordered contributes zero.
| instrument | The instrument. |
| side | The side. |
Definition at line 165 of file portfolio.hpp.
Referenced by algotradelib::strategy::RiskGuard::evaluate(), and algotradelib::strategy::reconcile().
| std::chrono::system_clock::time_point algotradelib::strategy::Portfolio< Broker >::as_of_ {} |
When the holdings were received, on this machine's clock.
Brokers do not timestamp holdings, so this is when the reply arrived, not when the figures were true. Staleness checks measure against it.
Definition at line 84 of file portfolio.hpp.
Referenced by listing_newer_than_holdings(), and algotradelib::backtest::ReplayLedger::snapshot().
| std::vector<BuyingPower> algotradelib::strategy::Portfolio< Broker >::buying_power_ |
What may be spent, per currency.
Live, the broker's figure; in a backtest, the ledger's modelled cash (the backtest report says so). Empty means no figure was read, which is not the same as zero; see BuyingPower.
Definition at line 71 of file portfolio.hpp.
Referenced by buying_power_in(), and algotradelib::backtest::ReplayLedger::snapshot().
| std::vector<Money> algotradelib::strategy::Portfolio< Broker >::cash_ |
Cash per currency, in a backtest only.
Definition at line 64 of file portfolio.hpp.
Referenced by cash_in(), and algotradelib::backtest::ReplayLedger::snapshot().
| std::vector<HoldingPosition> algotradelib::strategy::Portfolio< Broker >::positions_ |
Every position row the broker reported.
One instrument may appear in more than one row; size a holding with net_quantity_of.
Definition at line 56 of file portfolio.hpp.
Referenced by net_quantity_of(), algotradelib::strategy::SellableConventionProbe< Broker >::observe(), algotradelib::strategy::projected_gross_exposure(), quantity_of(), sellable_quantity_of(), sellable_reading_for(), and algotradelib::backtest::ReplayLedger::snapshot().
| SellableBasis algotradelib::strategy::Portfolio< Broker >::sellable_basis_ |
What the sellable_quantity_ figures on positions_ cover.
Set by whatever filled the positions. RiskGuard refuses to measure a sell against a figure whose basis is unestablished, which is the default, so a hand-built portfolio refuses checked sells until this is set.
Definition at line 112 of file portfolio.hpp.
Referenced by sellable_reading_for(), and algotradelib::backtest::ReplayLedger::snapshot().
| std::vector<WorkingOrder<Broker> > algotradelib::strategy::Portfolio< Broker >::working_ |
Orders still outstanding at the broker.
Use working_quantity to sum what is still to fill.
Definition at line 77 of file portfolio.hpp.
Referenced by algotradelib::strategy::closes_toward_flat(), find_working(), algotradelib::strategy::projected_gross_exposure(), algotradelib::strategy::reconcile(), and working_quantity().
| std::chrono::system_clock::time_point algotradelib::strategy::Portfolio< Broker >::working_as_of_ {} |
When the working-order listing was received, on the same clock as as_of_.
Set by a live source; the epoch (the default) means no stamped listing, as in a backtest.
Definition at line 90 of file portfolio.hpp.
Referenced by listing_newer_than_holdings().