 # Crypto Price Simulations using Monte Carlo and Python

In this post we will use Monte Carlo simulations to guess the Bitcoin price in the near future using Python. Therefore, I will explain some related statistics and ways to analyze the generated data. Furthermore, we will use crypto price simulations to compare the simulation to the actual price.

### Monte Carlo Simulation

The idea behind Monte Carlo simulations is that randomness can be used to solve deterministic problems. Therefore, random samples are repeated and afterwards a statistical analysis is performed on these samples.

With respect to price simulations Monte Carlo simulations can be used to model the random character of moving prices.

### Basic statistics

In order to analyse the data, we need some basic statistics. First, we need the arithmetic mean:

(1) Furthermore, the standard deviation is a measure of the spread of a distribution:

(2) Within financial calculations, the standard deviation is a measure for the price volatility.

After that, we need to calculate the relative daily change in price (return) using the following formula:

(3) Finally, the incremental random based change in price is calculated as follows:

(4) Here, is the latest price while is the previous price and is a random number. Therefore, this price progression can be desribed as a 1-dimensional random walk.

Furthermore, the random number is drawn from a normal (Gaussian) distribution. Hence, it is a function of the standard deviation and the mean. Therefore, we can write

(5) ### Summary of the Procedure

We calculate the standard deviation (volatility) of the daily return (relative price change) from a past time frame. Afterwards, we use this (constant) volatility from the past to predict the price in the future using the random walk theory.

### Set up the Crypto Price Simulation in Python

Let’s have a look at the schematic approach in Python:

• Import dependencies.
• Enter input: Coin name, exchange, trading currency, time frame of the past price data, number of simulations and predicted time frame.
• Afterwards, receive live price data via the API from CryptoCompare.
• The data is stored in a dataframe (df) using the Pandas data analysis library.
• Then, the percentage change and standard deviation function from Pandas are applied to the data frame.
• Afterwards the simulations are conducted within 2 loops. The first loop for the simulations, the second loop for the price progression within one simulation.
• Finally, plot the simulated time progression data and the histogram.

And here you go with the Python code.

#Author: johannes <info@numex-blog.com>, 22.12.17

import pandas as pd
import urllib, json
import matplotlib.pyplot as plt
import numpy as np
import matplotlib.mlab as mlab
from scipy.stats import norm

coinAcronym = "BTC"
exchange = "Kraken"
timeFrame = 30

url = "https://min-api.cryptocompare.com/data/histoday?fsym=" + coinAcronym + "&tsym=" + tradeCurrency + "&limit=" + str(timeFrame) + "&e=" + exchange

df = pd.DataFrame(data['Data'])
df.columns = [['close', 'high', 'low', 'open', 'time', 'volumefrom', 'volumeto']]
df.time = pd.to_datetime(df['time'], unit='s')
df = df.set_index(df.time)

df2 = pd.DataFrame()
df2 = df.close

returns = df2.pct_change()
volatility = returns.std()

numberSimulations = 100
predictedDays = 30
lastPrice = df.close[-1]

print lastPrice

results = pd.DataFrame()

sim = 0

while (sim < numberSimulations):

prices = [] #Empty list for each new simulation
days = 0 #reset days counter
prices.append(lastPrice) #Fill list with initial price

while (days < predictedDays):
prices.append(prices[days] * (1 + np.random.normal(0, volatility))) #Add random new price and add to list
days = days + 1 #increment days counter

results[str(sim)] = pd.Series(prices).values #Add column to pandas data frame
sim = sim + 1 #increment simulation counter

fig = plt.figure()
plt.plot(results)
plt.ylabel('Price in Euro')
plt.xlabel('Simulated days')
plt.show()

### Results

First of all, let’s have a look at the simulation No 1. It is the Monte Carlo simulation of the Bitcoin price. Therefore, we start at the 15.01.2018 at 11440 €, calculate the volatility (standard deviation) of the previous 200 days, do 25000 Monte Carlo simulations for the following 30 days. This means, that we are going to simulate 750000 prices over the progression of 30 days in the future. Let’s plot the histogram of the final prices after the simulated 30 days. Furthermore, lets plot a fit of the Gaussian distribution curve using the fit function from the scipy.stats function. Additionally, we plot the 25 % and 75 % quantile into the histogram.

The 25 % quantile means, that there is a 25 % chance that the crypto price goes below 8866 €. Regarding the 75 % quantile, there is a 25 % chance that it will go above 13379 €. Additionally, let us take further simulations into account. Therefore, let us specify the constant input data for the simulations:

• Date: 15.01.2018,
• Predicted days: 30,
• Starting price 11440 €.

Furthermore, the variable input data are specified in the following table.

NoTime frame of
volatility of
daily return
Volatility of
daily return
in %
No simulationQuantile
25 %
Quantile
75 %
12005,8925000886613379
22005,8910000885913427
31006,4825000882213458
41006,4810000870913399
54004,9925000924113195
64004,9910000927113157
74004,991000944113261
82005,891000927913431
91006,481000886213492
10307,041000846413858
11307,045000854113674
12307,0410000834113586
The average 25 % quantile of all 12 simulations is 8891 €, while the average 75 % quantile is 13443 €.

### Conclusion

In Monte Carlo simulations randomness can be used to solve deterministic problems. Furthermore, they can be used to model the random character of moving prices. Here, we used the Bitcoin volatility data of a certain period in the past to simulate the price for the following 30 days. Afterwards, we plotted a fit of the Gaussian distribution curve and the 25 % and 75 % quantile. This means, there is a 50 % chance, that the price will end up between these two quantiles. Let us reassess that after the 30 days passed.

Additionally, this crypto price simulations are not taking any market sentiment or events into account. Therefore, Google trends could be an indicator (You can have a look at Bitcoin article, in case you are interested).

Clearly, you can apply this method to stocks etc. But remember, never take simulation results too serious. I see it as a method to improve my educated guesses.

### Followup – Comparing Simulation and Reality

Like promised, I am comparing the simulated prices to the real prices. Therefore, let’s have a look at the Bitcoin price. On the 16.02.2018 it is around 8085.5 €. Our 25 % quantile of the simulations was 8891 €, while the 75 % quantile was 13443 €. Hence, the simulation results were too progressive, since the actual price is lower than the 25 % quantile. This does not mean, that this type of Monte Carlo simulation is never applicable. It simply means, it can be too progressive for very volatile markets.

If you are interested in a less progressive calculation method, under consideration of negative price jumps have a look a the following article: Advanced Crypto Price Simulations based on Monte Carlo.