PYield 0.6.0__tar.gz → 0.7.0__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.6.0 → pyield-0.7.0}/PKG-INFO +44 -29
- {pyield-0.6.0 → pyield-0.7.0}/README.md +43 -28
- pyield-0.7.0/pyield/__about__.py +1 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/__init__.py +3 -2
- pyield-0.7.0/pyield/data_access.py +88 -0
- pyield-0.7.0/pyield/indicators.py +59 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/treasuries.py +3 -19
- pyield-0.7.0/pyield/utils.py +55 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyproject.toml +1 -1
- pyield-0.6.0/pyield/__about__.py +0 -1
- pyield-0.6.0/pyield/data_access.py +0 -51
- pyield-0.6.0/pyield/utils.py +0 -26
- {pyield-0.6.0 → pyield-0.7.0}/LICENSE +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/bday.py +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/data_analysis.py +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/di/__init__.py +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/di/core.py +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/di/web.py +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/di/xml.py +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/holidays/__init__.py +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/holidays/br_holidays_new.txt +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/holidays/br_holidays_old.txt +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/holidays/core.py +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/pyield/py.typed +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/tests/__init__.py +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/tests/test_br_calendar.py +0 -0
- {pyield-0.6.0 → pyield-0.7.0}/tests/test_di.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: PYield
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.0
|
|
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>
|
|
@@ -67,12 +67,39 @@ pip install pyield
|
|
|
67
67
|
|
|
68
68
|
## How to use PYield
|
|
69
69
|
|
|
70
|
-
###
|
|
70
|
+
### Business Days Tools (Brazilian holidays are automatically considered)
|
|
71
71
|
```python
|
|
72
|
-
import pyield as yd
|
|
72
|
+
>>> import pyield as yd
|
|
73
|
+
|
|
74
|
+
# Count the number of business days between two dates
|
|
75
|
+
# Start date is included, end date is excluded
|
|
76
|
+
>>> yd.count_bdays(start='2023-12-29', end='2024-01-02')
|
|
77
|
+
1
|
|
78
|
+
|
|
79
|
+
# Get the next business day after a given date (offset=1)
|
|
80
|
+
>>> yd.offset_bdays(dates="2023-12-29", offset=1)
|
|
81
|
+
Timestamp('2024-01-02 00:00:00')
|
|
82
|
+
|
|
83
|
+
# Get the next business day if it is not a business day (offset=0)
|
|
84
|
+
>>> yd.offset_bdays(dates="2023-12-30", offset=0)
|
|
85
|
+
Timestamp('2024-01-02 00:00:00')
|
|
73
86
|
|
|
74
|
-
#
|
|
75
|
-
>>> yd.
|
|
87
|
+
# Since 2023-12-29 is a business day, it returns the same date (offset=0)
|
|
88
|
+
>>> yd.offset_bdays(dates="2023-12-29", offset=0)
|
|
89
|
+
Timestamp('2023-12-29 00:00:00')
|
|
90
|
+
|
|
91
|
+
# Generate a pandas series with the business days between two dates
|
|
92
|
+
>>> yd.generate_bdays(start='2023-12-29', end='2024-01-03')
|
|
93
|
+
0 2023-12-29
|
|
94
|
+
1 2024-01-02
|
|
95
|
+
2 2024-01-03
|
|
96
|
+
dtype: datetime64[ns]
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
### Futures Data
|
|
100
|
+
```python
|
|
101
|
+
# Fetch a DataFrame with the DI Futures data from B3
|
|
102
|
+
>>> yd.fetch_asset(asset_code="DI1", reference_date='2024-03-08')
|
|
76
103
|
|
|
77
104
|
TradeDate ExpirationCode ExpirationDate BDToExpiration ... LastRate LastAskRate LastBidRate SettlementRate
|
|
78
105
|
2024-03-08 J24 2024-04-01 15 ... 10.952 10.952 10.956 10.956
|
|
@@ -86,10 +113,10 @@ TradeDate ExpirationCode ExpirationDate BDToExpiration ... LastRate LastAskRat
|
|
|
86
113
|
|
|
87
114
|
### Treasury Bonds Data
|
|
88
115
|
```python
|
|
89
|
-
#
|
|
116
|
+
# Fetch a DataFrame with the NTN-B data from ANBIMA
|
|
90
117
|
# Anbima data is available for the last 5 working days
|
|
91
118
|
# Obs: Anbima members have access to the full history
|
|
92
|
-
>>> yd.
|
|
119
|
+
>>> yd.fetch_asset(asset_code="NTN-B", reference_date='2024-04-12')
|
|
93
120
|
|
|
94
121
|
BondType ReferenceDate MaturityDate BidRate AskRate IndicativeRate Price
|
|
95
122
|
NTN-B 2024-04-12 2024-08-15 0.07540 0.07504 0.07523 4,271.43565
|
|
@@ -116,29 +143,17 @@ NTN-F 2024-04-11 2033-01-01 -5.69000
|
|
|
116
143
|
NTN-F 2024-04-11 2035-01-01 -1.27000
|
|
117
144
|
```
|
|
118
145
|
|
|
119
|
-
###
|
|
146
|
+
### Indicators Data
|
|
120
147
|
```python
|
|
121
|
-
#
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
1
|
|
148
|
+
# Fetch the SELIC target rate from the Central Bank of Brazil
|
|
149
|
+
>>> yd.fetch_indicator(indicator_code="SELIC", reference_date='2024-04-12')
|
|
150
|
+
10.75
|
|
125
151
|
|
|
126
|
-
#
|
|
127
|
-
>>> yd.
|
|
128
|
-
|
|
152
|
+
# Fetch the IPCA monthly inflation rate from IBGE
|
|
153
|
+
>>> yd.fetch_indicator(indicator_code="IPCA", reference_date='2024-03-18')
|
|
154
|
+
0.16
|
|
129
155
|
|
|
130
|
-
#
|
|
131
|
-
>>> yd.
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
# Since 2023-12-29 is a business day, it returns the same date (offset=0)
|
|
135
|
-
>>> yd.offset_bdays(dates="2023-12-29", offset=0)
|
|
136
|
-
Timestamp('2023-12-29 00:00:00')
|
|
137
|
-
|
|
138
|
-
# Generate a pandas series with the business days between two dates
|
|
139
|
-
>>> yd.generate_bdays(start='2023-12-29', end='2024-01-03')
|
|
140
|
-
0 2023-12-29
|
|
141
|
-
1 2024-01-02
|
|
142
|
-
2 2024-01-03
|
|
143
|
-
dtype: datetime64[ns]
|
|
156
|
+
# If no data is yet available for the indicator, the function returns None
|
|
157
|
+
>>> yd.fetch_indicator(indicator_code="IPCA", reference_date='2024-04-10')
|
|
158
|
+
None
|
|
144
159
|
```
|
|
@@ -25,12 +25,39 @@ pip install pyield
|
|
|
25
25
|
|
|
26
26
|
## How to use PYield
|
|
27
27
|
|
|
28
|
-
###
|
|
28
|
+
### Business Days Tools (Brazilian holidays are automatically considered)
|
|
29
29
|
```python
|
|
30
|
-
import pyield as yd
|
|
30
|
+
>>> import pyield as yd
|
|
31
|
+
|
|
32
|
+
# Count the number of business days between two dates
|
|
33
|
+
# Start date is included, end date is excluded
|
|
34
|
+
>>> yd.count_bdays(start='2023-12-29', end='2024-01-02')
|
|
35
|
+
1
|
|
36
|
+
|
|
37
|
+
# Get the next business day after a given date (offset=1)
|
|
38
|
+
>>> yd.offset_bdays(dates="2023-12-29", offset=1)
|
|
39
|
+
Timestamp('2024-01-02 00:00:00')
|
|
40
|
+
|
|
41
|
+
# Get the next business day if it is not a business day (offset=0)
|
|
42
|
+
>>> yd.offset_bdays(dates="2023-12-30", offset=0)
|
|
43
|
+
Timestamp('2024-01-02 00:00:00')
|
|
31
44
|
|
|
32
|
-
#
|
|
33
|
-
>>> yd.
|
|
45
|
+
# Since 2023-12-29 is a business day, it returns the same date (offset=0)
|
|
46
|
+
>>> yd.offset_bdays(dates="2023-12-29", offset=0)
|
|
47
|
+
Timestamp('2023-12-29 00:00:00')
|
|
48
|
+
|
|
49
|
+
# Generate a pandas series with the business days between two dates
|
|
50
|
+
>>> yd.generate_bdays(start='2023-12-29', end='2024-01-03')
|
|
51
|
+
0 2023-12-29
|
|
52
|
+
1 2024-01-02
|
|
53
|
+
2 2024-01-03
|
|
54
|
+
dtype: datetime64[ns]
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Futures Data
|
|
58
|
+
```python
|
|
59
|
+
# Fetch a DataFrame with the DI Futures data from B3
|
|
60
|
+
>>> yd.fetch_asset(asset_code="DI1", reference_date='2024-03-08')
|
|
34
61
|
|
|
35
62
|
TradeDate ExpirationCode ExpirationDate BDToExpiration ... LastRate LastAskRate LastBidRate SettlementRate
|
|
36
63
|
2024-03-08 J24 2024-04-01 15 ... 10.952 10.952 10.956 10.956
|
|
@@ -44,10 +71,10 @@ TradeDate ExpirationCode ExpirationDate BDToExpiration ... LastRate LastAskRat
|
|
|
44
71
|
|
|
45
72
|
### Treasury Bonds Data
|
|
46
73
|
```python
|
|
47
|
-
#
|
|
74
|
+
# Fetch a DataFrame with the NTN-B data from ANBIMA
|
|
48
75
|
# Anbima data is available for the last 5 working days
|
|
49
76
|
# Obs: Anbima members have access to the full history
|
|
50
|
-
>>> yd.
|
|
77
|
+
>>> yd.fetch_asset(asset_code="NTN-B", reference_date='2024-04-12')
|
|
51
78
|
|
|
52
79
|
BondType ReferenceDate MaturityDate BidRate AskRate IndicativeRate Price
|
|
53
80
|
NTN-B 2024-04-12 2024-08-15 0.07540 0.07504 0.07523 4,271.43565
|
|
@@ -74,29 +101,17 @@ NTN-F 2024-04-11 2033-01-01 -5.69000
|
|
|
74
101
|
NTN-F 2024-04-11 2035-01-01 -1.27000
|
|
75
102
|
```
|
|
76
103
|
|
|
77
|
-
###
|
|
104
|
+
### Indicators Data
|
|
78
105
|
```python
|
|
79
|
-
#
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
1
|
|
106
|
+
# Fetch the SELIC target rate from the Central Bank of Brazil
|
|
107
|
+
>>> yd.fetch_indicator(indicator_code="SELIC", reference_date='2024-04-12')
|
|
108
|
+
10.75
|
|
83
109
|
|
|
84
|
-
#
|
|
85
|
-
>>> yd.
|
|
86
|
-
|
|
110
|
+
# Fetch the IPCA monthly inflation rate from IBGE
|
|
111
|
+
>>> yd.fetch_indicator(indicator_code="IPCA", reference_date='2024-03-18')
|
|
112
|
+
0.16
|
|
87
113
|
|
|
88
|
-
#
|
|
89
|
-
>>> yd.
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
# Since 2023-12-29 is a business day, it returns the same date (offset=0)
|
|
93
|
-
>>> yd.offset_bdays(dates="2023-12-29", offset=0)
|
|
94
|
-
Timestamp('2023-12-29 00:00:00')
|
|
95
|
-
|
|
96
|
-
# Generate a pandas series with the business days between two dates
|
|
97
|
-
>>> yd.generate_bdays(start='2023-12-29', end='2024-01-03')
|
|
98
|
-
0 2023-12-29
|
|
99
|
-
1 2024-01-02
|
|
100
|
-
2 2024-01-03
|
|
101
|
-
dtype: datetime64[ns]
|
|
114
|
+
# If no data is yet available for the indicator, the function returns None
|
|
115
|
+
>>> yd.fetch_indicator(indicator_code="IPCA", reference_date='2024-04-10')
|
|
116
|
+
None
|
|
102
117
|
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.7.0"
|
|
@@ -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
|
|
3
|
+
from .data_access import fetch_asset, fetch_indicator
|
|
4
4
|
from .data_analysis import calculate_spreads
|
|
5
5
|
|
|
6
6
|
__all__ = [
|
|
@@ -9,6 +9,7 @@ __all__ = [
|
|
|
9
9
|
"generate_bdays",
|
|
10
10
|
"offset_bdays",
|
|
11
11
|
"count_bdays",
|
|
12
|
-
"
|
|
12
|
+
"fetch_asset",
|
|
13
|
+
"fetch_indicator",
|
|
13
14
|
"calculate_spreads",
|
|
14
15
|
]
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
|
|
3
|
+
from . import di
|
|
4
|
+
from . import indicators as ir
|
|
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": DI Futures rates from B3.
|
|
24
|
+
reference_date (str | pd.Timestamp | None): The reference date for which data is
|
|
25
|
+
fetched. Defaults to the previous business day if None.
|
|
26
|
+
**kwargs: Additional keyword arguments, specifically:
|
|
27
|
+
- return_raw (bool): Whether to return raw data without processing. Defaults
|
|
28
|
+
to False.
|
|
29
|
+
|
|
30
|
+
Returns:
|
|
31
|
+
pd.DataFrame: A DataFrame containing the fetched data for the specified asset.
|
|
32
|
+
|
|
33
|
+
Raises:
|
|
34
|
+
ValueError: If the asset code is not recognized or supported.
|
|
35
|
+
|
|
36
|
+
Examples:
|
|
37
|
+
>>> fetch_asset('TRB', '2023-04-01')
|
|
38
|
+
>>> fetch_asset('DI1', '2023-04-01', return_raw=True)
|
|
39
|
+
"""
|
|
40
|
+
return_raw = kwargs.get("return_raw", False)
|
|
41
|
+
normalized_date = _normalize_date(reference_date)
|
|
42
|
+
|
|
43
|
+
if asset_code.lower() == "trb":
|
|
44
|
+
return tr.fetch_data(reference_date=normalized_date, return_raw=return_raw)
|
|
45
|
+
elif asset_code.lower() in ["ltn", "lft", "ntn-f", "ntn-b"]:
|
|
46
|
+
df = tr.fetch_data(reference_date=normalized_date)
|
|
47
|
+
return df.query(f"BondType == '{asset_code.upper()}'")
|
|
48
|
+
|
|
49
|
+
elif asset_code.lower() == "di1":
|
|
50
|
+
return di.fetch_data(
|
|
51
|
+
trade_date=normalized_date, source_type="bmf", return_raw=return_raw
|
|
52
|
+
)
|
|
53
|
+
else:
|
|
54
|
+
raise ValueError("Asset type not supported.")
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def fetch_indicator(
|
|
58
|
+
indicator_code: str,
|
|
59
|
+
reference_date: str | pd.Timestamp | None = None,
|
|
60
|
+
) -> float | None:
|
|
61
|
+
"""
|
|
62
|
+
Fetches data for a specified economic indicator and reference date.
|
|
63
|
+
|
|
64
|
+
Args:
|
|
65
|
+
indicator_code (str): The code for the economic indicator. Supported options:
|
|
66
|
+
- "SELIC": SELIC target rate from the Central Bank of Brazil.
|
|
67
|
+
- "IPCA": IPCA monthly inflation rate from IBGE.
|
|
68
|
+
reference_date (str | pd.Timestamp | None): The reference date for which data is
|
|
69
|
+
fetched. Defaults to the previous business day if None.
|
|
70
|
+
|
|
71
|
+
Returns:
|
|
72
|
+
pd.Series: A Series containing the fetched data for the specified indicator.
|
|
73
|
+
|
|
74
|
+
Raises:
|
|
75
|
+
ValueError: If the indicator code is not recognized or supported.
|
|
76
|
+
|
|
77
|
+
Examples:
|
|
78
|
+
>>> fetch_indicator('SELIC', '2023-04-01')
|
|
79
|
+
>>> fetch_indicator('IPCA', '2023-04-01')
|
|
80
|
+
"""
|
|
81
|
+
normalized_date = _normalize_date(reference_date)
|
|
82
|
+
|
|
83
|
+
if indicator_code.lower() == "selic":
|
|
84
|
+
return ir.fetch_selic_target(reference_date=normalized_date)
|
|
85
|
+
elif indicator_code.lower() == "ipca":
|
|
86
|
+
return ir.fetch_ipca_mr(reference_date=normalized_date)
|
|
87
|
+
else:
|
|
88
|
+
raise ValueError("Indicator type not supported.")
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
import requests
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def fetch_ipca_mr(reference_date: pd.Timestamp) -> float | None:
|
|
6
|
+
"""
|
|
7
|
+
Fetches the IPCA (Índice Nacional de Preços ao Consumidor Amplo) monthly rate
|
|
8
|
+
from the IBGE (Instituto Brasileiro de Geografia e Estatística) for a given
|
|
9
|
+
reference date.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
reference_date (pd.Timestamp): Reference date for the data. The function expects
|
|
13
|
+
a pandas Timestamp object.
|
|
14
|
+
|
|
15
|
+
Returns:
|
|
16
|
+
float | None: The IPCA monthly rate for the specified date as a float.
|
|
17
|
+
Returns None if data is not found or in case of an error.
|
|
18
|
+
|
|
19
|
+
Notes:
|
|
20
|
+
The function makes an API call to the IBGE's data portal to retrieve the
|
|
21
|
+
information. An example of the API call:
|
|
22
|
+
https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/202403/variaveis/63?localidades=N1[all]
|
|
23
|
+
where '202403' is the reference date in 'YYYYMM' format.
|
|
24
|
+
The API URL is constructed dynamically based on the reference date provided.
|
|
25
|
+
"""
|
|
26
|
+
# Format the date as 'YYYYMM' for the API endpoint
|
|
27
|
+
ipca_date = reference_date.strftime("%Y%m")
|
|
28
|
+
|
|
29
|
+
# Construct the API URL using the formatted date
|
|
30
|
+
api_url = f"https://servicodados.ibge.gov.br/api/v3/agregados/6691/periodos/{ipca_date}/variaveis/63?localidades=N1[all]"
|
|
31
|
+
|
|
32
|
+
# Send a GET request to the API
|
|
33
|
+
response = requests.get(api_url)
|
|
34
|
+
|
|
35
|
+
# Raises HTTPError, if one occurred
|
|
36
|
+
response.raise_for_status()
|
|
37
|
+
|
|
38
|
+
# Parse the JSON response
|
|
39
|
+
data = response.json()
|
|
40
|
+
|
|
41
|
+
# Extract and return the IPCA monthly growth rate if data is available
|
|
42
|
+
if data:
|
|
43
|
+
ipca_str = data[0]["resultados"][0]["series"][0]["serie"][ipca_date]
|
|
44
|
+
return float(ipca_str)
|
|
45
|
+
else:
|
|
46
|
+
return None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def fetch_selic_target(reference_date: pd.Timestamp) -> float | None:
|
|
50
|
+
# https://api.bcb.gov.br/dados/serie/bcdata.sgs.432/dados?formato=json&dataInicial=12/04/2024&dataFinal=12/04/2024
|
|
51
|
+
selic_date = reference_date.strftime("%d/%m/%Y")
|
|
52
|
+
api_url = f"https://api.bcb.gov.br/dados/serie/bcdata.sgs.432/dados?formato=json&dataInicial={selic_date}&dataFinal={selic_date}"
|
|
53
|
+
response = requests.get(api_url)
|
|
54
|
+
response.raise_for_status()
|
|
55
|
+
data = response.json()
|
|
56
|
+
if data:
|
|
57
|
+
return float(data[0]["valor"])
|
|
58
|
+
else:
|
|
59
|
+
return None
|
|
@@ -105,10 +105,8 @@ def fetch_data(reference_date: pd.Timestamp, return_raw=False) -> pd.DataFrame:
|
|
|
105
105
|
Fetches indicative treasury rates from ANBIMA for a specified reference date.
|
|
106
106
|
|
|
107
107
|
This function retrieves the indicative rates for Brazilian treasury securities
|
|
108
|
-
from ANBIMA, processing them into a structured pandas DataFrame.
|
|
109
|
-
|
|
110
|
-
Brazilian calendar. There is an option to return raw data directly from the source
|
|
111
|
-
without processing.
|
|
108
|
+
from ANBIMA, processing them into a structured pandas DataFrame.
|
|
109
|
+
There is an option to return raw data directly from the source without processing.
|
|
112
110
|
|
|
113
111
|
Parameters:
|
|
114
112
|
reference_date (str | pd.Timestamp, optional): The date for which to fetch the
|
|
@@ -123,12 +121,6 @@ def fetch_data(reference_date: pd.Timestamp, return_raw=False) -> pd.DataFrame:
|
|
|
123
121
|
data includes bond type, reference date, maturity date and various rates
|
|
124
122
|
(bid, ask, indicative) among others, depending on the `return_raw` flag.
|
|
125
123
|
|
|
126
|
-
Examples:
|
|
127
|
-
# Fetch processed indicative rates for the previous business day
|
|
128
|
-
>>> get_treasury_rates()
|
|
129
|
-
|
|
130
|
-
# Fetch raw indicative rates for a specific date
|
|
131
|
-
>>> get_treasury_rates("2023-12-28")
|
|
132
124
|
"""
|
|
133
125
|
df = _get_raw_df(reference_date)
|
|
134
126
|
|
|
@@ -150,20 +142,12 @@ def calculate_di_spreads(reference_date: pd.Timestamp) -> pd.DataFrame:
|
|
|
150
142
|
|
|
151
143
|
Parameters:
|
|
152
144
|
reference_date (str | pd.Timestamp, optional): The reference date for the DI
|
|
153
|
-
spread calculation.
|
|
154
|
-
business day according to the Brazilian calendar.
|
|
145
|
+
spread calculation.
|
|
155
146
|
|
|
156
147
|
Returns:
|
|
157
148
|
pd.DataFrame: A DataFrame containing the bond type, reference date, maturity
|
|
158
149
|
date, and the calculated DI spread in basis points. The data is sorted by
|
|
159
150
|
bond type and maturity date.
|
|
160
|
-
|
|
161
|
-
Examples:
|
|
162
|
-
# Calculate DI spreads for the previous business day
|
|
163
|
-
>>> calculate_treasury_di_spreads()
|
|
164
|
-
|
|
165
|
-
# Calculate DI spreads for a specific reference date
|
|
166
|
-
>>> calculate_treasury_di_spreads("2023-12-15")
|
|
167
151
|
"""
|
|
168
152
|
# Fetch DI rates and adjust the maturity date format for compatibility
|
|
169
153
|
df_di = di.fetch_data(reference_date)[["ExpirationDate", "SettlementRate"]]
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
|
|
3
|
+
from . import bday
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def _normalize_date(reference_date: str | pd.Timestamp | None = None) -> pd.Timestamp:
|
|
7
|
+
"""
|
|
8
|
+
Normalizes the given date to ensure it is a past business day at midnight. If no
|
|
9
|
+
date is provided, it defaults to the previous business day.
|
|
10
|
+
|
|
11
|
+
Args:
|
|
12
|
+
reference_date (str | pd.Timestamp | None): The date to normalize. Can be a
|
|
13
|
+
string, pandas Timestamp or None. If None, it defaults to the last business day.
|
|
14
|
+
|
|
15
|
+
Returns:
|
|
16
|
+
pd.Timestamp: A normalized pandas Timestamp representing a past business day at
|
|
17
|
+
midnight.
|
|
18
|
+
|
|
19
|
+
Raises:
|
|
20
|
+
ValueError: If the input date format is invalid, if the date is in the future,
|
|
21
|
+
or if the date is not a business day.
|
|
22
|
+
|
|
23
|
+
Notes:
|
|
24
|
+
- Normalization means setting the time component of the timestamp to midnight.
|
|
25
|
+
- The function checks if the normalized date is a business day and adjusts
|
|
26
|
+
accordingly.
|
|
27
|
+
- Business day calculations consider local market holidays.
|
|
28
|
+
|
|
29
|
+
Examples:
|
|
30
|
+
>>> _normalize_date('2023-04-01')
|
|
31
|
+
>>> _normalize_date(pd.Timestamp('2023-04-01 15:30'))
|
|
32
|
+
>>> _normalize_date()
|
|
33
|
+
"""
|
|
34
|
+
if isinstance(reference_date, str):
|
|
35
|
+
# Convert string date to Timestamp and normalize to midnight
|
|
36
|
+
normalized_date = pd.Timestamp(reference_date).normalize()
|
|
37
|
+
elif isinstance(reference_date, pd.Timestamp):
|
|
38
|
+
# Normalize Timestamp to midnight
|
|
39
|
+
normalized_date = reference_date.normalize()
|
|
40
|
+
elif reference_date is None:
|
|
41
|
+
# If no date is provided, use the previous business day
|
|
42
|
+
today = pd.Timestamp.today().normalize()
|
|
43
|
+
normalized_date = bday.offset_bdays(today, -1)
|
|
44
|
+
else:
|
|
45
|
+
raise ValueError("Invalid date format.")
|
|
46
|
+
|
|
47
|
+
# Validate that the date is not in the future
|
|
48
|
+
if normalized_date > pd.Timestamp.today().normalize():
|
|
49
|
+
raise ValueError("Reference date cannot be in the future.")
|
|
50
|
+
|
|
51
|
+
# Validate that the date is a business day
|
|
52
|
+
if not bday.is_bday(normalized_date):
|
|
53
|
+
raise ValueError("Reference date must be a business day.")
|
|
54
|
+
|
|
55
|
+
return normalized_date
|
pyield-0.6.0/pyield/__about__.py
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.6.0"
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
import pandas as pd
|
|
2
|
-
|
|
3
|
-
from . import di
|
|
4
|
-
from . import treasuries as tr
|
|
5
|
-
from .utils import _normalize_date
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def get_data(
|
|
9
|
-
asset_code: str,
|
|
10
|
-
reference_date: str | pd.Timestamp | None = None,
|
|
11
|
-
**kwargs,
|
|
12
|
-
) -> pd.DataFrame:
|
|
13
|
-
"""
|
|
14
|
-
Fetches data for a specified asset type and reference date.
|
|
15
|
-
|
|
16
|
-
Args:
|
|
17
|
-
asset (str): The asset type. Available options are:
|
|
18
|
-
- "TRB": Fetches indicative rates for Brazilian treasury bonds from ANBIMA.
|
|
19
|
-
- "LTN": Fetches indicative rates for Brazilian LTN bonds from ANBIMA.
|
|
20
|
-
- "LFT": Fetches indicative rates for Brazilian LFT bonds from ANBIMA.
|
|
21
|
-
- "NTN-F": Fetches indicative rates for Brazilian NTN-F bonds from ANBIMA.
|
|
22
|
-
- "NTN-B": Fetches indicative rates for Brazilian NTN-B bonds from ANBIMA.
|
|
23
|
-
- "DI1": Fetches DI Futures rates from B3.
|
|
24
|
-
reference_date (str): Reference date for the data in YYYY-MM-DD format.
|
|
25
|
-
|
|
26
|
-
Returns:
|
|
27
|
-
pd.DataFrame: A DataFrame containing the fetched data.
|
|
28
|
-
|
|
29
|
-
Raises:
|
|
30
|
-
ValueError: If the specified source or asset type is not supported.
|
|
31
|
-
"""
|
|
32
|
-
# Extract the internal use parameter with default value
|
|
33
|
-
return_raw = kwargs.get("return_raw", False)
|
|
34
|
-
|
|
35
|
-
# Validate the date, defaulting to the previous business day if not provided
|
|
36
|
-
normalized_date = _normalize_date(reference_date)
|
|
37
|
-
|
|
38
|
-
if asset_code.lower() == "trb":
|
|
39
|
-
# Fetch all indicative treasury rates from ANBIMA
|
|
40
|
-
return tr.fetch_data(reference_date=normalized_date, return_raw=return_raw)
|
|
41
|
-
elif asset_code.lower() in ["ltn", "lft", "ntn-f", "ntn-b"]:
|
|
42
|
-
# Fetch indicative rates for a specific type of Brazilian treasury bond
|
|
43
|
-
df = tr.fetch_data(reference_date=normalized_date)
|
|
44
|
-
return df.query(f"BondType == '{asset_code.upper()}'")
|
|
45
|
-
|
|
46
|
-
elif asset_code.lower() == "di1":
|
|
47
|
-
return di.fetch_data(
|
|
48
|
-
trade_date=normalized_date, source_type="bmf", return_raw=return_raw
|
|
49
|
-
)
|
|
50
|
-
else:
|
|
51
|
-
raise ValueError("Asset type not supported.")
|
pyield-0.6.0/pyield/utils.py
DELETED
|
@@ -1,26 +0,0 @@
|
|
|
1
|
-
import pandas as pd
|
|
2
|
-
|
|
3
|
-
from . import bday
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
def _normalize_date(reference_date: str | pd.Timestamp | None = None) -> pd.Timestamp:
|
|
7
|
-
if isinstance(reference_date, str):
|
|
8
|
-
normalized_date = pd.Timestamp(reference_date).normalize()
|
|
9
|
-
elif isinstance(reference_date, pd.Timestamp):
|
|
10
|
-
normalized_date = reference_date.normalize()
|
|
11
|
-
elif reference_date is None:
|
|
12
|
-
today = pd.Timestamp.today().normalize()
|
|
13
|
-
# Get last business day before today
|
|
14
|
-
normalized_date = bday.offset_bdays(today, -1)
|
|
15
|
-
else:
|
|
16
|
-
raise ValueError("Invalid date format.")
|
|
17
|
-
|
|
18
|
-
# Raise an error if the reference date is in the future
|
|
19
|
-
if normalized_date > pd.Timestamp.today().normalize():
|
|
20
|
-
raise ValueError("Reference date cannot be in the future.")
|
|
21
|
-
|
|
22
|
-
# Raise error if the reference date is not a business day
|
|
23
|
-
if not bday.is_bday(normalized_date):
|
|
24
|
-
raise ValueError("Reference date must be a business day.")
|
|
25
|
-
|
|
26
|
-
return normalized_date
|
|
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
|