Development of Automated Financial Analysis Tools using Python
Time Series Visualization, Portfolio Assets' Yearly Return and Co-Variance Matrix Computation, Investment Opportunity Set of Attainable Portfolios Visualization, Portfolio Optimization, Normality Test and Q-Q plot analysis tools or functions were developed through the use of Python.
Prerequisite
In order to develop and utilize the aforementioned financial analysis functions, package "pandas_datareader" for data retrieval from online financial source was installed.
On Unix, the package can be installed by typing in the following command:
sudo apt-get install python-pandas-datareader
The package can also be installed by typing in the following command in Command Prompt:
conda install pandas-datareader
In addition, the following packages have been imported for the development and usage:
As far as financial data source is concerned, the function relies on "Google Finance" for data source; thus, the stock quotes and date, for instance, are consistent with Google Finance stock quote and date formats. In order to allow multiple assets or portfolio to be the input data, each function input data is assumed to follow list format. The following image illustrate this:
Time Series Visualization of Portfolio
Tool or Function for plotting a normalized Time Series of Portfolio composed of assets such as stocks have been developed through the use of Python.
Crawling and Pre-Processing
Through the use of Pandas_Data Reader package, a function that crawls close price of stock from Google Finance was developed. To eliminate empty or N/A values, ".dropna()" function was used. The following Codes Highlight this:
Normalization and Plotting
In order to normalize and plot the time series plot of portfolio, the following codes have been implemented:
The followings highlight full source code for the function and demonstration.
Source Code
Demonstration
Portfolio composed of multiple stocks and Dates were inputted into the function. As a result, normalized stocks within the portfolio were visualized in time series manner as highlighted below:
Yearly Log Return of Assets and Co-Variance Matrix
A function for computing yearly log return for input assets and generating Co-Variance Matrix for input assets have been developed. 252 days were assumed as a yearly trading days.
As far as the development process is concerned, the crawling and pre-processing is similar to the process used for developing function of Time Series Visualization mentioned above. However, each data was shifted in denominator by one to compute returns and during pre-processing. The followings highlight the full source code and demonstration.
Source Code
Demonstration
The results are displayed after running the program and the results may be saved into text file, excel file or even saved into data base using SQL after connecting Python with the data base.
Investment Opportunity Set of Attainable Portfolios
A function for plotting possible sets of portfolio's expected return against volatility have been developed. Also, the function plots level of Sharpe Ratio for each set of portfolio simultaneously.
Crawling
Retrieving financial data and pre-processing is similar to that of functions mentioned above.
Monte Carlo Simulation
There is no limit to how many simulations that function can conduct; however, this function conducts 50,000 Monte Carlo Simulations in which generates 50,000 random Portfolio weights and assigns each weight to a possible set of portfolio.
Plotting
The 50,000 possible sets of portfolio expected return against volatility, as measured by standard deviation, are plotted by the function and Sharpe Ratio level has been illustrated in a color bar. Sharpe Ratio formula has been used for the color bar plotting.
The followings highlight the full source code and demonstration
Source Code
*Additional input parameter, risk free ratio, has been added for computing and Sharpe Ratio level for each set of possible portfolio.
Demonstration
*Conducting 50,000 Monte Carlo Simulation takes time (1~2 minutes) particularly when a portfolio size is big. This can be solved through Parallel Computing using technologies such as Hadoop Spark in which Python codes can be implemented.
Optimal Investment Portfolio
A function that automatically computes Optimal Investment Portfolio Asset Allocation ratios for: 1) Maximum Sharpe Ratio of Portfolio
2) Minimum Volatility of Portfolio
has been developed.
The function also computes expected return, volatility, as measured by standard deviation, and Sharpe Ratio for each criteria.
"scipy.optimize" package was used for this development.
Crawling
The process of retrieving financial data from online financial source is similar to those of mentioned above.
Pre-Processing and Monte Carlo Simulation
The pre-processing such as dropping N/A values and conducting Monte Carlo Simulation are same processes mentioned in "Investment Opportunity Set of Attainable Portfolios" section. Similar to the previous section, 10,000 Monte Carl Simulations are conduced whenever this function is utilized.
Function for returning important portfolio statistics
For future convenience, a new function that returns return, volatility, Sharpe Ratio was developed.
The following code highlights this:
Maximizing Sharpe Ratio
In order to accomplish optimization, "scipy.optimize.minimize" package was used. Minimize function computes minimization problem with constraints.
Sharpe Ratio can be defined as the following:
In order to maximize Sharpe Ratio, negative value of Sharpe Ratio can be minimized.
This can be achieved by the following:
As constraints, the following codes can be implemented in which weights should be between zero and 1.
The constraints are later inserted into minimize function as tuples.
All the variables and function are inserted into minimize function as the following:
The 'X' value which contains Asset Allocation Ratio information is extracted from 'opts' result and the expected return, Volatility and Sharpe Ratio values were addressed as well.
Minimize Volatility
Portfolio Optimization can be achieved by minimizing volatility or standard deviation as well. To achieve this, a separate function was developed:
With same constraints and boundaries, Optimal Asset Allocation Ratios can be achieved. Similarly, expected return, volatility and Sharpe Ratio can be computed for this Optimized Portfolio.
The followings highlight full source code and demonstration
Source Code
Demonstration
*Asset Allocation Ratios are printed in the order of Portfolio Assets. For instance, the portfolio in the example has Asiana, Korean_Air, S_oil, SK_Innovation, Samsung_Elec, LG_Elec and Naver_Corp as assets. This order are reflected and hence, we can conclude that 45% of money should be allocated to Samsung_Elec and 55% of money should be allocated to Naver_Corp in order to achieve a Portfolio with Maximum Sharpe Ratio.
Normality Test
A function that conducts normality test of each asset in a portfolio was developed. The function computes size, min, max, mean, std, skew, kurtosis based on distribution of stock log returns. Also, the function computes p-value to check normality of each asset.
*If p-value of Normality test is lower than 0.05, we reject the Null Hypothesis and say that the stock return does not follow normal distribution.
*If p-value of Normality test is bigger than 0.05, we accept the Null Hypothesis and say that the stock return follows like geometric brown motion, and thus follow normal distribution.
Crawling and Pre-Processing
The process of retrieving and pre-processing is same as that of process mentioned in other function development process.
Addressing Statistical Analysis
Using "Scipy" package, statistics such as size, min, max and kurtosis are printed for each asset in the portfolio using for loop and histogram for highlighting each asset's log return distribution for normality test is plotted by this function. The followings highlight full source code and demonstration
Source Code
Demonstration
Quantile-Quantile Plot
Through the use of Quantile-Quantile plot, one can see that if an asset return follows normal distribution or not. If the plot displays straight line, we can assume that it is a sign of normal distribution; however, if the plot displays curved or not straight behavior, one can assume that it is a sign of fat tail, outlier and not normal.
For this function, additional parameter called "chosen_stock_quote" has been added to ensure the user which asset he or she want to examine from the portfolio. The input parameter for "chosen_stock_quote" must be in Google Finance stock quote format.
Crawling and Pre-Processing
The process of retrieving and pre-processing is same as that of process mentioned in other function development process.
Plotting
Using "statsmodel.api" package, a function that generates Quantile-Quantile plot was developed.
The followings highlight the full source code and demonstration
Source Code
Demonstration
*An asset or stock "Asiana" from the portfolio is selected for Q-Q plot.
The plot highlights that the stock return for Asiana Airline does not follow normal distribution at it is not in the straight line but rather in curve line.
0コメント