algotradelib 0.0.0.9000
C++ header-only library for algorithmic trading
Loading...
Searching...
No Matches
algotradelib::backtest::ReplayLedger Class Reference

Positions, cash and realized profit, updated by each execution of a replay. More...

#include <algotradelib/src/backtest/ledger.hpp>

Public Member Functions

 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 &currency) 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 &currency) 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 &currency_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 &currency) const
 Returns total equity in a currency: cash plus positions at their last marks.

Detailed Description

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.

Constructor & Destructor Documentation

◆ ReplayLedger() [1/3]

algotradelib::backtest::ReplayLedger::ReplayLedger ( Decimal64 cash,
Currency currency,
ExecutionHistory history = ExecutionHistory::KEEP )
inline

Constructs a ledger holding only cash.

Parameters
cashThe starting cash.
currencyIts currency.
historyWhether to keep the executions applied (executions()).

Definition at line 87 of file ledger.hpp.

Referenced by operator=(), operator=(), ReplayLedger(), and ReplayLedger().

◆ ReplayLedger() [2/3]

algotradelib::backtest::ReplayLedger::ReplayLedger ( const ReplayLedger & other)
default

Copies a ledger.

Parameters
otherThe ledger to copy.

◆ ReplayLedger() [3/3]

algotradelib::backtest::ReplayLedger::ReplayLedger ( ReplayLedger && other)
inlinenoexcept

Moves a ledger.

Parameters
otherThe ledger to move from.

Definition at line 103 of file ledger.hpp.

Member Function Documentation

◆ apply()

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
execution_inThe execution; it may be one of executions(). Kept (copied) only with ExecutionHistory::KEEP.
currency_inThe instrument's currency.
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_allocIf an allocation fails; the ledger is then unchanged.

Definition at line 282 of file ledger.hpp.

◆ average_price_of()

std::optional< Decimal64 > algotradelib::backtest::ReplayLedger::average_price_of ( const InstrumentId & instrument) const
inline

Returns the average price of the position in an instrument.

Parameters
instrumentThe instrument.
Returns
The average price, or std::nullopt if flat or never held.

Definition at line 189 of file ledger.hpp.

◆ cash_in()

std::optional< Decimal64 > algotradelib::backtest::ReplayLedger::cash_in ( const Currency & currency) const
inline

Returns the cash held in a currency.

Parameters
currencyThe currency.
Returns
The amount, or std::nullopt if none is held.

Definition at line 163 of file ledger.hpp.

Referenced by apply(), equity(), and equity_in().

◆ clear_changed()

void algotradelib::backtest::ReplayLedger::clear_changed ( )
inlinenoexcept

Forgets which lots changed, after a snapshot that already shows them.

Definition at line 604 of file ledger.hpp.

Referenced by update().

◆ equity()

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.

◆ equity_in()

std::optional< Decimal64 > algotradelib::backtest::ReplayLedger::equity_in ( const Currency & currency) const
inline

Returns total equity in a currency: cash plus positions at their last marks.

Positions in other currencies are left out, not converted.

Parameters
currencyThe currency.
Returns
The equity, or std::nullopt if the arithmetic does not fit.

Definition at line 619 of file ledger.hpp.

Referenced by equity().

◆ executions()

const std::vector< Execution > & algotradelib::backtest::ReplayLedger::executions ( ) const
inline

Returns every execution applied.

Returns
The executions, oldest first.
Exceptions
std::logic_errorIf the ledger keeps none (ExecutionHistory::NONE, as a Backtester's does: its executions are in ReplayResult::executions_).

Definition at line 236 of file ledger.hpp.

◆ history()

ExecutionHistory algotradelib::backtest::ReplayLedger::history ( ) const
inlinenoexcept

Returns whether the ledger keeps the executions it applies.

Returns
The setting it was constructed with.

Definition at line 226 of file ledger.hpp.

Referenced by ReplayLedger().

◆ lot_of()

std::optional< std::size_t > algotradelib::backtest::ReplayLedger::lot_of ( const InstrumentId & instrument) const
inline

Returns where an instrument's lot is in positions(), if a fill ever opened one.

A lot keeps its place for the ledger's life (lots are never removed or reordered), so a caller marking the same instrument every bar can look it up once and use mark_lot.

Parameters
instrumentThe instrument.
Returns
Its place, or std::nullopt if no fill opened a lot in it.

Definition at line 479 of file ledger.hpp.

◆ mark()

bool algotradelib::backtest::ReplayLedger::mark ( const InstrumentId & instrument,
const Decimal64 & price )
inline

Marks positions in an instrument to a price, updating their market value.

Parameters
instrumentThe instrument.
priceThe new mark.
Returns
false if a position's value does not fit (that position keeps its old mark and value; Backtester halts the replay with mark_not_representable); true otherwise.

Definition at line 464 of file ledger.hpp.

◆ mark_lot()

bool algotradelib::backtest::ReplayLedger::mark_lot ( std::size_t lot,
const Decimal64 & price )
inline

Marks the lot at a place lot_of returned to a price, as mark does for its instrument.

Parameters
lotThe lot's place in positions().
priceThe new mark.
Returns
As mark.

Definition at line 489 of file ledger.hpp.

Referenced by mark().

◆ operator=() [1/2]

ReplayLedger & algotradelib::backtest::ReplayLedger::operator= ( const ReplayLedger & other)
inline

Replaces this ledger with a copy of another, entirely or not at all.

Parameters
otherThe ledger to copy.
Returns
This ledger.
Exceptions
std::bad_allocIf the copy cannot be made; this ledger is then unchanged.

Definition at line 150 of file ledger.hpp.

◆ operator=() [2/2]

ReplayLedger & algotradelib::backtest::ReplayLedger::operator= ( ReplayLedger && other)
inlinenoexcept

Replaces this ledger with a move of another; moving a ledger into itself leaves it unchanged.

Parameters
otherThe ledger to move from.
Returns
This ledger.

Definition at line 124 of file ledger.hpp.

◆ positions()

const std::vector< HoldingPosition > & algotradelib::backtest::ReplayLedger::positions ( ) const
inlinenoexcept

Returns every position, including ones that went flat.

Of the money fields only market_value_ (quantity times last mark) is filled; purchase_value_, unrealized_profit_loss_ and unrealized_return_ stay at their defaults.

Returns
The positions.

Definition at line 218 of file ledger.hpp.

◆ quantity_of()

std::optional< Decimal64 > algotradelib::backtest::ReplayLedger::quantity_of ( const InstrumentId & instrument) const
inline

Returns the quantity held of an instrument.

Parameters
instrumentThe instrument.
Returns
The quantity (zero if the position went flat), or std::nullopt if never held.

Definition at line 177 of file ledger.hpp.

◆ realized_in()

Decimal64 algotradelib::backtest::ReplayLedger::realized_in ( const Currency & currency) const
inline

Returns the profit realized by closing positions, in a currency (fees excluded).

Parameters
currencyThe currency.
Returns
The amount; zero if none.

Definition at line 201 of file ledger.hpp.

Referenced by apply().

◆ snapshot()

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
BrokerTagThe order-reference tag of the portfolio.
Parameters
as_ofThe snapshot time, for as_of_ and the buying-power stamps.
Returns
The portfolio.

Definition at line 520 of file ledger.hpp.

◆ update()

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
BrokerTagThe order-reference tag of the portfolio.
Parameters
portfolioThe portfolio to update.
as_ofThe time, for as_of_ and the buying-power stamps.
Exceptions
std::bad_allocIf 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.


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