A program for financial portfolio management, analysis and optimisation.
FinQuant is a program for financial portfolio management, analysis and optimisation.
This README only gives a brief overview of FinQuant. The interested reader should refer to its documentation.
Within a few lines of code, FinQuant can generate an object that holds your stock prices of your desired financial portfolio, analyses it, and can create plots of different kinds of Returns, Moving Averages, Moving Average Bands with buy/sell signals, and Bollinger Bands. It also allows for the optimisation based on the Efficient Frontier or a Monte Carlo run of the financial portfolio within a few lines of code. Some of the results are shown here.
finquant.portfolio.build_portfoliois a function that eases the creating of your portfolio. See below for one of several ways of using
from finquant.portfolio import build_portfolio names = ['GOOG', 'AMZN', 'MCD', 'DIS'] start_date = '2015-01-01' end_date = '2017-12-31' pf = build_portfolio(names=names, start_date=start_date, end_date=end_date)
pfis an instance of
finquant.portfolio.Portfolio, which contains the prices of the stocks in your portfolio. Then...
GOOG AMZN MCD DIS Date 2015-01-02 524.81 308.52 85.783317 90.586146 2015-01-05 513.87 302.19 84.835892 89.262380 2015-01-06 501.96 295.29 84.992263 88.788916
Nicely printing out the portfolio's properties
pf.properties()Depending on the stocks within your portfolio, the output looks something like the below.
Stocks: GOOG, AMZN, MCD, DIS Time window/frequency: 252 Risk free rate: 0.005 Portfolio expected return: 0.266 Portfolio volatility: 0.156 Portfolio Sharpe ratio: 1.674
Skewness: GOOG AMZN MCD DIS 0 0.124184 0.087516 0.58698 0.040569
Kurtosis: GOOG AMZN MCD DIS 0 -0.751818 -0.856101 -0.602008 -0.892666
Information: Allocation Name 0 0.25 GOOG 1 0.25 AMZN 2 0.25 MCD
### Cumulative Return
pf.compcumulativereturns().plot().axhline(y = 0, color = "black", lw = 3) ``` yields
from finquant.moving_average import compute_ma, ema # get stock data for disney dis = pf.get_stock("DIS").data.copy(deep=True) spans = [10, 50, 100, 150, 200] ma = compute_ma(dis, ema, spans, plot=True)
from finquant.moving_average import plot_bollinger_band # get stock data for disney dis = pf.get_stock("DIS").data.copy(deep=True) span=20 plot_bollinger_band(dis, sma, span)
# performs and plots results of Monte Carlo run (5000 iterations) opt_w, opt_res = pf.mc_optimisation(num_trials=5000) # plots the results of the Monte Carlo optimisation pf.mc_plot_results() # plots the Efficient Frontier pf.ef_plot_efrontier() # plots optimal portfolios based on Efficient Frontier pf.ef.plot_optimal_portfolios() # plots individual plots of the portfolio pf.plot_stocks()
As it is common for open-source projects, there are several ways to get hold of the code. Choose whichever suits you and your purposes best.
FinQuant depends on the following Python packages: - python>=3.5.0 - numpy>=1.15 - pandas>=0.24 - matplotlib>=1.5.1 - quandl>=3.4.5 - yfinance>=0.1.43 - scipy>=1.2.0 - pytest>=2.8.7
FinQuant can be obtained from PyPI
pip install FinQuant
Get the code from GitHub:
git clone https://github.com/fmilthaler/FinQuant.git
python setup.py install
Alternatively, if you do not wish to install FinQuant, you can also download/clone it as stated above, and then make sure to add it to your
This is the core of FinQuant.
finquant.portfolio.Portfolioprovides an object that holds prices of all stocks in your portfolio, and automatically computes the most common quantities for you. To make FinQuant an user-friendly program, that combines data analysis, visualisation and optimisation, the object provides interfaces to the main features that are provided in the modules in
To learn more about the object, please read through the documentation, docstring of the module/class, and/or have a look at the examples.
finquant.portfolio.Portfolioalso provides a function
build_portfoliowhich is designed to automatically generate an instance of
Portfoliofor the user's convenience. For more information on how to use
build_portfolio, please refer to the documentation, its
docstringand/or have a look at the examples.
Daily returns of stocks are often computed in different ways. FinQuant provides three different ways of computing the daily returns in
finquant.returns: 1. The cumulative return: 2. Percentage change of daily returns: 3. Log Return:
In addition to those, the module provides the function
historical_mean_return(data, freq=252), which computes the historical mean of the daily returns over a time period
finquant.moving_averageallows the computation and visualisation of Moving Averages of the stocks listed in the portfolio is also provided. It entails functions to compute and visualise the -
sma: Simple Moving Average, and -
ema: Exponential Moving Average. -
compute_ma: a Band of Moving Averages (of different time windows/spans) including Buy/Sell signals -
plot_bollinger_band: a Bollinger Band for -
An implementation of the Efficient Frontier (
finquant.efficient_frontier.EfficientFrontier) allows for the optimisation of the portfolio for -
minimum_volatilityMinimum Volatility, -
maximum_sharpe_ratioMaximum Sharpe Ratio -
efficient_returnMinimum Volatility for a given expected return -
efficient_volatilityMaximum Sharpe Ratio for a given target volatility
by performing a numerical solve to minimise/maximise an objective function.
Often it is useful to visualise the Efficient Frontier as well as the optimal solution. This can be achieved with the following methods: -
plot_efrontier: Plots the Efficient Frontier. If no minimum/maximum Return values are provided, the algorithm automatically chooses those limits for the Efficient Frontier based on the minimum/maximum Return values of all stocks within the given portfolio. -
plot_optimal_portfolios: Plots markers of the portfolios with the Minimum Volatility and Maximum Sharpe Ratio.
For reasons of user-friendliness, interfaces to these functions are provided in
finquant.portfolio.Portfolio. Please have a look at the documentation.
Alternatively a Monte Carlo run of
ntrials can be performed to find the optimal portfolios for - minimum volatility, - maximum Sharpe ratio
The approach branded as Efficient Frontier should be the preferred method for reasons of computational effort and accuracy. The latter approach is only included for the sake of completeness, and creation of beautiful plots.
For more information about the project and details on how to use it, please look at the examples provided in
Note: In the below examples,
pfrefers to an instance of
finquant.portfolio.Portfolio, the object that holds all stock prices and computes its most common quantities automatically. To make FinQuant a user-friendly program, that combines data analysis, visualisation and optimisation, the object also provides interfaces to the main features that are provided in the modules in
./finquant/and are discussed throughout this README.
./example/Example-Build-Portfolio-from-web.py: Shows how to use FinQuant to build a financial portfolio by downloading stock price data through the Python package
./example/Example-Build-Portfolio-from-file.py: Shows how to use FinQuant to build a financial portfolio by providing stock price data yourself, e.g. by reading data from disk/file.
./example/Example-Analysis.py: This example shows how to use an instance of
finquant.portfolio.Portfolio, get the portfolio's quantities, such as - Expected Returns, - Volatility, - Sharpe Ratio.
It also shows how to extract individual stocks from the given portfolio. Moreover it shows how to compute and visualise: - the different Returns provided by the module
finquant.returns, - Moving Averages, a band of Moving Averages, and a Bollinger Band.
./example/Example-Optimisation.py: This example focusses on the optimisation of a portfolio. To achieve this, the example shows the usage of
finquant.efficient_frontier.EfficientFrontierfor optimising the portfolio, for the - Minimum Volatility - Maximum Sharpe Ratio - Minimum Volatility for a given target Return - Maximum Sharpe Ratio for a given target Volatility.
Furthermore, it is also shown how the entire Efficient Frontier and the optimal portfolios can be computed and visualised. If needed, it also gives an example of plotting the individual stocks of the given portfolio within the computed Efficient Frontier.
Also, the optimisation of a portfolio and its visualisation based on a Monte Carlo is shown.
Finally, FinQuant's visualisation methods allow for overlays, if this is desired. Thus, with only the following few lines of code, one can create an overlay of the Monte Carlo run, the Efficient Frontier, its optimised portfolios for Minimum Volatility and Maximum Sharpe Ratio, as well as the portfolio's individual stocks.