from finalytics import Portfolio
portfolio = Portfolio(
ticker_symbols=["AAPL", "GOOG", "MSFT", "NVDA", "BTC-USD"],
benchmark_symbol="^GSPC",
start_date="2020-01-01",
end_date="2024-01-01",
interval="1d",
confidence_level=0.95,
risk_free_rate=0.02,
objective_function="max_sharpe",
asset_constraints=[(0, 1), (0, 1), (0, 1), (0, 1), (0, 1)],
categorical_constraints=[
(
"AssetClass",
["EQUITY", "EQUITY", "EQUITY", "EQUITY", "CRYPTO"],
[("EQUITY", 0.0, 0.8), ("CRYPTO", 0.0, 0.2)]
)
]
)Portfolio Module Documentation
Portfolio
A class representing a Portfolio object.
__new__
Create a new Portfolio object.
Parameters:
ticker_symbols(List[str]): List of ticker symbols in the portfolio.benchmark_symbol(str): The ticker symbol of the benchmark.start_date(str): The start date for historical data.end_date(str): The end date for historical data.interval(str): The interval for historical data.confidence_level(float): The confidence level for risk calculations.risk_free_rate(float): The risk-free rate for calculations.objective_function(Optional[str]): The objective function for optimization:max_sharpe: Maximize return per unit of risk (Sharpe ratio).max_sortino: Maximize return per unit of downside risk (Sortino ratio).min_vol: Minimize overall volatility.max_return: Maximize expected return.min_var: Minimize Value-at-Risk (VaR).min_cvar: Minimize Conditional Value-at-Risk (CVaR).min_drawdown: Minimize maximum portfolio drawdown.risk_parity: Risk Parity (equal risk contribution from each asset).max_diversification: Maximize Diversification ratio.hierarchical_risk_parity: Hierarchical Risk Parity (clustering-based allocation).
asset_constraints(Optional[List[Tuple[float, float]]]): List of tuples with the lower and upper bounds for the ticker weights.categorical_constraints(Optional[List[Tuple[str, List[str], List[Tuple[str, float, float]]]]]): Category-based constraints. Each tuple has the form:category_name(str): Name of the constraint group (e.g.,"AssetClass").category_per_symbol(List[str]): Assigns each ticker to a category, in the same order asticker_symbols.weight_per_category(List[Tuple[str, float, float]]): Tuples of(category_label, min_weight, max_weight)specifying bounds for each category.
weights(Optional[List[float]]): Dollar amounts for each asset in the portfolio. If provided, skips optimization and evaluates the explicit allocation directly. Fractional weights are derived asallocation[i] / sum(allocation).transactions(Optional[List[dict]]): Ad-hoc per-asset transactions (additions/withdrawals). Each transaction is a dict with keys:date(str): Date string (e.g.,"2024-01-15")ticker(str): Ticker symbolamount(float): Dollar amount (positive = addition, negative = withdrawal)
rebalance_strategy(Optional[dict]): Rebalancing strategy for the portfolio simulation. Possible formats:{"type": "calendar", "frequency": "monthly"}— rebalance on a fixed calendar schedule{"type": "threshold", "threshold": 0.05}— rebalance when any weight drifts more than 5% from target{"type": "calendar_or_threshold", "frequency": "quarterly", "threshold": 0.05}— rebalance on the earlier of the calendar trigger or threshold breach- Frequency values:
"monthly","quarterly","semi_annually","annually"
scheduled_cash_flows(Optional[List[dict]]): Recurring cash flow schedules. Each schedule is a dict with keys:amount(float): Dollar amount per occurrence (positive = addition, negative = withdrawal)frequency(str):"monthly","quarterly","semi_annually","annually"start_date(Optional[str]): Start date orNone(defaults to first portfolio date)end_date(Optional[str]): End date orNone(defaults to last portfolio date)allocation(str or dict): How cash is distributed across assets:"pro_rata"— distribute according to target weights"rebalance"— direct cash to underweight assets (additions) or withdraw from overweight (withdrawals){"custom": [0.4, 0.3, 0.2, 0.1]}— distribute according to a custom weight vector
tickers_data(Optional[List[DataFrame]]): Custom Polars DataFrames for each ticker (columns: timestamp, open, high, low, close, volume, adjclose).benchmark_data(Optional[DataFrame]): Custom Polars DataFrame for the benchmark.
Returns:
Portfolio: A Portfolio object.
Example: Optimization
Example: Explicit Allocation with Rebalancing and DCA
from finalytics import Portfolio
portfolio_dca = Portfolio(
ticker_symbols=["AAPL", "MSFT", "NVDA", "BTC-USD"],
benchmark_symbol="^GSPC",
start_date="2023-01-01",
end_date="2024-12-31",
interval="1d",
confidence_level=0.95,
risk_free_rate=0.02,
weights=[25000.0, 25000.0, 25000.0, 25000.0],
rebalance_strategy={"type": "calendar", "frequency": "quarterly"},
scheduled_cash_flows=[
{
"amount": 2000.0,
"frequency": "monthly",
"start_date": None,
"end_date": None,
"allocation": "pro_rata"
}
]
)
optimization_results
Get the optimization results for the portfolio.
Returns:
dict: Dictionary containing the portfolio optimization results, including:ticker_symbols,benchmark_symbol,start_date,end_date,intervalconfidence_level,risk_free_rateportfolio_returns(DataFrame),benchmark_returns(Series)objective_function,optimization_method,optimal_weightscategory_weights,efficient_frontier,risk_contributionsweights,starting_weights,ending_weightsstarting_values,ending_valuesoptimal_portfolio_returns(Series)- Performance metrics:
Daily Return,Daily Volatility,Cumulative Return,Annualized Return,Annualized Volatility,Alpha,Beta,Sharpe Ratio,Sortino Ratio,Active Return,Active Risk,Information Ratio,Calmar Ratio,Maximum Drawdown,Value at Risk,Expected Shortfall Money Weighted Return(annualized %, if applicable)
Example:
optimization_results = portfolio.optimization_results()
print(optimization_results){'ticker_symbols': ['AAPL', 'GOOG', 'MSFT', 'NVDA', 'BTC-USD'], 'benchmark_symbol': '^GSPC', 'start_date': '2020-01-01', 'end_date': '2024-01-01', 'interval': 1.0, 'confidence_level': 0.95, 'risk_free_rate': 0.02, 'portfolio_returns': shape: (1_460, 5)
┌───────────┬───────────┬───────────┬───────────┬───────────┐
│ AAPL ┆ GOOG ┆ MSFT ┆ NVDA ┆ BTC-USD │
│ --- ┆ --- ┆ --- ┆ --- ┆ --- │
│ f64 ┆ f64 ┆ f64 ┆ f64 ┆ f64 │
╞═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
│ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.0 ┆ -0.029819 │
│ -0.009722 ┆ -0.004907 ┆ -0.012452 ┆ -0.016006 ┆ 0.051452 │
│ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.008955 │
│ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.000089 │
│ 0.007968 ┆ 0.024657 ┆ 0.002585 ┆ 0.004194 ┆ 0.048291 │
│ … ┆ … ┆ … ┆ … ┆ … │
│ 0.000518 ┆ -0.009663 ┆ -0.001575 ┆ 0.002801 ┆ 0.021694 │
│ 0.002226 ┆ -0.001131 ┆ 0.003234 ┆ 0.002125 ┆ -0.01876 │
│ -0.005424 ┆ -0.002477 ┆ 0.002025 ┆ 0.0 ┆ -0.012397 │
│ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.001366 │
│ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.0 ┆ 0.002569 │
└───────────┴───────────┴───────────┴───────────┴───────────┘, 'benchmark_returns': shape: (1_460,)
Series: 'benchmark_returns' [f64]
[
0.0
-0.00706
0.0
0.0
0.003533
…
0.00143
0.00037
-0.002826
0.0
0.0
], 'objective_function': 'Maximize Sharpe Ratio', 'optimization_method': 'COBYLA', 'optimal_weights': [0.107764053740416, 4.930380657631324e-32, 6.162975822039155e-33, 0.6922359462595841, 0.2], 'category_weights': [('AssetClass', 'EQUITY', 0.8), ('AssetClass', 'CRYPTO', 0.2)], 'efficient_frontier': [[0.0009528657718558201, 0.016475410477559578], [0.0009530204850056825, 0.01647684209744702], [0.0009531598312161657, 0.01647822585606247], [0.000953307126180743, 0.016479574586381357], [0.0009534514171068193, 0.01648093770771352], [0.0009536058768282874, 0.01648237477565015], [0.0009538016479414117, 0.016484007830833772], [0.0009538897481532456, 0.01648507463233194], [0.0009540520560401239, 0.016486569151710595], [0.0009541951463007417, 0.016487940859254948], [0.0009543383670549831, 0.016489314137811222], [0.0009544861392283481, 0.016490711147944412], [0.0009547666600644916, 0.016492774682435193], [0.0009707803925493685, 0.016527540848762283], [0.0010454558073608583, 0.016960478833918], [0.0011201312221723483, 0.017482278854562706], [0.0011948066369838382, 0.018085318647833692], [0.001269482051795328, 0.018761675549440682], [0.001344157466606818, 0.0195038048210803], [0.0014188328814183078, 0.020304683421560856], [0.0014935082962297976, 0.021163358522516034], [0.0015681837110412876, 0.022078871994185335], [0.0016428591258527772, 0.02306109993207828], [0.0016887629532381081, 0.023736959052201544], [0.0016887543073330073, 0.023737167817500363], [0.0016888945613597456, 0.023737419291101257], [0.0016888770010209573, 0.023737508989223877], [0.0016888682531215702, 0.023737717615459807], [0.001688859437711419, 0.02373792362383865], [0.0016877679021339855, 0.023741090385670013], [0.0016877849309732402, 0.023741579808124365], [0.0016887766326125649, 0.023741861289503723], [0.001688088981239963, 0.023742060916204028], [0.0016881002320475237, 0.02374225680611678], [0.0016881924243583985, 0.023742322899874765], [0.0016881356663211613, 0.02374278544714162], [0.0016881236245503244, 0.023742968670205804], [0.0016881114270852011, 0.023743150525931806], [0.0016885242223414244, 0.023743256428519776], [0.001688870165847588, 0.02374397267028735], [0.0016888600792487648, 0.02374416736147115], [0.0016889072874387498, 0.023744223753662468], [0.0016888499467546763, 0.02374436167932614], [0.0016888574344311362, 0.023744789895801997], [0.001688141989917325, 0.023744853942483272], [0.0016888472109437048, 0.023744983486593253], [0.001688836941037628, 0.02374517669748754], [0.001688826624531036, 0.023745369526201047], [0.0016882907067801772, 0.023745428745619], [0.0016888250969845696, 0.02374567916027778], [0.0016888146873730728, 0.023745871230609575], [0.001688790721711966, 0.02374589133296952], [0.0016887800764002455, 0.02374608091146033], [0.0016887693796923627, 0.02374627004736351], [0.001688286573649828, 0.023746429152517852], [0.001688322373656632, 0.02374756383024199], [0.0016884837276113914, 0.02374886887289029], [0.0016884715328175654, 0.023749041664842267], [0.0016884593384695417, 0.02374921468729547], [0.001689983707272634, 0.023754274586374087], [0.001689996682942169, 0.0237547827641716], [0.0016899866271684982, 0.023754975066092247], [0.001689976516792105, 0.023755166867875037], [0.0016899663515469185, 0.023755358165908604], [0.0016899561311652517, 0.02375554895655771], [0.0016899458579173585, 0.023755739274929875], [0.0016899355430572574, 0.02375592929194564], [0.0016899391106483252, 0.023756355928902077], [0.001689928525260267, 0.0237565430939682], [0.0016899219177015645, 0.023756730377322125], [0.0016899113788281688, 0.023756918316673863], [0.001689909612094802, 0.023757222831810938], [0.001689891527438376, 0.023762614149518402], [0.0016898819513552722, 0.023762818825969402], [0.0016898901061045053, 0.02376325973683978], [0.0016898817350474678, 0.023763485549924423], [0.0016898803793387782, 0.023763801874346357], [0.001689870160690971, 0.02376400072968759], [0.0017175345406642676, 0.024104083637186222], [0.0017922099554757572, 0.025200280599015638]], 'risk_contributions': [0.0013228119854482236, 5.706331820458026e-34, 7.644427718121566e-35, 0.018840888453620704, 0.004173686678450068], 'weights': [0.107764053740416, 4.930380657631324e-32, 6.162975822039155e-33, 0.6922359462595841, 0.2], 'starting_weights': [0.107764053740416, 4.930380657631324e-32, 6.162975822039155e-33, 0.6922359462595841, 0.2], 'ending_weights': [0.03941628234528323, 1.412411935792325e-32, 2.0808676237277648e-33, 0.7974276232511074, 0.16315609440360943], 'starting_values': [0.107764053740416, 4.930380657631324e-32, 6.162975822039155e-33, 0.6922359462595841, 0.2], 'ending_values': [0.28362345573940567, 1.016313894465849e-31, 1.4973072833403289e-32, 5.737963215995802, 1.1740045627421767], 'optimal_portfolio_returns': shape: (1_460,)
Series: 'Portfolio Returns' [f64]
[
-0.005964
-0.002157
0.001842
0.000018
0.013734
…
0.00582
-0.001363
-0.002251
0.000222
0.000418
], 'Daily Return': 0.0016696570709417219, 'Daily Volatility': 0.025198004854455624, 'Cumulative Return': 6.1955912344774, 'Annualized Return': 0.8384386125838093, 'Annualized Volatility': 0.48140720679637394, 'Alpha': 0.4244607413279207, 'Beta': 1.5179087579220425, 'Sharpe Ratio': 1.7000963031490162, 'Sortino Ratio': 2.5457126662244853, 'Active Return': 0.487570280282255, 'Active Risk': 0.35249005920120646, 'Information Ratio': 1.3832171079864206, 'Calmar Ratio': 1.301866170776102, 'Maximum Drawdown': 0.6440282660421061, 'Value at Risk': -0.03830991258553664, 'Expected Shortfall': -0.057062714747767404, 'Money Weighted Return': 0.6383756304908901}
optimization_chart
Display the efficient frontier and allocation chart for the portfolio.
Parameters:
height(Optional[int]): Optional height of the plot in pixels, defaults to None.width(Optional[int]): Optional width of the plot in pixels, defaults to None.
Returns:
Plot: Plot object containing the portfolio optimization chart.
Example:
optimization_chart = portfolio.optimization_chart()
optimization_chart.show()
performance_stats
Compute the performance stats for the portfolio and its components.
Returns:
DataFrame: Polars DataFrame containing the performance stats.
Example:
performance_stats = portfolio.performance_stats()
print(performance_stats)shape: (6, 21)
┌───────────┬───────────┬───────────┬───────────┬───┬───────────┬───────────┬───────────┬──────────┐
│ Symbol ┆ Starting ┆ Ending ┆ Starting ┆ … ┆ Calmar ┆ Maximum ┆ Value at ┆ Expected │
│ --- ┆ Weight ┆ Weight ┆ Value ($) ┆ ┆ Ratio ┆ Drawdown ┆ Risk ┆ Shortfal │
│ str ┆ (%) ┆ (%) ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ l │
│ ┆ --- ┆ --- ┆ str ┆ ┆ str ┆ str ┆ str ┆ --- │
│ ┆ str ┆ str ┆ ┆ ┆ ┆ ┆ ┆ str │
╞═══════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡
│ AAPL ┆ 10.776405 ┆ 3.9416282 ┆ 0.1077640 ┆ … ┆ 1.1047 ┆ 31.4273% ┆ -2.6895% ┆ -4.1780% │
│ ┆ 3740416 ┆ 34528323 ┆ 53740416 ┆ ┆ ┆ ┆ ┆ │
│ GOOG ┆ 0.0000000 ┆ 0.0000000 ┆ 0.0000000 ┆ … ┆ 0.5987 ┆ 44.6019% ┆ -2.6861% ┆ -4.2193% │
│ ┆ 000000000 ┆ 000000000 ┆ 000000000 ┆ ┆ ┆ ┆ ┆ │
│ ┆ 000000000 ┆ 000000000 ┆ 000000000 ┆ ┆ ┆ ┆ ┆ │
│ ┆ 000… ┆ 000… ┆ 000… ┆ ┆ ┆ ┆ ┆ │
│ MSFT ┆ 0.0000000 ┆ 0.0000000 ┆ 0.0000000 ┆ … ┆ 0.8518 ┆ 37.1485% ┆ -2.5940% ┆ -3.9903% │
│ ┆ 000000000 ┆ 000000000 ┆ 000000000 ┆ ┆ ┆ ┆ ┆ │
│ ┆ 000000000 ┆ 000000000 ┆ 000000000 ┆ ┆ ┆ ┆ ┆ │
│ ┆ 000… ┆ 000… ┆ 000… ┆ ┆ ┆ ┆ ┆ │
│ NVDA ┆ 69.223594 ┆ 79.742762 ┆ 0.6922359 ┆ … ┆ 1.4499 ┆ 66.3351% ┆ -4.2895% ┆ -6.2503% │
│ ┆ 62595841 ┆ 32511073 ┆ 462595841 ┆ ┆ ┆ ┆ ┆ │
│ BTC-USD ┆ 20 ┆ 16.315609 ┆ 0.2 ┆ … ┆ 1.2407 ┆ 76.6346% ┆ -5.1001% ┆ -7.9813% │
│ ┆ ┆ 44036094 ┆ ┆ ┆ ┆ ┆ ┆ │
│ Portfolio ┆ 100.00000 ┆ 100 ┆ 1 ┆ … ┆ 1.3019 ┆ 64.4028% ┆ -3.8310% ┆ -5.7063% │
│ ┆ 000000001 ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
└───────────┴───────────┴───────────┴───────────┴───┴───────────┴───────────┴───────────┴──────────┘
performance_chart
Display the performance chart for the portfolio.
Parameters:
height(Optional[int]): Optional height of the plot in pixels, defaults to None.width(Optional[int]): Optional width of the plot in pixels, defaults to None.
Returns:
Plot: Plot object containing the performance chart.
Example:
performance_chart = portfolio.performance_chart()
performance_chart.show()
asset_returns_chart
Display the individual asset returns chart for the portfolio (percentage returns).
Parameters:
height(Optional[int]): Optional height of the plot in pixels, defaults to None.width(Optional[int]): Optional width of the plot in pixels, defaults to None.
Returns:
Plot: Plot object containing the asset returns chart.
Example:
asset_returns_chart = portfolio.asset_returns_chart()
asset_returns_chart.show()
portfolio_value_chart
Display the portfolio total value over time chart.
Parameters:
height(Optional[int]): Optional height of the plot in pixels, defaults to None.width(Optional[int]): Optional width of the plot in pixels, defaults to None.
Returns:
Plot: Plot object containing the portfolio value chart.
Example:
value_chart = portfolio.portfolio_value_chart()
value_chart.show()
returns_matrix
Display the returns correlation matrix for the portfolio.
Parameters:
height(Optional[int]): Optional height of the plot in pixels, defaults to None.width(Optional[int]): Optional width of the plot in pixels, defaults to None.
Returns:
Plot: Plot object containing the returns correlation matrix.
Example:
returns_matrix = portfolio.returns_matrix()
returns_matrix.show()
transaction_history_table
Get the transaction history table for the portfolio.
Returns a table of all transaction events during the simulation, including rebalances, cash flows, and combined events. Each row includes: portfolio value before/after, per-asset values, trade amounts, turnover, cumulative TWR and MWR.
Returns:
DataFrame: Polars DataFrame containing the transaction history.
Example:
history = portfolio_dca.transaction_history_table()
print(history)shape: (23, 11)
┌───────────┬───────────┬───────────┬───────────┬───┬───────────┬───────────┬───────────┬──────────┐
│ Date ┆ Event ┆ Portfolio ┆ Turnover ┆ … ┆ NVDA ┆ BTC-USD ┆ Cumulativ ┆ Cumulati │
│ --- ┆ --- ┆ Value ($) ┆ (%) ┆ ┆ Trade ($) ┆ Trade ($) ┆ e TWR (%) ┆ ve MWR │
│ str ┆ str ┆ --- ┆ --- ┆ ┆ --- ┆ --- ┆ --- ┆ (%) │
│ ┆ ┆ f64 ┆ f64 ┆ ┆ f64 ┆ f64 ┆ f64 ┆ --- │
│ ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ f64 │
╞═══════════╪═══════════╪═══════════╪═══════════╪═══╪═══════════╪═══════════╪═══════════╪══════════╡
│ 2023-02-0 ┆ Cash Flow ┆ 129693.93 ┆ 0.0 ┆ … ┆ 500.0 ┆ 500.0 ┆ 27.693936 ┆ 1857.664 │
│ 1 ┆ ┆ 6343 ┆ ┆ ┆ ┆ ┆ ┆ 857 │
│ 00:00:00 ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
│ 2023-03-0 ┆ Cash Flow ┆ 134080.35 ┆ 0.0 ┆ … ┆ 500.0 ┆ 500.0 ┆ 30.04355 ┆ 416.5346 │
│ 1 ┆ ┆ 0303 ┆ ┆ ┆ ┆ ┆ ┆ 32 │
│ 00:00:00 ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
│ 2023-04-0 ┆ Rebalance ┆ 161179.89 ┆ 8.764454 ┆ … ┆ -9498.825 ┆ -3627.712 ┆ 54.387418 ┆ 490.5191 │
│ 1 ┆ + Cash ┆ 9086 ┆ ┆ ┆ 516 ┆ 711 ┆ ┆ 45 │
│ 00:00:00 ┆ Flow ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
│ 2023-05-0 ┆ Cash Flow ┆ 167928.84 ┆ 0.0 ┆ … ┆ 500.0 ┆ 500.0 ┆ 58.936231 ┆ 309.3739 │
│ 1 ┆ ┆ 2611 ┆ ┆ ┆ ┆ ┆ ┆ 88 │
│ 00:00:00 ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
│ 2023-06-0 ┆ Cash Flow ┆ 190626.58 ┆ 0.0 ┆ … ┆ 500.0 ┆ 500.0 ┆ 78.525607 ┆ 305.8296 │
│ 1 ┆ ┆ 6367 ┆ ┆ ┆ ┆ ┆ ┆ 83 │
│ 00:00:00 ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
│ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … ┆ … │
│ 2024-08-0 ┆ Cash Flow ┆ 403820.81 ┆ 0.0 ┆ … ┆ 500.0 ┆ 500.0 ┆ 240.79136 ┆ 113.7951 │
│ 1 ┆ ┆ 446 ┆ ┆ ┆ ┆ ┆ ┆ 57 │
│ 00:00:00 ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
│ 2024-09-0 ┆ Cash Flow ┆ 406456.50 ┆ 0.0 ┆ … ┆ 500.0 ┆ 500.0 ┆ 241.32783 ┆ 105.2550 │
│ 1 ┆ ┆ 9469 ┆ ┆ ┆ ┆ ┆ 4 ┆ 3 │
│ 00:00:00 ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
│ 2024-10-0 ┆ Rebalance ┆ 411815.91 ┆ 1.922494 ┆ … ┆ 3402.3915 ┆ 649.21635 ┆ 244.14894 ┆ 98.89105 │
│ 1 ┆ + Cash ┆ 4492 ┆ ┆ ┆ 3 ┆ 1 ┆ 3 ┆ 8 │
│ 00:00:00 ┆ Flow ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
│ 2024-11-0 ┆ Cash Flow ┆ 440610.11 ┆ 0.0 ┆ … ┆ 500.0 ┆ 500.0 ┆ 266.54049 ┆ 99.43728 │
│ 1 ┆ ┆ 7468 ┆ ┆ ┆ ┆ ┆ 4 ┆ 9 │
│ 00:00:00 ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
│ 2024-12-0 ┆ Cash Flow ┆ 502512.27 ┆ 0.0 ┆ … ┆ 500.0 ┆ 500.0 ┆ 316.37268 ┆ 107.5773 │
│ 1 ┆ ┆ 4276 ┆ ┆ ┆ ┆ ┆ 1 ┆ 98 │
│ 00:00:00 ┆ ┆ ┆ ┆ ┆ ┆ ┆ ┆ │
└───────────┴───────────┴───────────┴───────────┴───┴───────────┴───────────┴───────────┴──────────┘
update_dates
Update the portfolio’s date range and re-fetch data for out-of-sample evaluation.
This method is for portfolios built from Yahoo Finance data (not custom data). It rebuilds all underlying ticker and benchmark data for the new date range. The optimization result (weights) is preserved so they can be evaluated out-of-sample on the new period.
After calling this method, call performance_stats() to evaluate the optimized weights on the new data (it recomputes automatically).
Parameters:
start_date(str): New start date (e.g.,"2024-01-01")end_date(str): New end date (e.g.,"2024-12-31")
Example:
# Optimize on 2022 data
portfolio = Portfolio(
ticker_symbols=["AAPL", "MSFT"],
start_date="2022-01-01",
end_date="2023-01-01",
objective_function="max_sharpe"
)
# Update to 2023 data for out-of-sample evaluation
portfolio.update_dates("2023-01-01", "2024-01-01")
print(portfolio.performance_stats())
report
Generate a report for the portfolio.
Parameters:
report_type(Optional[str]): The type of report to generate. Options are:"performance"— Performance analysis report"optimization"— Optimization results report
display(Optional[str]): The display mode for the report. Set to"notebook"for Jupyter Notebook display, defaults to opening in the default web browser.
Example:
portfolio.report("performance")
portfolio.report("optimization")
portfolio.report("performance", display="notebook") # Jupyter Notebook