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
PurposeTo 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 MethodWe will calculate prices for European options by applying Black Scholes method. As per Investopedia: "The BlackScholes formula (also called BlackScholesMerton) was the first widely used model for option pricing. It's used to calculate the theoretical value of Europeanstyle options using current stock prices, expected dividends, the option's strike price, expected interest rates, time to expiration and expected volatility." BlackScholes method only works for European options as they can be exercised only on the expiry date. Underlying DataAs we can see, the required input for BlackScholes method is same as the one we had downloaded and derived in Part1 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 BlackScholes' d1 and d2Now, we have all the underlying data; so let's aim to codify the above mentioned BlackScholes equations. The famous d1 and d2 values can be calculated using the below code: Calculating d1
Calculating d2
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 BlackScholes method
Running again for TSLA shortterm optionIn our previous blog, we ran MonteCarlo 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 BlackScholes
As we can see, these values are extremely close to the ones we got when running MonteCarlo 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. PutCall ParityEuropean Call and Put options must maintain a relationship called as PutCall parity. As Investopedia explains here: "Putcall 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, PutCall 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: PutCall Parity
Output for PutCall parity
As we can see above, both European Call and Put option prices do maintain the PutCall parity.
PurposeTo apply MonteCarlo simulations and calculate price of American style options. Use of MonteCarlo SimulationsIn 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 MonteCarlo simulations as I had pointed out in my previous blog. Calculate Expected PayoffsOnce we have expected prices calculated, next step is to calculate the Option payoffs for those prices.
Present Value of expected PayoffsNow, we have the expected Options payoffs; so the next step is to calculate their Present Value using the discount factor dependent on riskfree rate and time to expiry (in years). Discount Factor
We use the above discount factor to calculate the presentvalue of option payoffs returns through the MonteCarlo 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  TSLAWe 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?
PurposeTo perform MonteCarlo simulations and use it to calculate expected price at a given future date. What are MonteCarlo Simulations?MonteCarlo 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 MotionIn our case, we will make use of Gaussian Process that provides us with the random variable and then use it along with Asset Volatility, Riskfree rate, and time duration to calculate the expected price at a given future date. This continuoustime stochastic process represents the Brownian motion (aka the Wiener Process). Primary use of this Gaussian based MonteCarlo 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 dataIn order to successfully apply MonteCarlo 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
Volatility CalculationNow 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 SimulationsWe now have all the required inputs and are ready to apply Gaussian Process to calculate expected asset price using MonteCarlo simulations. For each simulation, we calculate expected price using below equation: ST = St * exp( (Rf− 0.5*σ^2)(T−t) + σϵ√(T−t) ) where, ST = Expected asset price at time St = Current asset price at time t (aka Spot price) Rf = Risk free rate or 3month 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 MonteCarlo simulations: MonteCarlo simulations and Wiener Process
Running Simulations on Tesla Stock (TSLA) for a future date
SourceCode
PurposeTo implement a Blockchain protocol by creating a cryptographically secure data structure in Python. What is BlockchainBefore 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 proofofwork (PoW) algorithm. Required Libraries and Initializing SchemaWe 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 blockWe 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. Genesis Block
Now let's add some transactionsTo 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 transactions
Create Block and MineNow, its time to do the real stuff  ProofofWork (PoW) implementation by using SHA256 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 Proofofwork) 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
Sourcecode
AutoCorrelation on 30 yr Mortgage Rates through lag 125Below is the autocorrelation on Mortgage rates. Autocorrelation clearly suggest that Rt is dependent on its previous values and its dependency is pretty significant even at Lag 125.
AutoCorrelation on Unadjusted (Raw) Returns through lag 48Here is the AutoCorrelation 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.AutoCorrelation on Adjusted Returns through lag 48Here is the AutoCorrelation Function (ACF) of Adjusted returns out through lag 48. Now, we see a very high correlation of Rt with Rt1 and it keeps declining as we go backwards. AR(1) model should fit well for these adjusted returns.Applying AR(1) model and checking ResidualsAfter 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.

