Bonjour je souhaiterais convertir ce code Python en VBA mais je n'ai que très peu de connaissance en VBA et pas le temps d'apprendre un nouveau languages.
Voici le code Pyhton c'est un pricer avec simulation monte carlo pour option asiatique et européenne
import math
import random as ran
def option_eu_MC(S0, K, T, r, sigma, call=True):
n=1000000
if call: # call
e = 1
else: # put
e = -1
sigmaT = sigma * math.sqrt(T)
S_T = S0 * math.exp(-sigmaT**2/2)
K_T = K * math.exp(-r*T)
gain = [max(e*( ran.lognormvariate(0,sigmaT)*S_T - K_T ) ,0)
for k in range(n)]
return( moyenne_ecart(gain) )
def moyenne_ecart(xlist):
n = len(xlist)
assert(n>1)
m = sum(xlist)/n
v = sum((x-m)**2 for x in xlist)/n
return( m, math.sqrt(v/(n-1)) )
def option_as_MC(S0, K, T, r, sigma, m, call=True):
n=1000000
drift = (r-sigma**2/2)*T/m
sigmaT= sigma*math.sqrt(T/m)
def echantillon():
somme = 0
Sk = S0
for k in range(m):
Sk *= ran.lognormvariate(drift, sigmaT)
somme += Sk
return(somme/m)
if call:
gain = [max(0, echantillon()-K) for k in range(n)]
else:
gain = [max(0, K-echantillon()) for k in range(n)]
P,sigmaP = moyenne_ecart(gain)
return( math.exp(-r*T)*P, math.exp(-r*T)*sigmaP)
Voici le code Pyhton c'est un pricer avec simulation monte carlo pour option asiatique et européenne
import math
import random as ran
def option_eu_MC(S0, K, T, r, sigma, call=True):
n=1000000
if call: # call
e = 1
else: # put
e = -1
sigmaT = sigma * math.sqrt(T)
S_T = S0 * math.exp(-sigmaT**2/2)
K_T = K * math.exp(-r*T)
gain = [max(e*( ran.lognormvariate(0,sigmaT)*S_T - K_T ) ,0)
for k in range(n)]
return( moyenne_ecart(gain) )
def moyenne_ecart(xlist):
n = len(xlist)
assert(n>1)
m = sum(xlist)/n
v = sum((x-m)**2 for x in xlist)/n
return( m, math.sqrt(v/(n-1)) )
def option_as_MC(S0, K, T, r, sigma, m, call=True):
n=1000000
drift = (r-sigma**2/2)*T/m
sigmaT= sigma*math.sqrt(T/m)
def echantillon():
somme = 0
Sk = S0
for k in range(m):
Sk *= ran.lognormvariate(drift, sigmaT)
somme += Sk
return(somme/m)
if call:
gain = [max(0, echantillon()-K) for k in range(n)]
else:
gain = [max(0, K-echantillon()) for k in range(n)]
P,sigmaP = moyenne_ecart(gain)
return( math.exp(-r*T)*P, math.exp(-r*T)*sigmaP)