A library for Portfolio management and optimization
Project description
portfolio_optimizer
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 asimport 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for portfolio_optimizer0-0.3.2.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 67e67f2e61858cdbacebf3cb0f9910444e00512979ed85389ba7a5e6de9b1165 |
|
MD5 | c59aa160bd8ad742d4942863fbb86e34 |
|
BLAKE2b-256 | 9dba7de47eee4b1d8938767baaa833cacf57c86a27d4baa6ce498eb0bea2d354 |
Hashes for portfolio_optimizer0-0.3.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f18fe21914ff47d5024d6af5944d331110ac34cff5a651f8fc2f0f043962511e |
|
MD5 | e0f12d2321cba4387403652d3baf1f8d |
|
BLAKE2b-256 | df86b838978d39ba649993623058ede52e4efe297e8a9c1df9f2248203dd30c6 |