This project is a Web application that uses Harry Markowitz's Nobel-Prize winning Portfolio Optimization model to generate optimal portfolios1. The user can enter the tickers of the companies they wish to invest in, and the tool will generate an optimal portfolio; it'll show you what percentage of your total capital you should invest in which company.
Some of the key features are:
- Users can calculate the optimal portfolio by simply entering the stock tickers.
- Users can create an account by signing up and can login if they have an existing account.
- Users can save a particular portfolio and can later view all the saved portfolios in their account.
I've created an API through which you can get access to the results of the portfolio optimizer alogorithm in your application. The response schema will be as follows:
{
"weights": array
"returns": number
"volatillity": number
"sharpe ratio": number
}
An example code to make a GET request in your python application:
import requests, json
BASE = "https://sharp-portfolio.herokuapp.com/"
tickers = "MSFT NFLX INFY AMZN"
response = requests.get(BASE + "api/" + tickers)
The tickers
is a space separated string of stock tickers.
The response
generated is a JSON object, and in this case will return a list of weights corresponding to the respective stocks.
You can get access to additional data by passing parameters in the GET request, like so:
response = requests.get(BASE + "api/" + tickers, {"returns": True, "volatility": True, "sharpe_ratio": True})
The three additional parameters will return the percentage return, percentage risk and the sharpe ratio respectively, for the particular set of stocks.
- Flask (Backend framework) - for handling all the application logic
- MongoDB Atlas - to store user information and portfolio data
- HTML
- CSS
The project is deployed on Heroku.
.
└── Portfolio-Optimizer/
├── application/
│ ├── static/
│ │ └── main.css
│ ├── templates/
│ │ ├── about.html
│ │ ├── account.html
│ │ ├── home.html
│ │ ├── layout.html
│ │ ├── login.html
│ │ └── signup.html
│ ├── app.py
│ ├── calculate.py
│ ├── database.py
│ ├── format.py
│ ├── PortfolioOptimizer.py
│ └── user.py
├── .gitignore
├── LICENSE.md
├── Procfile
├── README.md
└── requirements.txt
All the dependencies are listed inside requirements.txt
.
Entire code for the application is inside the application
directory.
The main portfolio optimizing algorithm is written in PortfolioOptimizer.py
and the script for the Flask application is written in app.py
.
database.py
contains all the logic to interact with the database.
- In future I plan to make the application more interactive. Especially the 'account' section, for the users to be able to manage their protfolios more interactively.
- I also plan to make the user interface more appealing adding more javascript and possibly using React.js.
- For any feature request create an issue
Footnotes
-
Markowitz, H. (1952). Portfolio Selection. The Journal of Finance, 7(1), 77–91. https://doi.org/10.1111/j.1540-6261.1952.tb01525.x ↩