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.
  • constraints (Optional[List[Tuple[float, float]]]): Constraints for optimization.

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': '1d', '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.972214 ┆ -0.490732 ┆ -1.24518  ┆ -1.600599 ┆ 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.966242 ┆ -0.157471 ┆ 0.280033  ┆ 2.169436  │
│ 0.22264   ┆ -0.113131 ┆ 0.323461  ┆ 0.21249   ┆ -1.876028 │
│ -0.542425 ┆ -0.247741 ┆ 0.202517  ┆ 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: '' [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, 0.0, 0.0, 0.644976443153294, 0.355023556846706], 'optimal_portfolio_returns': shape: (1_461,)
Series: '' [f64]
[
    0.0
    -1.058655
    0.794306
    0.317919
    0.003165
    …
    0.950816
    -0.528983
    -0.440119
    0.04849
    0.091192
], 'Daily Return': 0.18411850183442402, 'Daily Volatility': 2.516041287507096, 'Cumulative Return': 824.2277142199297, 'Annualized Return': 58.971064110412286, 'Annualized Volatility': 39.94091721068724, 'Alpha': -0.027832253294844934, 'Beta': 0.33236318140694787, 'Sharpe Ratio': 1.4263834706121417, 'Sortino Ratio': 1.8999668142605184, 'Active Return': 46.17296286172934, 'Active Risk': 29.982240391057896, 'Information Ratio': 1.540010428156672, 'Calmar Ratio': 0.6448516277607749, 'Maximum Drawdown': 91.44904280568738, 'Value at Risk': -3.6839837246076517, 'Expected Shortfall': -5.673427689724985}
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”.

Example:

portfolio.report("performance")