Skip to main content

A library for Portfolio management and optimization

Project description

portfolio_optimizer

PyPI version Downloads

A small python library for analysis of historical data of stocks. Especially for portfolio management and optimization.

Installation

pip install portfolio-optimizer0

Note: The library is still in development, so the version number is 0. You will need to call pip install portfolio-optimizer0 to install the library. However, you can import the library as import portfolio_optimizer.

After installation, you can import the library as follows:

import portfolio_optimizer as po

Usage

The 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.

from po.load_data import LoadData   # for loading data
from po.portfolio import Portfolio  # for portfolio management
from po.optimizer import Optimizer  # for optimization purpose
from po.frontier import EfficientFrontier  # for the plot of Efficient Frontier

# loading the data from NSE exchange you can use BSE also
ld = LoadData()
data_dic = ld.load_data(['HDFCBANK', 'RELIANCE', 'INFY', 'BAJAJ-AUTO', 'TATAMOTORS'], 'NSE')

# loading the benchmark separately, Indices are always downloaded from nasdaq
benchmark = 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 portfolio
portfolio.add_stock(data_dic)

# You can view you Portfolio summary also by
portfolio.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.0870947316063777
Portfolio Risk at equals weights: 0.5528354886819963
# Creating a Optimizer object and adding the portfolio
model = Optimizer()
model.add_portfolio(portfolio=portfolio)

# Optimizing the portfolio using CAPM
risk = 1
model_ = "capm"
optimized_res = model.optimize_portfolio(model=model, max_risk=risk)
print(optimized_res)
Optimized successfully.
Expected Portfolio's Returns : 1.1792
Risk : 0.8001
Expected weights:
--------------------
['HDFCBANK']: 47.44%
['RELIANCE']: 29.62%
['INFY']: 0.00%
['BAJAJ-AUTO']: 0.00%
['TATAMOTORS']: 22.94%

More Examples

For more detailed go through of the library, please refer to the notebook Walk through portfolio_optimizer

Documentation

The documentation is available at Documentation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

portfolio_optimizer0-0.3.2.tar.gz (4.2 MB view hashes)

Uploaded Source

Built Distribution

portfolio_optimizer0-0.3.2-py3-none-any.whl (15.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page