OptiFolio is a package for portfolio optimization. For optimization, a SciPy optimizer is used, while results can be visualized with Bokeh plots.
The package can also be seamlessly integrated with Yahoo Finance API, using Pandas Data Reader.
OptiFolio can be installed from PyPI:
pip install optifolio
- PortfolioOptimizer [object]: Optimize your portfolio based on Sharpe Ratio.
- fit [method]: Fits daily stock data into the optimizer. Generates annual measures.
- plot_efficient_frontier [method]: Generates a plot for efficient frontier, optimal portfolio, and individual stocks.
- plot_weights [method]: Creates a pie chart that displays portfolio weights for each ticker.
- plot_cumulative_return [method]: Generates a time series plot that displays portfolio performance over time.
The figures are generated with Bokeh, enabling easy implementation to modern web browsers.
from optifolio import PortfolioOptimizer
# Data from Yahoo Finance with Pandas Data Reader
import pandas_datareader.data as web
data = web.DataReader(['AMZN', 'AAPL', 'MSFT',
'NFLX', 'TSLA', 'BABA', 'JD'],
'yahoo',
start='2015/01/01',
end='2019/12/31')['Adj Close']
# Initiate the optimizer
model = PortfolioOptimizer()
# Optimize (w. max Sharpe Ratio)
model.fit(data, obj='sharpe')
model.plot_efficient_frontier()
model.plot_weights()
# Adding a benchmark to compare against
benchmark = web.DataReader(['^GSPC'],
'yahoo',
start='2015/01/01',
end='2019/12/31')['Adj Close']
model.plot_cumulative_return(benchmark_data=benchmark)
Kristian Bonnici
Contributions, issues, and feature requests are welcome!
Give a ⭐️ if you like this project!