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 | class AssetDataExtractor:
def __init__(self, query_mngr, onedrive_path="", today=dt.date.today()):
self.today = today
self.btg_extractor = btg_data_extraction.BTGDataExtractor(onedrive_path=onedrive_path)
self._print_btg_dates()
self.hidex = historical_data_extraction.HistoricalDataExtractor(today=self.today)
#swaps
self.swaps = {} # consolidado dia anterior
self.swaps_pnl = {} # p&l atualizado diario
self.swaps_exp = {} # ponta passiva diaria
# caixas
self.fut_adj_data = {} # lista com nome fundo : [qtd, ticker]
self.term_adj = {} # lista com nome fundo : total termos
self.query_mngr = query_mngr
def _print_btg_dates(self):
dates = self.btg_extractor.get_all_dates()
print("Carteiras BTG em uso:")
for tpl in dates:
print(tpl[0], ": ", tpl[1])
def __get_date_offset(self, ref_date, offset: int): # -> datetime
"""
Retorna a data informada em 'ref_date'(datetime) somada
de 'offset' dias(que pode ser negativo).
"""
delta_day = dt.timedelta(days=1)
return ref_date + delta_day * offset
#def get_usdbrl(self, usd_ticker="usdbrl bgn curncy", flds="px_last"):
# return blp.bdp(usd_ticker, flds).iloc[0,0]
def _update_swaps(self, fund_name):
"""Calcula o p&l swap do fundo solicitado.
Caso ainda nao tenha o valor do swap,
carrega o dia anterior da base historica/btg
"""
# Hardcoded para ndf feito nos fundos us
if fund_name in ["Fund b", "Fund a", "Hmx"]:
exps = {"Fund b" : 0, "Fund a" : 0, "Hmx" : 0}
pnl_fund_b = exps["Fund b"] * (-1) * (self.query_mngr.get_price("usdbrl curncy")/4.6991-1)
pnl_hmx = exps["Hmx"] * (-1) * (self.query_mngr.get_price("usdbrl curncy")/4.7004-1)
pnls = {"Fund b": pnl_fund_b, "Fund a" : 0, "Hmx" : pnl_hmx}
self.swaps_pnl[fund_name] = pnls[fund_name]
self.swaps_exp[fund_name] = exps[fund_name]
else:
if fund_name not in self.swaps:
# precisa resgatar/atualizar base historica
self.swaps[fund_name] = self.hidex.update_swap(fund_name, self.btg_extractor, self.query_mngr)
chg_pct_1d = self.query_mngr.get_pct_change("usdbrl curncy", recent_date=self.today, previous_date=self.query_mngr.get_bday_offset(self.today, -1))
print(f"Variação dólar: {chg_pct_1d*100}%")
new_exp = self.swaps[fund_name]["Exposição"] * (1 + chg_pct_1d)
self.swaps_pnl[fund_name] = self.swaps[fund_name]["Acumulado"] + (self.swaps[fund_name]["Exposição"]-new_exp)
self.swaps_pnl[fund_name] = self.swaps_pnl[fund_name].iat[-1]
self.swaps_exp[fund_name] = new_exp.iat[-1]
def get_swap_pnl(self, fund_name):
self._update_swaps(fund_name)
return self.swaps_pnl[fund_name]
def get_swap_exp(self, fund_name):
if fund_name not in self.swaps_exp:
self._update_swaps(self, fund_name)
return self.swaps_exp[fund_name]
# ---- VERSAO CAIXAS -----
def set_fut_data(self, fund_name, data):
# Para o correto funcionamento, precisa ser setado o quanto antes
self.fut_adj_data[fund_name] = data
def set_term_adj(self, fund_name, term_amnt):
fund_name = fund_name.title()
# Para o correto funcionamento, precisa ser setado o quanto antes
self.term_adj[fund_name] = term_amnt
def calculate_loan_bond(self, name):
loans = {
"jp loan" : {
"base" : -1015000,
"taxa" : 0.0131,
"inicio" : dt.date(2020, 11, 30),
"fim" : dt.date(2023, 10, 31)
},
"loan al" : {
"base" : -500000,
"taxa" : 0.0244,
"inicio" : dt.date(2021, 1, 21),
"fim" : dt.date(2022, 1, 21)
},
"al bond" : {
"base" : 500000,
"taxa" : 0.08,
"inicio" : dt.date(2021, 2, 13),
"fim" : dt.date(2022, 2, 13)
}
}
loan_data = loans[name]
days = (self.today - loan_data["inicio"]).days
pnl = loan_data["base"] * loan_data["taxa"] * days/365
return loan_data["base"] + pnl
# ----- VERSAO RETORNOS -----
def get_ptf_quota_data(self, fund_name):
try:
data = {
"Data" : self.btg_extractor.get_date(fund_name.title()),
"Fundo": fund_name,
"PL" : self.btg_extractor.get_pl(fund_name.title()),
"#_cota" : self.btg_extractor.get_qtd_cotas(fund_name.title()),
"Cota" : self.btg_extractor.get_cota(fund_name.title())
}
return data
except IndexError:
return {}
|