Portfolio¶
Instance of a portfolio, which is the main entry point to the library.
Portfolio ¶
Portfolio(
data: DataFrame,
input_type: str = "prices",
weights: Series | ndarray | dict | None = None,
name: str = "My Portfolio",
frequency: int = TRADING_DAYS_PER_YEAR,
risk_free_rate: float = DEFAULT_RISK_FREE_RATE,
asset_classes: dict[str, AssetClass] | None = None,
base_currency: str | None = None,
)
A collection of assets, their prices, and their weights, PortPy's main entry point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
Columns = asset symbols, index = a sorted, duplicate-free DatetimeIndex. See :func:portpy.core.align_calendars if you're combining assets with different trading calendars (e.g. crypto and equities), and :func:portpy.core.convert_to_base_currency if your assets aren't all in the same currency - both run before constructing the Portfolio, which never modifies your data itself. |
required |
input_type
|
str
|
"prices" (default) or "returns" - what data contains. |
'prices'
|
weights
|
Series | ndarray | dict | None
|
Optional weights (Series/dict/array, any order); defaults to equal-weight (1/N) across all columns in data. |
None
|
name
|
str
|
A label used in chart titles and comparisons. |
'My Portfolio'
|
frequency
|
int
|
Trading periods per year, used to annualize metrics (default 252 for daily equity data; use 365 for a crypto-only, 7-day-a-week portfolio, 12 for monthly data, etc.). |
TRADING_DAYS_PER_YEAR
|
risk_free_rate
|
float
|
Annual risk-free rate, used as the default rf in .metrics calls. |
DEFAULT_RISK_FREE_RATE
|
asset_classes
|
dict[str, AssetClass] | None
|
Optional {symbol: AssetClass} tags, purely for reporting/grouping - see portpy.core.AssetClass. |
None
|
base_currency
|
str | None
|
Optional 3-letter currency code (e.g. "USD"), purely
informational/for reporting - like asset_classes, it's never used
to convert anything. |
None
|
Source code in src/portpy/portfolio.py
prices
property
¶
Asset price DataFrame (reconstructed from returns at construction time if needed).
set_weights ¶
Validate, normalize (sum to 1), and apply new portfolio weights.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Series | ndarray | dict
|
A Series/dict keyed by asset name, or a plain array aligned
with |
required |
Source code in src/portpy/portfolio.py
set_risk_free_rate ¶
Set the annual risk-free rate used as the default rf in .metrics calls.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rate
|
float
|
The new annual risk-free rate (e.g. 0.04 for 4%/year). |
required |
Source code in src/portpy/portfolio.py
asset_returns ¶
Per-asset simple (or log) returns - the multi-asset DataFrame, not the portfolio aggregate.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log
|
bool
|
If True, return log returns instead of simple returns. |
False
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
A DataFrame of per-period returns, one column per asset. |
Source code in src/portpy/portfolio.py
returns ¶
The portfolio's own return series: asset returns combined by current weights.
Assumes weights are held constant each period (rebalanced back to target every period). For turnover/rebalancing effects, see :mod:portpy.models.management and (eventually) :mod:portpy.strategies.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
period
|
int
|
Compound returns over non-overlapping blocks of this many periods (e.g. period=21 on daily data for a rough monthly series) instead of period-over-period. |
1
|
log
|
bool
|
If True, return log returns instead of simple returns. |
False
|
Returns:
| Type | Description |
|---|---|
Series
|
The portfolio's return series, named after the portfolio. |
Source code in src/portpy/portfolio.py
price_index ¶
Synthetic aggregated portfolio value, rebased to base, built by compounding .returns().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
base
|
float
|
The starting value of the index. |
100.0
|
Returns:
| Type | Description |
|---|---|
Series
|
A synthetic price series, named after the portfolio, anchored one |
Series
|
period before the first return. |