PYield 0.32.6__tar.gz → 0.32.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyield-0.32.6 → pyield-0.32.7}/PKG-INFO +1 -1
- pyield-0.32.7/pyield/__about__.py +1 -0
- pyield-0.32.7/pyield/b3/futures/__init__.py +137 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/b3/futures/historical.py +25 -23
- {pyield-0.32.6 → pyield-0.32.7}/pyield/b3/futures/intraday.py +31 -13
- {pyield-0.32.6 → pyield-0.32.7}/pyield/b3/futures/xml.py +31 -23
- pyield-0.32.6/pyield/__about__.py +0 -1
- pyield-0.32.6/pyield/b3/futures/__init__.py +0 -89
- {pyield-0.32.6 → pyield-0.32.7}/.gitignore +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/LICENSE +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/README.md +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/__init__.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/anbima/__init__.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/anbima/ettj.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/anbima/ima.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/anbima/imaq.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/anbima/ipca.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/anbima/tpf.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/b3/__init__.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/b3/di.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/b3/di_over.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/b3/futures/common.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/bc/__init__.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/bc/auction.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/bc/bcdata.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/bc/ptax.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/bc/repo.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/bc/trade.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/bc/vna.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/bday.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/conftest.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/data_cache.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/date_converter.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/fwd.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/holidays/__init__.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/holidays/br_holidays_new.txt +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/holidays/br_holidays_old.txt +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/holidays/brholidays.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/ibge/__init__.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/ibge/ipca.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/interpolator.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/py.typed +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/retry.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/tpf/__init__.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/tpf/lft.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/tpf/ltn.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/tpf/ntnb.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/tpf/ntnc.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/tpf/ntnf.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyield/tpf/tools.py +0 -0
- {pyield-0.32.6 → pyield-0.32.7}/pyproject.toml +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.32.7"
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import datetime as dt
|
|
2
|
+
import logging
|
|
3
|
+
from typing import Literal
|
|
4
|
+
from zoneinfo import ZoneInfo
|
|
5
|
+
|
|
6
|
+
import pandas as pd
|
|
7
|
+
|
|
8
|
+
from pyield import bday
|
|
9
|
+
from pyield import date_converter as dc
|
|
10
|
+
from pyield.b3.futures.historical import fetch_bmf_data
|
|
11
|
+
from pyield.b3.futures.intraday import fetch_intraday_df
|
|
12
|
+
from pyield.b3.futures.xml import fetch_xml_data
|
|
13
|
+
from pyield.date_converter import DateScalar
|
|
14
|
+
|
|
15
|
+
BZ_TIMEZONE = ZoneInfo("America/Sao_Paulo")
|
|
16
|
+
ContractOptions = Literal["DI1", "DDI", "FRC", "DAP", "DOL", "WDO", "IND", "WIN"]
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
# Apesar do pregão terminar às 18:00h, os dados consolidados são disponibilizados
|
|
21
|
+
# normalmente somente após as 20:00h.
|
|
22
|
+
HISTORICAL_START_TIME = dt.time(20, 0)
|
|
23
|
+
|
|
24
|
+
# Pregão abre às 9:00, porém os dados têm atraso de 15 minutos.
|
|
25
|
+
# Esperar 1 minuto adicional para garantir que estejam disponíveis (9:16h).
|
|
26
|
+
INTRADAY_START_TIME = dt.time(9, 16)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _should_attempt_intraday_fetch(date: pd.Timestamp) -> bool:
|
|
30
|
+
"""Check if the current date is a trading day and if the market is open."""
|
|
31
|
+
|
|
32
|
+
last_bday = bday.last_business_day().date()
|
|
33
|
+
if date.date() != last_bday:
|
|
34
|
+
return False
|
|
35
|
+
|
|
36
|
+
# Pregão não abre na véspera de Natal e Ano Novo
|
|
37
|
+
today = dt.datetime.now(BZ_TIMEZONE).date()
|
|
38
|
+
special_closed_dates = { # Datas especiais
|
|
39
|
+
dt.date(today.year, 12, 24), # Véspera de Natal
|
|
40
|
+
dt.date(today.year, 12, 31), # Véspera de Ano Novo
|
|
41
|
+
}
|
|
42
|
+
if today in special_closed_dates:
|
|
43
|
+
return False
|
|
44
|
+
|
|
45
|
+
# Se não retornou False até aqui, é porque o dia é um dia de negociação
|
|
46
|
+
# e o mercado está aberto.
|
|
47
|
+
return True
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _get_historical_data(
|
|
51
|
+
contract_code: str,
|
|
52
|
+
date: pd.Timestamp,
|
|
53
|
+
) -> pd.DataFrame:
|
|
54
|
+
"""Fetches historical data for a specified futures contract and reference date."""
|
|
55
|
+
# First, try to fetch the data from BMF legacy service
|
|
56
|
+
df = fetch_bmf_data(contract_code, date)
|
|
57
|
+
if not df.empty:
|
|
58
|
+
return df # If data is available from BMF, return it
|
|
59
|
+
|
|
60
|
+
# If BMF data is not available, try to fetch the full XML report
|
|
61
|
+
return fetch_xml_data(date, contract_code, "PR")
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def futures(
|
|
65
|
+
contract_code: ContractOptions | str,
|
|
66
|
+
date: DateScalar,
|
|
67
|
+
) -> pd.DataFrame:
|
|
68
|
+
"""
|
|
69
|
+
Fetches data for a specified futures contract based on type and reference date.
|
|
70
|
+
|
|
71
|
+
Args:
|
|
72
|
+
contract_code (str): The B3 futures contract code identifying the derivative.
|
|
73
|
+
Supported contract codes are:
|
|
74
|
+
- "DI1": One-day Interbank Deposit Futures (Futuro de DI) from B3.
|
|
75
|
+
- "DDI": DI x U.S. Dollar Spread Futures (Futuro de Cupom Cambial) from B3.
|
|
76
|
+
- "FRC": Forward Rate Agreement (FRA) from B3.
|
|
77
|
+
- "DAP": DI x IPCA Spread Futures.
|
|
78
|
+
- "DOL": U.S. Dollar Futures from B3.
|
|
79
|
+
- "WDO": Mini U.S. Dollar Futures from B3.
|
|
80
|
+
- "IND": Ibovespa Futures from B3.
|
|
81
|
+
- "WIN": Mini Ibovespa Futures from B3.
|
|
82
|
+
date (DateScalar): The date for which to fetch the data.
|
|
83
|
+
If the reference date is a string, it should be in 'DD-MM-YYYY' format.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
pd.DataFrame: DataFrame containing the fetched data for the specified futures
|
|
87
|
+
contract.
|
|
88
|
+
|
|
89
|
+
Raises:
|
|
90
|
+
ValueError: If the futures contract code is not recognized or supported.
|
|
91
|
+
|
|
92
|
+
Examples:
|
|
93
|
+
>>> futures("DI1", "31-05-2024")
|
|
94
|
+
TradeDate TickerSymbol ... SettlementRate ForwardRate
|
|
95
|
+
0 2024-05-31 DI1M24 ... 0.10399 0.10399
|
|
96
|
+
1 2024-05-31 DI1N24 ... 0.1039 0.103896
|
|
97
|
+
2 2024-05-31 DI1Q24 ... 0.1037 0.103517
|
|
98
|
+
3 2024-05-31 DI1U24 ... 0.1036 0.1034
|
|
99
|
+
...
|
|
100
|
+
|
|
101
|
+
>>> futures("DAP", "31-05-2024")
|
|
102
|
+
TradeDate TickerSymbol ... SettlementRate ForwardRate
|
|
103
|
+
0 2024-05-31 DAPM24 ... 0.0555 0.0555
|
|
104
|
+
1 2024-05-31 DAPN24 ... 0.07524 0.086254
|
|
105
|
+
2 2024-05-31 DAPQ24 ... 0.0885 0.106631
|
|
106
|
+
3 2024-05-31 DAPU24 ... 0.0855 0.078171
|
|
107
|
+
...
|
|
108
|
+
"""
|
|
109
|
+
converted_date = dc.convert_input_dates(date)
|
|
110
|
+
selected_contract = str(contract_code).upper()
|
|
111
|
+
|
|
112
|
+
if _should_attempt_intraday_fetch(
|
|
113
|
+
converted_date
|
|
114
|
+
): # É um dia de negociação intraday
|
|
115
|
+
time = dt.datetime.now(BZ_TIMEZONE).time()
|
|
116
|
+
if time < INTRADAY_START_TIME: # Mercado não está aberto ainda
|
|
117
|
+
logger.warning("Market is not open yet. Returning an empty DataFrame. ")
|
|
118
|
+
return pd.DataFrame()
|
|
119
|
+
|
|
120
|
+
# Existe a chance de que os dados consolidados estejam disponíveis após as 20h
|
|
121
|
+
if time >= HISTORICAL_START_TIME:
|
|
122
|
+
df_hist = _get_historical_data(selected_contract, converted_date)
|
|
123
|
+
if not df_hist.empty:
|
|
124
|
+
logger.info("Consolidated data is already available and will be used.")
|
|
125
|
+
return df_hist
|
|
126
|
+
|
|
127
|
+
# Mercado está aberto e não há dados consolidados disponíveis ainda
|
|
128
|
+
return fetch_intraday_df(selected_contract)
|
|
129
|
+
|
|
130
|
+
else: # É um dia histórico
|
|
131
|
+
return _get_historical_data(selected_contract, converted_date)
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
__all__ = [
|
|
135
|
+
"futures",
|
|
136
|
+
"ContractOptions",
|
|
137
|
+
]
|
|
@@ -1,21 +1,15 @@
|
|
|
1
1
|
import io
|
|
2
|
+
import logging
|
|
2
3
|
|
|
3
4
|
import pandas as pd
|
|
4
5
|
import requests
|
|
5
6
|
|
|
6
7
|
from pyield import bday
|
|
7
8
|
from pyield.b3.futures import common
|
|
9
|
+
from pyield.fwd import forwards
|
|
8
10
|
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
"DI1": 252,
|
|
12
|
-
"DDI": 360,
|
|
13
|
-
"FRC": None,
|
|
14
|
-
"DOL": None,
|
|
15
|
-
"WDO": None,
|
|
16
|
-
"IND": None,
|
|
17
|
-
"WIN": None,
|
|
18
|
-
}
|
|
11
|
+
logger = logging.getLogger(__name__)
|
|
12
|
+
COUNT_CONVENTIONS = {"DAP": 252, "DI1": 252, "DDI": 360}
|
|
19
13
|
|
|
20
14
|
|
|
21
15
|
def get_old_expiration_date(expiration_code: str, date: pd.Timestamp) -> pd.Timestamp:
|
|
@@ -103,7 +97,7 @@ def _convert_prices_to_rates(
|
|
|
103
97
|
return rates.round(5)
|
|
104
98
|
|
|
105
99
|
|
|
106
|
-
def
|
|
100
|
+
def _fetch_url_data(contract_code: str, date: pd.Timestamp) -> pd.DataFrame:
|
|
107
101
|
"""
|
|
108
102
|
Fetch the historical futures data from B3 for a specific trade date. If the data is
|
|
109
103
|
not available, an empty DataFrame is returned.
|
|
@@ -183,14 +177,14 @@ def _rename_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
183
177
|
|
|
184
178
|
|
|
185
179
|
def process_df(
|
|
186
|
-
input_df: pd.DataFrame, date: pd.Timestamp,
|
|
180
|
+
input_df: pd.DataFrame, date: pd.Timestamp, contract_code: str
|
|
187
181
|
) -> pd.DataFrame:
|
|
188
182
|
df = input_df.copy()
|
|
189
183
|
df["TradeDate"] = date
|
|
190
184
|
# Convert to datetime64[ns] since it is pandas default type for timestamps
|
|
191
185
|
df["TradeDate"] = df["TradeDate"].astype("datetime64[ns]")
|
|
192
186
|
|
|
193
|
-
df["TickerSymbol"] =
|
|
187
|
+
df["TickerSymbol"] = contract_code + df["ExpirationCode"]
|
|
194
188
|
|
|
195
189
|
# Contract code format was changed in 22/05/2006
|
|
196
190
|
if date < pd.Timestamp("2006-05-22"):
|
|
@@ -198,7 +192,7 @@ def process_df(
|
|
|
198
192
|
get_old_expiration_date, args=(date,)
|
|
199
193
|
)
|
|
200
194
|
else:
|
|
201
|
-
expiration_day = 15 if
|
|
195
|
+
expiration_day = 15 if contract_code == "DAP" else 1
|
|
202
196
|
df["ExpirationDate"] = df["ExpirationCode"].apply(
|
|
203
197
|
common.get_expiration_date, args=(expiration_day,)
|
|
204
198
|
)
|
|
@@ -221,31 +215,34 @@ def process_df(
|
|
|
221
215
|
|
|
222
216
|
rate_cols = [col for col in df.columns if "Rate" in col]
|
|
223
217
|
# Prior to 17/01/2002 (inclusive), DI prices were not converted to rates
|
|
224
|
-
if date <= pd.Timestamp("2002-01-17") and
|
|
218
|
+
if date <= pd.Timestamp("2002-01-17") and contract_code == "DI1":
|
|
225
219
|
df = _adjust_older_contracts_rates(df, rate_cols)
|
|
226
220
|
else:
|
|
227
221
|
# Remove % and round to 5 (3 in %) dec. places in rate columns
|
|
228
222
|
df[rate_cols] = df[rate_cols].div(100).round(5)
|
|
229
223
|
|
|
230
|
-
|
|
224
|
+
count_convention = COUNT_CONVENTIONS.get(contract_code)
|
|
225
|
+
if count_convention == 252: # noqa
|
|
231
226
|
df["SettlementRate"] = _convert_prices_to_rates(
|
|
232
227
|
prices=df["SettlementPrice"],
|
|
233
228
|
days_to_expiration=df["BDaysToExp"],
|
|
234
229
|
count_convention=252,
|
|
235
230
|
)
|
|
236
|
-
elif
|
|
231
|
+
elif count_convention == 360: # noqa
|
|
237
232
|
df["SettlementRate"] = _convert_prices_to_rates(
|
|
238
233
|
prices=df["SettlementPrice"],
|
|
239
234
|
days_to_expiration=df["DaysToExp"],
|
|
240
235
|
count_convention=360,
|
|
241
236
|
)
|
|
242
237
|
|
|
243
|
-
# Calculate DV01 for DI1 contracts
|
|
244
|
-
if asset_code == "DI1":
|
|
238
|
+
if contract_code == "DI1": # Calculate DV01 for DI1 contracts
|
|
245
239
|
duration = df["BDaysToExp"] / 252
|
|
246
240
|
modified_duration = duration / (1 + df["SettlementRate"])
|
|
247
241
|
df["DV01"] = 0.0001 * modified_duration * df["SettlementPrice"]
|
|
248
242
|
|
|
243
|
+
if contract_code in {"DI1", "DAP"}: # Calculate forwards for DI1 and DAP contracts
|
|
244
|
+
df["ForwardRate"] = forwards(bdays=df["BDaysToExp"], rates=df["SettlementRate"])
|
|
245
|
+
|
|
249
246
|
return df
|
|
250
247
|
|
|
251
248
|
|
|
@@ -265,7 +262,6 @@ def _select_and_reorder_columns(df: pd.DataFrame):
|
|
|
265
262
|
"FinancialVolume",
|
|
266
263
|
"DV01",
|
|
267
264
|
"SettlementPrice",
|
|
268
|
-
"SettlementRate",
|
|
269
265
|
"OpenRate",
|
|
270
266
|
"MinRate",
|
|
271
267
|
"AvgRate",
|
|
@@ -273,12 +269,14 @@ def _select_and_reorder_columns(df: pd.DataFrame):
|
|
|
273
269
|
"CloseAskRate",
|
|
274
270
|
"CloseBidRate",
|
|
275
271
|
"CloseRate",
|
|
272
|
+
"SettlementRate",
|
|
273
|
+
"ForwardRate",
|
|
276
274
|
]
|
|
277
275
|
reordered_columns = [col for col in all_columns if col in df.columns]
|
|
278
276
|
return df[reordered_columns]
|
|
279
277
|
|
|
280
278
|
|
|
281
|
-
def
|
|
279
|
+
def fetch_bmf_data(contract_code: str, date: pd.Timestamp) -> pd.DataFrame:
|
|
282
280
|
"""
|
|
283
281
|
Fetchs the futures data for a given date from B3.
|
|
284
282
|
|
|
@@ -295,9 +293,13 @@ def fetch_historical_df(contract_code: str, date: pd.Timestamp) -> pd.DataFrame:
|
|
|
295
293
|
pd.DataFrame: A Pandas pd.DataFrame containing processed futures data. If
|
|
296
294
|
the data is not available, an empty DataFrame is returned.
|
|
297
295
|
"""
|
|
298
|
-
df_raw =
|
|
296
|
+
df_raw = _fetch_url_data(contract_code, date)
|
|
299
297
|
if df_raw.empty:
|
|
300
|
-
|
|
298
|
+
logger.warning(
|
|
299
|
+
f"No data found for {contract_code} on {date.strftime('%d-%m-%Y')}."
|
|
300
|
+
f" Returning an empty DataFrame."
|
|
301
|
+
)
|
|
302
|
+
return pd.DataFrame()
|
|
301
303
|
|
|
302
304
|
df = _rename_columns(df_raw)
|
|
303
305
|
df = process_df(df, date, contract_code)
|
|
@@ -1,15 +1,26 @@
|
|
|
1
|
+
import datetime as dt
|
|
2
|
+
import logging
|
|
1
3
|
from zoneinfo import ZoneInfo
|
|
2
4
|
|
|
3
5
|
import pandas as pd
|
|
4
6
|
import requests
|
|
5
7
|
|
|
6
8
|
from pyield import bday
|
|
9
|
+
from pyield.fwd import forwards
|
|
7
10
|
from pyield.retry import default_retry
|
|
8
11
|
|
|
9
|
-
# Timezone for Brazil
|
|
10
|
-
TIMEZONE_BZ = ZoneInfo("America/Sao_Paulo")
|
|
11
12
|
BASE_URL = "https://cotacao.b3.com.br/mds/api/v1/DerivativeQuotation"
|
|
12
13
|
|
|
14
|
+
# Timezone for Brazil
|
|
15
|
+
BZ_TIMEZONE = ZoneInfo("America/Sao_Paulo")
|
|
16
|
+
|
|
17
|
+
# Pregão abre às 9:00, porém os dados têm atraso de 15 minutos.
|
|
18
|
+
# Esperar 1 minuto adicional para garantir que estejam disponíveis (9:16h).
|
|
19
|
+
INTRADAY_START_TIME = dt.time(9, 16)
|
|
20
|
+
|
|
21
|
+
# Set up logging
|
|
22
|
+
logger = logging.getLogger(__name__)
|
|
23
|
+
|
|
13
24
|
|
|
14
25
|
@default_retry
|
|
15
26
|
def _fetch_b3_df(contract_code: str) -> pd.DataFrame:
|
|
@@ -78,7 +89,8 @@ def _rename_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
78
89
|
return df
|
|
79
90
|
|
|
80
91
|
|
|
81
|
-
def _process_df(raw_df: pd.DataFrame) -> pd.DataFrame:
|
|
92
|
+
def _process_df(raw_df: pd.DataFrame, contract_code: str) -> pd.DataFrame:
|
|
93
|
+
"""Process the raw DataFrame from B3 API."""
|
|
82
94
|
df = raw_df.copy()
|
|
83
95
|
|
|
84
96
|
# Clean and reformat the DataFrame columns
|
|
@@ -101,7 +113,7 @@ def _process_df(raw_df: pd.DataFrame) -> pd.DataFrame:
|
|
|
101
113
|
df["TradeDate"] = bday.last_business_day()
|
|
102
114
|
|
|
103
115
|
# Get current date and time
|
|
104
|
-
now = pd.Timestamp.now(
|
|
116
|
+
now = pd.Timestamp.now(BZ_TIMEZONE).round("s").tz_localize(None)
|
|
105
117
|
# Subtract 15 minutes from the current time to account for API delay
|
|
106
118
|
df["LastUpdate"] = now - pd.Timedelta(minutes=15)
|
|
107
119
|
|
|
@@ -118,14 +130,13 @@ def _process_df(raw_df: pd.DataFrame) -> pd.DataFrame:
|
|
|
118
130
|
rate_cols = [col for col in df.columns if "Rate" in col]
|
|
119
131
|
df[rate_cols] = df[rate_cols].div(100).round(5)
|
|
120
132
|
|
|
121
|
-
#
|
|
122
|
-
contract_type = df["TickerSymbol"].iloc[0][:3]
|
|
123
|
-
if contract_type in {"DI1", "DAP"}:
|
|
133
|
+
if contract_code in {"DI1", "DAP"}: # Add LastPrice for DI1 and DAP
|
|
124
134
|
byears = df["BDaysToExp"] / 252
|
|
125
|
-
df["LastPrice"] = 100_000 / ((1 + df["LastRate"]) **
|
|
135
|
+
df["LastPrice"] = 100_000 / ((1 + df["LastRate"]) ** byears)
|
|
126
136
|
df["LastPrice"] = df["LastPrice"].round(2).astype("Float64")
|
|
137
|
+
df["ForwardRate"] = forwards(bdays=df["BDaysToExp"], rates=df["LastRate"])
|
|
127
138
|
|
|
128
|
-
if
|
|
139
|
+
if contract_code == "DI1": # Add DV01 for DI1
|
|
129
140
|
duration = df["BDaysToExp"] / 252
|
|
130
141
|
modified_duration = duration / (1 + df["LastRate"])
|
|
131
142
|
df["DV01"] = 0.0001 * modified_duration * df["LastPrice"]
|
|
@@ -155,6 +166,7 @@ def _select_and_reorder_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
155
166
|
"TradeVolume",
|
|
156
167
|
"FinancialVolume",
|
|
157
168
|
"DV01",
|
|
169
|
+
"LastPrice",
|
|
158
170
|
"PrevSettlementRate",
|
|
159
171
|
"MinLimitRate",
|
|
160
172
|
"MaxLimitRate",
|
|
@@ -165,22 +177,28 @@ def _select_and_reorder_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
165
177
|
"LastAskRate",
|
|
166
178
|
"LastBidRate",
|
|
167
179
|
"LastRate",
|
|
168
|
-
"
|
|
180
|
+
"ForwardRate",
|
|
169
181
|
]
|
|
170
182
|
reordered_columns = [col for col in all_columns if col in df.columns]
|
|
171
183
|
return df[reordered_columns].copy()
|
|
172
184
|
|
|
173
185
|
|
|
174
|
-
def fetch_intraday_df(
|
|
186
|
+
def fetch_intraday_df(contract_code: str) -> pd.DataFrame:
|
|
175
187
|
"""
|
|
176
188
|
Fetch the latest futures data from B3.
|
|
177
189
|
|
|
178
190
|
Returns:
|
|
179
191
|
pd.DataFrame: A Pandas pd.DataFrame containing the latest DI futures data.
|
|
180
192
|
"""
|
|
181
|
-
raw_df = _fetch_b3_df(
|
|
193
|
+
raw_df = _fetch_b3_df(contract_code)
|
|
182
194
|
if raw_df.empty:
|
|
195
|
+
date_str = dt.datetime.now(BZ_TIMEZONE).strftime("%d-%m-%Y %H:%M")
|
|
196
|
+
logger.warning(
|
|
197
|
+
f"No data available for {contract_code} on {date_str}. "
|
|
198
|
+
f"Returning an empty DataFrame."
|
|
199
|
+
)
|
|
200
|
+
|
|
183
201
|
return pd.DataFrame()
|
|
184
|
-
df = _process_df(raw_df)
|
|
202
|
+
df = _process_df(raw_df, contract_code)
|
|
185
203
|
df = _select_and_reorder_columns(df)
|
|
186
204
|
return df
|
|
@@ -10,6 +10,8 @@ from lxml import etree
|
|
|
10
10
|
|
|
11
11
|
from pyield import bday
|
|
12
12
|
from pyield.b3.futures import common
|
|
13
|
+
from pyield.fwd import forwards
|
|
14
|
+
from pyield.retry import default_retry
|
|
13
15
|
|
|
14
16
|
logger = logging.getLogger(__name__)
|
|
15
17
|
|
|
@@ -25,6 +27,7 @@ def _get_file_from_path(file_path: Path) -> io.BytesIO:
|
|
|
25
27
|
return io.BytesIO(content)
|
|
26
28
|
|
|
27
29
|
|
|
30
|
+
@default_retry
|
|
28
31
|
def _get_file_from_url(date: pd.Timestamp, source_type: str) -> io.BytesIO:
|
|
29
32
|
"""
|
|
30
33
|
Types of XML files available:
|
|
@@ -49,13 +52,9 @@ def _get_file_from_url(date: pd.Timestamp, source_type: str) -> io.BytesIO:
|
|
|
49
52
|
|
|
50
53
|
response = requests.get(url, timeout=10)
|
|
51
54
|
|
|
52
|
-
#
|
|
53
|
-
if
|
|
55
|
+
# When a the date has no data, the file has less than 22 bytes
|
|
56
|
+
if len(response.content) < 1024: # noqa
|
|
54
57
|
date_str = date.strftime("%Y-%m-%d")
|
|
55
|
-
logger.warning(
|
|
56
|
-
f"File not found or too small ({len(response.content)} bytes) "
|
|
57
|
-
f"for {source_type} on {date_str}. Returning empty file."
|
|
58
|
-
)
|
|
59
58
|
return io.BytesIO()
|
|
60
59
|
|
|
61
60
|
return io.BytesIO(response.content)
|
|
@@ -197,8 +196,8 @@ def _process_df(df_raw: pd.DataFrame) -> pd.DataFrame:
|
|
|
197
196
|
df["TradeDate"] = df["TradeDate"].astype("datetime64[ns]")
|
|
198
197
|
|
|
199
198
|
expiration_code = df["TickerSymbol"].str[3:]
|
|
200
|
-
|
|
201
|
-
expiration_day = 15 if
|
|
199
|
+
contract_code = df["TickerSymbol"].str[:3].loc[0]
|
|
200
|
+
expiration_day = 15 if contract_code == "DAP" else 1
|
|
202
201
|
df["ExpirationDate"] = expiration_code.apply(
|
|
203
202
|
common.get_expiration_date, args=(expiration_day,)
|
|
204
203
|
)
|
|
@@ -221,6 +220,14 @@ def _process_df(df_raw: pd.DataFrame) -> pd.DataFrame:
|
|
|
221
220
|
if col in df.columns:
|
|
222
221
|
df[col] = df[col].fillna(0)
|
|
223
222
|
|
|
223
|
+
if contract_code == "DI1": # Calculate DV01 for DI1 contracts
|
|
224
|
+
duration = df["BDaysToExp"] / 252
|
|
225
|
+
modified_duration = duration / (1 + df["SettlementRate"])
|
|
226
|
+
df["DV01"] = 0.0001 * modified_duration * df["SettlementPrice"]
|
|
227
|
+
|
|
228
|
+
if contract_code in {"DI1", "DAP"}: # Calculate forwards for DI1 and DAP contracts
|
|
229
|
+
df["ForwardRate"] = forwards(bdays=df["BDaysToExp"], rates=df["SettlementRate"])
|
|
230
|
+
|
|
224
231
|
return df.sort_values(by=["ExpirationDate"], ignore_index=True)
|
|
225
232
|
|
|
226
233
|
|
|
@@ -236,8 +243,8 @@ def _select_and_reorder_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
236
243
|
"TradeCount",
|
|
237
244
|
"TradeVolume",
|
|
238
245
|
"FinancialVolume",
|
|
246
|
+
"DV01",
|
|
239
247
|
"SettlementPrice",
|
|
240
|
-
"SettlementRate",
|
|
241
248
|
"MinLimitRate",
|
|
242
249
|
"MaxLimitRate",
|
|
243
250
|
"BestBidRate",
|
|
@@ -247,19 +254,21 @@ def _select_and_reorder_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
247
254
|
"AvgRate",
|
|
248
255
|
"MaxRate",
|
|
249
256
|
"CloseRate",
|
|
257
|
+
"SettlementRate",
|
|
258
|
+
"ForwardRate",
|
|
250
259
|
]
|
|
251
260
|
selected_columns = [col for col in all_columns if col in df.columns]
|
|
252
261
|
return df[selected_columns]
|
|
253
262
|
|
|
254
263
|
|
|
255
|
-
def process_zip_file(zip_file: io.BytesIO,
|
|
264
|
+
def process_zip_file(zip_file: io.BytesIO, contract_code: str) -> pd.DataFrame:
|
|
256
265
|
if zip_file is None or zip_file.getbuffer().nbytes == 0:
|
|
257
|
-
logger.warning("Empty XML zip file.
|
|
266
|
+
logger.warning("Empty XML zip file. Probably the date has no data.")
|
|
258
267
|
return pd.DataFrame()
|
|
259
268
|
|
|
260
269
|
xml_file = _extract_xml_from_zip(zip_file)
|
|
261
270
|
|
|
262
|
-
di_data = _extract_data_from_xml(xml_file,
|
|
271
|
+
di_data = _extract_data_from_xml(xml_file, contract_code)
|
|
263
272
|
|
|
264
273
|
df_raw = _create_df_from_data(di_data)
|
|
265
274
|
|
|
@@ -269,16 +278,11 @@ def process_zip_file(zip_file: io.BytesIO, asset_code: str) -> pd.DataFrame:
|
|
|
269
278
|
|
|
270
279
|
df = _select_and_reorder_columns(df)
|
|
271
280
|
|
|
272
|
-
if asset_code == "DI1":
|
|
273
|
-
duration = df["BDaysToExp"] / 252
|
|
274
|
-
modified_duration = duration / (1 + df["SettlementRate"])
|
|
275
|
-
df["DV01"] = 0.0001 * modified_duration * df["SettlementPrice"]
|
|
276
|
-
|
|
277
281
|
return df
|
|
278
282
|
|
|
279
283
|
|
|
280
|
-
def
|
|
281
|
-
date: pd.Timestamp,
|
|
284
|
+
def fetch_xml_data(
|
|
285
|
+
date: pd.Timestamp, contract_code: str, source_type: Literal["PR", "SPR"]
|
|
282
286
|
) -> pd.DataFrame:
|
|
283
287
|
"""Fetches and processes an XML report from B3's website.
|
|
284
288
|
|
|
@@ -300,13 +304,17 @@ def fetch_xml_report(
|
|
|
300
304
|
ValueError: If the `source_type` is invalid or if no data is
|
|
301
305
|
available for the given date.
|
|
302
306
|
"""
|
|
303
|
-
|
|
304
|
-
|
|
307
|
+
try:
|
|
308
|
+
zip_file = _get_file_from_url(date, source_type)
|
|
309
|
+
df = process_zip_file(zip_file, contract_code)
|
|
310
|
+
except ValueError as e:
|
|
311
|
+
logger.warning(f"Error fetching XML data: {e}. Returning empty DataFrame.")
|
|
312
|
+
return pd.DataFrame()
|
|
305
313
|
|
|
306
314
|
return df
|
|
307
315
|
|
|
308
316
|
|
|
309
|
-
def read_xml_report(file_path: Path,
|
|
317
|
+
def read_xml_report(file_path: Path, contract_code: str) -> pd.DataFrame:
|
|
310
318
|
"""Reads and processes an XML report from a local file.
|
|
311
319
|
|
|
312
320
|
Reads a zipped XML report from the specified file path, extracts the
|
|
@@ -325,5 +333,5 @@ def read_xml_report(file_path: Path, asset_code: str) -> pd.DataFrame:
|
|
|
325
333
|
FileNotFoundError: If no file is found at the specified `file_path`.
|
|
326
334
|
"""
|
|
327
335
|
zip_file = _get_file_from_path(file_path)
|
|
328
|
-
df = process_zip_file(zip_file,
|
|
336
|
+
df = process_zip_file(zip_file, contract_code)
|
|
329
337
|
return df
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.32.6"
|
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import datetime as dt
|
|
2
|
-
import logging
|
|
3
|
-
from typing import Literal
|
|
4
|
-
from zoneinfo import ZoneInfo
|
|
5
|
-
|
|
6
|
-
import pandas as pd
|
|
7
|
-
|
|
8
|
-
from pyield import date_converter as dc
|
|
9
|
-
from pyield.b3.futures.historical import fetch_historical_df
|
|
10
|
-
from pyield.b3.futures.intraday import fetch_intraday_df
|
|
11
|
-
from pyield.b3.futures.xml import fetch_xml_report
|
|
12
|
-
from pyield.date_converter import DateScalar
|
|
13
|
-
|
|
14
|
-
TIMEZONE_BZ = ZoneInfo("America/Sao_Paulo")
|
|
15
|
-
ContractOptions = Literal["DI1", "DDI", "FRC", "DAP", "DOL", "WDO", "IND", "WIN"]
|
|
16
|
-
logger = logging.getLogger(__name__)
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
def futures(
|
|
20
|
-
contract_code: ContractOptions | str,
|
|
21
|
-
date: DateScalar,
|
|
22
|
-
) -> pd.DataFrame:
|
|
23
|
-
"""
|
|
24
|
-
Fetches data for a specified futures contract based on type and reference date.
|
|
25
|
-
|
|
26
|
-
Args:
|
|
27
|
-
contract_code (str): The B3 futures contract code identifying the derivative.
|
|
28
|
-
Supported contract codes are:
|
|
29
|
-
- "DI1": One-day Interbank Deposit Futures (Futuro de DI) from B3.
|
|
30
|
-
- "DDI": DI x U.S. Dollar Spread Futures (Futuro de Cupom Cambial) from B3.
|
|
31
|
-
- "FRC": Forward Rate Agreement (FRA) from B3.
|
|
32
|
-
- "DAP": DI x IPCA Spread Futures.
|
|
33
|
-
- "DOL": U.S. Dollar Futures from B3.
|
|
34
|
-
- "WDO": Mini U.S. Dollar Futures from B3.
|
|
35
|
-
- "IND": Ibovespa Futures from B3.
|
|
36
|
-
- "WIN": Mini Ibovespa Futures from B3.
|
|
37
|
-
date (DateScalar): The date for which to fetch the data.
|
|
38
|
-
If the reference date is a string, it should be in 'DD-MM-YYYY' format.
|
|
39
|
-
|
|
40
|
-
Returns:
|
|
41
|
-
pd.DataFrame: DataFrame containing the fetched data for the specified futures
|
|
42
|
-
contract.
|
|
43
|
-
|
|
44
|
-
Raises:
|
|
45
|
-
ValueError: If the futures contract code is not recognized or supported.
|
|
46
|
-
|
|
47
|
-
Examples:
|
|
48
|
-
>>> futures("DI1", "31-05-2024")
|
|
49
|
-
TradeDate TickerSymbol ... CloseBidRate CloseRate
|
|
50
|
-
0 2024-05-31 DI1M24 ... 0.10404 0.10404
|
|
51
|
-
1 2024-05-31 DI1N24 ... 0.1039 0.10386
|
|
52
|
-
2 2024-05-31 DI1Q24 ... 0.10374 0.10374
|
|
53
|
-
3 2024-05-31 DI1U24 ... 0.10365 0.10355
|
|
54
|
-
...
|
|
55
|
-
>>> futures("DAP", "31-05-2024")
|
|
56
|
-
TradeDate TickerSymbol ... CloseBidRate CloseRate
|
|
57
|
-
0 2024-05-31 DAPM24 ... <NA> <NA>
|
|
58
|
-
1 2024-05-31 DAPN24 ... <NA> <NA>
|
|
59
|
-
2 2024-05-31 DAPQ24 ... <NA> 0.0885
|
|
60
|
-
3 2024-05-31 DAPU24 ... <NA> 0.0865
|
|
61
|
-
...
|
|
62
|
-
"""
|
|
63
|
-
selected_contract = str(contract_code).upper()
|
|
64
|
-
converted_date = dc.convert_input_dates(date)
|
|
65
|
-
|
|
66
|
-
# First, try to fetch historical data from BMF site
|
|
67
|
-
df = fetch_historical_df(selected_contract, converted_date)
|
|
68
|
-
if df.empty:
|
|
69
|
-
# If historical data is not available, try to fetch the full XML report
|
|
70
|
-
# from the B3 site
|
|
71
|
-
try:
|
|
72
|
-
df = fetch_xml_report(converted_date, selected_contract, "PR")
|
|
73
|
-
logger.info(f"Using B3 XML report data for {selected_contract}")
|
|
74
|
-
except ValueError as e:
|
|
75
|
-
logger.error(f"Failed to fetch data for {selected_contract}: {e}")
|
|
76
|
-
raise
|
|
77
|
-
else:
|
|
78
|
-
logger.info(f"Using BMF historical data for {selected_contract}")
|
|
79
|
-
|
|
80
|
-
bz_today = dt.datetime.now(TIMEZONE_BZ).date()
|
|
81
|
-
# If there is no historical data available, try to fetch intraday data
|
|
82
|
-
if converted_date.date() == bz_today and df.empty:
|
|
83
|
-
df = fetch_intraday_df(selected_contract)
|
|
84
|
-
logger.info(f"Using B3 intraday data for {selected_contract}")
|
|
85
|
-
|
|
86
|
-
return df
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
__all__ = ["futures", "ContractOptions"]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|