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.972198 ┆ -0.490727 ┆ -1.245177 ┆ -1.60056 ┆ 5.145166  │
│ -0.972198 ┆ -0.490727 ┆ -1.245177 ┆ -1.60056 ┆ 0.895487  │
│ …         ┆ …         ┆ …         ┆ …        ┆ …         │
│ 0.222629  ┆ -0.113124 ┆ 0.323462  ┆ 0.212481 ┆ -1.876028 │
│ -0.542411 ┆ -0.24774  ┆ 0.202516  ┆ 0.0      ┆ -1.23969  │
│ -0.542411 ┆ -0.24774  ┆ 0.202516  ┆ 0.0      ┆ 0.136582  │
│ -0.542411 ┆ -0.24774  ┆ 0.202516  ┆ 0.0      ┆ 0.256862  │
└───────────┴───────────┴───────────┴──────────┴───────────┘, 'benchmark_returns': shape: (1_461,)
Series: '' [f64]
[
    0.0
    0.0
    -0.705987
    -0.705987
    -0.705987
    0.353337
    -0.280324
    0.490245
    0.665526
    -0.285518
    -0.285518
    -0.285518
    …
    0.586641
    -1.468427
    1.030147
    0.166006
    0.166006
    0.166006
    0.166006
    0.423169
    0.143046
    0.037017
    -0.282648
    -0.282648
    -0.282648
], '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.0011137223445658465, 0.0, 0.16918753774951317, 0.4484824059002946, 0.38121633400562643], 'optimal_portfolio_returns': shape: (1_461,)
Series: '' [f64]
[
    0.0
    -1.13676
    1.031847
    -0.5882
    -0.926176
    2.073632
    2.323751
    -0.036059
    -0.241061
    1.552802
    -0.44039
    0.896813
    …
    -0.711046
    -0.224612
    1.142026
    0.011898
    -0.323837
    -0.730492
    0.429109
    -0.539468
    0.92603
    -0.564905
    -0.438931
    0.085726
    0.131579
], 'Daily Return': 0.17728848355982682, 'Daily Volatility': 2.485146427319241, 'Cumulative Return': 745.3095266981472, 'Annualized Return': 56.263174024248656, 'Annualized Volatility': 39.450476509604194, 'Alpha': -0.009825574574644953, 'Beta': 0.40254485469585044, 'Sharpe Ratio': 1.3754757565738989, 'Sortino Ratio': 1.9491594586635441, 'Active Return': 33.845371001864756, 'Active Risk': 28.4372172655156, 'Information Ratio': 1.1901787255009426, 'Calmar Ratio': 2.2058559175318075, 'Maximum Drawdown': 25.506277892892957, 'Value at Risk': -3.5070950883940397, 'Expected Shortfall': -5.434861411110765}
performance_stats_table

Display the performance stats table plot 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 stats table.

Example:

performance_stats = portfolio.performance_stats_table()
performance_stats.show()
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()