|
| | ReplayLedger (Decimal64 cash, Currency currency, ExecutionHistory history=ExecutionHistory::KEEP) |
| | Constructs a ledger holding only cash.
|
| | ReplayLedger (const ReplayLedger &other)=default |
| | Copies a ledger.
|
| | ReplayLedger (ReplayLedger &&other) noexcept |
| | Moves a ledger.
|
| ReplayLedger & | operator= (ReplayLedger &&other) noexcept |
| | Replaces this ledger with a move of another; moving a ledger into itself leaves it unchanged.
|
| ReplayLedger & | operator= (const ReplayLedger &other) |
| | Replaces this ledger with a copy of another, entirely or not at all.
|
| std::optional< Decimal64 > | cash_in (const Currency ¤cy) const |
| | Returns the cash held in a currency.
|
| std::optional< Decimal64 > | quantity_of (const InstrumentId &instrument) const |
| | Returns the quantity held of an instrument.
|
| std::optional< Decimal64 > | average_price_of (const InstrumentId &instrument) const |
| | Returns the average price of the position in an instrument.
|
| Decimal64 | realized_in (const Currency ¤cy) const |
| | Returns the profit realized by closing positions, in a currency (fees excluded).
|
| const std::vector< HoldingPosition > & | positions () const noexcept |
| | Returns every position, including ones that went flat.
|
| ExecutionHistory | history () const noexcept |
| | Returns whether the ledger keeps the executions it applies.
|
| const std::vector< Execution > & | executions () const |
| | Returns every execution applied.
|
| std::optional< Decimal64 > | equity () const |
| | Returns total equity in the ledger's own currency (the constructor's), as equity_in would.
|
| bool | apply (const Execution &execution_in, const Currency ¤cy_in) |
| | Applies one execution to the position, cash and realized profit.
|
| bool | mark (const InstrumentId &instrument, const Decimal64 &price) |
| | Marks positions in an instrument to a price, updating their market value.
|
| std::optional< std::size_t > | lot_of (const InstrumentId &instrument) const |
| | Returns where an instrument's lot is in positions(), if a fill ever opened one.
|
| bool | mark_lot (std::size_t lot, const Decimal64 &price) |
| | Marks the lot at a place lot_of returned to a price, as mark does for its instrument.
|
| template<typename BrokerTag> |
| strategy::Portfolio< BrokerTag > | snapshot (std::chrono::system_clock::time_point as_of) const |
| | Returns the portfolio a strategy sees.
|
| template<typename BrokerTag> |
| void | update (strategy::Portfolio< BrokerTag > &portfolio, std::chrono::system_clock::time_point as_of) |
| | Brings a portfolio built by snapshot up to date, changing only what changed since.
|
| void | clear_changed () noexcept |
| | Forgets which lots changed, after a snapshot that already shows them.
|
| std::optional< Decimal64 > | equity_in (const Currency ¤cy) const |
| | Returns total equity in a currency: cash plus positions at their last marks.
|
Positions, cash and realized profit, updated by each execution of a replay.
Cash and profit are kept per currency and never converted.
Definition at line 79 of file ledger.hpp.
| bool algotradelib::backtest::ReplayLedger::apply |
( |
const Execution & | execution_in, |
|
|
const Currency & | currency_in ) |
|
inline |
Applies one execution to the position, cash and realized profit.
A trade through flat (selling 150 from a long of 100) closes the old position, realizing its profit against the old average, and opens the remainder at the new price. Commission, institution fee and tax are deducted from cash.
- Parameters
-
- Returns
- false, with the ledger unchanged, if the arithmetic does not fit, or if currency_in differs from the currency of a non-flat position in the instrument; true otherwise.
- Exceptions
-
| std::bad_alloc | If an allocation fails; the ledger is then unchanged. |
Definition at line 282 of file ledger.hpp.
| std::optional< Decimal64 > algotradelib::backtest::ReplayLedger::equity |
( |
| ) |
const |
|
inline |
Returns total equity in the ledger's own currency (the constructor's), as equity_in would.
The same value, digit for digit and in the same representation, as equity_in for that currency, without adding up every position: the ledger keeps a running total. It uses it only while the magnitudes of the cash and of every position it counts, added up and written at the largest scale among them, fit – which guarantees that equity_in's own additions, in any order, cannot overflow – and otherwise calls equity_in.
- Returns
- The equity, or std::nullopt if the arithmetic does not fit.
Definition at line 257 of file ledger.hpp.
template<typename BrokerTag>
| strategy::Portfolio< BrokerTag > algotradelib::backtest::ReplayLedger::snapshot |
( |
std::chrono::system_clock::time_point | as_of | ) |
const |
|
inline |
Returns the portfolio a strategy sees.
The model a replay presents: every held share is sellable (basis whole holding, deduct resting sells, unverified), buying power is this ledger's cash (no settlement is modelled), and cash_ is filled. Working orders are left empty; Backtester fills them from its queue.
- Template Parameters
-
| BrokerTag | The order-reference tag of the portfolio. |
- Parameters
-
| as_of | The snapshot time, for as_of_ and the buying-power stamps. |
- Returns
- The portfolio.
Definition at line 520 of file ledger.hpp.
template<typename BrokerTag>
| void algotradelib::backtest::ReplayLedger::update |
( |
strategy::Portfolio< BrokerTag > & | portfolio, |
|
|
std::chrono::system_clock::time_point | as_of ) |
|
inline |
Brings a portfolio built by snapshot up to date, changing only what changed since.
Afterwards its positions, sellable basis, cash, buying power and as_of_ are what snapshot would return now; its working orders and everything else are left alone. Only the lots that apply or mark changed since snapshot, clear_changed or the last update are rewritten, plus any opened since, so a bar that marked one lot rewrites one row.
The portfolio must be one this ledger's snapshot built, whose positions only update has changed since; anything else written to them stays.
- Template Parameters
-
| BrokerTag | The order-reference tag of the portfolio. |
- Parameters
-
| portfolio | The portfolio to update. |
| as_of | The time, for as_of_ and the buying-power stamps. |
- Exceptions
-
| std::bad_alloc | If a row, the cash or the buying power cannot be copied; the ledger still lists every changed lot, so a later update completes it. |
Definition at line 563 of file ledger.hpp.