Purpose
To develop an easy to use utility tool that downloads historical prices of an asset and then allows us to perform various statistical analysis along with charting.
What does it do?
Before we get into the details, I would like to mention the functionalities provided by this tool. Its essence lies in the ease to use and a onestop shop for all the basic tools data modeling, analytics, and plotting.
Here is the list of functionalities our Stock Analyzer is capable of:
Let's Begin
For all the examples below, I will use Tesla stock (Nasdaq Ticker = TSLA).
In order to initiate an object of this class, we need to pass a Stockticker. As soon as we pass a ticker to the constructor, appropriate calls are made to Quandl/Yahoo/Morningstar APIs to fetch historical prices & volume data for this ticker. By default, the calls are made to collect last 252 trading days data; but one can tweak that by also providing a number to optional hist_start_date parameter. Another optional parameter is refresh, which does not make calls to the web if previously downloaded historical data is already stored in local files. Further, along with downloading data for this ticker, additional calls are made to download data for SNP 500 Index. As we will see later, this is needed to perform linear regression and various charting options.
Download historical Prices & Volume from Web
Output after downloading date
Basic Statistical Analysis
Now, we have the data, so we can perform the basic statistical data analysis by leveraging Pandas' useful functions for Mean, Standard Deviation, Daily Returns.
Compute basic statistical properties
Output for basic statistical properties
Plot Daily Prices
plot function of this class provides a convenient utility to draw chart for daily close prices of the stock. My implementation is based on Bokeh library; but if we provide optional parameter useMatplot then the chart is plotted using the famous Matplotlib. I personally prefer Bokeh as it provides interactive charts try it out!
Generate plot for Daily prices
Plot Daily Returns
plot_returns function of this class provides a convenient utility to draw chart for daily returns of the stock. Just like plot function (above), it also provides ability to draw chart either using Bokeh or matplotlib.
Generate plot for Daily Returns
Plot returns against SNP 500
plot_returns_against_snp500 function provides us the utility to visualize returns of the Stock against the returns of SNP 500 index. Similar to the above two functions, it provides plot in both matplotlib as well as the interactive webbased browser chart using Bokeh.
Generate plot for Daily Returns against that of SNP500
Plot Candlestick chart with Volume
Candlestick plots are the most popular charts when it comes to visualizing stock prices grouped under fixed intervals. Stock Analyzer's plot_candlestick function provides this ability to visualize these historical prices in a candlestick chart. Like other plots show above, we will go through the code later in this blog.
Generate Candlestick chart with Volume
Plotting dynamic Moving Averages
One of the most convenient use of my tool Stock Analyzer, is to dynamically generate moving averages and have them plotted for visualization. plot_moving_averages method takes in below three optional parameters:
Generate Moving Averages and Plot them along with daily Price
Code to calculate moving averages
Linear Regression against the Index
We will now apply Linear regression on these returns and see how strong is the relationship between SNP 500 returns with TSLA returns.
Below is the sourcecode we use to apply Linear regression on these two set of values.
Apply Ordinary Least Square model to perform Linear regression
Once the OLS model is applied, we can go ahead plot the regression values using the below function.
Plot OLS on Returns
Finally, one does not need to code above and just use the below functions to apply linear regression, plot it and retrieve Stock's Alpha and Beta along with model's RStatistics, FStatistic, etc  simple to use!
Apply Ordinary Least Square model to perform Linear regression
Output Stock's Alpha and Beta against the Index
Source Code
Your comment will be posted after it is approved.
Leave a Reply. 
AuthorI am a passionate, driven polyglot programmer and architect with a knack of solving complex problems in quick and efficient way. Along with programming, software development, financial products, and management expertise, I also bring skills in statistical modeling, empowering me to work on challenging projects that require combination of software development and quantitative analysis. Categories
All
