Package: portfolio-optimizer0
portfolio_optimizerA small python library for analysis of historical data of stocks. Especially for portfolio management and optimization.Installationpipinstallportfolio-optimizer0Note: The library is still in development, so the version number is 0. You will need to callpip install portfolio-optimizer0to install the library. However, you can import the library asimport portfolio_optimizer.After installation, you can import the library as follows:importportfolio_optimizeraspoUsageThe end goal of the library is to provide a simple interface for portfolio management and optimization. The library is still in development, so the interface is not yet stable. The following example shows how to use the library to optimize a portfolio of stocks.frompo.load_dataimportLoadData# for loading datafrompo.portfolioimportPortfolio# for portfolio managementfrompo.optimizerimportOptimizer# for optimization purposefrompo.frontierimportEfficientFrontier# for the plot of Efficient Frontier# loading the data from NSE exchange you can use BSE alsold=LoadData()data_dic=ld.load_data(['HDFCBANK','RELIANCE','INFY','BAJAJ-AUTO','TATAMOTORS'],'NSE')# loading the benchmark separately, Indices are always downloaded from nasdaqbenchmark=ld.load_data(['^NSEI'],'nasdaq')# Creating a Portfolio object, with the freq="M" and benchmark as NSEI (Nifty 50)portfolio=Portfolio(freq="M",benchmark=benchmark)# Adding the stocks to the portfolioportfolio.add_stock(data_dic)# You can view you Portfolio summary also byportfolio.portfolio_summary()Portfolio Summary*****************Stocks in the Portfolio : *************************{"['HDFCBANK', 'RELIANCE', 'INFY', 'BAJAJ-AUTO', 'TATAMOTORS']"}Beta :******|      |   HDFCBANK |   RELIANCE |     INFY |   BAJAJ-AUTO |   TATAMOTORS ||------+------------+------------+----------+--------------+--------------|| beta |    1.00174 |    1.04458 | 0.543611 |      1.01237 |      1.59273 |Expected Returns :******************|    |   HDFCBANK |   RELIANCE |     INFY |   BAJAJ-AUTO |   TATAMOTORS ||----+------------+------------+----------+--------------+--------------||  0 |    1.05617 |    1.09172 | 0.676051 |        1.065 |      1.54654 |The covariance matrix is as follows***********************************|            |   HDFCBANK |   RELIANCE |       INFY |   BAJAJ-AUTO |   TATAMOTORS ||------------+------------+------------+------------+--------------+--------------|| HDFCBANK   | 0.00565169 | 0.00383894 | 0.00144691 |   0.00458971 |   0.00610401 || RELIANCE   | 0.00383894 | 0.00795272 | 0.00227559 |   0.00466296 |   0.0060295  || INFY       | 0.00144691 | 0.00227559 | 0.00679807 |   0.00154204 |   0.00296804 || BAJAJ-AUTO | 0.00458971 | 0.00466296 | 0.00154204 |   0.0110113  |   0.00849118 || TATAMOTORS | 0.00610401 | 0.0060295  | 0.00296804 |   0.00849118 |   0.0228973  |Portfolio Returns at equals weights: 1.0870947316063777Portfolio Risk at equals weights: 0.5528354886819963# Creating a Optimizer object and adding the portfoliomodel=Optimizer()model.add_portfolio(portfolio=portfolio)# Optimizing the portfolio using CAPMrisk=1model_="capm"optimized_res=model.optimize_portfolio(model=model,max_risk=risk)print(optimized_res)Optimized successfully.Expected Portfolio's Returns : 1.1792Risk : 0.8001Expected weights:--------------------['HDFCBANK']: 47.44%['RELIANCE']: 29.62%['INFY']: 0.00%['BAJAJ-AUTO']: 0.00%['TATAMOTORS']: 22.94%More ExamplesFor more detailed go through of the library, please refer to the notebookWalk through portfolio_optimizerDocumentationThe documentation is available atDocumentation