Skip to content

Commit 3c01173

Browse files
add requested changes by quant-man
1 parent 70d7b2f commit 3c01173

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

turingquant/optimizers.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,32 @@ def __init__(self, df_close, num_portfolios = 10000, risk_free = 0):
2525
self.df = df_close
2626
self.num_portfolios = num_portfolios
2727
self.risk_free = risk_free
28-
28+
self.wallets = self._generate_wallets()
29+
30+
def _generate_wallets(self):
31+
'''
32+
Gera carteiras com pesos aleatórios.
33+
34+
Returns:
35+
wallets (dict): dicionário contendo os valores 'weights', 'returns', 'vol' e 'sharpe_ratio'
36+
de todos os portfólios gerados
37+
'''
2938
# vetores de dados
3039
portfolio_weights = []
3140
portfolio_exp_returns = []
3241
portfolio_vol = []
3342
portfolio_sharpe = []
3443

3544
# retorno simples
36-
r = df.pct_change()
45+
r = self.df.pct_change()
3746
mean_returns = r.mean() * 252
3847

3948
# matriz de covariância
4049
covariance = np.cov(r[1:].T)
4150

4251
for i in range(self.num_portfolios):
4352
# gerando pesos aleatórios
44-
k = np.random.rand(len(df.columns))
53+
k = np.random.rand(len(self.df.columns))
4554
w = k / sum (k)
4655

4756
# retorno
@@ -58,11 +67,13 @@ def __init__(self, df_close, num_portfolios = 10000, risk_free = 0):
5867
portfolio_vol.append(vol)
5968
portfolio_sharpe.append(sharpe)
6069

61-
self.wallets = {'weights': portfolio_weights,
70+
wallets = {'weights': portfolio_weights,
6271
'returns': portfolio_exp_returns,
6372
'vol':portfolio_vol,
6473
'sharpe': portfolio_sharpe}
65-
74+
75+
return wallets
76+
6677
def plot_efficient_frontier(self, method = 'sharpe_ratio'):
6778
'''
6879
Plota gráfico com a fronteira eficiente dos portfólios gerados.
@@ -134,4 +145,5 @@ def best_portfolio(self, method = 'sharpe_ratio'):
134145

135146
indice = np.array(returns).argmax()
136147

137-
return weights[indice]
148+
return weights[indice]
149+

0 commit comments

Comments
 (0)