PYield 0.40.6__tar.gz → 0.40.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyield-0.40.6 → pyield-0.40.7}/.gitignore +0 -1
- {pyield-0.40.6 → pyield-0.40.7}/PKG-INFO +17 -21
- {pyield-0.40.6 → pyield-0.40.7}/README.md +16 -20
- pyield-0.40.7/pyield/__about__.py +1 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/b3/di1.py +8 -6
- {pyield-0.40.6 → pyield-0.40.7}/pyield/interpolator.py +54 -10
- {pyield-0.40.6 → pyield-0.40.7}/pyield/tn/auctions.py +29 -28
- {pyield-0.40.6 → pyield-0.40.7}/pyield/tn/ntnb.py +39 -26
- {pyield-0.40.6 → pyield-0.40.7}/pyield/tn/ntnf.py +9 -13
- pyield-0.40.6/pyield/__about__.py +0 -1
- {pyield-0.40.6 → pyield-0.40.7}/LICENSE +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/__init__.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/anbima/__init__.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/anbima/difusao.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/anbima/ettj_intraday.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/anbima/ettj_last.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/anbima/ima.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/anbima/imaq.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/anbima/tpf.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/b3/__init__.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/b3/di_over.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/b3/futures/__init__.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/b3/futures/common.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/b3/futures/historical.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/b3/futures/intraday.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/b3/futures/xml.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bc/__init__.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bc/auction.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bc/ptax_api.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bc/rates.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bc/repo.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bc/trades_intraday.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bc/trades_monthly.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bc/vna.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bday/__init__.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bday/core.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bday/holidays/__init__.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bday/holidays/br_holidays_new.txt +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bday/holidays/br_holidays_old.txt +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/bday/holidays/brholidays.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/config.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/conftest.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/converters.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/data_cache.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/fwd.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/ipca/__init__.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/ipca/historical.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/ipca/projected.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/py.typed +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/retry.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/tn/__init__.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/tn/benchmark.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/tn/lft.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/tn/ltn.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/tn/ntnc.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/tn/pre.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/tn/tools.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyield/types.py +0 -0
- {pyield-0.40.6 → pyield-0.40.7}/pyproject.toml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: PYield
|
|
3
|
-
Version: 0.40.
|
|
3
|
+
Version: 0.40.7
|
|
4
4
|
Summary: A Python library for analysis of fixed income instruments in Brazil
|
|
5
5
|
Project-URL: Source, https://github.com/crdcj/PYield
|
|
6
6
|
Author-email: Carlos Carvalho <cr.cj@outlook.com>
|
|
@@ -46,7 +46,21 @@ Description-Content-Type: text/markdown
|
|
|
46
46
|
|
|
47
47
|
# PYield: Brazilian Fixed Income Toolkit
|
|
48
48
|
|
|
49
|
-
PYield is a Python library designed for the analysis of Brazilian fixed income instruments. Leveraging the power of
|
|
49
|
+
PYield is a Python library designed for the analysis of Brazilian fixed income instruments. Leveraging the power of Polars and Requests, PYield simplifies the process of obtaining and processing data from key sources such as ANBIMA, BCB, IBGE and B3.
|
|
50
|
+
|
|
51
|
+
## Examples & Quickstart
|
|
52
|
+
|
|
53
|
+
A quickstart notebook is available in the examples/ directory and can be opened directly in Google Colab:
|
|
54
|
+
|
|
55
|
+
[](https://colab.research.google.com/github/crdcj/PYield/blob/main/examples/pyield_quickstart.ipynb)
|
|
56
|
+
|
|
57
|
+
It demonstrates:
|
|
58
|
+
|
|
59
|
+
- Package installation.
|
|
60
|
+
- Basic use of the business day calendar (bday.count, bday.offset).
|
|
61
|
+
- DI futures query (futures).
|
|
62
|
+
|
|
63
|
+
More examples may be added later (treasury bonds, curve interpolation, inflation). Contributions are welcome.
|
|
50
64
|
|
|
51
65
|
---
|
|
52
66
|
### ✅ Polars migration from version 0.40.0 onwards:
|
|
@@ -74,10 +88,9 @@ Visit the [full documentation for PYield](https://crdcj.github.io/PYield/).
|
|
|
74
88
|
|
|
75
89
|
## Overview & Navigation
|
|
76
90
|
|
|
77
|
-
PYield is split into focused namespaces. Each function and class has a rich docstring (the online site is generated from them via MkDocs). The quickest learning path is
|
|
91
|
+
PYield is split into focused namespaces. Each function and class has a rich docstring (the online site is generated from them via MkDocs). The quickest learning path is accessing the documentation and following the function/class examples verbatim.
|
|
78
92
|
|
|
79
93
|
### Top-Level Namespace (Cheat Sheet)
|
|
80
|
-
Symbols exported directly by `pyield.__all__` (available via `from pyield import ...`). Prefer these first:
|
|
81
94
|
|
|
82
95
|
| Symbol | Kind | Purpose |
|
|
83
96
|
|--------|------|---------|
|
|
@@ -98,14 +111,6 @@ Symbols exported directly by `pyield.__all__` (available via `from pyield import
|
|
|
98
111
|
| `pre` | module | Zero-coupon (pré-fixado) helpers. |
|
|
99
112
|
| `ipca` | module | Inflation historical & projections. |
|
|
100
113
|
|
|
101
|
-
|
|
102
|
-
### Learning Path
|
|
103
|
-
1. Install: `pip install pyield`.
|
|
104
|
-
2. Choose a domain: e.g. bonds (`from pyield import ntnb`).
|
|
105
|
-
3. Inspect: `dir(ntnb)` then `help(ntnb.quotation)`.
|
|
106
|
-
4. Reproduce the docstring example verbatim.
|
|
107
|
-
5. Chain outputs (Polars DataFrames / Series) into your pipeline; convert to pandas only if unavoidable.
|
|
108
|
-
|
|
109
114
|
### Minimal Quick Start
|
|
110
115
|
```python
|
|
111
116
|
from pyield import bday, ntnb, bc
|
|
@@ -122,15 +127,6 @@ selic = bc.selic_over("31-05-2024")
|
|
|
122
127
|
print(business_days, q, selic)
|
|
123
128
|
```
|
|
124
129
|
|
|
125
|
-
### Docstrings = Source of Truth
|
|
126
|
-
If any README description conflicts with behavior, prefer the docstring. It documents:
|
|
127
|
-
* Parameters & types
|
|
128
|
-
* Edge cases / null semantics
|
|
129
|
-
* References (e.g. ANBIMA methodology PDFs)
|
|
130
|
-
* Examples guaranteed to run (CI checks)
|
|
131
|
-
|
|
132
|
-
See also: [Custom Types & Data Handling](#custom-types--data-handling) for input normalization rules.
|
|
133
|
-
|
|
134
130
|
## Installation
|
|
135
131
|
|
|
136
132
|
You can install PYield using pip:
|
|
@@ -4,7 +4,21 @@
|
|
|
4
4
|
|
|
5
5
|
# PYield: Brazilian Fixed Income Toolkit
|
|
6
6
|
|
|
7
|
-
PYield is a Python library designed for the analysis of Brazilian fixed income instruments. Leveraging the power of
|
|
7
|
+
PYield is a Python library designed for the analysis of Brazilian fixed income instruments. Leveraging the power of Polars and Requests, PYield simplifies the process of obtaining and processing data from key sources such as ANBIMA, BCB, IBGE and B3.
|
|
8
|
+
|
|
9
|
+
## Examples & Quickstart
|
|
10
|
+
|
|
11
|
+
A quickstart notebook is available in the examples/ directory and can be opened directly in Google Colab:
|
|
12
|
+
|
|
13
|
+
[](https://colab.research.google.com/github/crdcj/PYield/blob/main/examples/pyield_quickstart.ipynb)
|
|
14
|
+
|
|
15
|
+
It demonstrates:
|
|
16
|
+
|
|
17
|
+
- Package installation.
|
|
18
|
+
- Basic use of the business day calendar (bday.count, bday.offset).
|
|
19
|
+
- DI futures query (futures).
|
|
20
|
+
|
|
21
|
+
More examples may be added later (treasury bonds, curve interpolation, inflation). Contributions are welcome.
|
|
8
22
|
|
|
9
23
|
---
|
|
10
24
|
### ✅ Polars migration from version 0.40.0 onwards:
|
|
@@ -32,10 +46,9 @@ Visit the [full documentation for PYield](https://crdcj.github.io/PYield/).
|
|
|
32
46
|
|
|
33
47
|
## Overview & Navigation
|
|
34
48
|
|
|
35
|
-
PYield is split into focused namespaces. Each function and class has a rich docstring (the online site is generated from them via MkDocs). The quickest learning path is
|
|
49
|
+
PYield is split into focused namespaces. Each function and class has a rich docstring (the online site is generated from them via MkDocs). The quickest learning path is accessing the documentation and following the function/class examples verbatim.
|
|
36
50
|
|
|
37
51
|
### Top-Level Namespace (Cheat Sheet)
|
|
38
|
-
Symbols exported directly by `pyield.__all__` (available via `from pyield import ...`). Prefer these first:
|
|
39
52
|
|
|
40
53
|
| Symbol | Kind | Purpose |
|
|
41
54
|
|--------|------|---------|
|
|
@@ -56,14 +69,6 @@ Symbols exported directly by `pyield.__all__` (available via `from pyield import
|
|
|
56
69
|
| `pre` | module | Zero-coupon (pré-fixado) helpers. |
|
|
57
70
|
| `ipca` | module | Inflation historical & projections. |
|
|
58
71
|
|
|
59
|
-
|
|
60
|
-
### Learning Path
|
|
61
|
-
1. Install: `pip install pyield`.
|
|
62
|
-
2. Choose a domain: e.g. bonds (`from pyield import ntnb`).
|
|
63
|
-
3. Inspect: `dir(ntnb)` then `help(ntnb.quotation)`.
|
|
64
|
-
4. Reproduce the docstring example verbatim.
|
|
65
|
-
5. Chain outputs (Polars DataFrames / Series) into your pipeline; convert to pandas only if unavoidable.
|
|
66
|
-
|
|
67
72
|
### Minimal Quick Start
|
|
68
73
|
```python
|
|
69
74
|
from pyield import bday, ntnb, bc
|
|
@@ -80,15 +85,6 @@ selic = bc.selic_over("31-05-2024")
|
|
|
80
85
|
print(business_days, q, selic)
|
|
81
86
|
```
|
|
82
87
|
|
|
83
|
-
### Docstrings = Source of Truth
|
|
84
|
-
If any README description conflicts with behavior, prefer the docstring. It documents:
|
|
85
|
-
* Parameters & types
|
|
86
|
-
* Edge cases / null semantics
|
|
87
|
-
* References (e.g. ANBIMA methodology PDFs)
|
|
88
|
-
* Examples guaranteed to run (CI checks)
|
|
89
|
-
|
|
90
|
-
See also: [Custom Types & Data Handling](#custom-types--data-handling) for input normalization rules.
|
|
91
|
-
|
|
92
88
|
## Installation
|
|
93
89
|
|
|
94
90
|
You can install PYield using pip:
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.40.7"
|
|
@@ -132,8 +132,8 @@ def data(
|
|
|
132
132
|
.filter(pl.col("BondType").is_in(["LTN", "NTN-F"]))
|
|
133
133
|
.unique(subset=["ReferenceDate", "MaturityDate"])
|
|
134
134
|
.select(
|
|
135
|
-
pl.col("ReferenceDate")
|
|
136
|
-
pl.col("MaturityDate")
|
|
135
|
+
TradeDate=pl.col("ReferenceDate"),
|
|
136
|
+
ExpirationDate=pl.col("MaturityDate"),
|
|
137
137
|
)
|
|
138
138
|
)
|
|
139
139
|
|
|
@@ -253,7 +253,7 @@ def interpolate_rates(
|
|
|
253
253
|
0.13881
|
|
254
254
|
]
|
|
255
255
|
|
|
256
|
-
>>> # With extrapolation set to False, the second rate will be
|
|
256
|
+
>>> # With extrapolation set to False, the second rate will be null
|
|
257
257
|
>>> # Note: 0.13576348733268917 is shown as 0.135763
|
|
258
258
|
>>> di1.interpolate_rates(
|
|
259
259
|
... dates="25-04-2025",
|
|
@@ -264,7 +264,7 @@ def interpolate_rates(
|
|
|
264
264
|
Series: 'FlatFwdRate' [f64]
|
|
265
265
|
[
|
|
266
266
|
0.135763
|
|
267
|
-
|
|
267
|
+
null
|
|
268
268
|
]
|
|
269
269
|
|
|
270
270
|
Raises:
|
|
@@ -288,6 +288,8 @@ def interpolate_rates(
|
|
|
288
288
|
return pl.Series(dtype=pl.Float64)
|
|
289
289
|
|
|
290
290
|
s_bdays = bday.count(dfi["TradeDate"], dfi["ExpirationDate"])
|
|
291
|
+
|
|
292
|
+
# Inicializa FlatFwdRate como None
|
|
291
293
|
dfi = dfi.with_columns(BDaysToExp=s_bdays, FlatFwdRate=None)
|
|
292
294
|
|
|
293
295
|
# Load DI rates dataset filtered by the provided reference dates
|
|
@@ -321,8 +323,8 @@ def interpolate_rates(
|
|
|
321
323
|
.alias("FlatFwdRate")
|
|
322
324
|
)
|
|
323
325
|
|
|
324
|
-
# Return the
|
|
325
|
-
return dfi.get_column("FlatFwdRate")
|
|
326
|
+
# Return the interpolated rates with nulls where interpolation was not possible
|
|
327
|
+
return dfi.get_column("FlatFwdRate").fill_nan(None)
|
|
326
328
|
|
|
327
329
|
|
|
328
330
|
def interpolate_rate(
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import bisect
|
|
2
|
-
from typing import Literal
|
|
2
|
+
from typing import Literal, overload
|
|
3
3
|
|
|
4
4
|
import polars as pl
|
|
5
5
|
|
|
@@ -32,21 +32,39 @@ class Interpolator:
|
|
|
32
32
|
>>> known_bdays = [30, 60, 90]
|
|
33
33
|
>>> known_rates = [0.045, 0.05, 0.055]
|
|
34
34
|
|
|
35
|
-
Linear interpolation
|
|
35
|
+
Linear interpolation:
|
|
36
36
|
>>> linear = Interpolator("linear", known_bdays, known_rates)
|
|
37
37
|
>>> linear(45)
|
|
38
38
|
0.0475
|
|
39
39
|
|
|
40
|
-
Flat forward interpolation
|
|
40
|
+
Flat forward interpolation:
|
|
41
41
|
>>> fforward = Interpolator("flat_forward", known_bdays, known_rates)
|
|
42
42
|
>>> fforward(45)
|
|
43
43
|
0.04833068080970859
|
|
44
44
|
|
|
45
|
+
Array interpolation (polars shows 6 decimal places by default):
|
|
46
|
+
>>> fforward([15, 45, 75, 100])
|
|
47
|
+
shape: (4,)
|
|
48
|
+
Series: 'interpolated_rate' [f64]
|
|
49
|
+
[
|
|
50
|
+
0.045
|
|
51
|
+
0.048331
|
|
52
|
+
0.052997
|
|
53
|
+
null
|
|
54
|
+
]
|
|
55
|
+
|
|
45
56
|
>>> print(fforward(100)) # Extrapolation disabled by default
|
|
46
57
|
nan
|
|
47
58
|
|
|
48
|
-
>>> print(fforward(-10))
|
|
59
|
+
>>> print(fforward(-10)) # Invalid input returns NaN
|
|
49
60
|
nan
|
|
61
|
+
|
|
62
|
+
If extrapolation is enabled, the last known rate is used:
|
|
63
|
+
>>> fforward_extrap = Interpolator(
|
|
64
|
+
... "flat_forward", known_bdays, known_rates, extrapolate=True
|
|
65
|
+
... )
|
|
66
|
+
>>> print(fforward_extrap(100))
|
|
67
|
+
0.055
|
|
50
68
|
"""
|
|
51
69
|
|
|
52
70
|
def __init__(
|
|
@@ -152,18 +170,44 @@ class Interpolator:
|
|
|
152
170
|
f_t = (time - time_j) / (time_k - time_j)
|
|
153
171
|
return (f_j * (f_k / f_j) ** f_t) ** (1 / time) - 1
|
|
154
172
|
|
|
155
|
-
|
|
173
|
+
@overload
|
|
174
|
+
def interpolate(self, bdays: int) -> float: ...
|
|
175
|
+
@overload
|
|
176
|
+
def interpolate(self, bdays: ArrayLike) -> pl.Series: ...
|
|
177
|
+
|
|
178
|
+
def interpolate(self, bdays: int | ArrayLike) -> float | pl.Series:
|
|
179
|
+
"""
|
|
180
|
+
Interpolates rates for given business day(s).
|
|
181
|
+
|
|
182
|
+
Args:
|
|
183
|
+
bdays: int or ArrayLike - Business day(s) for interpolation
|
|
184
|
+
|
|
185
|
+
Returns:
|
|
186
|
+
float or pl.Series - Interpolated rate(s)
|
|
187
|
+
"""
|
|
188
|
+
if hasattr(bdays, "__len__"):
|
|
189
|
+
bday_series = pl.Series(
|
|
190
|
+
name="interpolated_rate", values=bdays, dtype=pl.Int64
|
|
191
|
+
)
|
|
192
|
+
result = bday_series.map_elements(
|
|
193
|
+
self._interpolated_rate, return_dtype=pl.Float64
|
|
194
|
+
)
|
|
195
|
+
return result.fill_nan(None)
|
|
196
|
+
else:
|
|
197
|
+
return self._interpolated_rate(bdays)
|
|
198
|
+
|
|
199
|
+
def _interpolated_rate(self, bday: int) -> float:
|
|
156
200
|
"""
|
|
157
201
|
Finds the appropriate interpolation point and returns the interest rate
|
|
158
202
|
interpolated by the specified method from that point.
|
|
159
203
|
|
|
160
204
|
Args:
|
|
161
|
-
bday (int): Number of business days for which the interest rate
|
|
162
|
-
|
|
205
|
+
bday (int): Number of business days for which the interest rate
|
|
206
|
+
is to be calculated.
|
|
163
207
|
|
|
164
208
|
Returns:
|
|
165
|
-
float: The interest rate interpolated by the specified method
|
|
166
|
-
the given number of business days. If the input is out of range and
|
|
209
|
+
float: The interest rate interpolated by the specified method
|
|
210
|
+
for the given number of business days. If the input is out of range and
|
|
167
211
|
extrapolation is disabled, returns float("nan").
|
|
168
212
|
"""
|
|
169
213
|
# Validate input
|
|
@@ -197,7 +241,7 @@ class Interpolator:
|
|
|
197
241
|
|
|
198
242
|
raise ValueError(f"Interpolation method '{method}' not recognized.")
|
|
199
243
|
|
|
200
|
-
def __call__(self, bday: int) -> float:
|
|
244
|
+
def __call__(self, bday: int | ArrayLike) -> float | pl.Series:
|
|
201
245
|
"""
|
|
202
246
|
Allows the instance to be called as a function to perform interpolation.
|
|
203
247
|
|
|
@@ -132,30 +132,32 @@ def _transform_raw_data(raw_data: list[dict]) -> pl.DataFrame:
|
|
|
132
132
|
# Conversão de datas
|
|
133
133
|
cs.starts_with("data_").str.strptime(pl.Date, "%d/%m/%Y"),
|
|
134
134
|
# Cálculos de totais
|
|
135
|
-
|
|
136
|
-
"
|
|
135
|
+
quantidade_ofertada_total=(
|
|
136
|
+
pl.sum_horizontal("quantidade_ofertada_1v", "quantidade_ofertada_2v")
|
|
137
137
|
),
|
|
138
|
-
|
|
139
|
-
"
|
|
138
|
+
quantidade_aceita_total=(
|
|
139
|
+
pl.sum_horizontal("quantidade_aceita_1v", "quantidade_aceita_2v")
|
|
140
140
|
),
|
|
141
|
-
|
|
142
|
-
"
|
|
141
|
+
financeiro_aceito_total=(
|
|
142
|
+
pl.sum_horizontal("financeiro_aceito_1v", "financeiro_aceito_2v")
|
|
143
143
|
),
|
|
144
144
|
# Cálculo do financeiro ofertado
|
|
145
|
-
pl.when(
|
|
145
|
+
financeiro_ofertado_1v=pl.when(
|
|
146
|
+
pl.col("quantidade_ofertada_1v") == pl.col("quantidade_aceita_1v")
|
|
147
|
+
)
|
|
146
148
|
.then(pl.col("financeiro_aceito_1v"))
|
|
147
|
-
.otherwise(
|
|
148
|
-
.
|
|
149
|
-
|
|
149
|
+
.otherwise(pl.col("quantidade_ofertada_1v") * pl.col("pu_medio")),
|
|
150
|
+
financeiro_ofertado_2v=pl.when(
|
|
151
|
+
pl.col("quantidade_ofertada_2v") == pl.col("quantidade_aceita_2v")
|
|
152
|
+
)
|
|
150
153
|
.then(pl.col("financeiro_aceito_2v"))
|
|
151
|
-
.otherwise(
|
|
152
|
-
.alias("financeiro_ofertado_2v"),
|
|
154
|
+
.otherwise(pl.col("quantidade_ofertada_2v") * pl.col("pu_medio")),
|
|
153
155
|
# Cálculo das taxas de colocação
|
|
154
|
-
(
|
|
155
|
-
"
|
|
156
|
+
colocacao_1v=(
|
|
157
|
+
pl.col("quantidade_aceita_1v") / pl.col("quantidade_ofertada_1v")
|
|
156
158
|
),
|
|
157
|
-
(
|
|
158
|
-
"
|
|
159
|
+
colocacao_2v=(
|
|
160
|
+
pl.col("quantidade_aceita_2v") / pl.col("quantidade_ofertada_2v")
|
|
159
161
|
),
|
|
160
162
|
# Deixar um marcador de que o pu_medio não é original
|
|
161
163
|
tipo_pu_medio=pl.when(pl.col("pu_medio") == 0)
|
|
@@ -164,22 +166,21 @@ def _transform_raw_data(raw_data: list[dict]) -> pl.DataFrame:
|
|
|
164
166
|
)
|
|
165
167
|
.with_columns(
|
|
166
168
|
# Cálculo do financeiro ofertado
|
|
167
|
-
|
|
168
|
-
"
|
|
169
|
+
financeiro_ofertado_total=(
|
|
170
|
+
pl.sum_horizontal("financeiro_ofertado_1v", "financeiro_ofertado_2v")
|
|
169
171
|
),
|
|
170
|
-
(
|
|
172
|
+
colocacao_total=(
|
|
171
173
|
pl.col("quantidade_aceita_total") / pl.col("quantidade_ofertada_total")
|
|
172
|
-
)
|
|
174
|
+
),
|
|
173
175
|
# Algumas vezes o prazo não vem na API, então calculamos
|
|
174
|
-
(
|
|
175
|
-
|
|
176
|
-
.
|
|
176
|
+
dias_corridos=(
|
|
177
|
+
pl.col("data_vencimento") - pl.col("data_liquidacao_1v")
|
|
178
|
+
).dt.total_days(),
|
|
177
179
|
# Algumas vezes o PU médio vem zero da API, então recalculamos
|
|
178
|
-
pl.when(pl.col("pu_medio") == 0)
|
|
180
|
+
pu_medio=pl.when(pl.col("pu_medio") == 0)
|
|
179
181
|
.then((pl.col("financeiro_aceito_1v") / pl.col("quantidade_aceita_1v")))
|
|
180
182
|
.otherwise(pl.col("pu_medio"))
|
|
181
|
-
.round(6)
|
|
182
|
-
.alias("pu_medio"),
|
|
183
|
+
.round(6),
|
|
183
184
|
)
|
|
184
185
|
.with_columns(
|
|
185
186
|
# Arredondamentos e transformações que criam/alteram colunas sem condicional
|
|
@@ -205,7 +206,7 @@ def _transform_raw_data(raw_data: list[dict]) -> pl.DataFrame:
|
|
|
205
206
|
# Cálculo de dias úteis (requer acesso a colunas já convertidas)
|
|
206
207
|
dias_uteis = bday.count(df["data_liquidacao_1v"], df["data_vencimento"])
|
|
207
208
|
df = df.with_columns(dias_uteis.alias("dias_uteis"))
|
|
208
|
-
return df.sort(
|
|
209
|
+
return df.sort("data_1v", "titulo", "data_vencimento")
|
|
209
210
|
|
|
210
211
|
|
|
211
212
|
def _add_duration(df: pl.DataFrame) -> pl.DataFrame:
|
|
@@ -300,7 +301,7 @@ def _add_dv01_usd(df: pl.DataFrame) -> pl.DataFrame:
|
|
|
300
301
|
"""
|
|
301
302
|
Adiciona o DV01 em USD usando um join_asof para encontrar a PTAX mais recente.
|
|
302
303
|
"""
|
|
303
|
-
auction_date = df
|
|
304
|
+
auction_date = df["data_1v"].first()
|
|
304
305
|
# Busca o DataFrame da PTAX
|
|
305
306
|
df_ptax = _fetch_ptax_data(auction_date=auction_date)
|
|
306
307
|
if df_ptax.is_empty():
|
|
@@ -376,18 +376,23 @@ def _create_bootstrap_dataframe(
|
|
|
376
376
|
# Generate coupon dates up to the longest maturity date
|
|
377
377
|
all_coupon_dates = _generate_all_coupon_dates(settlement, maturities.max())
|
|
378
378
|
bdays_to_mat = bday.count(settlement, all_coupon_dates)
|
|
379
|
+
ytm_rates = flat_fwd.interpolate(bdays_to_mat)
|
|
379
380
|
|
|
380
381
|
df = (
|
|
381
|
-
pl.DataFrame(
|
|
382
|
+
pl.DataFrame(
|
|
383
|
+
{
|
|
384
|
+
"MaturityDate": all_coupon_dates,
|
|
385
|
+
"BDToMat": bdays_to_mat,
|
|
386
|
+
"BYears": bdays_to_mat / 252,
|
|
387
|
+
"YTM": ytm_rates,
|
|
388
|
+
}
|
|
389
|
+
)
|
|
382
390
|
.with_columns(
|
|
383
|
-
|
|
384
|
-
YTM=pl.col("BDToMat").map_elements(flat_fwd, return_dtype=pl.Float64),
|
|
385
|
-
Coupon=COUPON_PMT,
|
|
391
|
+
Coupon=pl.lit(COUPON_PMT),
|
|
386
392
|
SpotRate=pl.lit(None, dtype=pl.Float64),
|
|
387
393
|
)
|
|
388
394
|
.sort("MaturityDate")
|
|
389
395
|
)
|
|
390
|
-
|
|
391
396
|
return df
|
|
392
397
|
|
|
393
398
|
|
|
@@ -599,20 +604,17 @@ def bei_rates(
|
|
|
599
604
|
settlement = cv.convert_dates(settlement)
|
|
600
605
|
ntnb_maturities = cv.convert_dates(ntnb_maturities)
|
|
601
606
|
|
|
602
|
-
|
|
607
|
+
ff_interpolator = ip.Interpolator(
|
|
603
608
|
method="flat_forward",
|
|
604
609
|
known_bdays=bday.count(settlement, nominal_maturities),
|
|
605
610
|
known_rates=nominal_rates,
|
|
606
611
|
extrapolate=True,
|
|
607
612
|
)
|
|
608
|
-
|
|
613
|
+
df_spot = spot_rates(settlement, ntnb_maturities, ntnb_rates)
|
|
609
614
|
df = (
|
|
610
|
-
|
|
611
|
-
.rename({"SpotRate": "RIR"})
|
|
615
|
+
df_spot.rename({"SpotRate": "RIR"})
|
|
612
616
|
.with_columns(
|
|
613
|
-
NIR=
|
|
614
|
-
nir_interpolator, return_dtype=pl.Float64
|
|
615
|
-
)
|
|
617
|
+
NIR=ff_interpolator(df_spot["BDToMat"]),
|
|
616
618
|
)
|
|
617
619
|
.with_columns(
|
|
618
620
|
BEI=((pl.col("NIR") + 1) / (pl.col("RIR") + 1)) - 1,
|
|
@@ -629,20 +631,31 @@ def duration(
|
|
|
629
631
|
rate: float,
|
|
630
632
|
) -> float:
|
|
631
633
|
"""
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
634
|
+
Calculate the Macaulay duration of the NTN-B bond in business years.
|
|
635
|
+
|
|
636
|
+
Formula:
|
|
637
|
+
Sum( t * CFₜ / (1 + y)ᵗ )
|
|
638
|
+
MacD = ---------------------------------
|
|
639
|
+
Current Bond Price
|
|
640
|
+
|
|
641
|
+
Where:
|
|
642
|
+
t = time in years until payment
|
|
643
|
+
CFₜ = cash flow at time t
|
|
644
|
+
y = yield to maturity (periodic)
|
|
645
|
+
Price = Sum( CFₜ / (1 + y)ᵗ )
|
|
646
|
+
|
|
647
|
+
Args:
|
|
648
|
+
settlement (DateLike): The settlement date of the operation.
|
|
649
|
+
maturity (DateLike): The maturity date of the NTN-B bond.
|
|
650
|
+
rate (float): The discount rate used to calculate the duration.
|
|
651
|
+
|
|
652
|
+
Returns:
|
|
653
|
+
float: The Macaulay duration of the NTN-B bond in business years.
|
|
654
|
+
|
|
655
|
+
Examples:
|
|
656
|
+
>>> from pyield import ntnb
|
|
657
|
+
>>> ntnb.duration("23-08-2024", "15-08-2060", 0.061005)
|
|
658
|
+
15.08305431313046
|
|
646
659
|
"""
|
|
647
660
|
df = cash_flows(settlement, maturity)
|
|
648
661
|
if df.is_empty():
|
|
@@ -340,17 +340,16 @@ def spot_rates( # noqa
|
|
|
340
340
|
|
|
341
341
|
# 4. Construir DataFrame inicial
|
|
342
342
|
bdays_to_mat = bday.count(settlement, all_coupon_dates)
|
|
343
|
+
ytm_rates = ntnf_rate_interpolator(bdays_to_mat)
|
|
343
344
|
df = pl.DataFrame(
|
|
344
345
|
{
|
|
345
346
|
"MaturityDate": all_coupon_dates,
|
|
346
347
|
"BDToMat": bdays_to_mat,
|
|
348
|
+
"BYears": bdays_to_mat / 252,
|
|
349
|
+
"YTM": ytm_rates,
|
|
347
350
|
}
|
|
348
351
|
).with_columns(
|
|
349
|
-
|
|
350
|
-
Coupon=COUPON_PMT,
|
|
351
|
-
YTM=pl.col("BDToMat").map_elements(
|
|
352
|
-
ntnf_rate_interpolator, return_dtype=pl.Float64
|
|
353
|
-
),
|
|
352
|
+
Coupon=pl.lit(COUPON_PMT),
|
|
354
353
|
)
|
|
355
354
|
|
|
356
355
|
# 5. Loop de bootstrap (iterativo por dependência sequencial)
|
|
@@ -601,9 +600,10 @@ def premium( # noqa
|
|
|
601
600
|
)
|
|
602
601
|
|
|
603
602
|
bdays_to_payments = bday.count(settlement, df_cf["PaymentDate"])
|
|
604
|
-
df = df_cf.with_columns(
|
|
605
|
-
|
|
606
|
-
|
|
603
|
+
df = df_cf.with_columns(
|
|
604
|
+
BDToMat=bdays_to_payments,
|
|
605
|
+
BYears=bdays_to_payments / 252,
|
|
606
|
+
DIRate=ff_interpolator(bdays_to_payments),
|
|
607
607
|
)
|
|
608
608
|
|
|
609
609
|
bond_price = tools.calculate_present_value(
|
|
@@ -711,11 +711,7 @@ def di_net_spread( # noqa
|
|
|
711
711
|
|
|
712
712
|
df = df.with_columns(
|
|
713
713
|
BDaysToPayment=bdays_to_payment,
|
|
714
|
-
|
|
715
|
-
DIRateInterp=pl.col("BDaysToPayment").map_elements(
|
|
716
|
-
function=ff_interpolator,
|
|
717
|
-
return_dtype=pl.Float64,
|
|
718
|
-
),
|
|
714
|
+
DIRateInterp=ff_interpolator(bdays_to_payment),
|
|
719
715
|
)
|
|
720
716
|
|
|
721
717
|
# Extração dos dados para o cálculo numérico
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.40.6"
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|