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.

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:

Here is the list of functionalities our Stock Analyzer is capable of:

- Download historical data for a given publicly traded stock ticker
- Compute basic statistical points on this data -ex. Mean, Variance, Standard Deviation, Volatility, Returns, etc.
- Plot daily close price
- Plot daily returns
- Plot daily returns against an Index return, ex. SNP 500
- Plot daily prices in a Candlestick chart along with Volume
- Calculate and Plot user-defined on-the-fly moving averages
- Compute Linear regression by applying ordinary least square (OLS) model against SNP 500
- Display and return linear regression values, such as R-Squared, F-Statistic, BIC, Standard Error, etc.
- Plot Linear regression model

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.

In order to initiate an object of this class, we need to pass a Stock-ticker. As soon as we pass a

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

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

Generate plot for Daily prices

Generate plot for Daily Returns

Generate plot for Daily Returns against that of SNP-500

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

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:

- Window1: An integer specifying number of days for which we need to generate Moving Average 1 - default value is 14 days
- Window2: An integer specifying number of days for which we need to generate Moving Average 2 - default value is 42 days
- useMatplot: Boolean flag suggesting whether to use matplotlib or Bokeh for charting - default is to use Bokeh.

Generate Moving Averages and Plot them along with daily Price

Code to calculate moving averages

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.

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

For complete source-code, please check this project at my GitHub repository @ https://github.com/shashank-khanna/Stock-Analyzer |

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.

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.

"

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.

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:

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.

- CallOptionPrice =SpotPrice*N(d1) − Strike*exp(−r(T−t))*N(d2))
- PutOptionPrice = Strike*exp(−r(T−t)) *N(−d2) − SpotPrice*N(−d1)

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

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.

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.

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:

"

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:

Put-Call Parity

Output for Put-Call parity

As we can see above, both European Call and Put option prices do maintain the Put-Call parity.

| For complete source-code, please check this project at my GitHub repository @ https://github.com/shashank-khanna/Option-Pricing |

To apply Monte-Carlo simulations and calculate price of American style options.

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.

Once we have expected prices calculated, next step is to calculate the Option payoffs for those prices.

- For Call Options, the expected payoff is:
- If Expected Price is
than Strike Price, then 0**less** - Else (Expected Price - Strike Price)

- If Expected Price is
- For Put Options, the expected payoff is:
- If Expected Price is
than Strike Price, then 0**greater** - Else (Strike Price - Expected Price)

- If Expected Price is

- Call Option Payoff = max(0,ExpectedPrice−Strike)
- Put Option Payoff = max(0,Strike-ExpectedPrice)

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).

Discount Factor

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

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

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.

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.

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.

"

Basically, given a set of parameters and some random variables, we perform thousands of simulations and then seek expected outcome.

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.

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

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.

- Asset Volatility ( sigma )
- Spot Price ( S )
- Risk Free Rate ( Rf )
- Time to maturity (in years) ( T-t )

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:

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'

Below code reflects how easy it is in Pandas:

Calculate annualized volatility from daily asset prices

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) )**

where,

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:

where,

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

| Full source-code available at my GitHub repository @ https://github.com/shashank-khanna/Option-Pricing/blob/master/option_pricing/american_option_pricing.py. |

To implement a Blockchain protocol by creating a cryptographically secure data structure in Python.

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.

"

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.

We will need Python's *hashlib* and *json* libraries.

Next, we need to define the schema for Transactions and Blocks.

- We will use
*json*to convert list of transactions into a JSON format message that can be then used to create a block. *hashlib*will be used to perform the Proof-of-Work (PoW) using SHA-256 Cryptographic hash algorithm.

Next, we need to define the schema for Transactions and Blocks.

- Each
**Transaction**will have a Sender, a Recipient, and an quantity. Let's name them*From*,*To*, and*Amount.* - Each
**Block**will have:*Index*representing Block number in the Blockchain.*Timestamp*representing exact timestamp the block was created aka mined.*Transactions*lists out all the transactions done since the previous block got created*Hash*contains the Proof-of-Work (PoW) that when hashed with previous block's hash returns a value beginning with four zeroes or "0000". This can be changed and thereby setting the difficulty level of the PoW algorithm.*PreviousHash*is the SHA-256 hash representation of previous block.

Imports and Setting schema for Transaction & 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.

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

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).

Below functions helps us in creating these transactions and have them added to the list of pending (aka mineable transactions).

Create transactions

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.

In order to perform PoW, we need two inputs:

- PreviousBlock
- LastProof or Previous Block's Hash attribute in our case

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

| Full source-code available at my GitHub repository @ https://github.com/shashank-khanna/Blockchain-Py |

Below 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.

]]>EDU>> [parametersCSAdj, LLCSAdj, errorsCSAdj] = armaxfilter(adjret, 0,AROrder);

EDU>> [acCSResid1, acstdCSResid1] = sacf(errorsCSAdj1,48);

EDU>> parametersCSAdj parametersCSAdj = 0.6917