PYield 0.10.0__tar.gz → 0.10.2__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.
Files changed (40) hide show
  1. {pyield-0.10.0 → pyield-0.10.2}/PKG-INFO +55 -45
  2. {pyield-0.10.0 → pyield-0.10.2}/README.md +54 -44
  3. pyield-0.10.2/pyield/__about__.py +1 -0
  4. pyield-0.10.2/pyield/__init__.py +18 -0
  5. {pyield-0.10.0 → pyield-0.10.2}/pyield/bday.py +43 -48
  6. pyield-0.10.2/pyield/bonds/lft.py +77 -0
  7. {pyield-0.10.0 → pyield-0.10.2}/pyield/bonds/ltn.py +36 -9
  8. {pyield-0.10.0 → pyield-0.10.2}/pyield/bonds/ntnb.py +60 -39
  9. {pyield-0.10.0 → pyield-0.10.2}/pyield/bonds/ntnf.py +50 -23
  10. pyield-0.10.0/pyield/spreads.py → pyield-0.10.2/pyield/bonds/utils.py +33 -42
  11. pyield-0.10.0/pyield/date_validator.py → pyield-0.10.2/pyield/date_converter.py +6 -8
  12. pyield-0.10.2/pyield/fetchers/__init__.py +6 -0
  13. pyield-0.10.0/pyield/fetchers/anbima.py → pyield-0.10.2/pyield/fetchers/anbima_data.py +4 -4
  14. {pyield-0.10.0/pyield/fetchers/futures → pyield-0.10.2/pyield/fetchers/futures_data}/__init__.py +8 -9
  15. {pyield-0.10.0/pyield/fetchers/futures → pyield-0.10.2/pyield/fetchers/futures_data}/intraday.py +3 -4
  16. {pyield-0.10.0 → pyield-0.10.2}/pyield/fetchers/indicators.py +10 -13
  17. {pyield-0.10.0 → pyield-0.10.2}/pyproject.toml +1 -1
  18. pyield-0.10.2/tests/test_bday.py +57 -0
  19. {pyield-0.10.0 → pyield-0.10.2}/tests/test_futures_xml.py +2 -2
  20. pyield-0.10.0/pyield/__about__.py +0 -1
  21. pyield-0.10.0/pyield/__init__.py +0 -22
  22. pyield-0.10.0/pyield/bonds/utils.py +0 -36
  23. pyield-0.10.0/tests/__init__.py +0 -0
  24. pyield-0.10.0/tests/test_bday.py +0 -45
  25. {pyield-0.10.0 → pyield-0.10.2}/LICENSE +0 -0
  26. {pyield-0.10.0 → pyield-0.10.2}/pyield/bonds/__init__.py +0 -0
  27. {pyield-0.10.0/pyield/fetchers/futures → pyield-0.10.2/pyield/fetchers/futures_data}/common.py +0 -0
  28. {pyield-0.10.0/pyield/fetchers/futures → pyield-0.10.2/pyield/fetchers/futures_data}/historical.py +0 -0
  29. {pyield-0.10.0/pyield/fetchers/futures → pyield-0.10.2/pyield/fetchers/futures_data}/xml.py +0 -0
  30. {pyield-0.10.0 → pyield-0.10.2}/pyield/fetchers/projections.py +0 -0
  31. {pyield-0.10.0 → pyield-0.10.2}/pyield/holidays/__init__.py +0 -0
  32. {pyield-0.10.0 → pyield-0.10.2}/pyield/holidays/br_holidays_new.txt +0 -0
  33. {pyield-0.10.0 → pyield-0.10.2}/pyield/holidays/br_holidays_old.txt +0 -0
  34. {pyield-0.10.0 → pyield-0.10.2}/pyield/holidays/holidays_manager.py +0 -0
  35. {pyield-0.10.0 → pyield-0.10.2}/pyield/interpolator.py +0 -0
  36. {pyield-0.10.0 → pyield-0.10.2}/pyield/py.typed +0 -0
  37. {pyield-0.10.0/pyield/fetchers → pyield-0.10.2/tests}/__init__.py +0 -0
  38. {pyield-0.10.0 → pyield-0.10.2}/tests/data/SPRD231222.zip +0 -0
  39. {pyield-0.10.0 → pyield-0.10.2}/tests/data/SPRD240426.zip +0 -0
  40. {pyield-0.10.0 → pyield-0.10.2}/tests/test_futures.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: PYield
3
- Version: 0.10.0
3
+ Version: 0.10.2
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>
@@ -43,7 +43,7 @@ Description-Content-Type: text/markdown
43
43
 
44
44
  [![PyPI version](https://img.shields.io/pypi/v/pyield.svg)](https://pypi.python.org/pypi/pyield)
45
45
  [![Made with Python](https://img.shields.io/badge/Python->=3.12-blue?logo=python&logoColor=white)](https://python.org "Go to Python homepage")
46
- [![License](https://img.shields.io/badge/License-MIT-blue)](#license)
46
+ [![License](https://img.shields.io/badge/License-MIT-blue)](https://github.com/crdcj/PYield/blob/main/LICENSE)
47
47
 
48
48
  # PYield: Brazilian Fixed Income Toolkit
49
49
 
@@ -51,6 +51,10 @@ Description-Content-Type: text/markdown
51
51
 
52
52
  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, BCB, IBGE and B3.
53
53
 
54
+ ## Documentation
55
+
56
+ For the full documentation, visit [here](https://crdcj.github.io/PYield/).
57
+
54
58
  ## Features
55
59
 
56
60
  - **Data Collection**: Automated fetching of data from ANBIMA and B3.
@@ -76,35 +80,70 @@ For production code, it is recommended to parse date strings with `pandas.to_dat
76
80
  For example:
77
81
  ```python
78
82
  import pandas as pd
79
-
80
83
  # Converting a date string to a pandas Timestamp with a specific format
81
84
  date = pd.to_datetime("2024/31/05", format="%Y/%d/%m")
82
85
  date = pd.to_datetime("05-31-2024", format="%m-%d-%Y")
83
86
  ```
87
+ ### Brazilian Treasury Bonds Tools
88
+ ```python
89
+ >>> from pyield import ntnb, ntnf, ltn
90
+
91
+ # Calculate the quote of a NTN-B bond as per ANBIMA's rules
92
+ >>> ntnb.quote("31-05-2024", "15-05-2035", 0.061490)
93
+ 99.3651
94
+ >>> ntnb.quote("31-05-2024", "15-08-2060", 0.061878)
95
+ 99.5341
96
+
97
+ # Calculate the DI Spread of a LTN bonds in a given date
98
+ >>> ntnf.di_spreads("17-07-2024")
99
+ 2025-01-01 -2.31
100
+ 2027-01-01 -1.88
101
+ 2029-01-01 -3.26
102
+ 2031-01-01 3.61
103
+ 2033-01-01 -3.12
104
+ 2035-01-01 -1.00
105
+ Name: DISpread, dtype: Float64
106
+
107
+ # Get ANBIMA's indicative rates for LTN bonds
108
+ >>> ltn.anbima_rates("17-07-2024")
109
+ 2024-10-01 0.104236
110
+ 2025-01-01 0.105400
111
+ 2025-04-01 0.107454
112
+ 2025-07-01 0.108924
113
+ 2025-10-01 0.110751
114
+ 2026-01-01 0.111753
115
+ 2026-04-01 0.112980
116
+ 2026-07-01 0.113870
117
+ 2026-10-01 0.114592
118
+ 2027-07-01 0.116090
119
+ 2028-01-01 0.117160
120
+ 2028-07-01 0.118335
121
+ 2030-01-01 0.120090
122
+ Name: IndicativeRate, dtype: Float64
123
+ ```
84
124
 
85
125
  ### Business Days Tools (Brazilian holidays are automatically considered)
86
126
  ```python
87
- >>> import pyield as yd
88
-
127
+ >>> from pyield import bday
89
128
  # Count the number of business days between two dates
90
129
  # Start date is included, end date is excluded
91
- >>> yd.bday.count(start='29-12-2023', end='02-01-2024')
130
+ >>> bday.count(start='29-12-2023', end='02-01-2024')
92
131
  1
93
132
 
94
133
  # Get the next business day after a given date (offset=1)
95
- >>> yd.bday.offset(dates="29-12-2023", offset=1)
134
+ >>> bday.offset(dates="29-12-2023", offset=1)
96
135
  Timestamp('2024-01-02 00:00:00')
97
136
 
98
137
  # Get the next business day if it is not a business day (offset=0)
99
- >>> yd.bday.offset(dates="30-12-2023", offset=0)
138
+ >>> bday.offset(dates="30-12-2023", offset=0)
100
139
  Timestamp('2024-01-02 00:00:00')
101
140
 
102
141
  # Since 29-12-2023 is a business day, it returns the same date (offset=0)
103
- >>> yd.bday.offset(dates="29-12-2023", offset=0)
142
+ >>> bday.offset(dates="29-12-2023", offset=0)
104
143
  Timestamp('2023-12-29 00:00:00')
105
144
 
106
145
  # Generate a pandas series with the business days between two dates
107
- >>> yd.bday.generate(start='29-12-2023', end='03-01-2024')
146
+ >>> bday.generate(start='29-12-2023', end='03-01-2024')
108
147
  0 2023-12-29
109
148
  1 2024-01-02
110
149
  2 2024-01-03
@@ -113,6 +152,7 @@ dtype: datetime64[ns]
113
152
 
114
153
  ### Futures Data
115
154
  ```python
155
+ >>> import pyield as yd
116
156
  # Fetch current DI Futures data from B3 (15 minutes delay)
117
157
  >>> yd.futures(contract_code="DI1")
118
158
  TradeTime TickerSymbol ExpirationDate BDaysToExp ... MaxRate LastAskRate LastBidRate CurrentRate
@@ -136,40 +176,9 @@ TradeDate TickerSymbol ExpirationDate BDaysToExp ... LastRate LastAskRate LastB
136
176
  2024-03-08 DI1F39 2039-01-03 3713 ... <NA> <NA> <NA> 10.85
137
177
  ```
138
178
 
139
- ### Treasury Bonds Data
140
- ```python
141
- # Fetch a DataFrame with the NTN-B data from ANBIMA
142
- # Anbima data is available for the last 5 working days
143
- # Obs: Anbima members have access to the full history
144
- >>> yd.anbima(bond_type='NTN-B', reference_date='12-04-2024')
145
-
146
- BondType ReferenceDate MaturityDate BidRate AskRate IndicativeRate Price
147
- NTN-B 2024-04-12 2024-08-15 0.07540 0.07504 0.07523 4,271.43565
148
- NTN-B 2024-04-12 2025-05-15 0.05945 0.05913 0.05930 4,361.34391
149
- NTN-B 2024-04-12 2026-08-15 0.05927 0.05897 0.05910 4,301.40082
150
- ... ... ... ... ... ... ...
151
- NTN-B 2024-04-12 2050-08-15 0.06039 0.06006 0.06023 4,299.28233
152
- NTN-B 2024-04-12 2055-05-15 0.06035 0.05998 0.06017 4,367.13360
153
- NTN-B 2024-04-12 2060-08-15 0.06057 0.06016 0.06036 4,292.26323
154
- ```
155
-
156
- ### Spreads Calculation
157
- ```python
158
- # Calculate the spread between two DI Futures contracts and the pre-fix bonds
159
- >>> yd.spread(spread_type="di_pre", reference_date="11-04-2024")
160
-
161
- BondType ReferenceDate MaturityDate DISpread
162
- LTN 2024-04-11 2024-07-01 -20.28
163
- LTN 2024-04-11 2024-10-01 -10.19
164
- LTN 2024-04-11 2025-01-01 -15.05
165
- ... ... ... ...
166
- NTN-F 2024-04-11 2031-01-01 -0.66
167
- NTN-F 2024-04-11 2033-01-01 -5.69
168
- NTN-F 2024-04-11 2035-01-01 -1.27
169
- ```
170
-
171
179
  ### Indicators Data
172
180
  ```python
181
+ >>> import pyield as yd
173
182
  # Fetch the SELIC target rate from the Central Bank of Brazil
174
183
  >>> yd.indicator(indicator_code="SELIC", reference_date='12-04-2024')
175
184
  0.1075 # 10.75%
@@ -185,6 +194,7 @@ None
185
194
 
186
195
  ### Projections Data
187
196
  ```python
197
+ >>> import pyield as yd
188
198
  # Fetch current month projection for IPCA from IBGE API
189
199
  >>> ipca = yd.projection(projection_code="IPCA_CM")
190
200
  >>> print(ipca)
@@ -200,20 +210,20 @@ IndicatorProjection(
200
210
 
201
211
  ### Interpolation Tools
202
212
  ```python
203
-
213
+ >>> from pyield import Interpolator
204
214
  # Interpolate interest rates for specific business days using the Interpolator class.
205
215
 
206
216
  # Initialize the Interpolator with known business days and interest rates.
207
217
  >>> known_bdays = [30, 60, 90]
208
218
  >>> known_rates = [0.045, 0.05, 0.055]
209
- >>> linear_interpolator = yd.Interpolator("linear", known_bdays, known_rates)
219
+ >>> linear_interpolator = Interpolator("linear", known_bdays, known_rates)
210
220
 
211
221
  # Interpolate the interest rate for a given number of business days.
212
222
  >>> linear_interpolator(45)
213
223
  0.0475
214
224
 
215
225
  # Use the flat forward method for interpolation.
216
- >>> ff_interpolator = yd.Interpolator("flat_forward", known_bdays, known_rates)
226
+ >>> ff_interpolator = Interpolator("flat_forward", known_bdays, known_rates)
217
227
  >>> ff_interpolator(45)
218
228
  0.04833068080970859
219
229
  ```
@@ -1,6 +1,6 @@
1
1
  [![PyPI version](https://img.shields.io/pypi/v/pyield.svg)](https://pypi.python.org/pypi/pyield)
2
2
  [![Made with Python](https://img.shields.io/badge/Python->=3.12-blue?logo=python&logoColor=white)](https://python.org "Go to Python homepage")
3
- [![License](https://img.shields.io/badge/License-MIT-blue)](#license)
3
+ [![License](https://img.shields.io/badge/License-MIT-blue)](https://github.com/crdcj/PYield/blob/main/LICENSE)
4
4
 
5
5
  # PYield: Brazilian Fixed Income Toolkit
6
6
 
@@ -8,6 +8,10 @@
8
8
 
9
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, BCB, IBGE and B3.
10
10
 
11
+ ## Documentation
12
+
13
+ For the full documentation, visit [here](https://crdcj.github.io/PYield/).
14
+
11
15
  ## Features
12
16
 
13
17
  - **Data Collection**: Automated fetching of data from ANBIMA and B3.
@@ -33,35 +37,70 @@ For production code, it is recommended to parse date strings with `pandas.to_dat
33
37
  For example:
34
38
  ```python
35
39
  import pandas as pd
36
-
37
40
  # Converting a date string to a pandas Timestamp with a specific format
38
41
  date = pd.to_datetime("2024/31/05", format="%Y/%d/%m")
39
42
  date = pd.to_datetime("05-31-2024", format="%m-%d-%Y")
40
43
  ```
44
+ ### Brazilian Treasury Bonds Tools
45
+ ```python
46
+ >>> from pyield import ntnb, ntnf, ltn
47
+
48
+ # Calculate the quote of a NTN-B bond as per ANBIMA's rules
49
+ >>> ntnb.quote("31-05-2024", "15-05-2035", 0.061490)
50
+ 99.3651
51
+ >>> ntnb.quote("31-05-2024", "15-08-2060", 0.061878)
52
+ 99.5341
53
+
54
+ # Calculate the DI Spread of a LTN bonds in a given date
55
+ >>> ntnf.di_spreads("17-07-2024")
56
+ 2025-01-01 -2.31
57
+ 2027-01-01 -1.88
58
+ 2029-01-01 -3.26
59
+ 2031-01-01 3.61
60
+ 2033-01-01 -3.12
61
+ 2035-01-01 -1.00
62
+ Name: DISpread, dtype: Float64
63
+
64
+ # Get ANBIMA's indicative rates for LTN bonds
65
+ >>> ltn.anbima_rates("17-07-2024")
66
+ 2024-10-01 0.104236
67
+ 2025-01-01 0.105400
68
+ 2025-04-01 0.107454
69
+ 2025-07-01 0.108924
70
+ 2025-10-01 0.110751
71
+ 2026-01-01 0.111753
72
+ 2026-04-01 0.112980
73
+ 2026-07-01 0.113870
74
+ 2026-10-01 0.114592
75
+ 2027-07-01 0.116090
76
+ 2028-01-01 0.117160
77
+ 2028-07-01 0.118335
78
+ 2030-01-01 0.120090
79
+ Name: IndicativeRate, dtype: Float64
80
+ ```
41
81
 
42
82
  ### Business Days Tools (Brazilian holidays are automatically considered)
43
83
  ```python
44
- >>> import pyield as yd
45
-
84
+ >>> from pyield import bday
46
85
  # Count the number of business days between two dates
47
86
  # Start date is included, end date is excluded
48
- >>> yd.bday.count(start='29-12-2023', end='02-01-2024')
87
+ >>> bday.count(start='29-12-2023', end='02-01-2024')
49
88
  1
50
89
 
51
90
  # Get the next business day after a given date (offset=1)
52
- >>> yd.bday.offset(dates="29-12-2023", offset=1)
91
+ >>> bday.offset(dates="29-12-2023", offset=1)
53
92
  Timestamp('2024-01-02 00:00:00')
54
93
 
55
94
  # Get the next business day if it is not a business day (offset=0)
56
- >>> yd.bday.offset(dates="30-12-2023", offset=0)
95
+ >>> bday.offset(dates="30-12-2023", offset=0)
57
96
  Timestamp('2024-01-02 00:00:00')
58
97
 
59
98
  # Since 29-12-2023 is a business day, it returns the same date (offset=0)
60
- >>> yd.bday.offset(dates="29-12-2023", offset=0)
99
+ >>> bday.offset(dates="29-12-2023", offset=0)
61
100
  Timestamp('2023-12-29 00:00:00')
62
101
 
63
102
  # Generate a pandas series with the business days between two dates
64
- >>> yd.bday.generate(start='29-12-2023', end='03-01-2024')
103
+ >>> bday.generate(start='29-12-2023', end='03-01-2024')
65
104
  0 2023-12-29
66
105
  1 2024-01-02
67
106
  2 2024-01-03
@@ -70,6 +109,7 @@ dtype: datetime64[ns]
70
109
 
71
110
  ### Futures Data
72
111
  ```python
112
+ >>> import pyield as yd
73
113
  # Fetch current DI Futures data from B3 (15 minutes delay)
74
114
  >>> yd.futures(contract_code="DI1")
75
115
  TradeTime TickerSymbol ExpirationDate BDaysToExp ... MaxRate LastAskRate LastBidRate CurrentRate
@@ -93,40 +133,9 @@ TradeDate TickerSymbol ExpirationDate BDaysToExp ... LastRate LastAskRate LastB
93
133
  2024-03-08 DI1F39 2039-01-03 3713 ... <NA> <NA> <NA> 10.85
94
134
  ```
95
135
 
96
- ### Treasury Bonds Data
97
- ```python
98
- # Fetch a DataFrame with the NTN-B data from ANBIMA
99
- # Anbima data is available for the last 5 working days
100
- # Obs: Anbima members have access to the full history
101
- >>> yd.anbima(bond_type='NTN-B', reference_date='12-04-2024')
102
-
103
- BondType ReferenceDate MaturityDate BidRate AskRate IndicativeRate Price
104
- NTN-B 2024-04-12 2024-08-15 0.07540 0.07504 0.07523 4,271.43565
105
- NTN-B 2024-04-12 2025-05-15 0.05945 0.05913 0.05930 4,361.34391
106
- NTN-B 2024-04-12 2026-08-15 0.05927 0.05897 0.05910 4,301.40082
107
- ... ... ... ... ... ... ...
108
- NTN-B 2024-04-12 2050-08-15 0.06039 0.06006 0.06023 4,299.28233
109
- NTN-B 2024-04-12 2055-05-15 0.06035 0.05998 0.06017 4,367.13360
110
- NTN-B 2024-04-12 2060-08-15 0.06057 0.06016 0.06036 4,292.26323
111
- ```
112
-
113
- ### Spreads Calculation
114
- ```python
115
- # Calculate the spread between two DI Futures contracts and the pre-fix bonds
116
- >>> yd.spread(spread_type="di_pre", reference_date="11-04-2024")
117
-
118
- BondType ReferenceDate MaturityDate DISpread
119
- LTN 2024-04-11 2024-07-01 -20.28
120
- LTN 2024-04-11 2024-10-01 -10.19
121
- LTN 2024-04-11 2025-01-01 -15.05
122
- ... ... ... ...
123
- NTN-F 2024-04-11 2031-01-01 -0.66
124
- NTN-F 2024-04-11 2033-01-01 -5.69
125
- NTN-F 2024-04-11 2035-01-01 -1.27
126
- ```
127
-
128
136
  ### Indicators Data
129
137
  ```python
138
+ >>> import pyield as yd
130
139
  # Fetch the SELIC target rate from the Central Bank of Brazil
131
140
  >>> yd.indicator(indicator_code="SELIC", reference_date='12-04-2024')
132
141
  0.1075 # 10.75%
@@ -142,6 +151,7 @@ None
142
151
 
143
152
  ### Projections Data
144
153
  ```python
154
+ >>> import pyield as yd
145
155
  # Fetch current month projection for IPCA from IBGE API
146
156
  >>> ipca = yd.projection(projection_code="IPCA_CM")
147
157
  >>> print(ipca)
@@ -157,20 +167,20 @@ IndicatorProjection(
157
167
 
158
168
  ### Interpolation Tools
159
169
  ```python
160
-
170
+ >>> from pyield import Interpolator
161
171
  # Interpolate interest rates for specific business days using the Interpolator class.
162
172
 
163
173
  # Initialize the Interpolator with known business days and interest rates.
164
174
  >>> known_bdays = [30, 60, 90]
165
175
  >>> known_rates = [0.045, 0.05, 0.055]
166
- >>> linear_interpolator = yd.Interpolator("linear", known_bdays, known_rates)
176
+ >>> linear_interpolator = Interpolator("linear", known_bdays, known_rates)
167
177
 
168
178
  # Interpolate the interest rate for a given number of business days.
169
179
  >>> linear_interpolator(45)
170
180
  0.0475
171
181
 
172
182
  # Use the flat forward method for interpolation.
173
- >>> ff_interpolator = yd.Interpolator("flat_forward", known_bdays, known_rates)
183
+ >>> ff_interpolator = Interpolator("flat_forward", known_bdays, known_rates)
174
184
  >>> ff_interpolator(45)
175
185
  0.04833068080970859
176
186
  ```
@@ -0,0 +1 @@
1
+ __version__ = "0.10.2"
@@ -0,0 +1,18 @@
1
+ from . import bday, interpolator
2
+ from .__about__ import __version__
3
+ from .bonds import lft, ltn, ntnb, ntnf
4
+ from .fetchers import anbima, futures, indicator, projection
5
+
6
+ __all__ = [
7
+ "__version__",
8
+ "anbima",
9
+ "bday",
10
+ "futures",
11
+ "indicator",
12
+ "interpolator",
13
+ "ntnb",
14
+ "ntnf",
15
+ "ltn",
16
+ "projection",
17
+ "lft",
18
+ ]
@@ -1,10 +1,10 @@
1
1
  from datetime import datetime
2
- from typing import Literal, overload
2
+ from typing import Any, Literal, overload
3
3
 
4
4
  import numpy as np
5
5
  import pandas as pd
6
6
 
7
- from .holidays import BrHolidays
7
+ from . import holidays
8
8
 
9
9
  SingleDateTypes = str | np.datetime64 | pd.Timestamp | datetime
10
10
  SeriesDateTypes = list | tuple | np.ndarray | pd.Series | pd.Index | pd.DatetimeIndex
@@ -12,7 +12,7 @@ SeriesDateTypes = list | tuple | np.ndarray | pd.Series | pd.Index | pd.Datetime
12
12
  TO_SERIES_TYPES = (list, tuple, np.ndarray, pd.Series, pd.Index, pd.DatetimeIndex)
13
13
 
14
14
  # Initialize the BrHolidays class
15
- br_holidays = BrHolidays()
15
+ br_holidays = holidays.BrHolidays()
16
16
 
17
17
 
18
18
  @overload
@@ -45,29 +45,6 @@ def _normalize_input_dates(
45
45
  raise ValueError("Invalid date format.")
46
46
 
47
47
 
48
- def is_business_day(date: SingleDateTypes | None = None) -> bool:
49
- """
50
- Checks if the input date is a business day.
51
-
52
- Args:
53
- date (str | np.datetime64 | pd.Timestamp | datetime, optional): The date to
54
- check. If None, the current date is used.
55
-
56
- Returns:
57
- bool: True if the input date is a business day, False otherwise.
58
-
59
- Examples:
60
- >>> yd.is_bday("2023-12-25") # Christmas
61
- False
62
- >>> yd.is_bday() # Check if today is a business day
63
- True
64
- """
65
- normalized_date = _normalize_input_dates(date)
66
- # Shift the date if it is not a business day
67
- adjusted_date = offset(normalized_date, 0)
68
- return normalized_date == adjusted_date
69
-
70
-
71
48
  def _convert_to_numpy_date(
72
49
  dates: pd.Timestamp | pd.Series,
73
50
  ) -> np.datetime64 | np.ndarray:
@@ -118,8 +95,7 @@ def offset(
118
95
  types and holiday adjustments.
119
96
 
120
97
  Args:
121
- dates (str | np.datetime64 | pd.Timestamp | datetime | list | tuple | np.ndarray
122
- | pd.Series | pd.Index | pd.DatetimeIndex, optional):
98
+ dates (SingleDateTypes | SeriesDateTypes | None, optional):
123
99
  The date(s) to offset. Can be a single date in various formats (string,
124
100
  `datetime`, `Timestamp`, etc.) or a collection of dates (list, tuple,
125
101
  `Series`, etc.). If None, the current date is used.
@@ -135,23 +111,23 @@ def offset(
135
111
  Defaults to "infer".
136
112
 
137
113
  Returns:
138
- pd.Timestamp | pd.Series: If a single date is provided, returns a single
114
+ pd.Timestamp | pd.Series: If a single date is provided, returns a single
139
115
  `Timestamp` of the offset date. If a series of dates is provided, returns a
140
116
  `Series` of offset dates.
141
117
 
142
118
  Examples:
143
119
  >>> date = "2023-12-23" # Saturday before Christmas
144
- >>> yd.offset_bdays(date, 0)
120
+ >>> bday.offset(date, 0)
145
121
  Timestamp('2023-12-26')
146
122
 
147
123
  >>> date = "2023-12-22" # Friday before Christmas
148
- >>> yd.offset_bdays(date, 0)
124
+ >>> bday.offset(date, 0)
149
125
  Timestamp('2023-12-22') # No offset because it's a business day
150
126
 
151
- >>> yd.offset_bdays(date, 1)
127
+ >>> bday.offset(date, 1)
152
128
  Timestamp('2023-12-26') # Offset to the next business day
153
129
 
154
- >>> yd.offset_bdays(date, -1)
130
+ >>> bday.offset(date, -1)
155
131
  Timestamp('2023-12-21') # Offset to the previous business day
156
132
 
157
133
  Note:
@@ -228,13 +204,11 @@ def count(
228
204
  excluding them from the business day count.
229
205
 
230
206
  Args:
231
- start (str | np.datetime64 | pd.Timestamp | datetime | list | tuple |
232
- np.ndarray | pd.Series | pd.Index | pd.DatetimeIndex, optional):
233
- The start date(s) for counting. Defaults to the current date if None.
234
- end (str | np.datetime64 | pd.Timestamp | datetime | list | tuple |
235
- np.ndarray | pd.Series | pd.Index | pd.DatetimeIndex, optional):
236
- The end date(s) for counting, which are excluded from the count themselves.
237
- Defaults to the current date if None.
207
+ start (SingleDateTypes | SeriesDateTypes | None, optional): The start date(s)
208
+ for counting. Defaults to the last business day available if None.
209
+ end (SingleDateTypes | SeriesDateTypes| None, optional): The end date(s) for
210
+ counting, which are excluded from the count themselves. Defaults to the last
211
+ business day available if None.
238
212
  holiday_list (Literal["old", "new", "infer"], optional):
239
213
  Specifies which set of holidays to consider in the count. 'old' or 'new'
240
214
  refer to predefined holiday lists, while 'infer' automatically selects the
@@ -242,8 +216,7 @@ def count(
242
216
 
243
217
  Returns:
244
218
  int | pd.Series: Returns an integer if `start` and `end` are single dates,
245
- or a Series if either or both are arrays of dates. The value(s) represent
246
- the count of business days between `start` and `end`.
219
+ or a Series if any of them is an array of dates.
247
220
 
248
221
  Notes:
249
222
  - This function is a wrapper around `numpy.busday_count`, adapted to work
@@ -257,12 +230,11 @@ def count(
257
230
  https://numpy.org/doc/stable/reference/generated/numpy.busday_count.html.
258
231
 
259
232
  Examples:
260
- >>> yd.count_bdays("2023-12-15", "2024-01-01")
233
+ >>> bday.count("2023-12-15", "2024-01-01")
261
234
  10
262
-
263
235
  >>> start = "2023-01-01"
264
236
  >>> end = pd.to_datetime(["2023-01-31", "2023-03-01"])
265
- >>> yd.count_bdays(start, end)
237
+ >>> bday.count(start, end)
266
238
  pd.Series([22, 40], dtype='int64')
267
239
  """
268
240
  normalized_start = _normalize_input_dates(start)
@@ -290,7 +262,7 @@ def generate(
290
262
  end: SingleDateTypes | None = None,
291
263
  inclusive: Literal["both", "neither", "left", "right"] = "both",
292
264
  holiday_list: Literal["old", "new", "infer"] = "infer",
293
- **kwargs,
265
+ **kwargs: dict[str, Any],
294
266
  ) -> pd.Series:
295
267
  """
296
268
  Generates a Series of business days between a `start` and `end` date, considering
@@ -298,7 +270,7 @@ def generate(
298
270
  inclusion options for start and end dates. It wraps `pandas.bdate_range`.
299
271
 
300
272
  Args:
301
- start (str | np.datetime64 | pd.Timestamp | datetime, optional):
273
+ start (SingleDateTypes | None, optional):
302
274
  The start date for generating business days. Defaults to None, using the
303
275
  current date.
304
276
  end (str | np.datetime64 | pd.Timestamp | datetime, optional):
@@ -320,7 +292,7 @@ def generate(
320
292
  start and end dates, considering the specified holidays.
321
293
 
322
294
  Examples:
323
- >>> yd.generate_bdays(start="2023-12-22", end="2024-01-02")
295
+ >>> bday.generate(start="2023-12-22", end="2024-01-02")
324
296
  pd.Series(['2023-12-22', '2023-12-26', '2023-12-27', '2023-12-28', '2023-12-29',
325
297
  '2024-01-02'], dtype='datetime64[ns]')
326
298
 
@@ -346,3 +318,26 @@ def generate(
346
318
  **kwargs,
347
319
  )
348
320
  return pd.Series(result)
321
+
322
+
323
+ def is_business_day(date: SingleDateTypes | None = None) -> bool:
324
+ """
325
+ Checks if the input date is a business day.
326
+
327
+ Args:
328
+ date (str | np.datetime64 | pd.Timestamp | datetime, optional): The date to
329
+ check. If None, the current date is used.
330
+
331
+ Returns:
332
+ bool: True if the input date is a business day, False otherwise.
333
+
334
+ Examples:
335
+ >>> yd.is_bday("2023-12-25") # Christmas
336
+ False
337
+ >>> yd.is_bday() # Check if today is a business day
338
+ True
339
+ """
340
+ normalized_date = _normalize_input_dates(date)
341
+ # Shift the date if it is not a business day
342
+ adjusted_date = offset(normalized_date, 0)
343
+ return normalized_date == adjusted_date
@@ -0,0 +1,77 @@
1
+ import pandas as pd
2
+
3
+ from .. import bday
4
+ from .. import date_converter as dc
5
+ from .. import fetchers as ft
6
+ from . import utils as ut
7
+
8
+
9
+ def anbima_data(reference_date: str | pd.Timestamp) -> pd.DataFrame:
10
+ """
11
+ Fetch LFT Anbima data for the given reference date.
12
+
13
+ Args:
14
+ reference_date (str | pd.Timestamp): The reference date for fetching the data.
15
+
16
+ Returns:
17
+ pd.DataFrame: A DataFrame containing the Anbima data for the reference date.
18
+ """
19
+ return ft.anbima(bond_type="LFT", reference_date=reference_date)
20
+
21
+
22
+ def anbima_rates(reference_date: str | pd.Timestamp) -> pd.Series:
23
+ """
24
+ Fetch LFT Anbima indicative rates for the given reference date.
25
+
26
+ Args:
27
+ reference_date (str | pd.Timestamp): The reference date for fetching the data.
28
+
29
+ Returns:
30
+ pd.Series: A Series containing the rates indexed by maturity date.
31
+ """
32
+ df = anbima_data(reference_date)
33
+ # Set MaturityDate as index
34
+ df = df.set_index("MaturityDate")
35
+ df.index.name = None
36
+ # Return as Series
37
+ return df["IndicativeRate"]
38
+
39
+
40
+ def quote(
41
+ settlement_date: str | pd.Timestamp,
42
+ maturity_date: str | pd.Timestamp,
43
+ yield_rate: float,
44
+ ) -> float:
45
+ """
46
+ Calculate the quote of a LFT bond using Anbima rules.
47
+
48
+ Args:
49
+ settlement_date (str | pd.Timestamp): The settlement date of the bond.
50
+ maturity_date (str | pd.Timestamp): The maturity date of the bond.
51
+ yield_rate (float): The annualized yield of the bond
52
+
53
+ Returns:
54
+ float: The quote of the bond.
55
+
56
+ Examples:
57
+ Calculate the quote of a LFT bond with a 0.02 yield rate:
58
+ >>> lft.quote(
59
+ ... settlement_date="24-07-2024",
60
+ ... maturity_date="01-09-2030",
61
+ ... yield_rate=0.001717, # 0.1717%
62
+ ... )
63
+ 98.9645
64
+ """
65
+ # Validate and normalize dates
66
+ settlement_date = dc.convert_date(settlement_date)
67
+ maturity_date = dc.convert_date(maturity_date)
68
+
69
+ # The number of bdays between settlement (inclusive) and the maturity (exclusive)
70
+ bdays = bday.count(settlement_date, maturity_date)
71
+
72
+ # Calculate the number of periods truncated as per Anbima rules
73
+ num_of_years = ut.truncate(bdays / 252, 14)
74
+
75
+ discount_factor = 1 / (1 + yield_rate) ** num_of_years
76
+
77
+ return ut.truncate(100 * discount_factor, 4)