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 one-stop shop for all the basic tools data modeling, analytics, and plotting.
Here is the list of functionalities our Stock Analyzer is capable of:
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 Stock-ticker. 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 web-based browser chart using Bokeh.
Generate plot for Daily Returns against that of SNP-500
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 source-code 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 R-Statistics, F-Statistic, etc - simple to use!
Apply Ordinary Least Square model to perform Linear regression
Output Stock's Alpha and Beta against the Index
To calculate prices of European Call and Put options using the famous Black & Scholes method. Attempt has been made to perform this calculation even for Dividend paying stocks; though still need some improvements.
Black Scholes Method
We will calculate prices for European options by applying Black Scholes method. As per Investopedia:
"The Black-Scholes formula (also called Black-Scholes-Merton) was the first widely used model for option pricing. It's used to calculate the theoretical value of European-style options using current stock prices, expected dividends, the option's strike price, expected interest rates, time to expiration and expected volatility."
Black-Scholes method only works for European options as they can be exercised only on the expiry date.
As we can see, the required input for Black-Scholes method is same as the one we had downloaded and derived in Part-1 of this blog. Therefore, in this blog we will skip details on how to get historical asset prices and use them to calculate asset volatility.
Calculate Black-Scholes' d1 and d2
Now, we have all the underlying data; so let's aim to codify the above mentioned Black-Scholes equations. The famous d1 and d2 values can be calculated using the below code:
Once we have values for d1 and d2, we can plug these values in the overall equations for Call and Put option price as shown below:
If dividend is not zero, then it is subtracted from the risk free rate in the below calculations.
Calculating Call and Put option prices using Black-Scholes method
Running again for TSLA short-term option
In our previous blog, we ran Monte-Carlo simulations to calculate price of American option on TSLA stock expiring Sept 7th with Strike price of $370. We will use the same parameter here to calculate the price of European options using our above code.
Since the expiry is just a month ahead, prices of European options should be close to those of American ones. This is because time to expiry is short; giving lesser chance to the American option holder to exercise - the only difference between the two option types.
Once we run the code as available in my GitHub repository here; we get the following output.
Output of calculating European option prices using Black-Scholes
As we can see, these values are extremely close to the ones we got when running Monte-Carlo simulations. As a reminder, from that blog we got these values:
INFO: ### Call Price calculated at 11.162400
INFO: ### Put Price calculated at 25.102147
Prices compared - $11.159 vs $11.162 and $25.137 vs $25.102; it definitely looks we are consistent in our approach with these short duration options.
European Call and Put options must maintain a relationship called as Put-Call parity. As Investopedia explains here:
"Put-call parity is a principle that defines the relationship between the price of European put options and European call options of the same class, that is, with the same underlying asset, strike price and expiration date."
This must be true always; else there will be an arbitrage opportunity -i.e. returns without any risks. Basically, Put-Call parity states that below relationship must always be held:
CALLprice + PV( Strikeprice ) = PUTprice + SPOTprice
We can verify whether this parity holds using the below code:
Output for Put-Call parity
As we can see above, both European Call and Put option prices do maintain the Put-Call parity.
To apply Monte-Carlo simulations and calculate price of American style options.
Use of Monte-Carlo Simulations
In order to calculate Option prices, we need to calculate the expected price at Expiry date of the underlying asset. One of the ways to do this is by running Monte-Carlo simulations as I had pointed out in my previous blog.
Calculate Expected Payoffs
Once we have expected prices calculated, next step is to calculate the Option payoffs for those prices.
Present Value of expected Payoffs
Now, we have the expected Options payoffs; so the next step is to calculate their Present Value using the discount factor dependent on risk-free rate and time to expiry (in years).
We use the above discount factor to calculate the present-value of option payoffs returns through the Monte-Carlo simulations.
Ct = PV(E[max(0,PriceAtExpiry−Strike)])
Pt = PV(E[max(0,Strike−PriceAtExpiry)])
Below code performs this exact calculation and returns the American Call and Put Option prices
Calculate Present Value of Expected Option Payoffs
Running for an extremely volatile asset - TSLA
We have everything setup, so lets start these simulations and calculations on TSLA stock to calculate prices for Call and Put option expiring Sept 7th, 2018 (i.e. 1 month from now) and with Strike price of $370.
The output we receive is Call Option priced at $11.162 and Put option prices at $25.102. Given the current Spot price is $355.49 (as of EOD Aug 10th), this price somewhat looks fine; but let's confirm it with other sites.
Output for TSLA Strike 370 expiring September 7th, 2018
As we can see from above Yahoo screenshots, our calculated option prices are pretty close to the ones found in Yahoo. I wonder if the recent news of Tesla going private adds additional volatility to the stock; causing this minor increase in both Call and Put option prices. What do you think?
To perform Monte-Carlo simulations and use it to calculate expected price at a given future date.
What are Monte-Carlo Simulations?
Monte-Carlo simulations are basically simulations of probability. As per investopedia:
"Monte Carlo simulations are used to model the probability of different outcomes in a process that cannot easily be predicted due to the intervention of random variables. "
Basically, given a set of parameters and some random variables, we perform thousands of simulations and then seek expected outcome.
Gaussian Process and Brownian Motion
In our case, we will make use of Gaussian Process that provides us with the random variable and then use it along with Asset Volatility, Risk-free rate, and time duration to calculate the expected price at a given future date.
This continuous-time stochastic process represents the Brownian motion (aka the Wiener Process).
Primary use of this Gaussian based Monte-Carlo simulations is to generate asset prices is used to calculate American Option prices, which we will go over in another blog.
Below is the Python library to generate value from Gaussian distribution.
Gaussian random variable in Python
Fetch underlying data
In order to successfully apply Monte-Carlo simulations on this Gaussian process, we need to get below data:
Panda library has a useful function to download these prices from Morningstar, Quandl, Yahoo, etc. Downloads from Yahoo are having some issue recently; hence, in our case we will use combination of Quandl and Morningstar.
Download Asset Prices
Download Treasury rate
Now that we have historical asset prices, we will go ahead and calculate the asset volatility.
We do this by first calculating the Log returns for each day. Once, we have log returns for each day, we calculate the standard deviation using the ever helpful Pandas' std function. Note, this will provide us with daily standard deviation; hence, we need to annualize it by multiplying it with 252 (i.e. expected number of trading days in an year). Once we have annualized standard deviation, we take square root of it to calculate annualized volatility.
Below code reflects how easy it is in Pandas:
Calculate annualized volatility from daily asset prices
Apply Gaussian Process and perform Simulations
We now have all the required inputs and are ready to apply Gaussian Process to calculate expected asset price using Monte-Carlo simulations. For each simulation, we calculate expected price using below equation:
ST = St * exp( (Rf− 0.5*σ^2)(T−t) + σϵ√(T−t) )
ST = Expected asset price at time
St = Current asset price at time t (aka Spot price)
Rf = Risk free rate or 3-month treasury rate
σ = Annualized Volatility
(T−t) = Time to maturity in years
ϵ = Random variable based on Gaussian Distribution
Once, we get expected stock price using above equation, we repeat this calculation for N number of simulations. The only variable changing in each simulation is the Gaussian process; thereby, we representing a continuous time stochastic process, i.e. Wiener process.
Below Python code performs these Monte-Carlo simulations:
Monte-Carlo simulations and Wiener Process
Running Simulations on Tesla Stock (TSLA) for a future date
To implement a Blockchain protocol by creating a cryptographically secure data structure in Python.
What is Blockchain
Before we look at the implementation, lets take a quick look at what is a Blockchain. As per Wikipedia:
"A blockchain is a growing list of records, called blocks, which are linked using cryptography. Each block contains a cryptographic hash of the previous block, a timestamp, and transaction data (generally represented as a merkle tree root hash). By design, a blockchain is resistant to modification of the data. It is "an open, distributed ledger that can record transactions between two parties efficiently and in a verifiable and permanent way."
We will implement the Blockchain data structure by providing ability to group current transactions, create blocks and then link that block with the current blockchain by applying a proof-of-work (PoW) algorithm.
Required Libraries and Initializing Schema
We will need Python's hashlib and json libraries.
Next, we need to define the schema for Transactions and Blocks.
Imports and Setting schema for Transaction & Block
Create a Genesis block
We need to initialize the Blockchain by first creating an empty block, called the genesis block.
Since, this is the first block we manually specify value for Previous Hash and a Proof Hash and use that to create this block. This is then added to our blockchain.
Below code performs this functionality as seen from the output logs.
Now let's add some transactions
To create a transaction, we need 3 required values - From, To, and Amount, as seen in the schema above.
Below functions helps us in creating these transactions and have them added to the list of pending (aka mineable transactions).
Create Block and Mine
Now, its time to do the real stuff - Proof-of-Work (PoW) implementation by using SHA-256 cryptographic hash algorithms.
In order to perform PoW, we need two inputs:
We use input 1) - and dump it into JSON format (as show below in hash_block function) in an ordered way to maintain sanity over hashes. This dump is then passed onto hashlib's sha256 algorithm (also shown below).
Now, we have LastProof and Hash of LastBlock. Let's start the guess work (aka Proof-of-work) by incrementing a variable, called guess, by 1 and then computing its hash concatenated with already computed LastProof and LastBlock's hash. As soon as we get a hash that starts with "0000" (or we can change it depending on difficulty level we want to set), that "guess" becomes our proof for this block.
Functions to create block and perform PoW
Auto-Correlation on Unadjusted (Raw) Returns through lag 48
Here is the Auto-Correlation Function (ACF) of raw returns out through lag 48. We see a pattern here that suggests that values of Returns follow a pattern that is strong with t=12. As we know, home prices are seasonal, ex. Home prices tend to be higher in summers than winters, below ACF chart clearly depicts this relation.
Auto-Correlation on Adjusted Returns through lag 48
Here is the Auto-Correlation Function (ACF) of Adjusted returns out through lag 48. Now, we see a very high correlation of Rt with Rt-1 and it keeps declining as we go backwards. AR(1) model should fit well for these adjusted returns.
Applying AR(1) model and checking Residuals
After importing the adjusted returns we execute the below commands in Matlab to fit an AR(1) model. Beta value returned is 0.6917 with 0 intercept.