QuantJourney Backtester

QuantJourney Backtester

Share product feedback

Thank you.

Your note is now in the QuantJourney inbox.

backtester/portfolio/calc/exposures.py

exposures.py:
Market value, long-short exposure and participation analytics.

This file translates units and prices into exposure quantities that a PM can reason about: market value, long exposure, short exposure, turnover and market/volume participation.

Import from backtester.portfolio.calc import exposures

When To Read This

  • 01
    You need to explain how positions become exposure charts.
  • 02
    You are adding a portfolio construction or execution report section.
  • 03
    You want turnover to be dollar-based when prices are available.

File Anatomy

  • Position valuation: units x adjusted close prices.
  • Long-short split: positive units and negative units are separated before summing.
  • Turnover: delta units, optionally multiplied by price for dollar turnover.
  • Participation: exposure or turnover scaled against market cap or volume data.

Data Contract

Inputs

  • prices_adj_close: DataFrame dates x instruments.
  • units: DataFrame with position quantities, same date index and instrument columns.
  • optional prices, market_cap and volumes matrices for turnover and participation.

Outputs

  • Exposure matrix by instrument.
  • Long/Short aggregate DataFrame by date.
  • Turnover and participation DataFrames.

Invariants

  • Units are reindexed to price columns before valuation.
  • Dollar turnover is preferred when prices are supplied.
  • Legacy binary turnover remains available for backward compatibility.

Public API And Key Internals

compute_exposures

function
compute_exposures(prices_adj_close, units)

Multiplies aligned units by adjusted close prices.

Returns

pd.DataFrame dates x instruments.

compute_short_long_exposure

function
compute_short_long_exposure(prices_adj_close, units)

Aggregates positive and negative market value into Long and Short columns.

Returns

pd.DataFrame with Long and Short columns.

compute_turnover

function
compute_turnover(units, instruments, add_total=False, prices=None)

Measures position changes; uses dollar turnover when prices are provided.

Returns

pd.DataFrame.

market_cap_participation / volume_participation

function
volume_participation(turnover, volumes, *, trade_value=100_000_000)

Scales exposure or turnover against external liquidity context.

Returns

pd.DataFrame.

Implementation Notes

  • The module is intentionally close to portfolio accounting vocabulary: units, prices, value, turnover.
  • Turnover accepts MultiIndex units and filters instrument columns when needed.
  • Participation functions are simple scalers; the quality of market-cap and volume inputs matters.

Code Walkthrough

Exposure calculation used in a portfolio packet

Use dollar turnover when price data is available; otherwise you only know that positions changed.

exposures_usage.py Python
from backtester.portfolio.calc import exposures

market_value = exposures.compute_exposures(adj_close, units)
long_short = exposures.compute_short_long_exposure(adj_close, units)
turnover = exposures.compute_turnover(
    units=units,
    instruments=["AAPL", "MSFT", "SPY"],
    prices=adj_close,
    add_total=True,
)

Key implementation: dollar turnover fallback

The branch keeps old behavior but allows institutional dollar turnover when prices are passed.

backtester/portfolio/calc/exposures.py Python
position_changes = numeric_units.diff().abs()

if prices is not None:
    price_al = prices.reindex(
        columns=numeric_units.columns,
        index=numeric_units.index,
    )
    turnover = position_changes * price_al.fillna(method="ffill")
else:
    turnover = (position_changes != 0).astype(float)

if add_total:
    turnover["Total"] = turnover.sum(axis=1)