-
Notifications
You must be signed in to change notification settings - Fork 67
/
montecarlo.py
51 lines (36 loc) · 1.48 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
43
44
45
46
47
48
49
50
51
import random, json
import matplotlib.pyplot as plt
import pandas as pd
plt.style.use('dark_background')
fig, ax = plt.subplots(figsize=(13,11))
mu = 0.04 # media diaria del pct_change
sigma = 2 # Volatilidad diaria del pct_change
ruedas = 250
simulaciones = 1000
perdida_max = 0.3 # %de perdida maxima tolerada
capital = [[100] for i in range(simulaciones)]
quiebres = {'parcial':0, 'final':0}
for j in range(simulaciones):
for i in range(ruedas):
v = random.normalvariate(mu,sigma)
capital[j].append(capital[j][i] * (1+v/100))
ax.plot(capital[j], lw=1, alpha=0.05, color='white')
if min(capital[j]) < (1-perdida_max)*100:
quiebres['parcial'] += 1
if capital[j][i] < (1-perdida_max)*100:
quiebres['final'] += 1
ax.plot([(1-perdida_max)*100]*ruedas, 'r--')
ax.set_yscale('log')
fig.suptitle(f'Montecarlo básico cartera mu diario={mu}, sigma diario={sigma}',
y=0.17, fontsize=18, color='silver')
plt.show()
df = pd.DataFrame(capital).round(2)
resumen = {}
resumen['minTemporal'] = df.min().min()
resumen['maxTemporal'] = df.max().max()
resumen['minFinal'] = df.transpose().iloc[-1:].squeeze().min()
resumen['maxFinal'] = df.transpose().iloc[-1:].squeeze().max()
resumen['medio'] = round(df.transpose().iloc[-1:].squeeze().mean(),2)
resumen['errorAbsMedia'] = round(resumen['medio'] * 1/(simulaciones**0.5),2)
resumen['quiebres'] = quiebres
print(json.dumps(resumen, indent=5))