-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.py
57 lines (33 loc) · 1.17 KB
/
test.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
52
53
54
55
56
57
from hack_itau_quant import EfficientFrontier, BloombergData
import matplotlib.pyplot as plt
import numpy as np
close_prices = BloombergData.get_hack_data()
returns = close_prices.pct_change()[1:]
expected_returns = returns.mean()
cov_matrix = returns.cov()
ef = EfficientFrontier(expected_returns, cov_matrix)
target_return = 0.0014
target_risk = 0.024
w1 = ef.efficient_return(target_return)
w2 = ef.efficient_risk(target_risk)
print("EFFICIENT RETURN: ", w1)
print("EFFICIENT RISK: ", w2)
# e = ef_opt(expected_returns, cov_matrix)
# fig, ax = plt.subplots()
# plotting.plot_efficient_frontier(e, ax=ax, show_assets=False)
# plt.savefig('pyopt.png')
ef.plot_efficient_frontier()
returns, risks = [], []
for r in tqdm(np.arange(0.0009, 0.0022, 0.00005)):
try:
w = ef.efficient_return(r).reshape(4, 1)
rs = np.dot(w.T, expected_returns)
sigma = np.sqrt(np.dot(w.T, np.dot(cov_matrix, w)))
returns.append(float(rs))
risks.append(float(sigma))
except:
pass
# plt.plot(np.array(risks), np.array(returns), label='Otimizador Hack - Solver')
# plt.legend()
# plt.savefig('test_solver.png')
# print(ef.max_loss(-0.06, 1))