PYield 0.7.3__tar.gz → 0.7.4__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.7.3 → pyield-0.7.4}/PKG-INFO +2 -1
- pyield-0.7.4/pyield/__about__.py +1 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/__init__.py +2 -1
- pyield-0.7.4/pyield/data_access.py +148 -0
- pyield-0.7.4/pyield/futures/__init__.py +13 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/futures/common.py +4 -3
- {pyield-0.7.3 → pyield-0.7.4}/pyield/futures/ddi.py +2 -2
- {pyield-0.7.3 → pyield-0.7.4}/pyield/futures/di.py +8 -14
- pyield-0.7.4/pyield/futures/frc.py +94 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/indicators.py +58 -4
- pyield-0.7.4/pyield/projections.py +87 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/treasuries.py +1 -1
- {pyield-0.7.3 → pyield-0.7.4}/pyield/utils.py +10 -10
- {pyield-0.7.3 → pyield-0.7.4}/pyproject.toml +2 -1
- {pyield-0.7.3 → pyield-0.7.4}/tests/test_futures.py +3 -3
- pyield-0.7.3/pyield/__about__.py +0 -1
- pyield-0.7.3/pyield/data_access.py +0 -95
- pyield-0.7.3/pyield/futures/__init__.py +0 -11
- {pyield-0.7.3 → pyield-0.7.4}/LICENSE +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/README.md +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/bday.py +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/data_analysis.py +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/futures/di_xml.py +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/holidays/__init__.py +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/holidays/br_holidays_new.txt +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/holidays/br_holidays_old.txt +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/holidays/core.py +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/pyield/py.typed +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/tests/__init__.py +0 -0
- {pyield-0.7.3 → pyield-0.7.4}/tests/test_bday.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: PYield
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.4
|
|
4
4
|
Summary: A Python library for analysis of fixed income instruments in Brazil
|
|
5
5
|
Keywords: fixed-income, brazil, finance, analysis, bonds
|
|
6
6
|
Author-Email: Carlos Carvalho <cr.cj@outlook.com>
|
|
@@ -38,6 +38,7 @@ Requires-Dist: numpy
|
|
|
38
38
|
Requires-Dist: beautifulsoup4
|
|
39
39
|
Requires-Dist: html5lib
|
|
40
40
|
Requires-Dist: lxml
|
|
41
|
+
Requires-Dist: python-calamine>=0.2.0
|
|
41
42
|
Description-Content-Type: text/markdown
|
|
42
43
|
|
|
43
44
|
[](https://pypi.python.org/pypi/pyield)
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.7.4"
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
from .__about__ import __version__
|
|
2
2
|
from .bday import count_bdays, generate_bdays, is_bday, offset_bdays
|
|
3
|
-
from .data_access import fetch_asset, fetch_indicator
|
|
3
|
+
from .data_access import fetch_asset, fetch_indicator, fetch_projection
|
|
4
4
|
from .data_analysis import calculate_spreads
|
|
5
5
|
|
|
6
6
|
__all__ = [
|
|
@@ -11,5 +11,6 @@ __all__ = [
|
|
|
11
11
|
"count_bdays",
|
|
12
12
|
"fetch_asset",
|
|
13
13
|
"fetch_indicator",
|
|
14
|
+
"fetch_projection",
|
|
14
15
|
"calculate_spreads",
|
|
15
16
|
]
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
|
|
3
|
+
from . import futures as ft
|
|
4
|
+
from . import indicators as it
|
|
5
|
+
from . import projections as pr
|
|
6
|
+
from . import treasuries as tr
|
|
7
|
+
from .utils import _normalize_date
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def fetch_asset(
|
|
11
|
+
asset_code: str,
|
|
12
|
+
reference_date: str | pd.Timestamp | None = None,
|
|
13
|
+
**kwargs,
|
|
14
|
+
) -> pd.DataFrame:
|
|
15
|
+
"""
|
|
16
|
+
Fetches data for a specified asset based on type and reference date.
|
|
17
|
+
|
|
18
|
+
Args:
|
|
19
|
+
asset_code (str): The asset code identifying the type of financial asset.
|
|
20
|
+
Supported options:
|
|
21
|
+
- "TRB": Treasury bonds (indicative rates from ANBIMA).
|
|
22
|
+
- "LTN", "LFT", "NTN-F", "NTN-B": Specific types of Brazilian treasury bonds
|
|
23
|
+
(indicative rates from ANBIMA).
|
|
24
|
+
- "DI1": One-day Interbank Deposit Futures (Futuro de DI) from B3.
|
|
25
|
+
- "DDI": DI x U.S. Dollar Spread Futures (Futuro de Cupom Cambial) from B3.
|
|
26
|
+
- "FRC": Forward Rate Agreement (FRA) from B3.
|
|
27
|
+
reference_date (str | pd.Timestamp | None): The reference date for which data is
|
|
28
|
+
fetched. Defaults to the last business day if None.
|
|
29
|
+
**kwargs: Additional keyword arguments, specifically:
|
|
30
|
+
- return_raw (bool): Whether to return raw data without processing. Defaults
|
|
31
|
+
to False.
|
|
32
|
+
|
|
33
|
+
Returns:
|
|
34
|
+
pd.DataFrame: A DataFrame containing the fetched data for the specified asset.
|
|
35
|
+
|
|
36
|
+
Raises:
|
|
37
|
+
ValueError: If the asset code is not recognized or supported.
|
|
38
|
+
|
|
39
|
+
Examples:
|
|
40
|
+
>>> fetch_asset('TRB', '2023-04-01')
|
|
41
|
+
>>> fetch_asset('DI1', '2023-04-01', return_raw=True)
|
|
42
|
+
"""
|
|
43
|
+
return_raw = kwargs.get("return_raw", False)
|
|
44
|
+
normalized_date = _normalize_date(reference_date)
|
|
45
|
+
|
|
46
|
+
if asset_code.lower() == "trb":
|
|
47
|
+
return tr.fetch_bonds(reference_date=normalized_date, return_raw=return_raw)
|
|
48
|
+
elif asset_code.lower() in ["ltn", "lft", "ntn-f", "ntn-b"]:
|
|
49
|
+
df = tr.fetch_bonds(reference_date=normalized_date)
|
|
50
|
+
return df.query(f"BondType == '{asset_code.upper()}'")
|
|
51
|
+
|
|
52
|
+
elif asset_code.lower() == "di1":
|
|
53
|
+
today = pd.Timestamp.today().normalize()
|
|
54
|
+
if normalized_date == today:
|
|
55
|
+
return ft.fetch_last_di()
|
|
56
|
+
else:
|
|
57
|
+
return ft.fetch_past_di(trade_date=normalized_date, return_raw=return_raw)
|
|
58
|
+
elif asset_code.lower() == "ddi":
|
|
59
|
+
return ft.fetch_past_ddi(trade_date=normalized_date, return_raw=return_raw)
|
|
60
|
+
elif asset_code.lower() == "frc":
|
|
61
|
+
return ft.fetch_past_frc(trade_date=normalized_date, return_raw=return_raw)
|
|
62
|
+
else:
|
|
63
|
+
raise ValueError("Asset type not supported.")
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def fetch_indicator(
|
|
67
|
+
indicator_code: str,
|
|
68
|
+
reference_date: str | pd.Timestamp | None = None,
|
|
69
|
+
) -> float | None:
|
|
70
|
+
"""
|
|
71
|
+
Fetches data for a specified economic indicator and reference date.
|
|
72
|
+
|
|
73
|
+
Args:
|
|
74
|
+
indicator_code (str): The code for the economic indicator. Supported options:
|
|
75
|
+
- "SELIC": SELIC target rate from the Central Bank of Brazil, expressed as
|
|
76
|
+
an annual rate (p.a.).
|
|
77
|
+
- "DI": Interbank Deposit rate (DI) from B3 expressed as a daily rate,
|
|
78
|
+
calculated on a daily basis and expressed per diem (p.d.).
|
|
79
|
+
- "IPCA": IPCA monthly inflation rate from IBGE, expressed per month (p.m.).
|
|
80
|
+
- "VNA_LFT": VNA (Valor Nominal Atualizado) of LFT (Letra Financeira do
|
|
81
|
+
Tesouro), which reflects updated nominal values for these bonds.
|
|
82
|
+
- reference_date (str | pd.Timestamp | None): The reference date for which data
|
|
83
|
+
is fetched. Defaults to the last business day if None.
|
|
84
|
+
|
|
85
|
+
Returns:
|
|
86
|
+
float | None: The value of the specified economic indicator for the reference
|
|
87
|
+
date. Returns None if data is not found.
|
|
88
|
+
|
|
89
|
+
Raises:
|
|
90
|
+
ValueError: If the indicator code is not recognized or supported.
|
|
91
|
+
|
|
92
|
+
Examples:
|
|
93
|
+
>>> fetch_indicator('SELIC', '2023-04-01')
|
|
94
|
+
0.1075 # Indicates a SELIC target rate of 10.75% p.a.
|
|
95
|
+
>>> fetch_indicator('IPCA', '2023-03-10')
|
|
96
|
+
0.0016 # Indicates an IPCA monthly rate of 0.16% p.m.
|
|
97
|
+
>>> fetch_indicator('DI', '2023-04-17')
|
|
98
|
+
0.00040168 # Indicates a DI daily rate of 0.02% p.d.
|
|
99
|
+
"""
|
|
100
|
+
normalized_date = _normalize_date(reference_date)
|
|
101
|
+
|
|
102
|
+
if indicator_code.lower() == "selic":
|
|
103
|
+
return it.fetch_selic_target(reference_date=normalized_date)
|
|
104
|
+
elif indicator_code.lower() == "ipca":
|
|
105
|
+
return it.fetch_ipca_mr(reference_date=normalized_date)
|
|
106
|
+
elif indicator_code.lower() == "di":
|
|
107
|
+
return it.fetch_di(reference_date=normalized_date)
|
|
108
|
+
elif indicator_code.lower() == "vna_lft":
|
|
109
|
+
return it.fetch_vna_selic(reference_date=normalized_date)
|
|
110
|
+
else:
|
|
111
|
+
raise ValueError("Indicator type not supported.")
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
def fetch_projection(projection_code: str) -> dict:
|
|
115
|
+
"""
|
|
116
|
+
Fetches a financial projection for a specified code and reference date.
|
|
117
|
+
|
|
118
|
+
Args:
|
|
119
|
+
projection_code (str): The code for the financial projection. Supported options:
|
|
120
|
+
- "IPCA_CM": IPCA projection for the current month from ANBIMA.
|
|
121
|
+
|
|
122
|
+
Returns:
|
|
123
|
+
IndicatorProjection: An instance of IndicatorProjection containing:
|
|
124
|
+
- last_updated (pd.Timestamp): The datetime when the projection was last
|
|
125
|
+
updated.
|
|
126
|
+
- reference_month_ts (pd.Timestamp): The month to which the projection
|
|
127
|
+
applies.
|
|
128
|
+
- reference_month_br (str): The formatted month as a string formatted using
|
|
129
|
+
the pt_BR locale.
|
|
130
|
+
- projected_value (float): The projected indicator value.
|
|
131
|
+
|
|
132
|
+
Raises:
|
|
133
|
+
ValueError: If the projection code is not recognized or supported.
|
|
134
|
+
|
|
135
|
+
Examples:
|
|
136
|
+
>>> fetch_projection('IPCA_CM')
|
|
137
|
+
IndicatorProjection(
|
|
138
|
+
last_updated=pd.Timestamp('2024-04-19 18:55:00'),
|
|
139
|
+
reference_month_ts=pd.Timestamp('2024-04-01 00:00:00'),
|
|
140
|
+
reference_month_br='ABR/2024',
|
|
141
|
+
projected_value=0.35
|
|
142
|
+
)
|
|
143
|
+
"""
|
|
144
|
+
|
|
145
|
+
if projection_code.lower() == "ipca_cm":
|
|
146
|
+
return pr.fetch_current_month_ipca_projection()
|
|
147
|
+
else:
|
|
148
|
+
raise ValueError("Projection type not supported.")
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
from .common import get_expiration_date, get_old_expiration_date
|
|
2
|
+
from .ddi import fetch_past_ddi
|
|
3
|
+
from .di import fetch_last_di, fetch_past_di
|
|
4
|
+
from .frc import fetch_past_frc
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"fetch_past_di",
|
|
8
|
+
"fetch_last_di",
|
|
9
|
+
"fetch_past_ddi",
|
|
10
|
+
"fetch_past_frc",
|
|
11
|
+
"get_expiration_date",
|
|
12
|
+
"get_old_expiration_date",
|
|
13
|
+
]
|
|
@@ -131,7 +131,7 @@ def get_old_expiration_date(
|
|
|
131
131
|
return pd.NaT # type: ignore
|
|
132
132
|
|
|
133
133
|
|
|
134
|
-
def
|
|
134
|
+
def fetch_past_raw_df(asset_code: str, trade_date: pd.Timestamp) -> pd.DataFrame:
|
|
135
135
|
"""
|
|
136
136
|
Fetch the historical futures data from B3 for a specific trade date.
|
|
137
137
|
|
|
@@ -170,12 +170,13 @@ def fetch_past_data(asset_code: str, trade_date: pd.Timestamp) -> pd.DataFrame:
|
|
|
170
170
|
df["VAR. PTOS."] = df["VAR. PTOS."].astype(pd.StringDtype())
|
|
171
171
|
|
|
172
172
|
# Force "AJUSTE CORRIG. (4)" to be float, since it can be also read as int
|
|
173
|
-
|
|
173
|
+
if "AJUSTE CORRIG. (4)" in df.columns:
|
|
174
|
+
df["AJUSTE CORRIG. (4)"] = df["AJUSTE CORRIG. (4)"].astype(pd.Float64Dtype())
|
|
174
175
|
|
|
175
176
|
return df
|
|
176
177
|
|
|
177
178
|
|
|
178
|
-
def
|
|
179
|
+
def fetch_last_raw_df(future_code: str) -> pd.DataFrame:
|
|
179
180
|
"""
|
|
180
181
|
Fetch the latest data for a given future code from B3 derivatives quotation API.
|
|
181
182
|
|
|
@@ -149,7 +149,7 @@ def _process_raw_df(df: pd.DataFrame, trade_date: pd.Timestamp) -> pd.DataFrame:
|
|
|
149
149
|
return df[ordered_cols]
|
|
150
150
|
|
|
151
151
|
|
|
152
|
-
def
|
|
152
|
+
def fetch_past_ddi(trade_date: pd.Timestamp, return_raw: bool = False) -> pd.DataFrame:
|
|
153
153
|
"""
|
|
154
154
|
Fetchs the DDI futures data for a given date from B3.
|
|
155
155
|
|
|
@@ -172,7 +172,7 @@ def fetch_ddi(trade_date: pd.Timestamp, return_raw: bool = False) -> pd.DataFram
|
|
|
172
172
|
- DaysToExpiration: number of business days to ExpirationDate.
|
|
173
173
|
- OpenContracts: number of open contracts at the start of the trading day.
|
|
174
174
|
"""
|
|
175
|
-
df_raw = common.
|
|
175
|
+
df_raw = common.fetch_past_raw_df(asset_code="DDI", trade_date=trade_date)
|
|
176
176
|
if return_raw:
|
|
177
177
|
return df_raw
|
|
178
178
|
return _process_raw_df(df_raw, trade_date)
|
|
@@ -41,7 +41,7 @@ def _convert_prices_in_older_contracts(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
41
41
|
return df
|
|
42
42
|
|
|
43
43
|
|
|
44
|
-
def
|
|
44
|
+
def _process_past_raw_df(df: pd.DataFrame, trade_date: pd.Timestamp) -> pd.DataFrame:
|
|
45
45
|
"""
|
|
46
46
|
Internal function to process and transform raw DI futures data.
|
|
47
47
|
|
|
@@ -151,7 +151,7 @@ def _process_past_data(df: pd.DataFrame, trade_date: pd.Timestamp) -> pd.DataFra
|
|
|
151
151
|
return df[ordered_cols]
|
|
152
152
|
|
|
153
153
|
|
|
154
|
-
def
|
|
154
|
+
def _process_last_raw_di_df(raw_df: pd.DataFrame) -> pd.DataFrame:
|
|
155
155
|
df = raw_df.copy()
|
|
156
156
|
|
|
157
157
|
# Columns to be renamed
|
|
@@ -188,20 +188,18 @@ def _process_last_di_data(raw_df: pd.DataFrame) -> pd.DataFrame:
|
|
|
188
188
|
return df[rename_columns.values()]
|
|
189
189
|
|
|
190
190
|
|
|
191
|
-
def
|
|
191
|
+
def fetch_last_di() -> pd.DataFrame:
|
|
192
192
|
"""
|
|
193
193
|
Fetch the latest DI futures data from B3.
|
|
194
194
|
|
|
195
195
|
Returns:
|
|
196
196
|
pd.DataFrame: A Pandas pd.DataFrame containing the latest DI futures data.
|
|
197
197
|
"""
|
|
198
|
-
raw_df = common.
|
|
199
|
-
return
|
|
198
|
+
raw_df = common.fetch_last_raw_df(future_code="DI1")
|
|
199
|
+
return _process_last_raw_di_df(raw_df)
|
|
200
200
|
|
|
201
201
|
|
|
202
|
-
def
|
|
203
|
-
trade_date: pd.Timestamp, return_raw: bool = False
|
|
204
|
-
) -> pd.DataFrame:
|
|
202
|
+
def fetch_past_di(trade_date: pd.Timestamp, return_raw: bool = False) -> pd.DataFrame:
|
|
205
203
|
"""
|
|
206
204
|
Fetchs the DI futures data for a given date from B3.
|
|
207
205
|
|
|
@@ -216,15 +214,11 @@ def fetch_past_di_data(
|
|
|
216
214
|
Returns:
|
|
217
215
|
pd.DataFrame: A Pandas pd.DataFrame containing processed DI futures data.
|
|
218
216
|
|
|
219
|
-
Examples:
|
|
220
|
-
>>> from pyield.futures import di
|
|
221
|
-
>>> di.fetch_di(pd.Timestamp("2021-01-04"))
|
|
222
|
-
|
|
223
217
|
Notes:
|
|
224
218
|
- BDaysToExp: number of business days to ExpirationDate.
|
|
225
219
|
- OpenContracts: number of open contracts at the start of the trading day.
|
|
226
220
|
"""
|
|
227
|
-
df_raw = common.
|
|
221
|
+
df_raw = common.fetch_past_raw_df(asset_code="DI1", trade_date=trade_date)
|
|
228
222
|
if return_raw:
|
|
229
223
|
return df_raw
|
|
230
|
-
return
|
|
224
|
+
return _process_past_raw_df(df_raw, trade_date)
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
|
|
3
|
+
from . import common
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def _process_past_data(df: pd.DataFrame, trade_date: pd.Timestamp) -> pd.DataFrame:
|
|
7
|
+
"""
|
|
8
|
+
Internal function to process and transform raw DI futures data.
|
|
9
|
+
|
|
10
|
+
Args:
|
|
11
|
+
df (pd.DataFrame): the raw DI DataFrame.
|
|
12
|
+
trade_date: a datetime-like object representing the trade date.
|
|
13
|
+
|
|
14
|
+
Returns:
|
|
15
|
+
pd.DataFrame: Processed and transformed data as a Pandas pd.DataFrame.
|
|
16
|
+
"""
|
|
17
|
+
# Check if the pd.DataFrame is empty
|
|
18
|
+
if df.empty:
|
|
19
|
+
return df
|
|
20
|
+
|
|
21
|
+
rename_dict = {
|
|
22
|
+
"VENCTO": "ExpirationCode",
|
|
23
|
+
"NÚM. NEGOC.": "TradeCount",
|
|
24
|
+
"CONTR. NEGOC.": "TradeVolume",
|
|
25
|
+
"VOL.": "FinancialVolume",
|
|
26
|
+
"AJUSTE DE REF.": "SettlementRate",
|
|
27
|
+
"PREÇO ABERTU.": "FirstRate",
|
|
28
|
+
"PREÇO MÍN.": "MinRate",
|
|
29
|
+
"PREÇO MÉD.": "AvgRate",
|
|
30
|
+
"PREÇO MÁX.": "MaxRate",
|
|
31
|
+
"ÚLT. PREÇO": "CloseRate",
|
|
32
|
+
"VAR. PTOS.": "PointsVariation",
|
|
33
|
+
# Attention: bid/ask rates are inverted
|
|
34
|
+
"ÚLT.OF. COMPRA": "CloseAskRate",
|
|
35
|
+
"ÚLT.OF. VENDA": "CloseBidRate",
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
df = df.rename(columns=rename_dict)
|
|
39
|
+
|
|
40
|
+
df["TradeDate"] = trade_date
|
|
41
|
+
# Convert to datetime64[ns] since it is pandas default type for timestamps
|
|
42
|
+
df["TradeDate"] = df["TradeDate"].astype("datetime64[ns]")
|
|
43
|
+
|
|
44
|
+
df["ExpirationDate"] = df["ExpirationCode"].apply(common.get_expiration_date)
|
|
45
|
+
|
|
46
|
+
# Replace 0 values in rate columns with pd.NA and remove percentage
|
|
47
|
+
rate_cols = [col for col in df.columns if "Rate" in col]
|
|
48
|
+
for col in rate_cols:
|
|
49
|
+
df[col] = df[col].replace(0, pd.NA)
|
|
50
|
+
# Round to 5 decimal places (3 in %) since it is the contract's precision
|
|
51
|
+
df[col] = (df[col] / 100).round(5)
|
|
52
|
+
|
|
53
|
+
df["TickerSymbol"] = "FRC" + df["ExpirationCode"]
|
|
54
|
+
|
|
55
|
+
# Filter and order columns
|
|
56
|
+
ordered_cols = [
|
|
57
|
+
"TradeDate",
|
|
58
|
+
"TickerSymbol",
|
|
59
|
+
# "ExpirationCode",
|
|
60
|
+
"ExpirationDate",
|
|
61
|
+
"TradeCount",
|
|
62
|
+
"TradeVolume",
|
|
63
|
+
"FinancialVolume",
|
|
64
|
+
"SettlementRate",
|
|
65
|
+
"FirstRate",
|
|
66
|
+
"MinRate",
|
|
67
|
+
"AvgRate",
|
|
68
|
+
"MaxRate",
|
|
69
|
+
"CloseAskRate",
|
|
70
|
+
"CloseBidRate",
|
|
71
|
+
"CloseRate",
|
|
72
|
+
]
|
|
73
|
+
return df[ordered_cols]
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def fetch_past_frc(trade_date: pd.Timestamp, return_raw: bool = False) -> pd.DataFrame:
|
|
77
|
+
"""
|
|
78
|
+
Fetchs the DI futures data for a given date from B3.
|
|
79
|
+
|
|
80
|
+
This function fetches and processes the DI futures data from B3 for a specific
|
|
81
|
+
trade date. It's the primary external interface for accessing DI data.
|
|
82
|
+
|
|
83
|
+
Args:
|
|
84
|
+
trade_date (pd.Timestamp): The trade date to fetch the DI futures data.
|
|
85
|
+
raw (bool): If True, returns the raw data as a Pandas pd.DataFrame.
|
|
86
|
+
Defaults to False.
|
|
87
|
+
|
|
88
|
+
Returns:
|
|
89
|
+
pd.DataFrame: A Pandas pd.DataFrame containing processed DI futures data.
|
|
90
|
+
"""
|
|
91
|
+
df_raw = common.fetch_past_raw_df(asset_code="FRC", trade_date=trade_date)
|
|
92
|
+
if return_raw:
|
|
93
|
+
return df_raw
|
|
94
|
+
return _process_past_data(df_raw, trade_date)
|
|
@@ -41,7 +41,7 @@ def fetch_ipca_mr(reference_date: pd.Timestamp) -> float | None:
|
|
|
41
41
|
# Extract and return the IPCA monthly growth rate if data is available
|
|
42
42
|
if data:
|
|
43
43
|
ipca_str = data[0]["resultados"][0]["series"][0]["serie"][ipca_date]
|
|
44
|
-
return float(ipca_str)
|
|
44
|
+
return round(float(ipca_str) / 100, 4)
|
|
45
45
|
else:
|
|
46
46
|
return None
|
|
47
47
|
|
|
@@ -52,8 +52,62 @@ def fetch_selic_target(reference_date: pd.Timestamp) -> float | None:
|
|
|
52
52
|
api_url = f"https://api.bcb.gov.br/dados/serie/bcdata.sgs.432/dados?formato=json&dataInicial={selic_date}&dataFinal={selic_date}"
|
|
53
53
|
response = requests.get(api_url)
|
|
54
54
|
response.raise_for_status()
|
|
55
|
-
|
|
56
|
-
if
|
|
57
|
-
|
|
55
|
+
|
|
56
|
+
if selic_date in response.text:
|
|
57
|
+
data = response.json()
|
|
58
|
+
return round(float(data[0]["valor"]) / 100, 4)
|
|
58
59
|
else:
|
|
59
60
|
return None
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def fetch_di(reference_date: pd.Timestamp) -> float | None:
|
|
64
|
+
# https://api.bcb.gov.br/dados/serie/bcdata.sgs.11/dados?formato=json&dataInicial=12/04/2024&dataFinal=12/04/2024
|
|
65
|
+
di_date = reference_date.strftime("%d/%m/%Y")
|
|
66
|
+
api_url = f"https://api.bcb.gov.br/dados/serie/bcdata.sgs.11/dados?formato=json&dataInicial={di_date}&dataFinal={di_date}"
|
|
67
|
+
response = requests.get(api_url)
|
|
68
|
+
response.raise_for_status()
|
|
69
|
+
|
|
70
|
+
if di_date in response.text:
|
|
71
|
+
data = response.json()
|
|
72
|
+
return round(float(data[0]["valor"]) / 100, 8)
|
|
73
|
+
else:
|
|
74
|
+
return None
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def fetch_vna_selic(reference_date: pd.Timestamp) -> float | None:
|
|
78
|
+
# url example: https://www3.bcb.gov.br/novoselic/rest/arquivosDiarios/pub/download/3/20240418APC238
|
|
79
|
+
|
|
80
|
+
url_base = "https://www3.bcb.gov.br/novoselic/rest/arquivosDiarios/pub/download/3/"
|
|
81
|
+
url_file = f"{reference_date.strftime("%Y%m%d")}APC238"
|
|
82
|
+
url_vna = url_base + url_file
|
|
83
|
+
response = requests.get(url_vna)
|
|
84
|
+
response.raise_for_status()
|
|
85
|
+
file_text = response.text
|
|
86
|
+
|
|
87
|
+
# Finding the part that contains the table
|
|
88
|
+
start_of_table = file_text.find("EMISSAO")
|
|
89
|
+
end_of_table = file_text.find("99999999*")
|
|
90
|
+
|
|
91
|
+
# Extracting the table
|
|
92
|
+
table_text = file_text[start_of_table:end_of_table].strip()
|
|
93
|
+
table_lines = table_text.splitlines()
|
|
94
|
+
|
|
95
|
+
# Remove empty lines
|
|
96
|
+
table_lines = [line.strip() for line in table_lines if line.strip()]
|
|
97
|
+
|
|
98
|
+
# Remove first line
|
|
99
|
+
body_lines = table_lines[1:]
|
|
100
|
+
|
|
101
|
+
vnas = []
|
|
102
|
+
for line in body_lines:
|
|
103
|
+
vna_str = line.split()[-1].replace(",", ".")
|
|
104
|
+
vna_float = float(vna_str)
|
|
105
|
+
vnas.append(vna_float)
|
|
106
|
+
|
|
107
|
+
# Raise error if all values are not the same
|
|
108
|
+
vna_value = vnas[0]
|
|
109
|
+
if any(vna_value != vna for vna in vnas):
|
|
110
|
+
bcb_url = "https://www.bcb.gov.br/estabilidadefinanceira/selicbaixar"
|
|
111
|
+
raise ValueError(f"VNA values are not the same. Please check data at {bcb_url}")
|
|
112
|
+
|
|
113
|
+
return vna_value
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import io
|
|
2
|
+
import locale
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
|
|
5
|
+
import pandas as pd
|
|
6
|
+
import python_calamine as pc
|
|
7
|
+
import requests
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass
|
|
11
|
+
class IndicatorProjection:
|
|
12
|
+
last_updated: pd.Timestamp # Date and time of the last update
|
|
13
|
+
reference_month_ts: pd.Timestamp # Timestamp to which the projection applies
|
|
14
|
+
reference_month_br: str # Brazilian formatted month (e.g., "ABR/2024")
|
|
15
|
+
projected_value: float # Projected value
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def fetch_current_month_ipca_projection() -> IndicatorProjection:
|
|
19
|
+
"""
|
|
20
|
+
Fetches the current month's IPCA projection from the ANBIMA website and returns it
|
|
21
|
+
as an IndicatorProjection instance.
|
|
22
|
+
|
|
23
|
+
This function retrieves and parses the Excel file that contains economic indicators,
|
|
24
|
+
specifically looking for the IPCA projection. It extracts the date of the last
|
|
25
|
+
update and the IPCA projection for the reference month.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
None
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
IndicatorProjection: An instance of IndicatorProjection containing:
|
|
32
|
+
- last_updated (pd.Timestamp): The datetime when the data was last updated.
|
|
33
|
+
- reference_month_ts (pd.Timestamp): The month to which the IPCA projection
|
|
34
|
+
applies.
|
|
35
|
+
- reference_month_br (str): The formatted month as a string
|
|
36
|
+
(e.g., "ABR/2024") using the pt_BR locale.
|
|
37
|
+
- projected_value (float): The projected IPCA value.
|
|
38
|
+
|
|
39
|
+
Example:
|
|
40
|
+
>>> projection = fetch_current_month_ipca_projection()
|
|
41
|
+
>>> print(projection)
|
|
42
|
+
IndicatorProjection(
|
|
43
|
+
last_updated=pd.Timestamp('2024-04-19 18:55:00'),
|
|
44
|
+
reference_month_ts=pd.Timestamp('2024-04-01 00:00:00'),
|
|
45
|
+
reference_month_br='ABR/2024',
|
|
46
|
+
projected_value=0.35
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
Data file format example after parsing:
|
|
50
|
+
- ['Data e Hora da Última Atualização: 19/04/2024 - 18:55 h', '', '']
|
|
51
|
+
- ...
|
|
52
|
+
- ['IPCA1', 'Projeção (abr/24)', 0.35]
|
|
53
|
+
- ...
|
|
54
|
+
"""
|
|
55
|
+
# Define the URL and get the data
|
|
56
|
+
url = "https://www.anbima.com.br/informacoes/indicadores/arqs/indicadores.xls"
|
|
57
|
+
response = requests.get(url)
|
|
58
|
+
excel_data = io.BytesIO(response.content)
|
|
59
|
+
|
|
60
|
+
# Load the workbook and select the first sheet
|
|
61
|
+
workbook = pc.load_workbook(excel_data)
|
|
62
|
+
first_sheet = workbook.sheet_names[0]
|
|
63
|
+
data = workbook.get_sheet_by_name(first_sheet).to_python(skip_empty_area=True)
|
|
64
|
+
|
|
65
|
+
# Extract projection update date and time from the first row
|
|
66
|
+
last_update_str = data[0][0].split("Atualização:")[-1].strip()
|
|
67
|
+
last_updated = pd.to_datetime(last_update_str, format="%d/%m/%Y - %H:%M h")
|
|
68
|
+
|
|
69
|
+
# Find the row containing the IPCA projection and extract its data
|
|
70
|
+
ipca_data = next(line for line in data if "IPCA1" in line)
|
|
71
|
+
|
|
72
|
+
# Convert the last element of the IPCA data row to float for the projection value
|
|
73
|
+
ipca_value = round(float(ipca_data[-1]) / 100, 4)
|
|
74
|
+
|
|
75
|
+
# Extract and format the reference month
|
|
76
|
+
month_text = ipca_data[1].split("(")[-1].split(")")[0]
|
|
77
|
+
locale.setlocale(locale.LC_TIME, "pt_BR.UTF-8")
|
|
78
|
+
ipca_month_ts = pd.to_datetime(month_text, format="%b/%y")
|
|
79
|
+
ipca_month_br = ipca_month_ts.strftime("%b/%Y").upper()
|
|
80
|
+
locale.setlocale(locale.LC_TIME, "") # Reset locale to default
|
|
81
|
+
|
|
82
|
+
return IndicatorProjection(
|
|
83
|
+
last_updated=last_updated,
|
|
84
|
+
reference_month_ts=ipca_month_ts,
|
|
85
|
+
reference_month_br=ipca_month_br,
|
|
86
|
+
projected_value=ipca_value,
|
|
87
|
+
)
|
|
@@ -150,7 +150,7 @@ def calculate_di_spreads(reference_date: pd.Timestamp) -> pd.DataFrame:
|
|
|
150
150
|
bond type and maturity date.
|
|
151
151
|
"""
|
|
152
152
|
# Fetch DI rates and adjust the maturity date format for compatibility
|
|
153
|
-
df_di = ft.
|
|
153
|
+
df_di = ft.fetch_past_di(reference_date)[["ExpirationDate", "SettlementRate"]]
|
|
154
154
|
|
|
155
155
|
# Renaming the columns to match the ANBIMA structure
|
|
156
156
|
df_di.rename(columns={"ExpirationDate": "MaturityDate"}, inplace=True)
|
|
@@ -3,7 +3,7 @@ import pandas as pd
|
|
|
3
3
|
from . import bday
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
def _normalize_date(
|
|
6
|
+
def _normalize_date(input_date: str | pd.Timestamp | None = None) -> pd.Timestamp:
|
|
7
7
|
"""
|
|
8
8
|
Normalizes the given date to ensure it is a past business day at midnight. If no
|
|
9
9
|
date is provided, it defaults to the last business day.
|
|
@@ -31,25 +31,25 @@ def _normalize_date(reference_date: str | pd.Timestamp | None = None) -> pd.Time
|
|
|
31
31
|
>>> _normalize_date(pd.Timestamp('2023-04-01 15:30'))
|
|
32
32
|
>>> _normalize_date()
|
|
33
33
|
"""
|
|
34
|
-
if isinstance(
|
|
34
|
+
if isinstance(input_date, str):
|
|
35
35
|
# Convert string date to Timestamp and normalize to midnight
|
|
36
|
-
normalized_date = pd.Timestamp(
|
|
37
|
-
elif isinstance(
|
|
36
|
+
normalized_date = pd.Timestamp(input_date).normalize()
|
|
37
|
+
elif isinstance(input_date, pd.Timestamp):
|
|
38
38
|
# Normalize Timestamp to midnight
|
|
39
|
-
normalized_date =
|
|
40
|
-
elif
|
|
39
|
+
normalized_date = input_date.normalize()
|
|
40
|
+
elif input_date is None:
|
|
41
41
|
# If no date is provided, use the last available business day
|
|
42
42
|
today = pd.Timestamp.today().normalize()
|
|
43
43
|
normalized_date = bday.offset_bdays(dates=today, offset=0, roll="backward")
|
|
44
44
|
else:
|
|
45
|
-
raise ValueError("
|
|
45
|
+
raise ValueError(f"Date format not recognized: {input_date}")
|
|
46
46
|
|
|
47
|
+
error_date = normalized_date.strftime("%d-%m-%Y")
|
|
47
48
|
# Validate that the date is not in the future
|
|
48
49
|
if normalized_date > pd.Timestamp.today().normalize():
|
|
49
|
-
raise ValueError("
|
|
50
|
-
|
|
50
|
+
raise ValueError(f"Date {error_date} is in the future")
|
|
51
51
|
# Validate that the date is a business day
|
|
52
52
|
if not bday.is_bday(normalized_date):
|
|
53
|
-
raise ValueError("
|
|
53
|
+
raise ValueError(f"Date {error_date} is not a business day")
|
|
54
54
|
|
|
55
55
|
return normalized_date
|
|
@@ -43,7 +43,7 @@ def test_settlement_rate_with_old_holiday_list():
|
|
|
43
43
|
|
|
44
44
|
# 22-12-2023 is before the new holiday calendar
|
|
45
45
|
test_date = pd.Timestamp("2023-12-22")
|
|
46
|
-
df = ft.
|
|
46
|
+
df = ft.fetch_past_di(trade_date=test_date)
|
|
47
47
|
tickers = list(settlement_rates.keys()) # noqa: F841
|
|
48
48
|
result = df.query("TickerSymbol in @tickers")["SettlementRate"].to_list()
|
|
49
49
|
assert result == list(settlement_rates.values())
|
|
@@ -68,7 +68,7 @@ def test_settlement_rates_with_current_holiday_list():
|
|
|
68
68
|
"DI1F33": 0.10331,
|
|
69
69
|
}
|
|
70
70
|
test_date = pd.Timestamp("2023-12-26")
|
|
71
|
-
df = ft.
|
|
71
|
+
df = ft.fetch_past_di(trade_date=test_date)
|
|
72
72
|
tickers = list(settlement_rates.keys()) # noqa: F841
|
|
73
73
|
results = df.query("TickerSymbol in @tickers")["SettlementRate"].to_list()
|
|
74
74
|
assert results == list(settlement_rates.values())
|
|
@@ -77,4 +77,4 @@ def test_settlement_rates_with_current_holiday_list():
|
|
|
77
77
|
def test_non_business_day():
|
|
78
78
|
non_business_day = pd.Timestamp("2023-12-24")
|
|
79
79
|
with pytest.raises(ValueError):
|
|
80
|
-
ft.
|
|
80
|
+
ft.fetch_past_di(trade_date=non_business_day)
|
pyield-0.7.3/pyield/__about__.py
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.7.3"
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import pandas as pd
|
|
2
|
-
|
|
3
|
-
from . import futures as ft
|
|
4
|
-
from . import indicators as it
|
|
5
|
-
from . import treasuries as tr
|
|
6
|
-
from .utils import _normalize_date
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
def fetch_asset(
|
|
10
|
-
asset_code: str,
|
|
11
|
-
reference_date: str | pd.Timestamp | None = None,
|
|
12
|
-
**kwargs,
|
|
13
|
-
) -> pd.DataFrame:
|
|
14
|
-
"""
|
|
15
|
-
Fetches data for a specified asset based on type and reference date.
|
|
16
|
-
|
|
17
|
-
Args:
|
|
18
|
-
asset_code (str): The asset code identifying the type of financial asset.
|
|
19
|
-
Supported options:
|
|
20
|
-
- "TRB": Treasury bonds (indicative rates from ANBIMA).
|
|
21
|
-
- "LTN", "LFT", "NTN-F", "NTN-B": Specific types of Brazilian treasury bonds
|
|
22
|
-
(indicative rates from ANBIMA).
|
|
23
|
-
- "DI1": One-day Interbank Deposit Futures (Futuro de DI) from B3.
|
|
24
|
-
- "DDI": DI x U.S. Dollar Spread Futures (Futuro de Cupom Cambial) from B3.
|
|
25
|
-
reference_date (str | pd.Timestamp | None): The reference date for which data is
|
|
26
|
-
fetched. Defaults to the last business day if None.
|
|
27
|
-
**kwargs: Additional keyword arguments, specifically:
|
|
28
|
-
- return_raw (bool): Whether to return raw data without processing. Defaults
|
|
29
|
-
to False.
|
|
30
|
-
|
|
31
|
-
Returns:
|
|
32
|
-
pd.DataFrame: A DataFrame containing the fetched data for the specified asset.
|
|
33
|
-
|
|
34
|
-
Raises:
|
|
35
|
-
ValueError: If the asset code is not recognized or supported.
|
|
36
|
-
|
|
37
|
-
Examples:
|
|
38
|
-
>>> fetch_asset('TRB', '2023-04-01')
|
|
39
|
-
>>> fetch_asset('DI1', '2023-04-01', return_raw=True)
|
|
40
|
-
"""
|
|
41
|
-
return_raw = kwargs.get("return_raw", False)
|
|
42
|
-
normalized_date = _normalize_date(reference_date)
|
|
43
|
-
|
|
44
|
-
if asset_code.lower() == "trb":
|
|
45
|
-
return tr.fetch_bonds(reference_date=normalized_date, return_raw=return_raw)
|
|
46
|
-
elif asset_code.lower() in ["ltn", "lft", "ntn-f", "ntn-b"]:
|
|
47
|
-
df = tr.fetch_bonds(reference_date=normalized_date)
|
|
48
|
-
return df.query(f"BondType == '{asset_code.upper()}'")
|
|
49
|
-
|
|
50
|
-
elif asset_code.lower() == "di1":
|
|
51
|
-
today = pd.Timestamp.today().normalize()
|
|
52
|
-
if normalized_date == today:
|
|
53
|
-
return ft.fetch_last_di_data()
|
|
54
|
-
else:
|
|
55
|
-
return ft.fetch_past_di_data(
|
|
56
|
-
trade_date=normalized_date, return_raw=return_raw
|
|
57
|
-
)
|
|
58
|
-
elif asset_code.lower() == "ddi":
|
|
59
|
-
return ft.fetch_ddi(trade_date=normalized_date, return_raw=return_raw)
|
|
60
|
-
else:
|
|
61
|
-
raise ValueError("Asset type not supported.")
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
def fetch_indicator(
|
|
65
|
-
indicator_code: str,
|
|
66
|
-
reference_date: str | pd.Timestamp | None = None,
|
|
67
|
-
) -> float | None:
|
|
68
|
-
"""
|
|
69
|
-
Fetches data for a specified economic indicator and reference date.
|
|
70
|
-
|
|
71
|
-
Args:
|
|
72
|
-
indicator_code (str): The code for the economic indicator. Supported options:
|
|
73
|
-
- "SELIC": SELIC target rate from the Central Bank of Brazil.
|
|
74
|
-
- "IPCA": IPCA monthly inflation rate from IBGE.
|
|
75
|
-
reference_date (str | pd.Timestamp | None): The reference date for which data is
|
|
76
|
-
fetched. Defaults to the last business day if None.
|
|
77
|
-
|
|
78
|
-
Returns:
|
|
79
|
-
pd.Series: A Series containing the fetched data for the specified indicator.
|
|
80
|
-
|
|
81
|
-
Raises:
|
|
82
|
-
ValueError: If the indicator code is not recognized or supported.
|
|
83
|
-
|
|
84
|
-
Examples:
|
|
85
|
-
>>> fetch_indicator('SELIC', '2023-04-01')
|
|
86
|
-
>>> fetch_indicator('IPCA', '2023-04-01')
|
|
87
|
-
"""
|
|
88
|
-
normalized_date = _normalize_date(reference_date)
|
|
89
|
-
|
|
90
|
-
if indicator_code.lower() == "selic":
|
|
91
|
-
return it.fetch_selic_target(reference_date=normalized_date)
|
|
92
|
-
elif indicator_code.lower() == "ipca":
|
|
93
|
-
return it.fetch_ipca_mr(reference_date=normalized_date)
|
|
94
|
-
else:
|
|
95
|
-
raise ValueError("Indicator type not supported.")
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
from .common import get_expiration_date, get_old_expiration_date
|
|
2
|
-
from .ddi import fetch_ddi
|
|
3
|
-
from .di import fetch_last_di_data, fetch_past_di_data
|
|
4
|
-
|
|
5
|
-
__all__ = [
|
|
6
|
-
"fetch_past_di_data",
|
|
7
|
-
"fetch_last_di_data",
|
|
8
|
-
"fetch_ddi",
|
|
9
|
-
"get_expiration_date",
|
|
10
|
-
"get_old_expiration_date",
|
|
11
|
-
]
|
|
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
|