Monte Carlo Simulation and Risk Analysis of Bitcoin

Note: This post does not represent any expert guidelines in finance or bitcoin. This article is a wrap-up of a class project regarded stock market risk analysis. 

– Data Preparation from Stack Overflow: link
– language: Python
– Example code for Stock Market Project, Inspired by Jose Portilla’s Github Repo


TL;DR

The daily return chart shows that 25% of the time it's around 0%. The tail of the daily return is very long, as much as 400%.



The Monte Carlo simulation shows that we will have between $5600 to $9000 range.
The most interesting part is here. If we look 10% empirical quantile of the final price distribution to estimate the Value at Risk for the Bitcoin price, which looks to be $925.49 for every investment of $6837.31, which accounts for 13.5% of the investment. 

This means that for every initial coin you purchase at 6837.31, $925 is at risk 90% of the time from Monte Carlo Simulation. 



Import the Library

import pandas as pd
from pandas import Series,DataFrame
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline
from __future__ import division

Clean the Data

bitcoin_df.columns = ['trading_ts', 'price','volume']
bitcoin_df['datetime_trade']=pd.to_datetime(bitcoin_df['trading_ts'],unit='s')
bitcoin_df['Daily Return'] = bitcoin_df['price'].pct_change()
bitcoin_1617 = bitcoin_df[(bitcoin_df['datetime_trade']> datetime.date(2016,1,1))]
tech_rets = bitcoin_1617[['price']].pct_change()
tech_rets.columns = ['Close']
rets = tech_rets.dropna()
sns.distplot(bitcoin_1617['Daily Return'].dropna(),bins=100,color='purple')

Stock Monte Carlo Simulation

The Monte Carlo function can be found at this Repo. It generates the simulation of the next 365 days based on the mean and standard deviation of the empirical data. 

start_price = 6837.31
# Set up our time horizon
days = 365

# Now our delta
dt = 1/days

# Now let's grab our mu (drift) from the expected return data we got for AAPL
mu = tech_rets.mean()['Close']

# Now let's grab the volatility of the stock from the std() of the average return
sigma = tech_rets.std()['Close']

for run in xrange(100):
    plt.plot(stock_monte_carlo(start_price,days,mu,sigma))
plt.xlabel("Days")
plt.ylabel("Price")  
plt.title('Monte Carlo Analysis for Bitcoin')
runs = 10000

# Create an empty matrix to hold the end price data
simulations = np.zeros(runs)

# Set the print options of numpy to only display 0-5 points from an array to suppress output
np.set_printoptions(threshold=5)

for run in xrange(runs):    
    # Set the simulation data point as the last stock price for that run
    simulations[run] = stock_monte_carlo(start_price,days,mu,sigma)[days-1];

Comments