Created
February 16, 2023 02:41
-
-
Save gaburipeach/6536c4ec1df5d430b7f1aaddc1b838d8 to your computer and use it in GitHub Desktop.
mc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import matplotlib.pyplot as plt | |
def gbm_simulation(S0, mu, sigma, dt, T, N): | |
''' | |
S0: initial stock price | |
mu: expected return | |
sigma: volatility | |
dt: time step | |
T: total time | |
N: number of simulation steps | |
''' | |
# Generate the random steps | |
steps = np.random.normal(loc=mu * dt, scale=sigma * np.sqrt(dt), size=(N, int(T/dt))) | |
print(steps.shape) | |
# Calculate the stock price path | |
S = np.zeros_like(steps) | |
for i in range(1, steps.shape[0]): | |
S[i, :] = (steps[i]+1).cumprod(axis=0) * S0 | |
return S | |
# Set the parameters | |
S0 = 21818 | |
mu = 0.023 | |
sigma = 0.5898 | |
dt = 1/365 | |
T = 320/365 | |
N = 1000000 | |
# Simulate the time series | |
S = gbm_simulation(S0, mu, sigma, dt, T, N) | |
# Plot the results | |
plt.plot(S[1:100].T) | |
plt.title(f"Monte Carlo Simulation of BTC price \n vol: {sigma} start_price: {S0} rf_rate: {mu}") | |
plt.xlabel('Days') | |
plt.ylabel('BTC Price') | |
plt.show() | |
LOW = 15000 | |
num_touches = sum([min(x)<LOW for x in S]) | |
num_touches/N | |
LOW = 15000 | |
num_touches = sum([x[-1]<LOW for x in S]) | |
num_touches/N | |
sum([x[-1]>LOW and min(x) < LOW for x in S])/N | |
0.226031/(0.226031+0.322444) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment