Created
August 6, 2021 04:17
-
-
Save mtmoses/ef13b66b1a44d71aac32fa880437b65d to your computer and use it in GitHub Desktop.
This file contains 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
from fbm import FBM | |
from fbm import fbm, fgn, times | |
from fbm import MBM | |
from fbm import mbm, mgn, times | |
import math | |
def MMAR(K, simulated_H, simulated_lambda, simulated_sigma, original_price_history, magnitude_parameter, GRAPHS): | |
# --- VARIABLES --- | |
# K | |
# adjust K depending on how many days you want to simulate (e.g. if K=13, you'll simulate 2^13=8192 days) | |
# simulated_H | |
# the Hurst parameter for the fBm process | |
# simulated_lambda | |
# the mean for the lognormal cascade | |
# simulated_sigma | |
# the variance for the lognormal cascade | |
# original_price_history | |
# the price history of the market under study (used for starting the prices from the right time!) | |
# magnitude_parameter | |
# adjust this up or down to change the range of price changes (e.g. if prices are swinging too wildly every day, then adjust this downwards) | |
# GRAPHS | |
# Boolean - either True or False - use True if you want the MMAR to simulate graphs for you | |
# --- MESSAGE --- | |
if GRAPHS == True: | |
print("Performing an MMAR simulation with parameters:\n\nH = " + str(simulated_H) + "\nlambda = " + str(simulated_lambda) + "\nsigma = " + str(simulated_sigma) + "\nfBm magnitude = " + str(magnitude_parameter)+ "\n") | |
# --- CASCADE --- | |
new_cascade = list(np.array(lognormal_cascade(k=K, v=1, ln_lambda = simulated_lambda, ln_theta = simulated_sigma)).flat) | |
if GRAPHS == True: | |
# plt.figure(figsize=(24,2)) | |
plt.xticks(np.arange(0, 2**(K)+1, 2**(K-3))) | |
plt.title("Binomial Cascade") | |
plt.xlabel("Conventional time\n(days)") | |
plt.ylabel('"Mass"') | |
plt.plot(new_cascade, color="crimson", linewidth=0.5) | |
plt.show() | |
# --- TRADING TIME --- | |
tradingtime = 2**K*np.cumsum(new_cascade)/sum(new_cascade) | |
if GRAPHS == True: | |
plt.xticks(np.arange(0, 2**(K)+1, 2**(K-3))) | |
plt.yticks(np.arange(0, 2**(K)+1, 2**(K-3))) | |
plt.title("Trading time\n(normalized)") | |
plt.xlabel("Conventional time\n(days)") | |
plt.ylabel('"Trading time"\n(\u03B8 (t), normalized)') | |
plt.plot(tradingtime, color="orangered") | |
# --- FBM (Fractional Brownian Motion) --- | |
new_fbm_class = FBM(n = 10*2**K+1, hurst = simulated_H, length = magnitude_parameter, method='daviesharte') | |
new_fbm_simulation = new_fbm_class.fbm() | |
if GRAPHS == True: | |
plt.figure(figsize=(24,2)) | |
plt.xticks(np.arange(0, 10*2**(K)+1, 10*2**(K-3))) | |
plt.title("Fractional Brownian Motion") | |
plt.xlabel("t") | |
plt.ylabel('fBm (t)') | |
plt.plot(new_fbm_simulation, color="orange") | |
plt.show() | |
# --- MMAR XT's --- | |
simulated_xt_array = [0 for x in range(0, len(tradingtime))] | |
for i in range(0, len(tradingtime)): | |
simulated_xt_array[i] = new_fbm_simulation[int(tradingtime[i]*10)] | |
if GRAPHS == True: | |
plt.title("MMAR generated Xt") | |
plt.xticks(np.arange(0, 2**(K)+1, 2**(K-3))) | |
plt.xlabel("Time\n(days)") | |
plt.ylabel('X(t)\n(Natural log growth since beginning)') | |
plt.grid(b=True) | |
plt.fill_between(np.arange(0, 2**K, 1) , simulated_xt_array, color="darkviolet", alpha=0.2) | |
plt.show() | |
# --- PRICES --- | |
if GRAPHS == True: | |
plt.title("MMAR generated Price") | |
plt.xticks(np.arange(0, 2**(K)+1, 2**(K-3))) | |
plt.xlabel("Time\n(days)") | |
plt.ylabel('Price level') | |
plt.grid(b=True) | |
plt.fill_between(np.arange(0, 2**K, 1) , original_price_history[0]*np.exp(simulated_xt_array), color="limegreen", alpha=0.2) | |
plt.show() | |
# --- LN CHANGES --- | |
if GRAPHS == True: | |
ln_simulated_xt_array = [0 for x in range(0, len(simulated_xt_array)-1)] | |
for i in range(1,len(simulated_xt_array)): | |
ln_simulated_xt_array[i-1] = np.log((original_price_history[0]*np.exp(simulated_xt_array[i]))/(original_price_history[0]*np.exp(simulated_xt_array[i-1]))) | |
plt.figure(figsize=(24,5)) | |
plt.title("Price increments") | |
plt.xticks(np.arange(0, 2**(K)+1, 2**(K-3))) | |
plt.xlabel("Time\n(days)") | |
plt.ylabel('Change\n(%)') | |
plt.grid(b=True) | |
plt.plot(ln_simulated_xt_array, color="darkviolet", linewidth=0.5) | |
plt.gca().set_yticklabels(['{:.0f}'.format(x*100) for x in plt.gca().get_yticks()]) | |
plt.show() | |
print("The number of price increments that equal zero is: " + str(list(np.abs(ln_simulated_xt_array)).count(0))) | |
# --- END --- | |
return simulated_xt_array |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment