Portfolio analysis

Analyse the portfolio development over the time.

  • Portfolio situation at the start of the each tick

  • Currently held assets

  • Net asset value (NAV)

  • Asset valuation change

class tradingstrategy.analysis.portfolioanalyzer.AssetSnapshot

Asset holdings at a specific timepoint.

quantity: float

How many tokens of the asset we had

market_value: tradingstrategy.types.USDollarAmount

Daily market value of the tokens

__init__(quantity: float, market_value: tradingstrategy.types.USDollarAmount, realised_pnl: tradingstrategy.types.USDollarAmount, unrealised_pnl: tradingstrategy.types.USDollarAmount, total_pnl: tradingstrategy.types.USDollarAmount) None
class tradingstrategy.analysis.portfolioanalyzer.PortfolioSnapshot

Represents the portfolio status at the start of the day/candle snapshot

tick: int

A running counter where the first backtest simulated event is tick 1, the next one tick 2

cash_balances: Dict[str, float]

What reserve currencies we have. E.g. USD: 10_000 Resever currenecies expressed as strings for the backwards compatibiltiy.

asset_snapshots: Dict[tradingstrategy.types.PrimaryKey, tradingstrategy.analysis.portfolioanalyzer.AssetSnapshot]

What reserve currencies we have. E.g. USD: 10_000 Resever currenecies expressed as strings for the backwards compatibiltiy.

state_details: Optional[Dict] = None

Internal state dump of the algorithm when this trade was made. This is mostly useful when doing the trade analysis try to understand why some trades were made. It also allows you to reconstruct the portfolio state over the time.

get_ordered_assets_stable() List[Tuple[tradingstrategy.types.PrimaryKey, tradingstrategy.analysis.portfolioanalyzer.AssetSnapshot]]

Return asset snapshots in a stable order between days.

get_ordered_assets_by_weight() List[Tuple[tradingstrategy.types.PrimaryKey, tradingstrategy.analysis.portfolioanalyzer.AssetSnapshot]]

Return asset snapshots in an order where the heaviest asset is first.

__init__(tick: int, cash_balances: Dict[str, float], asset_snapshots: Dict[tradingstrategy.types.PrimaryKey, tradingstrategy.analysis.portfolioanalyzer.AssetSnapshot], state_details: Optional[Dict] = None) None
class tradingstrategy.analysis.portfolioanalyzer.PortfolioAnalyzer

Represents the portfolio analysis over the backtest period.

get_max_assets_held_once() int

Find out what was the max number of assets the strategy was holding at the same time.

__init__(snapshots: Dict[pandas._libs.tslibs.timestamps.Timestamp, tradingstrategy.analysis.portfolioanalyzer.PortfolioSnapshot]) None
tradingstrategy.analysis.portfolioanalyzer.expand_snapshot_to_row(exchange_universe: tradingstrategy.exchange.ExchangeUniverse, pair_universe: tradingstrategy.pair.LegacyPairUniverse, ts: pandas._libs.tslibs.timestamps.Timestamp, snapshot: tradingstrategy.analysis.portfolioanalyzer.PortfolioSnapshot, max_assets: int, timestamp_format: str) dict

Create DataFrame rows from each portfolio snapshot.

tradingstrategy.analysis.portfolioanalyzer.expand_timeline(exchange_universe: tradingstrategy.exchange.ExchangeUniverse, pair_universe: tradingstrategy.pair.LegacyPairUniverse, analyzer: tradingstrategy.analysis.portfolioanalyzer.PortfolioAnalyzer, create_html_styles=True, vmin=- 0.3, vmax=0.2, timestamp_format='%Y-%m-%d') pandas.core.frame.DataFrame

Console output for the portfolio development over the time.

Each row presents the portfolio status at the end of the day/candle.

The outputted data frame is intented to be human readable and not for programmatic manipulation.

Parameters
  • create_html_styles – Create a formatter function that can be applied to hide and recolour columns.

  • vmax – Trade success % to have the extreme green color.

  • vmin – The % of lost capital on the trade to have the extreme red color.

  • timestamp_format – How to format Opened at column, as passed to strftime()

Returns

pd.Dataframe rendering the portfolio development over the time