-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmontecarlo.py
42 lines (35 loc) · 1.12 KB
/
montecarlo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import numpy as np
import pandas as pd
from pandas_datareader import data as wb
import matplotlib.pyplot as plt
import seaborn as sns
from scipy.stats import norm
import datetime
file = pd.read_csv("BTC.csv", parse_dates=True)
date = pd.to_datetime(file.time)
data = file.close
#plt.plot(date, data)
#plt.show()
log_returns = np.log(1 + data.pct_change())
u = log_returns.mean()
var = log_returns.var()
drift = u - (0.5*var)
stdev = log_returns.std()
days = 30 # 1 month prediction
iterations = 100
Z = norm.ppf(np.random.rand(days, iterations))
daily_returns = np.exp(drift + stdev * Z)
price_paths = np.zeros_like(daily_returns)
price_paths[0] = data.iloc[0]
for t in range(1, days):
price_paths[t] = price_paths[t-1]*daily_returns[t]
base = date.iloc[0]
future_dates = [base + datetime.timedelta(days=x) for x in range(days)]
plt.plot(date, data, linestyle='dashed')
plt.plot(future_dates, price_paths)
plt.ylabel("Close price (BTC/USDT)")
plt.show()
sns.histplot(price_paths[-1,:], bins = 20, stat = "frequency")
plt.xlabel("Close price (BTC/USDT)")
plt.ylabel("Frequency")
plt.show()