-1
What’s wrong with this code?
#calculando retornos diários máximos e mínimos:
max_std = 3
max = {}
min = {}
retornos_stds = retornos_dia.std()
retornos_med = retornos_dia.mean()
for acao in acoes:
max[acao] = retornos_med[acao] + max_std * retornos_stds[acao]
min[acao] = retornos_med[acao] - max_std * retornos_stds[acao]
so far no problem, the maximum and minimum of each action are calculated normally and stored in the dictionaries "max" and "min"
#clipando:
retorno_clipado = {}
for acao in acoes:
for contar,r in enumerate(retornos_dia[acao]):
if r > max[acao]:
retorno_clipado[acao][contar] = max[acao]
elif r < min[acao]:
retorno_clipado[acao][contar] = min[acao]
else:
retorno_clipado[acao][contar] = retornos_dia[acao][contar]
retorno_clipado
here occurs the problem, the following error is returned:
KeyError Traceback (most recent call last)
<ipython-input-69-fd5fc22b112d> in <module>
8 retorno_clipado[acao][contar] = min[acao]
9 else:
---> 10 retorno_clipado[acao][contar] = retornos_dia[acao][contar]
11
12 retorno_clipado
KeyError: 'PETR3'
'PETR3' is the first element of shares