PYield 0.5.1__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.5.1 → pyield-0.7.0}/PKG-INFO +77 -29
- pyield-0.7.0/README.md +117 -0
- pyield-0.7.0/pyield/__about__.py +1 -0
- pyield-0.7.0/pyield/__init__.py +15 -0
- pyield-0.5.1/pyield/calendar.py → pyield-0.7.0/pyield/bday.py +69 -47
- pyield-0.7.0/pyield/data_access.py +88 -0
- pyield-0.7.0/pyield/data_analysis.py +31 -0
- pyield-0.7.0/pyield/di/__init__.py +6 -0
- {pyield-0.5.1 → pyield-0.7.0}/pyield/di/core.py +26 -25
- {pyield-0.5.1 → pyield-0.7.0}/pyield/di/web.py +36 -37
- {pyield-0.5.1 → pyield-0.7.0}/pyield/di/xml.py +5 -5
- pyield-0.7.0/pyield/indicators.py +59 -0
- pyield-0.5.1/pyield/anbima.py → pyield-0.7.0/pyield/treasuries.py +35 -76
- pyield-0.7.0/pyield/utils.py +55 -0
- {pyield-0.5.1 → pyield-0.7.0}/pyproject.toml +11 -1
- {pyield-0.5.1 → pyield-0.7.0}/tests/test_br_calendar.py +7 -7
- {pyield-0.5.1 → pyield-0.7.0}/tests/test_di.py +3 -3
- pyield-0.5.1/README.md +0 -69
- pyield-0.5.1/pyield/__about__.py +0 -1
- pyield-0.5.1/pyield/__init__.py +0 -16
- pyield-0.5.1/pyield/di/__init__.py +0 -6
- {pyield-0.5.1 → pyield-0.7.0}/LICENSE +0 -0
- {pyield-0.5.1 → pyield-0.7.0}/pyield/holidays/__init__.py +0 -0
- {pyield-0.5.1 → pyield-0.7.0}/pyield/holidays/br_holidays_new.txt +0 -0
- {pyield-0.5.1 → pyield-0.7.0}/pyield/holidays/br_holidays_old.txt +0 -0
- {pyield-0.5.1 → pyield-0.7.0}/pyield/holidays/core.py +0 -0
- {pyield-0.5.1 → pyield-0.7.0}/pyield/py.typed +0 -0
- {pyield-0.5.1 → pyield-0.7.0}/tests/__init__.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>
|
|
@@ -44,7 +44,7 @@ Description-Content-Type: text/markdown
|
|
|
44
44
|
[](https://python.org "Go to Python homepage")
|
|
45
45
|
[](#license)
|
|
46
46
|
|
|
47
|
-
# PYield:
|
|
47
|
+
# PYield: Fixed Income Toolbox for Brazilian Markets
|
|
48
48
|
|
|
49
49
|
## Introduction
|
|
50
50
|
|
|
@@ -55,7 +55,7 @@ Welcome to PYield, a Python library designed for the analysis of fixed income in
|
|
|
55
55
|
- **Data Collection**: Automated fetching of data from ANBIMA and B3.
|
|
56
56
|
- **Data Processing**: Efficient processing and normalization of fixed income data.
|
|
57
57
|
- **Analysis Tools**: Built-in functions for common analysis tasks in fixed income markets.
|
|
58
|
-
- **Easy Integration**: Seamless integration with
|
|
58
|
+
- **Easy Integration**: Seamless integration with pandas data analysis workflows.
|
|
59
59
|
- **Type Hints**: Full support for static type checking, enhancing development experience and code quality.
|
|
60
60
|
|
|
61
61
|
## Installation
|
|
@@ -67,30 +67,14 @@ pip install pyield
|
|
|
67
67
|
|
|
68
68
|
## How to use PYield
|
|
69
69
|
|
|
70
|
-
### DI Futures Data
|
|
71
|
-
```python
|
|
72
|
-
import pyield as yd
|
|
73
|
-
|
|
74
|
-
# Get a pandas dataframe with the DI processed data from B3 (default)
|
|
75
|
-
>>> yd.get_di(trade_date='2024-03-08')
|
|
76
|
-
TradeDate ExpirationCode ExpirationDate BDToExpiration ... LastRate LastAskRate LastBidRate SettlementRate
|
|
77
|
-
2024-03-08 J24 2024-04-01 15 ... 10.952 10.952 10.956 10.956
|
|
78
|
-
2024-03-08 K24 2024-05-02 37 ... 10.776 10.774 10.780 10.777
|
|
79
|
-
2024-03-08 M24 2024-06-03 58 ... 10.604 10.602 10.604 10.608
|
|
80
|
-
... ... ... ... ... ... ... ... ...
|
|
81
|
-
2024-03-08 F37 2037-01-02 3213 ... <NA> <NA> <NA> 10.859
|
|
82
|
-
2024-03-08 F38 2038-01-04 3462 ... <NA> <NA> <NA> 10.859
|
|
83
|
-
2024-03-08 F39 2039-01-03 3713 ... <NA> <NA> <NA> 10.85
|
|
84
|
-
```
|
|
85
|
-
|
|
86
70
|
### Business Days Tools (Brazilian holidays are automatically considered)
|
|
87
71
|
```python
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
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
|
|
94
78
|
|
|
95
79
|
# Get the next business day after a given date (offset=1)
|
|
96
80
|
>>> yd.offset_bdays(dates="2023-12-29", offset=1)
|
|
@@ -104,8 +88,72 @@ Timestamp('2024-01-02 00:00:00')
|
|
|
104
88
|
>>> yd.offset_bdays(dates="2023-12-29", offset=0)
|
|
105
89
|
Timestamp('2023-12-29 00:00:00')
|
|
106
90
|
|
|
107
|
-
#
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
1
|
|
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')
|
|
103
|
+
|
|
104
|
+
TradeDate ExpirationCode ExpirationDate BDToExpiration ... LastRate LastAskRate LastBidRate SettlementRate
|
|
105
|
+
2024-03-08 J24 2024-04-01 15 ... 10.952 10.952 10.956 10.956
|
|
106
|
+
2024-03-08 K24 2024-05-02 37 ... 10.776 10.774 10.780 10.777
|
|
107
|
+
2024-03-08 M24 2024-06-03 58 ... 10.604 10.602 10.604 10.608
|
|
108
|
+
... ... ... ... ... ... ... ... ...
|
|
109
|
+
2024-03-08 F37 2037-01-02 3213 ... <NA> <NA> <NA> 10.859
|
|
110
|
+
2024-03-08 F38 2038-01-04 3462 ... <NA> <NA> <NA> 10.859
|
|
111
|
+
2024-03-08 F39 2039-01-03 3713 ... <NA> <NA> <NA> 10.85
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
### Treasury Bonds Data
|
|
115
|
+
```python
|
|
116
|
+
# Fetch a DataFrame with the NTN-B data from ANBIMA
|
|
117
|
+
# Anbima data is available for the last 5 working days
|
|
118
|
+
# Obs: Anbima members have access to the full history
|
|
119
|
+
>>> yd.fetch_asset(asset_code="NTN-B", reference_date='2024-04-12')
|
|
120
|
+
|
|
121
|
+
BondType ReferenceDate MaturityDate BidRate AskRate IndicativeRate Price
|
|
122
|
+
NTN-B 2024-04-12 2024-08-15 0.07540 0.07504 0.07523 4,271.43565
|
|
123
|
+
NTN-B 2024-04-12 2025-05-15 0.05945 0.05913 0.05930 4,361.34391
|
|
124
|
+
NTN-B 2024-04-12 2026-08-15 0.05927 0.05897 0.05910 4,301.40082
|
|
125
|
+
... ... ... ... ... ... ...
|
|
126
|
+
NTN-B 2024-04-12 2050-08-15 0.06039 0.06006 0.06023 4,299.28233
|
|
127
|
+
NTN-B 2024-04-12 2055-05-15 0.06035 0.05998 0.06017 4,367.13360
|
|
128
|
+
NTN-B 2024-04-12 2060-08-15 0.06057 0.06016 0.06036 4,292.26323
|
|
129
|
+
```
|
|
130
|
+
|
|
131
|
+
### Spreads Calculation
|
|
132
|
+
```python
|
|
133
|
+
# Calculate the spread between two DI Futures contracts and the pre-fix bonds
|
|
134
|
+
>>> yd.calculate_spreads(spread_type="di_vs_pre", reference_date="2024-4-11")
|
|
135
|
+
|
|
136
|
+
BondType ReferenceDate MaturityDate DISpread
|
|
137
|
+
LTN 2024-04-11 2024-07-01 -20.28000
|
|
138
|
+
LTN 2024-04-11 2024-10-01 -10.19000
|
|
139
|
+
LTN 2024-04-11 2025-01-01 -15.05000
|
|
140
|
+
... ... ... ...
|
|
141
|
+
NTN-F 2024-04-11 2031-01-01 -0.66000
|
|
142
|
+
NTN-F 2024-04-11 2033-01-01 -5.69000
|
|
143
|
+
NTN-F 2024-04-11 2035-01-01 -1.27000
|
|
144
|
+
```
|
|
145
|
+
|
|
146
|
+
### Indicators Data
|
|
147
|
+
```python
|
|
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
|
|
151
|
+
|
|
152
|
+
# Fetch the IPCA monthly inflation rate from IBGE
|
|
153
|
+
>>> yd.fetch_indicator(indicator_code="IPCA", reference_date='2024-03-18')
|
|
154
|
+
0.16
|
|
155
|
+
|
|
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
|
|
111
159
|
```
|
pyield-0.7.0/README.md
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
[](https://pypi.python.org/pypi/pyield)
|
|
2
|
+
[](https://python.org "Go to Python homepage")
|
|
3
|
+
[](#license)
|
|
4
|
+
|
|
5
|
+
# PYield: Fixed Income Toolbox for Brazilian Markets
|
|
6
|
+
|
|
7
|
+
## Introduction
|
|
8
|
+
|
|
9
|
+
Welcome to PYield, a Python library designed for the analysis of fixed income instruments in Brazil. This library is tailored for financial analysts, researchers, and enthusiasts interested in the Brazilian fixed income market. Leveraging the power of popular Python libraries like Pandas and Requests, PYield simplifies the process of obtaining and processing data from key sources such as ANBIMA and B3.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- **Data Collection**: Automated fetching of data from ANBIMA and B3.
|
|
14
|
+
- **Data Processing**: Efficient processing and normalization of fixed income data.
|
|
15
|
+
- **Analysis Tools**: Built-in functions for common analysis tasks in fixed income markets.
|
|
16
|
+
- **Easy Integration**: Seamless integration with pandas data analysis workflows.
|
|
17
|
+
- **Type Hints**: Full support for static type checking, enhancing development experience and code quality.
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
You can install PYield using pip:
|
|
22
|
+
```sh
|
|
23
|
+
pip install pyield
|
|
24
|
+
```
|
|
25
|
+
|
|
26
|
+
## How to use PYield
|
|
27
|
+
|
|
28
|
+
### Business Days Tools (Brazilian holidays are automatically considered)
|
|
29
|
+
```python
|
|
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')
|
|
44
|
+
|
|
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')
|
|
61
|
+
|
|
62
|
+
TradeDate ExpirationCode ExpirationDate BDToExpiration ... LastRate LastAskRate LastBidRate SettlementRate
|
|
63
|
+
2024-03-08 J24 2024-04-01 15 ... 10.952 10.952 10.956 10.956
|
|
64
|
+
2024-03-08 K24 2024-05-02 37 ... 10.776 10.774 10.780 10.777
|
|
65
|
+
2024-03-08 M24 2024-06-03 58 ... 10.604 10.602 10.604 10.608
|
|
66
|
+
... ... ... ... ... ... ... ... ...
|
|
67
|
+
2024-03-08 F37 2037-01-02 3213 ... <NA> <NA> <NA> 10.859
|
|
68
|
+
2024-03-08 F38 2038-01-04 3462 ... <NA> <NA> <NA> 10.859
|
|
69
|
+
2024-03-08 F39 2039-01-03 3713 ... <NA> <NA> <NA> 10.85
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Treasury Bonds Data
|
|
73
|
+
```python
|
|
74
|
+
# Fetch a DataFrame with the NTN-B data from ANBIMA
|
|
75
|
+
# Anbima data is available for the last 5 working days
|
|
76
|
+
# Obs: Anbima members have access to the full history
|
|
77
|
+
>>> yd.fetch_asset(asset_code="NTN-B", reference_date='2024-04-12')
|
|
78
|
+
|
|
79
|
+
BondType ReferenceDate MaturityDate BidRate AskRate IndicativeRate Price
|
|
80
|
+
NTN-B 2024-04-12 2024-08-15 0.07540 0.07504 0.07523 4,271.43565
|
|
81
|
+
NTN-B 2024-04-12 2025-05-15 0.05945 0.05913 0.05930 4,361.34391
|
|
82
|
+
NTN-B 2024-04-12 2026-08-15 0.05927 0.05897 0.05910 4,301.40082
|
|
83
|
+
... ... ... ... ... ... ...
|
|
84
|
+
NTN-B 2024-04-12 2050-08-15 0.06039 0.06006 0.06023 4,299.28233
|
|
85
|
+
NTN-B 2024-04-12 2055-05-15 0.06035 0.05998 0.06017 4,367.13360
|
|
86
|
+
NTN-B 2024-04-12 2060-08-15 0.06057 0.06016 0.06036 4,292.26323
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
### Spreads Calculation
|
|
90
|
+
```python
|
|
91
|
+
# Calculate the spread between two DI Futures contracts and the pre-fix bonds
|
|
92
|
+
>>> yd.calculate_spreads(spread_type="di_vs_pre", reference_date="2024-4-11")
|
|
93
|
+
|
|
94
|
+
BondType ReferenceDate MaturityDate DISpread
|
|
95
|
+
LTN 2024-04-11 2024-07-01 -20.28000
|
|
96
|
+
LTN 2024-04-11 2024-10-01 -10.19000
|
|
97
|
+
LTN 2024-04-11 2025-01-01 -15.05000
|
|
98
|
+
... ... ... ...
|
|
99
|
+
NTN-F 2024-04-11 2031-01-01 -0.66000
|
|
100
|
+
NTN-F 2024-04-11 2033-01-01 -5.69000
|
|
101
|
+
NTN-F 2024-04-11 2035-01-01 -1.27000
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Indicators Data
|
|
105
|
+
```python
|
|
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
|
|
109
|
+
|
|
110
|
+
# Fetch the IPCA monthly inflation rate from IBGE
|
|
111
|
+
>>> yd.fetch_indicator(indicator_code="IPCA", reference_date='2024-03-18')
|
|
112
|
+
0.16
|
|
113
|
+
|
|
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
|
|
117
|
+
```
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.7.0"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .__about__ import __version__
|
|
2
|
+
from .bday import count_bdays, generate_bdays, is_bday, offset_bdays
|
|
3
|
+
from .data_access import fetch_asset, fetch_indicator
|
|
4
|
+
from .data_analysis import calculate_spreads
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"__version__",
|
|
8
|
+
"is_bday",
|
|
9
|
+
"generate_bdays",
|
|
10
|
+
"offset_bdays",
|
|
11
|
+
"count_bdays",
|
|
12
|
+
"fetch_asset",
|
|
13
|
+
"fetch_indicator",
|
|
14
|
+
"calculate_spreads",
|
|
15
|
+
]
|
|
@@ -1,16 +1,13 @@
|
|
|
1
|
-
from typing import Literal, overload, Union
|
|
2
1
|
from datetime import datetime
|
|
2
|
+
from typing import Literal, overload
|
|
3
3
|
|
|
4
4
|
import numpy as np
|
|
5
5
|
import pandas as pd
|
|
6
6
|
|
|
7
|
-
|
|
8
7
|
from .holidays import BrHolidays
|
|
9
8
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
SeriesDateTypes = Union[list, tuple, np.ndarray, pd.Series, pd.Index, pd.DatetimeIndex]
|
|
9
|
+
SingleDateTypes = str | np.datetime64 | pd.Timestamp | datetime
|
|
10
|
+
SeriesDateTypes = list | tuple | np.ndarray | pd.Series | pd.Index | pd.DatetimeIndex
|
|
14
11
|
|
|
15
12
|
TO_TIMESTAMP_TYPES = (str, np.datetime64, datetime)
|
|
16
13
|
TO_SERIES_TYPES = (list, tuple, np.ndarray, pd.Series, pd.Index, pd.DatetimeIndex)
|
|
@@ -113,25 +110,33 @@ def offset_bdays(
|
|
|
113
110
|
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
114
111
|
) -> pd.Timestamp | pd.Series:
|
|
115
112
|
"""
|
|
116
|
-
Offsets the dates to the next or previous business day, considering brazilian
|
|
117
|
-
This function supports both single dates and collections of dates,
|
|
118
|
-
intelligently to return either a single offset date or a series of
|
|
119
|
-
It is a wrapper for `numpy.busday_offset` adapted for Pandas data
|
|
113
|
+
Offsets the dates to the next or previous business day, considering brazilian
|
|
114
|
+
holidays. This function supports both single dates and collections of dates,
|
|
115
|
+
handling them intelligently to return either a single offset date or a series of
|
|
116
|
+
offset dates. It is a wrapper for `numpy.busday_offset` adapted for Pandas data
|
|
117
|
+
types and holiday adjustments.
|
|
120
118
|
|
|
121
119
|
Args:
|
|
122
|
-
dates (str | np.datetime64 | pd.Timestamp | datetime | list | tuple | np.ndarray
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
120
|
+
dates (str | np.datetime64 | pd.Timestamp | datetime | list | tuple | np.ndarray
|
|
121
|
+
| pd.Series | pd.Index | pd.DatetimeIndex, optional):
|
|
122
|
+
The date(s) to offset. Can be a single date in various formats (string,
|
|
123
|
+
`datetime`, `Timestamp`, etc.) or a collection of dates (list, tuple,
|
|
124
|
+
`Series`, etc.). If None, the current date is used.
|
|
125
|
+
offset (int): The number of business days to offset the dates. Positive for
|
|
126
|
+
future dates, negative for past dates. Zero will return the same date if
|
|
127
|
+
it's a business day, or the next business day otherwise.
|
|
128
|
+
roll (Literal["forward", "backward"], optional): Direction to roll the date if
|
|
129
|
+
it falls on a holiday or weekend. 'forward' to the next business day,
|
|
130
|
+
'backward' to the previous. Defaults to 'forward'.
|
|
131
|
+
holiday_list (Literal["old", "new", "infer"], optional):
|
|
132
|
+
The list of holidays to consider. 'old' or 'new' use predefined lists, while
|
|
133
|
+
'infer' determines the most appropriate list based on the input dates.
|
|
134
|
+
Defaults to "infer".
|
|
131
135
|
|
|
132
136
|
Returns:
|
|
133
|
-
pd.Timestamp | pd.Series: If a single date is provided, returns a single
|
|
134
|
-
If a series of dates is provided, returns a
|
|
137
|
+
pd.Timestamp | pd.Series: If a single date is provided, returns a single
|
|
138
|
+
`Timestamp` of the offset date. If a series of dates is provided, returns a
|
|
139
|
+
`Series` of offset dates.
|
|
135
140
|
|
|
136
141
|
Examples:
|
|
137
142
|
>>> date = '2023-12-23' # Saturday before Christmas
|
|
@@ -149,8 +154,9 @@ def offset_bdays(
|
|
|
149
154
|
Timestamp('2023-12-21') # Offset to the previous business day
|
|
150
155
|
|
|
151
156
|
Note:
|
|
152
|
-
This function uses `numpy.busday_offset` under the hood, which means it follows
|
|
153
|
-
for business day calculations. For detailed
|
|
157
|
+
This function uses `numpy.busday_offset` under the hood, which means it follows
|
|
158
|
+
the same conventions and limitations for business day calculations. For detailed
|
|
159
|
+
information on error handling and behavior, refer to the `numpy.busday_offset`
|
|
154
160
|
documentation: https://numpy.org/doc/stable/reference/generated/numpy.busday_offset.html
|
|
155
161
|
"""
|
|
156
162
|
normalized_dates = _normalize_input_dates(dates)
|
|
@@ -209,30 +215,40 @@ def count_bdays(
|
|
|
209
215
|
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
210
216
|
) -> int | pd.Series:
|
|
211
217
|
"""
|
|
212
|
-
Counts the number of business days between a `start` and `end` date(s), inclusively
|
|
213
|
-
the start date and exclusively for the end date. The function can handle single
|
|
214
|
-
arrays of dates, and mixed inputs, returning either a single integer or a
|
|
215
|
-
depending on the inputs. It accounts for specified holidays, effectively
|
|
216
|
-
them from the business day count.
|
|
218
|
+
Counts the number of business days between a `start` and `end` date(s), inclusively
|
|
219
|
+
for the start date and exclusively for the end date. The function can handle single
|
|
220
|
+
dates, arrays of dates, and mixed inputs, returning either a single integer or a
|
|
221
|
+
Series depending on the inputs. It accounts for specified holidays, effectively
|
|
222
|
+
excluding them from the business day count.
|
|
217
223
|
|
|
218
224
|
Args:
|
|
219
|
-
start (str | np.datetime64 | pd.Timestamp | datetime | list | tuple |
|
|
225
|
+
start (str | np.datetime64 | pd.Timestamp | datetime | list | tuple |
|
|
226
|
+
np.ndarray | pd.Series | pd.Index | pd.DatetimeIndex, optional):
|
|
220
227
|
The start date(s) for counting. Defaults to the current date if None.
|
|
221
|
-
end (str | np.datetime64 | pd.Timestamp | datetime | list | tuple |
|
|
222
|
-
|
|
228
|
+
end (str | np.datetime64 | pd.Timestamp | datetime | list | tuple |
|
|
229
|
+
np.ndarray | pd.Series | pd.Index | pd.DatetimeIndex, optional):
|
|
230
|
+
The end date(s) for counting, which are excluded from the count themselves.
|
|
231
|
+
Defaults to the current date if None.
|
|
223
232
|
holiday_list (Literal["old", "new", "infer"], optional):
|
|
224
|
-
Specifies which set of holidays to consider in the count. 'old' or 'new'
|
|
225
|
-
while 'infer' automatically selects the
|
|
233
|
+
Specifies which set of holidays to consider in the count. 'old' or 'new'
|
|
234
|
+
refer to predefined holiday lists, while 'infer' automatically selects the
|
|
235
|
+
list based on the most recent date in the input. Defaults to "infer".
|
|
226
236
|
|
|
227
237
|
Returns:
|
|
228
238
|
int | pd.Series: Returns an integer if `start` and `end` are single dates,
|
|
229
|
-
or a Series if either or both are arrays of dates. The value(s) represent
|
|
239
|
+
or a Series if either or both are arrays of dates. The value(s) represent
|
|
240
|
+
the count of business days between `start` and `end`.
|
|
230
241
|
|
|
231
242
|
Notes:
|
|
232
|
-
- This function is a wrapper around `numpy.busday_count`, adapted to work
|
|
233
|
-
|
|
234
|
-
-
|
|
235
|
-
|
|
243
|
+
- This function is a wrapper around `numpy.busday_count`, adapted to work
|
|
244
|
+
directly with various Pandas and Numpy date formats.
|
|
245
|
+
- It supports flexible date inputs, including single dates, lists, Series, and
|
|
246
|
+
more, for both `start` and `end` parameters.
|
|
247
|
+
- The return type depends on the input types: single dates return an int, while
|
|
248
|
+
arrays of dates return a pd.Series with the count for each date range.
|
|
249
|
+
- See `numpy.busday_count` documentation for more details on how holidays are
|
|
250
|
+
handled and how business day counts are calculated:
|
|
251
|
+
https://numpy.org/doc/stable/reference/generated/numpy.busday_count.html.
|
|
236
252
|
|
|
237
253
|
Examples:
|
|
238
254
|
>>> yd.count_bdays('2023-12-15', '2024-01-01')
|
|
@@ -277,28 +293,34 @@ def generate_bdays(
|
|
|
277
293
|
|
|
278
294
|
Args:
|
|
279
295
|
start (str | np.datetime64 | pd.Timestamp | datetime, optional):
|
|
280
|
-
The start date for generating business days. Defaults to None, using the
|
|
296
|
+
The start date for generating business days. Defaults to None, using the
|
|
297
|
+
current date.
|
|
281
298
|
end (str | np.datetime64 | pd.Timestamp | datetime, optional):
|
|
282
|
-
The end date for generating business days. Defaults to None, using the
|
|
299
|
+
The end date for generating business days. Defaults to None, using the
|
|
300
|
+
current date.
|
|
283
301
|
inclusive (Literal["both", "neither", "left", "right"], optional):
|
|
284
302
|
Determines which of the start and end dates are included in the result.
|
|
285
303
|
Valid options are 'both', 'neither', 'left', 'right'. Defaults to 'both'.
|
|
286
304
|
holiday_list (Literal["old", "new", "infer"], optional):
|
|
287
|
-
Specifies the list of holidays to consider. 'old' or 'new' refer to
|
|
288
|
-
'infer' selects the list based on the most recent date in
|
|
305
|
+
Specifies the list of holidays to consider. 'old' or 'new' refer to
|
|
306
|
+
predefined lists, 'infer' selects the list based on the most recent date in
|
|
307
|
+
the range. Defaults to "infer".
|
|
289
308
|
**kwargs:
|
|
290
|
-
Additional keyword arguments passed to `pandas.bdate_range` for
|
|
309
|
+
Additional keyword arguments passed to `pandas.bdate_range` for
|
|
310
|
+
customization.
|
|
291
311
|
|
|
292
312
|
Returns:
|
|
293
|
-
pd.Series: A Series representing a range of business days between the specified
|
|
294
|
-
considering the specified holidays.
|
|
313
|
+
pd.Series: A Series representing a range of business days between the specified
|
|
314
|
+
start and end dates, considering the specified holidays.
|
|
295
315
|
|
|
296
316
|
Examples:
|
|
297
317
|
>>> yd.generate_bdays(start='2023-12-22', end='2024-01-02')
|
|
298
|
-
pd.Series(['2023-12-22', '2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29',
|
|
318
|
+
pd.Series(['2023-12-22', '2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29',
|
|
319
|
+
'2024-01-02'], dtype='datetime64[ns]')
|
|
299
320
|
|
|
300
321
|
Note:
|
|
301
|
-
For detailed information on parameters and error handling, refer to
|
|
322
|
+
For detailed information on parameters and error handling, refer to
|
|
323
|
+
`pandas.bdate_range` documentation:
|
|
302
324
|
https://pandas.pydata.org/docs/reference/api/pandas.bdate_range.html.
|
|
303
325
|
"""
|
|
304
326
|
normalized_start = _normalize_input_dates(start)
|
|
@@ -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,31 @@
|
|
|
1
|
+
import pandas as pd
|
|
2
|
+
|
|
3
|
+
from . import treasuries as tr
|
|
4
|
+
from .utils import _normalize_date
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def calculate_spreads(spread_type: str, reference_date: pd.Timestamp) -> pd.DataFrame:
|
|
8
|
+
"""
|
|
9
|
+
Calculates spreads between assets based on the specified spread type.
|
|
10
|
+
If no reference date is provided, the function uses the previous business day.
|
|
11
|
+
|
|
12
|
+
Parameters:
|
|
13
|
+
spread_type (str): The type of spread to calculate. Available options are:
|
|
14
|
+
- "DI_vs_PRE": the spread between DI Futures and Treasury Pre-Fixed bonds.
|
|
15
|
+
reference_date (str | pd.Timestamp, optional): The reference date for the
|
|
16
|
+
spread calculation. If None or not provided, defaults to the previous
|
|
17
|
+
business day according to the Brazilian calendar.
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
pd.DataFrame: A DataFrame containing the calculated spread in basis points.
|
|
21
|
+
The data is sorted by asset type and maturity/expiration date.
|
|
22
|
+
|
|
23
|
+
Raises:
|
|
24
|
+
ValueError: If an invalid spread type is provided.
|
|
25
|
+
"""
|
|
26
|
+
# Normalize the reference date
|
|
27
|
+
normalized_date = _normalize_date(reference_date)
|
|
28
|
+
if spread_type.lower() == "di_vs_pre":
|
|
29
|
+
return tr.calculate_di_spreads(normalized_date)
|
|
30
|
+
else:
|
|
31
|
+
raise ValueError("Invalid spread type.")
|