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.
    • 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.
  • constraints (Optional[List[Tuple[float, float]]]): Constraints for optimization.
  • weights (Optional[List[float]]): Weights for the assets in the portfolio. If provided, it will override the optimization process.

Returns:

  • Portfolio: A Portfolio object.

Example:

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",
                      constraints=[(0, 1), (0, 1), (0, 1), (0, 1), (0,1)])
optimization_results

Get the optimization results for the portfolio.

Returns:

  • dict: Dictionary containing the portfolio optimization results.

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_461, 5)
┌───────────┬───────────┬───────────┬───────────┬───────────┐
│ AAPL      ┆ GOOG      ┆ MSFT      ┆ NVDA      ┆ BTC-USD   │
│ ---       ┆ ---       ┆ ---       ┆ ---       ┆ ---       │
│ f64       ┆ f64       ┆ f64       ┆ f64       ┆ f64       │
╞═══════════╪═══════════╪═══════════╪═══════════╪═══════════╡
│ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.0       │
│ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.0       ┆ -2.981929 │
│ -0.972236 ┆ -0.490736 ┆ -1.245142 ┆ -1.600602 ┆ 5.145166  │
│ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.895487  │
│ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.008915  │
│ …         ┆ …         ┆ …         ┆ …         ┆ …         │
│ 0.051787  ┆ -0.966238 ┆ -0.157492 ┆ 0.28004   ┆ 2.169436  │
│ 0.222642  ┆ -0.113135 ┆ 0.323492  ┆ 0.212486  ┆ -1.876028 │
│ -0.542419 ┆ -0.247734 ┆ 0.202506  ┆ 0.0       ┆ -1.23969  │
│ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.136582  │
│ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.0       ┆ 0.256862  │
└───────────┴───────────┴───────────┴───────────┴───────────┘, 'benchmark_returns': shape: (1_461,)
Series: 'roc-1' [f64]
[
    0.0
    0.0
    -0.705987
    0.0
    0.0
    …
    0.143046
    0.037017
    -0.282648
    0.0
    0.0
], 'objective_function': 'Maximize Sharpe Ratio', 'optimization_method': 'Simple Gradient Descent', 'constraints': [(0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0), (0.0, 1.0)], 'optimal_weights': [0.0, 9.629649721936179e-33, 5.483024002279236e-18, 0.654564010144285, 0.345435989855715], 'optimal_portfolio_returns': shape: (1_461,)
Series: 'Portfolio Returns' [f64]
[
    0.0
    -1.030066
    0.729629
    0.309333
    0.003079
    …
    0.932705
    -0.508962
    -0.428233
    0.04718
    0.088729
], 'Daily Return': 0.1841332762535209, 'Daily Volatility': 2.5161473903720664, 'Cumulative Return': 824.5696946448293, 'Annualized Return': 95.71091273736363, 'Annualized Volatility': 48.070928396254196, 'Alpha': -0.028097205442391623, 'Beta': 0.33377542844834635, 'Sharpe Ratio': 1.9494300581194062, 'Sortino Ratio': 2.598689485382585, 'Active Return': 73.30812095365008, 'Active Risk': 35.99398490693686, 'Information Ratio': 2.036676993202882, 'Calmar Ratio': 1.0466953990041188, 'Maximum Drawdown': 91.44103702799117, 'Value at Risk': -3.6996011430145908, 'Expected Shortfall': -5.676228942012344}
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_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.

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()
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()
report

Generate a report for the portfolio.

Parameters:

  • report_type (Optional[str]): The type of report to generate. Options are “performance”.
  • display (Optional[str]): The display mode for the report. set to “notebook” for Jupyter Notebook display, defaults to “browser”.

Example:

portfolio.report("performance")