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)
 Constructs a ledger holding only cash.
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.
const std::vector< Execution > & executions () const noexcept
 Returns every execution applied.
bool apply (const Execution &execution, const Currency &currency)
 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.
template<typename BrokerTag>
strategy::Portfolio< BrokerTag > snapshot (std::chrono::system_clock::time_point as_of) const
 Returns the portfolio a strategy sees.
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 61 of file ledger.hpp.

Constructor & Destructor Documentation

◆ ReplayLedger()

algotradelib::backtest::ReplayLedger::ReplayLedger ( Decimal64 cash,
Currency currency )
inline

Constructs a ledger holding only cash.

Parameters
cashThe starting cash.
currencyIts currency.

Definition at line 68 of file ledger.hpp.

Member Function Documentation

◆ apply()

bool algotradelib::backtest::ReplayLedger::apply ( const Execution & execution,
const Currency & currency )
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
executionThe execution.
currencyThe instrument's currency.
Returns
false, with the ledger unchanged, if the arithmetic does not fit, or if currency differs from the currency of a non-flat position in the instrument; true otherwise.
Note
If an allocation throws partway, the ledger can be left partly updated.

Definition at line 157 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 105 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 77 of file ledger.hpp.

Referenced by apply(), and equity_in().

◆ 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 367 of file ledger.hpp.

◆ executions()

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

Returns every execution applied.

Returns
The executions, oldest first.

Definition at line 142 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 298 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 136 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 91 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 119 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 332 of file ledger.hpp.


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