Skip to content

Instantly share code, notes, and snippets.

@gaburipeach
Created February 16, 2023 02:41
Show Gist options
  • Save gaburipeach/6536c4ec1df5d430b7f1aaddc1b838d8 to your computer and use it in GitHub Desktop.
Save gaburipeach/6536c4ec1df5d430b7f1aaddc1b838d8 to your computer and use it in GitHub Desktop.
mc
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