-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_simulation.py
More file actions
executable file
·182 lines (152 loc) · 5.37 KB
/
Copy pathtest_simulation.py
File metadata and controls
executable file
·182 lines (152 loc) · 5.37 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/usr/bin/env python3
"""
Teste da simulação para verificar se os resultados estão corretos
"""
# Inputs
initial_investment = 1000.0
price_now = {
"MPW": 5.48,
"SLG": 59.28,
}
annual_div = {
"MPW": 0.32,
"SLG": 3.09,
}
freq = {
"MPW": 4, # quarterly
"SLG": 12, # monthly
}
price_cagr = {
"MPW": 0.02, # 2% a.a.
"SLG": 0.03, # 3% a.a.
}
div_cagr = {
"MPW": 0.00, # 0% a.a.
"SLG": 0.01, # 1% a.a.
}
years = 10
months = years * 12
def simulate(ticker):
P0 = price_now[ticker]
D_annual0 = annual_div[ticker]
n = freq[ticker]
price_g = (1 + price_cagr[ticker]) ** (1/12) - 1
div_g_annual = div_cagr[ticker]
if n == 12:
div_per_payment0 = D_annual0 / 12.0
pay_interval = 1
else:
div_per_payment0 = D_annual0 / 4.0
pay_interval = 3
shares = initial_investment / P0
price = P0
div_per_payment = div_per_payment0
total_div_received = 0.0
current_annual_div = D_annual0
print(f"\n{'='*60}")
print(f"Simulação {ticker}")
print(f"{'='*60}")
print(f"Preço inicial: US$ {P0:.2f}")
print(f"Dividendo anual inicial: US$ {D_annual0:.2f}")
print(f"Ações iniciais: {shares:.4f}")
print(f"Frequência de pagamento: {n}x/ano")
for m in range(1, months+1):
# Price grows monthly
price *= (1 + price_g)
# Apply dividend growth annually
if m % 12 == 0 and m > 0:
current_annual_div *= (1 + div_g_annual)
if n == 12:
div_per_payment = current_annual_div / 12.0
else:
div_per_payment = current_annual_div / 4.0
# Determine if this is a pay month
pay = False
if n == 12:
pay = True
else:
pay = (m % pay_interval == 0)
if pay:
cash_div = shares * div_per_payment
total_div_received += cash_div
shares += cash_div / price
ending_value = shares * price
run_rate_annual = current_annual_div * shares
# Calculate yields
yield_on_cost = (run_rate_annual / initial_investment) * 100
current_yield = (run_rate_annual / ending_value) * 100
avg_annual_yield = (total_div_received / initial_investment / years) * 100
print(f"\nResultados após {years} anos:")
print(f"-" * 60)
print(f"Preço final: US$ {price:.2f}")
print(f"Ações finais: {shares:.4f}")
print(f"Valor final da posição: US$ {ending_value:,.2f}")
print(f"Dividendo anual no ano 10: US$ {current_annual_div:.2f}")
print(f"\nDividendos:")
print(f" Run-rate anual no fim: US$ {run_rate_annual:.2f}")
print(f" Total de dividendos recebidos (10 anos): US$ {total_div_received:.2f}")
print(f"\nMétricas de Yield:")
print(f" YoC Final (Yield-on-Cost): {yield_on_cost:.2f}% a.a.")
print(f" Current Yield: {current_yield:.2f}% a.a.")
print(f" DY Médio no período: {avg_annual_yield:.2f}% a.a.")
return {
"ending_price": price,
"ending_shares": shares,
"ending_value": ending_value,
"total_dividends": total_div_received,
"run_rate_annual": run_rate_annual,
"yield_on_cost": yield_on_cost,
"current_yield": current_yield,
"avg_annual_yield": avg_annual_yield
}
print("\n" + "="*60)
print("TESTE DA SIMULAÇÃO DE INVESTIMENTOS - 10 ANOS")
print("="*60)
res_mpw = simulate("MPW")
res_slg = simulate("SLG")
print(f"\n{'='*60}")
print("VALORES ESPERADOS (da sua tabela)")
print(f"{'='*60}")
print("\nMPW:")
print(f" Run-rate anual no fim: US$ 98,76/ano")
print(f" YoC Final: 9,88% a.a.")
print(f" Current Yield: 4,79% a.a.")
print(f" Dividendos totais: US$ 769,53")
print(f" DY Médio: 7,70% a.a.")
print("\nSLG:")
print(f" Run-rate anual no fim: US$ 92,28/ano")
print(f" YoC Final: 9,23% a.a.")
print(f" Current Yield: 4,28% a.a.")
print(f" Dividendos totais: US$ 707,46")
print(f" DY Médio: 7,07% a.a.")
print(f"\n{'='*60}")
print("COMPARAÇÃO")
print(f"{'='*60}")
def compare(label, expected, actual):
diff = actual - expected
pct_diff = (diff / expected * 100) if expected != 0 else 0
status = "✓" if abs(pct_diff) < 1 else "✗"
print(f"{status} {label}:")
print(f" Esperado: {expected:.2f} | Obtido: {actual:.2f} | Diferença: {diff:+.2f} ({pct_diff:+.1f}%)")
print("\nMPW:")
compare("Run-rate anual", 98.76, res_mpw["run_rate_annual"])
compare("YoC Final (%)", 9.88, res_mpw["yield_on_cost"])
compare("Current Yield (%)", 4.79, res_mpw["current_yield"])
compare("Dividendos totais", 769.53, res_mpw["total_dividends"])
compare("DY Médio (%)", 7.70, res_mpw["avg_annual_yield"])
print("\nSLG:")
compare("Run-rate anual", 92.28, res_slg["run_rate_annual"])
compare("YoC Final (%)", 9.23, res_slg["yield_on_cost"])
compare("Current Yield (%)", 4.28, res_slg["current_yield"])
compare("Dividendos totais", 707.46, res_slg["total_dividends"])
compare("DY Médio (%)", 7.07, res_slg["avg_annual_yield"])
print("\n" + "="*60)
print("RETORNO TOTAL")
print("="*60)
total_return_mpw = (res_mpw["ending_value"] / initial_investment - 1) * 100
total_return_slg = (res_slg["ending_value"] / initial_investment - 1) * 100
avg_return = (total_return_mpw + total_return_slg) / 2
print(f"\nRetorno Total MPW: {total_return_mpw:.2f}%")
print(f"Retorno Total SLG: {total_return_slg:.2f}%")
print(f"Retorno Médio (ambos): {avg_return:.2f}%")
print(f"\nCAGR Médio aproximado: ~{avg_return/10:.2f}% a.a.")