Portfolio Tools
The portfoliotools module provides functions for portfolio optimization, simulation, and analysis.
Portfolio Optimization
- openseries.portfoliotools.efficient_frontier(eframe, num_ports=5000, seed=71, bounds=None, frontier_points=200, minimize_method='SLSQP', *, tweak=True)[source]
Identify an efficient frontier.
- Parameters:
eframe (OpenFrame) – Portfolio data.
num_ports (int) – Number of possible portfolios to simulate. Defaults to 5000.
seed (int) – The seed for the random process. Defaults to 71.
bounds (tuple[tuple[float, float], ...] | None) – The range of minimum and maximum allowed allocations for each asset.
frontier_points (int) – Number of points along frontier to optimize. Defaults to 200.
minimize_method (LiteralMinimizeMethods) – The method passed into the scipy.minimize function. Defaults to SLSQP.
tweak (bool) – Cutting the frontier to exclude multiple points with almost the same risk. Defaults to True.
- Returns:
The efficient frontier data, simulation data and optimal portfolio.
- Return type:
tuple[DataFrame, DataFrame, NDArray[float64]]
Portfolio Simulation
- openseries.portfoliotools.simulate_portfolios(simframe, num_ports, seed)[source]
Generate random weights for simulated portfolios.
Portfolio Constraints
- openseries.portfoliotools.constrain_optimized_portfolios(data, serie, portfolioname='Current Portfolio', simulations=10000, curve_points=200, bounds=None, minimize_method='SLSQP')[source]
Constrain optimized portfolios to those that improve on the current one.
- Parameters:
data (OpenFrame) – Portfolio data.
serie (OpenTimeSeries) – A timeseries representing the current portfolio.
portfolioname (str) – Name of the portfolio. Defaults to “Current Portfolio”.
simulations (int) – Number of possible portfolios to simulate. Defaults to 10000.
curve_points (int) – Number of optimal portfolios on the efficient frontier. Defaults to 200.
bounds (tuple[tuple[float, float], ...] | None) – The range of minimum and maximum allowed allocations for each asset.
minimize_method (LiteralMinimizeMethods) – The method passed into the scipy.minimize function. Defaults to SLSQP.
- Returns:
The constrained optimal portfolio data.
- Return type:
tuple[OpenFrame, OpenTimeSeries, OpenFrame, OpenTimeSeries]
Visualization
- openseries.portfoliotools.prepare_plot_data(assets, current, optimized)[source]
Prepare data to be used as point_frame in the sharpeplot function.
- Parameters:
assets (OpenFrame) – Portfolio data with individual assets and a weighted portfolio.
current (OpenTimeSeries) – The current or initial portfolio based on given weights.
optimized (NDArray[float64]) – Data optimized with the efficient_frontier method.
- Returns:
The data prepared with mean returns, volatility and weights.
- Return type:
DataFrame
- openseries.portfoliotools.sharpeplot(sim_frame=None, line_frame=None, point_frame=None, point_frame_mode='markers', filename=None, directory=None, titletext=None, output_type='file', include_plotlyjs='cdn', *, title=True, add_logo=True, auto_open=True)[source]
Create scatter plot coloured by Sharpe Ratio.
- Parameters:
sim_frame (DataFrame | None) – Data from the simulate_portfolios method.
line_frame (DataFrame | None) – Data from the efficient_frontier method.
point_frame (DataFrame | None) – Data to highlight current and efficient portfolios.
point_frame_mode (LiteralLinePlotMode) – Which type of scatter to use. Defaults to markers.
filename (str | None) – Name of the Plotly html file.
directory (DirectoryPath | None) – Directory where Plotly html file is saved.
titletext (str | None) – Text for the plot title.
output_type (LiteralPlotlyOutput) – Determines output type. Defaults to “file”.
include_plotlyjs (LiteralPlotlyJSlib) – Determines how the plotly.js library is included in the output. Defaults to “cdn”.
title (bool) – Whether to add standard plot title. Defaults to True.
add_logo (bool) – Whether to add Captor logo. Defaults to True.
auto_open (bool) – Determines whether to open a browser window with the plot. Defaults to True.
- Returns:
The scatter plot with simulated and optimized results.
- Return type: