algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::runner::Portfolio< Broker > Struct Template Reference

What the account holds and which orders are still working, as a strategy sees it. More...

#include <algotradelib/src/strategy/portfolio.hpp>

Inheritance diagram for algotradelib::runner::Portfolio< Broker >:
Collaboration diagram for algotradelib::runner::Portfolio< Broker >:

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 &currency) const
 Returns the cash held in a currency (backtest only; see cash_).
std::optional< Decimal64 > buying_power_in (const Currency &currency) 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.

Detailed Description

template<typename Broker>
struct algotradelib::runner::Portfolio< Broker >

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.

Template Parameters
BrokerThe 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.

Member Function Documentation

◆ buying_power_in()

template<typename Broker>
std::optional< Decimal64 > algotradelib::strategy::Portfolio< Broker >::buying_power_in ( const Currency & currency) const
inline

Returns the buying power in a currency.

Live, the broker's figure; in a backtest, the ledger's modelled cash.

Parameters
currencyThe currency.
Returns
The amount, or std::nullopt if unknown (not read, or the broker refused), which is not the same as zero.

Definition at line 270 of file portfolio.hpp.

◆ cash_in()

template<typename Broker>
std::optional< Decimal64 > algotradelib::strategy::Portfolio< Broker >::cash_in ( const Currency & currency) const
inline

Returns the cash held in a currency (backtest only; see cash_).

Parameters
currencyThe currency.
Returns
The amount, or std::nullopt if none is recorded (always, live).

Definition at line 252 of file portfolio.hpp.

◆ find_working()

template<typename Broker>
const WorkingOrder< Broker > * algotradelib::strategy::Portfolio< Broker >::find_working ( const OrderRef< Broker > & ref) const
inline

Finds a working order by its reference.

Parameters
refThe order reference.
Returns
A pointer into working_, or nullptr if no working order has that reference.

Definition at line 304 of file portfolio.hpp.

◆ listing_newer_than_holdings()

template<typename Broker>
bool algotradelib::strategy::Portfolio< Broker >::listing_newer_than_holdings ( ) const
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).

Returns
true if both are stamped and the listing arrived after the holdings.

Definition at line 101 of file portfolio.hpp.

◆ net_quantity_of()

template<typename Broker>
std::optional< Decimal64 > algotradelib::strategy::Portfolio< Broker >::net_quantity_of ( const InstrumentId & instrument) const
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.

Parameters
instrumentThe instrument.
Returns
The sum, which is zero when no row matches; std::nullopt only if the sum overflowed (a broken snapshot, which must not be treated as flat).

Definition at line 140 of file portfolio.hpp.

◆ quantity_of()

template<typename Broker>
std::optional< Decimal64 > algotradelib::strategy::Portfolio< Broker >::quantity_of ( const InstrumentId & instrument) const
inline

Returns the quantity of the first position row for an instrument.

Parameters
instrumentThe instrument.
Returns
The first matching row's quantity, or std::nullopt if no row matches.
Warning
A holding can be split across rows. To size an order, or to ask whether anything is held, use net_quantity_of.

Definition at line 122 of file portfolio.hpp.

◆ sellable_quantity_of()

template<typename Broker>
std::optional< Decimal64 > algotradelib::strategy::Portfolio< Broker >::sellable_quantity_of ( const InstrumentId & instrument) const
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.

Parameters
instrumentThe instrument.
Returns
The first matching row's figure, or std::nullopt if the broker gave none or there is no position.

Definition at line 290 of file portfolio.hpp.

◆ sellable_reading_for()

template<typename Broker>
SellableReading algotradelib::strategy::Portfolio< Broker >::sellable_reading_for ( const InstrumentId & instrument) const
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.

Parameters
instrumentThe instrument.
Returns
The figure and its basis.

Definition at line 209 of file portfolio.hpp.

◆ working_quantity()

template<typename Broker>
Decimal64 algotradelib::strategy::Portfolio< Broker >::working_quantity ( const InstrumentId & instrument,
OrderSide side ) const
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.

Parameters
instrumentThe instrument.
sideThe side.
Returns
The remaining quantity, zero if there is none.

Definition at line 165 of file portfolio.hpp.

Member Data Documentation

◆ as_of_

template<typename Broker>
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.

◆ buying_power_

template<typename Broker>
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.

◆ cash_

template<typename Broker>
std::vector<Money> algotradelib::strategy::Portfolio< Broker >::cash_

Cash per currency, in a backtest only.

Warning
Only the backtest ledger fills this; live it stays empty, so cash_in returns a number in a backtest and std::nullopt live for the same strategy. To size against money, read buying_power_ instead.

Definition at line 64 of file portfolio.hpp.

◆ positions_

template<typename Broker>
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.

◆ sellable_basis_

template<typename Broker>
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.

◆ working_

template<typename Broker>
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.

◆ working_as_of_

template<typename Broker>
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.


The documentation for this struct was generated from the following file: