PYield 0.3.6__tar.gz → 0.3.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.3.6 → pyield-0.3.7}/PKG-INFO +1 -1
- pyield-0.3.7/pyield/__about__.py +1 -0
- pyield-0.3.7/pyield/__init__.py +15 -0
- {pyield-0.3.6 → pyield-0.3.7}/pyield/anbima.py +21 -14
- {pyield-0.3.6 → pyield-0.3.7}/pyield/br_calendar.py +115 -44
- {pyield-0.3.6 → pyield-0.3.7}/pyield/br_holidays.py +5 -4
- {pyield-0.3.6 → pyield-0.3.7}/pyield/di_futures.py +11 -7
- {pyield-0.3.6 → pyield-0.3.7}/pyield/di_web.py +8 -7
- {pyield-0.3.6 → pyield-0.3.7}/pyield/di_xml.py +11 -12
- {pyield-0.3.6 → pyield-0.3.7}/pyproject.toml +1 -1
- pyield-0.3.6/pyield/__about__.py +0 -1
- pyield-0.3.6/pyield/__init__.py +0 -4
- {pyield-0.3.6 → pyield-0.3.7}/LICENSE +0 -0
- {pyield-0.3.6 → pyield-0.3.7}/README.md +0 -0
- {pyield-0.3.6 → pyield-0.3.7}/pyield/br_holidays_new.txt +0 -0
- {pyield-0.3.6 → pyield-0.3.7}/pyield/br_holidays_old.txt +0 -0
- {pyield-0.3.6 → pyield-0.3.7}/pyield/py.typed +0 -0
- {pyield-0.3.6 → pyield-0.3.7}/tests/__init__.py +0 -0
- {pyield-0.3.6 → pyield-0.3.7}/tests/test_br_calendar.py +0 -0
- {pyield-0.3.6 → pyield-0.3.7}/tests/test_di.py +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.3.7"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
from .__about__ import __version__
|
|
2
|
+
from .di_futures import get_di, read_di
|
|
3
|
+
from .br_calendar import count_bdays, generate_bdays, offset_bdays
|
|
4
|
+
from .anbima import get_treasury_rates, calculate_treasury_di_spreads
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"__version__",
|
|
8
|
+
"get_di",
|
|
9
|
+
"read_di",
|
|
10
|
+
"count_bdays",
|
|
11
|
+
"generate_bdays",
|
|
12
|
+
"offset_bdays",
|
|
13
|
+
"get_treasury_rates",
|
|
14
|
+
"calculate_treasury_di_spreads",
|
|
15
|
+
]
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import pandas as pd
|
|
2
|
+
from pandas import Timestamp, DataFrame
|
|
2
3
|
from urllib.error import HTTPError
|
|
3
4
|
|
|
4
5
|
from . import di_futures as di
|
|
@@ -12,9 +13,7 @@ ANBIMA_MEMBER_URL = "http://www.anbima.associados.rtm/merc_sec/arqs/"
|
|
|
12
13
|
BP_CONVERSION_FACTOR = 10_000
|
|
13
14
|
|
|
14
15
|
|
|
15
|
-
def process_reference_date(
|
|
16
|
-
reference_date: str | pd.Timestamp | None = None,
|
|
17
|
-
) -> pd.Timestamp:
|
|
16
|
+
def process_reference_date(reference_date: str | Timestamp | None = None) -> Timestamp:
|
|
18
17
|
"""
|
|
19
18
|
Process the given reference date, converting it to a pandas Timestamp. If no date is provided,
|
|
20
19
|
use the previous business day based on the Brazilian calendar.
|
|
@@ -26,17 +25,25 @@ def process_reference_date(
|
|
|
26
25
|
- pd.Timestamp: The processed reference date.
|
|
27
26
|
"""
|
|
28
27
|
if reference_date: # Force reference_date to be a pd.Timestamp
|
|
29
|
-
processed_date = pd.
|
|
28
|
+
processed_date = pd.to_datetime(reference_date)
|
|
30
29
|
else: # If no reference_date is given, use the previous business day
|
|
31
30
|
today = pd.Timestamp.today().normalize()
|
|
32
|
-
processed_date = cl.offset_bdays(today, -1)
|
|
31
|
+
processed_date = cl.offset_bdays(today, -1) # type: ignore
|
|
32
|
+
|
|
33
|
+
# Check if the reference date is Timestamp
|
|
34
|
+
if not isinstance(processed_date, Timestamp):
|
|
35
|
+
raise ValueError("Reference date must be a pandas Timestamp.")
|
|
36
|
+
|
|
37
|
+
# Raise an error if the reference date is in the future
|
|
38
|
+
if processed_date > pd.Timestamp.today().normalize():
|
|
39
|
+
raise ValueError("Reference date cannot be in the future.")
|
|
33
40
|
|
|
34
41
|
return processed_date
|
|
35
42
|
|
|
36
43
|
|
|
37
44
|
def get_raw_data(
|
|
38
|
-
reference_date: str |
|
|
39
|
-
) ->
|
|
45
|
+
reference_date: str | Timestamp | None = None, is_anbima_member: bool = False
|
|
46
|
+
) -> DataFrame:
|
|
40
47
|
"""
|
|
41
48
|
Fetch indicative rates from ANBIMA for a specific date.
|
|
42
49
|
|
|
@@ -75,7 +82,7 @@ def get_raw_data(
|
|
|
75
82
|
return df
|
|
76
83
|
|
|
77
84
|
|
|
78
|
-
def process_raw_data(
|
|
85
|
+
def process_raw_data(df_raw: DataFrame) -> DataFrame:
|
|
79
86
|
"""
|
|
80
87
|
Process raw data from ANBIMA by filtering selected columns, renaming them, and adjusting data formats.
|
|
81
88
|
|
|
@@ -104,8 +111,8 @@ def process_raw_data(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
104
111
|
# "Criterio": "Criteria",
|
|
105
112
|
}
|
|
106
113
|
select_columns = list(selected_columns_dict.keys())
|
|
107
|
-
df =
|
|
108
|
-
df.rename(columns=selected_columns_dict
|
|
114
|
+
df = df_raw[select_columns].copy()
|
|
115
|
+
df = df.rename(columns=selected_columns_dict)
|
|
109
116
|
|
|
110
117
|
# Remove percentage from rates
|
|
111
118
|
rate_cols = ["BidRate", "AskRate", "IndicativeRate"]
|
|
@@ -118,10 +125,10 @@ def process_raw_data(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
118
125
|
|
|
119
126
|
|
|
120
127
|
def get_treasury_rates(
|
|
121
|
-
reference_date: str |
|
|
128
|
+
reference_date: str | Timestamp | None = None,
|
|
122
129
|
return_raw=False,
|
|
123
130
|
is_anbima_member=False,
|
|
124
|
-
) ->
|
|
131
|
+
) -> DataFrame:
|
|
125
132
|
"""
|
|
126
133
|
Fetch and process indicative rates from ANBIMA for a specific date.
|
|
127
134
|
If no date is provided, the previous business day based on the Brazilian calendar
|
|
@@ -146,9 +153,9 @@ def get_treasury_rates(
|
|
|
146
153
|
|
|
147
154
|
|
|
148
155
|
def calculate_treasury_di_spreads(
|
|
149
|
-
reference_date: str |
|
|
156
|
+
reference_date: str | Timestamp | None = None,
|
|
150
157
|
is_anbima_member=False,
|
|
151
|
-
) ->
|
|
158
|
+
) -> DataFrame:
|
|
152
159
|
"""
|
|
153
160
|
Calculate the DI spread for LTN and NTN-F bonds based on ANBIMA's indicative rates.
|
|
154
161
|
If no date is provided, the previous business day based on the Brazilian calendar
|
|
@@ -1,7 +1,8 @@
|
|
|
1
|
-
from typing import Literal
|
|
1
|
+
from typing import Literal, overload
|
|
2
2
|
|
|
3
|
-
import pandas as pd
|
|
4
3
|
import numpy as np
|
|
4
|
+
import pandas as pd
|
|
5
|
+
from pandas import Series, Timestamp
|
|
5
6
|
|
|
6
7
|
from .br_holidays import BrHolidays
|
|
7
8
|
|
|
@@ -9,9 +10,28 @@ from .br_holidays import BrHolidays
|
|
|
9
10
|
br_holidays = BrHolidays()
|
|
10
11
|
|
|
11
12
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
13
|
+
@overload
|
|
14
|
+
def format_input_dates(dates: str | Timestamp | None) -> Timestamp: ...
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@overload
|
|
18
|
+
def format_input_dates(dates: Series) -> Series: ...
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
# Implementação da função que atende às sobrecargas acima
|
|
22
|
+
def format_input_dates(dates: str | Timestamp | Series | None) -> Timestamp | Series:
|
|
23
|
+
if dates is None:
|
|
24
|
+
return pd.Timestamp.today().normalize()
|
|
25
|
+
result = pd.to_datetime(dates)
|
|
26
|
+
if isinstance(result, pd.Series):
|
|
27
|
+
return result
|
|
28
|
+
elif result is pd.NaT:
|
|
29
|
+
raise ValueError("Invalid date format.")
|
|
30
|
+
else:
|
|
31
|
+
return pd.Timestamp(result).normalize()
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def convert_to_numpy_date(dates: Timestamp | Series) -> np.datetime64 | np.ndarray:
|
|
15
35
|
"""
|
|
16
36
|
Converts the input dates to a numpy datetime64[D] format.
|
|
17
37
|
|
|
@@ -27,11 +47,27 @@ def convert_to_numpy_date(
|
|
|
27
47
|
return dates.to_numpy().astype("datetime64[D]")
|
|
28
48
|
|
|
29
49
|
|
|
50
|
+
@overload
|
|
51
|
+
def offset_bdays(
|
|
52
|
+
dates: str | Timestamp | None = None,
|
|
53
|
+
offset: int = 0,
|
|
54
|
+
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
55
|
+
) -> Timestamp: ...
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
@overload
|
|
59
|
+
def offset_bdays(
|
|
60
|
+
dates: Series,
|
|
61
|
+
offset: int = 0,
|
|
62
|
+
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
63
|
+
) -> Series: ...
|
|
64
|
+
|
|
65
|
+
|
|
30
66
|
def offset_bdays(
|
|
31
|
-
dates: str |
|
|
32
|
-
offset: int,
|
|
67
|
+
dates: str | Timestamp | Series | None = None,
|
|
68
|
+
offset: int = 0,
|
|
33
69
|
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
34
|
-
):
|
|
70
|
+
) -> Timestamp | Series:
|
|
35
71
|
"""
|
|
36
72
|
Offsets the dates to the next or previous business day. This function is a wrapper
|
|
37
73
|
for `numpy.busday_offset` to be used directly with Pandas data types that infers the
|
|
@@ -65,22 +101,53 @@ def offset_bdays(
|
|
|
65
101
|
>>> yd.offset_bdays(date, -1)
|
|
66
102
|
Timestamp('2023-12-21') # Offset to the previous business day
|
|
67
103
|
"""
|
|
68
|
-
|
|
69
|
-
dates_pd = pd.to_datetime(dates)
|
|
104
|
+
formatted_dates = format_input_dates(dates)
|
|
70
105
|
|
|
71
|
-
selected_holidays = br_holidays.get_applicable_holidays(
|
|
106
|
+
selected_holidays = br_holidays.get_applicable_holidays(
|
|
107
|
+
formatted_dates, holiday_list
|
|
108
|
+
)
|
|
72
109
|
selected_holidays_np = convert_to_numpy_date(selected_holidays)
|
|
73
110
|
|
|
74
|
-
dates_np = convert_to_numpy_date(
|
|
111
|
+
dates_np = convert_to_numpy_date(formatted_dates)
|
|
75
112
|
offsetted_dates_np = np.busday_offset(
|
|
76
113
|
dates_np, offsets=offset, roll="forward", holidays=selected_holidays_np
|
|
77
114
|
)
|
|
115
|
+
if isinstance(offsetted_dates_np, np.datetime64):
|
|
116
|
+
return pd.Timestamp(offsetted_dates_np, unit="ns")
|
|
117
|
+
else:
|
|
118
|
+
result = pd.to_datetime(offsetted_dates_np, unit="ns")
|
|
119
|
+
return pd.Series(result)
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
@overload
|
|
123
|
+
def count_bdays(
|
|
124
|
+
start: str | Timestamp | None = None,
|
|
125
|
+
end: str | Timestamp | None = None,
|
|
126
|
+
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
127
|
+
) -> int: ...
|
|
78
128
|
|
|
79
|
-
# Convert the dates back to a pandas datetime64[ns] format
|
|
80
|
-
return pd.to_datetime(offsetted_dates_np, unit="ns")
|
|
81
129
|
|
|
130
|
+
@overload
|
|
131
|
+
def count_bdays(
|
|
132
|
+
start: Series,
|
|
133
|
+
end: str | Timestamp | Series | None,
|
|
134
|
+
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
135
|
+
) -> Series: ...
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
@overload
|
|
139
|
+
def count_bdays(
|
|
140
|
+
start: str | Timestamp | Series | None,
|
|
141
|
+
end: Series,
|
|
142
|
+
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
143
|
+
) -> Series: ...
|
|
82
144
|
|
|
83
|
-
|
|
145
|
+
|
|
146
|
+
def count_bdays(
|
|
147
|
+
start: str | Timestamp | Series | None = None,
|
|
148
|
+
end: str | Timestamp | Series | None = None,
|
|
149
|
+
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
150
|
+
) -> int | Series:
|
|
84
151
|
"""
|
|
85
152
|
Counts the number of business days between a `start` (inclusive) and `end`
|
|
86
153
|
(exclusive). If an end date is earlier than the start date, the count will be
|
|
@@ -88,16 +155,15 @@ def count_bdays(start, end, holiday_list: Literal["old", "new", "infer"] = "infe
|
|
|
88
155
|
with Pandas data types.
|
|
89
156
|
|
|
90
157
|
Args:
|
|
91
|
-
start (str |
|
|
92
|
-
end (str |
|
|
158
|
+
start (str | Timestamp, optional): The start date. Defaults to None.
|
|
159
|
+
end (str | Timestamp optional): The end date. Defaults to None.
|
|
93
160
|
holiday_list (str, optional): The list of holidays to use. Defaults to "infer",
|
|
94
161
|
which infers the right list of holidays based on the most recent date in
|
|
95
162
|
the input.
|
|
96
163
|
|
|
97
164
|
Returns:
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
returns an ndarray of integers.
|
|
165
|
+
int | pd.Series: The number of business days between the start date and end date.
|
|
166
|
+
Returns an integer if the result is a single value, otherwise returns a Series.
|
|
101
167
|
|
|
102
168
|
Notes:
|
|
103
169
|
- For more information on error handling, see numpy.busday_count documentation at
|
|
@@ -115,30 +181,36 @@ def count_bdays(start, end, holiday_list: Literal["old", "new", "infer"] = "infe
|
|
|
115
181
|
>>> start = '2023-01-01'
|
|
116
182
|
>>> end = pd.to_datetime(['2023-01-31', '2023-03-01'])
|
|
117
183
|
>>> yd.count_bdays(start, end)
|
|
118
|
-
|
|
184
|
+
pd.Series([22, 40], dtype='int64')
|
|
119
185
|
"""
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
end_pd = pd.to_datetime(end)
|
|
186
|
+
formatted_start = format_input_dates(start)
|
|
187
|
+
formatted_end = format_input_dates(end)
|
|
123
188
|
|
|
124
189
|
# Determine which list of holidays to use
|
|
125
|
-
selected_holidays = br_holidays.get_applicable_holidays(
|
|
190
|
+
selected_holidays = br_holidays.get_applicable_holidays(
|
|
191
|
+
formatted_start, holiday_list
|
|
192
|
+
)
|
|
126
193
|
selected_holidays_np = convert_to_numpy_date(selected_holidays)
|
|
127
194
|
|
|
128
195
|
# Convert inputs to numpy datetime64[D] before calling numpy.busday_count
|
|
129
|
-
start_np = convert_to_numpy_date(
|
|
130
|
-
end_np = convert_to_numpy_date(
|
|
196
|
+
start_np = convert_to_numpy_date(formatted_start)
|
|
197
|
+
end_np = convert_to_numpy_date(formatted_end)
|
|
131
198
|
|
|
132
|
-
|
|
199
|
+
result_np = np.busday_count(start_np, end_np, holidays=selected_holidays_np)
|
|
200
|
+
if isinstance(result_np, np.ndarray):
|
|
201
|
+
# Return pandas Int64 type for type consistency
|
|
202
|
+
return pd.Series(result_np, dtype="Int64")
|
|
203
|
+
else:
|
|
204
|
+
return int(result_np)
|
|
133
205
|
|
|
134
206
|
|
|
135
207
|
def generate_bdays(
|
|
136
|
-
start: str |
|
|
137
|
-
end: str |
|
|
138
|
-
inclusive="both",
|
|
208
|
+
start: str | Timestamp | None = None,
|
|
209
|
+
end: str | Timestamp | None = None,
|
|
210
|
+
inclusive: Literal["both", "neither", "left", "right"] = "both",
|
|
139
211
|
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
140
212
|
**kwargs,
|
|
141
|
-
) ->
|
|
213
|
+
) -> Series:
|
|
142
214
|
"""
|
|
143
215
|
Generates a Series of business days between a `start` (inclusive) and `end`
|
|
144
216
|
(inclusive) that takes into account the list of brazilian holidays as the default.
|
|
@@ -179,24 +251,23 @@ def generate_bdays(
|
|
|
179
251
|
2023-12-29 2023-12-29
|
|
180
252
|
dtype: object
|
|
181
253
|
"""
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
else:
|
|
185
|
-
start_pd = pd.Timestamp.today().normalize()
|
|
254
|
+
formatted_start = format_input_dates(start)
|
|
255
|
+
formatted_end = format_input_dates(end)
|
|
186
256
|
|
|
187
|
-
if
|
|
188
|
-
|
|
189
|
-
else:
|
|
190
|
-
end_pd = pd.Timestamp.today().normalize()
|
|
257
|
+
if isinstance(formatted_start, Series) or isinstance(formatted_end, Series):
|
|
258
|
+
raise ValueError("The start and end dates must be single dates.")
|
|
191
259
|
|
|
192
|
-
selected_holidays = br_holidays.get_applicable_holidays(
|
|
260
|
+
selected_holidays = br_holidays.get_applicable_holidays(
|
|
261
|
+
formatted_start, holiday_list
|
|
262
|
+
)
|
|
193
263
|
selected_holidays_list = selected_holidays.to_list()
|
|
194
264
|
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
265
|
+
result = pd.bdate_range(
|
|
266
|
+
formatted_start,
|
|
267
|
+
formatted_end,
|
|
198
268
|
freq="C",
|
|
199
269
|
inclusive=inclusive,
|
|
200
270
|
holidays=selected_holidays_list,
|
|
201
271
|
**kwargs,
|
|
202
272
|
)
|
|
273
|
+
return pd.Series(result)
|
|
@@ -2,6 +2,7 @@ from pathlib import Path
|
|
|
2
2
|
from typing import Literal
|
|
3
3
|
|
|
4
4
|
import pandas as pd
|
|
5
|
+
from pandas import Series, Timestamp
|
|
5
6
|
|
|
6
7
|
|
|
7
8
|
class BrHolidays:
|
|
@@ -15,7 +16,7 @@ class BrHolidays:
|
|
|
15
16
|
self.new_holidays = self._load_holidays(new_holidays_path)
|
|
16
17
|
self.old_holidays = self._load_holidays(old_holidays_path)
|
|
17
18
|
|
|
18
|
-
def _load_holidays(self, file_path: Path) ->
|
|
19
|
+
def _load_holidays(self, file_path: Path) -> Series:
|
|
19
20
|
"""Loads the list of holidays from a text file and returns it as a pd.Series of pd.Timestamp."""
|
|
20
21
|
df = pd.read_csv(file_path, header=None, names=["date"], comment="#")
|
|
21
22
|
# Convert the dates to a pd.Series of pd.Timestamp
|
|
@@ -23,9 +24,9 @@ class BrHolidays:
|
|
|
23
24
|
|
|
24
25
|
def get_applicable_holidays(
|
|
25
26
|
self,
|
|
26
|
-
dates:
|
|
27
|
+
dates: Timestamp | Series,
|
|
27
28
|
holiday_list: Literal["old", "new", "infer"] = "infer",
|
|
28
|
-
) ->
|
|
29
|
+
) -> Series:
|
|
29
30
|
"""
|
|
30
31
|
Returns the correct list of holidays to use based on the most recent date in the input.
|
|
31
32
|
|
|
@@ -38,7 +39,7 @@ class BrHolidays:
|
|
|
38
39
|
Returns:
|
|
39
40
|
pd.Series: The list of holidays to use.
|
|
40
41
|
"""
|
|
41
|
-
if isinstance(dates,
|
|
42
|
+
if isinstance(dates, Timestamp):
|
|
42
43
|
earliest_date = dates
|
|
43
44
|
else:
|
|
44
45
|
earliest_date = dates.min()
|
|
@@ -1,14 +1,15 @@
|
|
|
1
|
-
from typing import Literal
|
|
1
|
+
from typing import Literal
|
|
2
2
|
from pathlib import Path
|
|
3
|
+
|
|
3
4
|
import pandas as pd
|
|
4
|
-
import
|
|
5
|
+
from pandas import DataFrame, Timestamp
|
|
5
6
|
|
|
6
7
|
from . import di_web as diw
|
|
7
8
|
from . import di_xml as dix
|
|
8
9
|
from . import br_calendar as brc
|
|
9
10
|
|
|
10
11
|
|
|
11
|
-
def get_expiration_date(expiration_code: str) ->
|
|
12
|
+
def get_expiration_date(expiration_code: str) -> Timestamp:
|
|
12
13
|
"""
|
|
13
14
|
Internal function to convert the expiration code into its expiration date.
|
|
14
15
|
|
|
@@ -63,15 +64,18 @@ def get_expiration_date(expiration_code: str) -> Optional[pd.Timestamp]:
|
|
|
63
64
|
year = int("20" + expiration_code[-2:])
|
|
64
65
|
# The expiration date is always the first business day of the month
|
|
65
66
|
expiration = pd.Timestamp(year, month, 1)
|
|
67
|
+
|
|
66
68
|
# Adjust to the next business day when expiration date is a weekend or a holiday
|
|
67
|
-
|
|
69
|
+
adj_expiration = brc.offset_bdays(expiration, offset=0)
|
|
70
|
+
|
|
71
|
+
return adj_expiration
|
|
68
72
|
|
|
69
73
|
except (KeyError, ValueError):
|
|
70
|
-
|
|
74
|
+
raise ValueError("Invalid expiration code.")
|
|
71
75
|
|
|
72
76
|
|
|
73
77
|
def get_di(
|
|
74
|
-
trade_date: str |
|
|
78
|
+
trade_date: str | Timestamp,
|
|
75
79
|
source_type: Literal["bmf", "b3", "b3s"] = "bmf",
|
|
76
80
|
return_raw: bool = False,
|
|
77
81
|
) -> pd.DataFrame:
|
|
@@ -121,7 +125,7 @@ def get_di(
|
|
|
121
125
|
raise ValueError("source_type must be either 'bmf', 'b3' or 'b3s'.")
|
|
122
126
|
|
|
123
127
|
|
|
124
|
-
def read_di(file_path: Path, return_raw: bool = False) ->
|
|
128
|
+
def read_di(file_path: Path, return_raw: bool = False) -> DataFrame:
|
|
125
129
|
"""
|
|
126
130
|
Reads a DI futures data file and returns a DataFrame.
|
|
127
131
|
|
|
@@ -3,6 +3,7 @@ import warnings
|
|
|
3
3
|
from typing import Optional
|
|
4
4
|
|
|
5
5
|
import pandas as pd
|
|
6
|
+
from pandas import Timestamp, Series, DataFrame
|
|
6
7
|
import requests
|
|
7
8
|
|
|
8
9
|
from . import br_calendar as brc
|
|
@@ -10,8 +11,8 @@ from . import di_futures as dif
|
|
|
10
11
|
|
|
11
12
|
|
|
12
13
|
def get_old_expiration_date(
|
|
13
|
-
ExpirationCode: str, trade_date:
|
|
14
|
-
) -> Optional[
|
|
14
|
+
ExpirationCode: str, trade_date: Timestamp
|
|
15
|
+
) -> Optional[Timestamp]:
|
|
15
16
|
"""
|
|
16
17
|
Internal function to convert an old DI contract code into its ExpirationDate date. Valid for
|
|
17
18
|
contract codes up to 21-05-2006.
|
|
@@ -71,7 +72,7 @@ def get_old_expiration_date(
|
|
|
71
72
|
return pd.NaT # type: ignore
|
|
72
73
|
|
|
73
74
|
|
|
74
|
-
def get_raw_di(trade_date:
|
|
75
|
+
def get_raw_di(trade_date: Timestamp) -> DataFrame:
|
|
75
76
|
"""
|
|
76
77
|
Internal function to fetch raw DI futures data from B3 for a specific trade date.
|
|
77
78
|
|
|
@@ -119,7 +120,7 @@ def get_raw_di(trade_date: pd.Timestamp) -> pd.DataFrame:
|
|
|
119
120
|
return pd.DataFrame()
|
|
120
121
|
|
|
121
122
|
|
|
122
|
-
def convert_prices_to_rates(prices:
|
|
123
|
+
def convert_prices_to_rates(prices: Series, bd: Series) -> Series:
|
|
123
124
|
"""
|
|
124
125
|
Internal function to convert DI futures prices to rates.
|
|
125
126
|
|
|
@@ -136,7 +137,7 @@ def convert_prices_to_rates(prices: pd.Series, bd: pd.Series) -> pd.Series:
|
|
|
136
137
|
return 100 * rates
|
|
137
138
|
|
|
138
139
|
|
|
139
|
-
def convert_prices_in_older_contracts(df:
|
|
140
|
+
def convert_prices_in_older_contracts(df: DataFrame) -> DataFrame:
|
|
140
141
|
# Prior to 01/01/2002, prices were not converted to rates
|
|
141
142
|
convert_cols = [
|
|
142
143
|
"FirstRate",
|
|
@@ -156,7 +157,7 @@ def convert_prices_in_older_contracts(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
156
157
|
return df
|
|
157
158
|
|
|
158
159
|
|
|
159
|
-
def process_di(df:
|
|
160
|
+
def process_di(df: DataFrame, trade_date: Timestamp) -> DataFrame:
|
|
160
161
|
"""
|
|
161
162
|
Internal function to process and transform raw DI futures data.
|
|
162
163
|
|
|
@@ -265,7 +266,7 @@ def process_di(df: pd.DataFrame, trade_date: pd.Timestamp) -> pd.DataFrame:
|
|
|
265
266
|
return df[ordered_cols]
|
|
266
267
|
|
|
267
268
|
|
|
268
|
-
def get_di(trade_date:
|
|
269
|
+
def get_di(trade_date: Timestamp, return_raw: bool = False) -> DataFrame:
|
|
269
270
|
"""
|
|
270
271
|
Gets the DI futures data for a given date from B3.
|
|
271
272
|
|
|
@@ -4,13 +4,14 @@ from pathlib import Path
|
|
|
4
4
|
|
|
5
5
|
import requests
|
|
6
6
|
import pandas as pd
|
|
7
|
+
from pandas import Timestamp, DataFrame
|
|
7
8
|
from lxml import etree
|
|
8
9
|
|
|
9
10
|
from . import di_futures as dif
|
|
10
11
|
from . import br_calendar as brc
|
|
11
12
|
|
|
12
13
|
|
|
13
|
-
def get_file_from_url(trade_date:
|
|
14
|
+
def get_file_from_url(trade_date: Timestamp, source_type: str) -> io.BytesIO:
|
|
14
15
|
"""
|
|
15
16
|
Types of XML files available:
|
|
16
17
|
Full Price Report (all assets)
|
|
@@ -119,7 +120,7 @@ def extract_di_data_from_xml(xml_file: io.BytesIO) -> list[dict]:
|
|
|
119
120
|
return di_data
|
|
120
121
|
|
|
121
122
|
|
|
122
|
-
def create_df_from_di_data(di1_data: list) ->
|
|
123
|
+
def create_df_from_di_data(di1_data: list) -> DataFrame:
|
|
123
124
|
# Criar um DataFrame com os dados coletados
|
|
124
125
|
df = pd.DataFrame(di1_data)
|
|
125
126
|
|
|
@@ -128,7 +129,7 @@ def create_df_from_di_data(di1_data: list) -> pd.DataFrame:
|
|
|
128
129
|
return pd.read_csv(file, dtype_backend="numpy_nullable")
|
|
129
130
|
|
|
130
131
|
|
|
131
|
-
def filter_and_order_pr_df(df:
|
|
132
|
+
def filter_and_order_pr_df(df: DataFrame) -> DataFrame:
|
|
132
133
|
selected_columns = [
|
|
133
134
|
"TradDt",
|
|
134
135
|
"TckrSymb",
|
|
@@ -164,7 +165,7 @@ def filter_and_order_pr_df(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
164
165
|
return df[selected_columns]
|
|
165
166
|
|
|
166
167
|
|
|
167
|
-
def filter_and_order_sprd_df(df:
|
|
168
|
+
def filter_and_order_sprd_df(df: DataFrame) -> DataFrame:
|
|
168
169
|
cols = [
|
|
169
170
|
"TradDt",
|
|
170
171
|
"TckrSymb",
|
|
@@ -186,7 +187,7 @@ def filter_and_order_sprd_df(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
186
187
|
return df[cols]
|
|
187
188
|
|
|
188
189
|
|
|
189
|
-
def standardize_column_names(df:
|
|
190
|
+
def standardize_column_names(df: DataFrame) -> DataFrame:
|
|
190
191
|
rename_dict = {
|
|
191
192
|
"TradDt": "TradeDate",
|
|
192
193
|
"TckrSymb": "Ticker",
|
|
@@ -223,7 +224,7 @@ def standardize_column_names(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
223
224
|
return df.rename(columns=rename_dict)
|
|
224
225
|
|
|
225
226
|
|
|
226
|
-
def process_di_df(df_raw:
|
|
227
|
+
def process_di_df(df_raw: DataFrame) -> DataFrame:
|
|
227
228
|
df = df_raw.copy()
|
|
228
229
|
# Convert to datetime64[ns] since it is pandas default type for timestamps
|
|
229
230
|
df["TradDt"] = df["TradDt"].astype("datetime64[ns]")
|
|
@@ -243,11 +244,7 @@ def process_di_df(df_raw: pd.DataFrame) -> pd.DataFrame:
|
|
|
243
244
|
return df.sort_values(by=["ExpirationDate"], ignore_index=True)
|
|
244
245
|
|
|
245
246
|
|
|
246
|
-
def get_di(
|
|
247
|
-
trade_date: pd.Timestamp,
|
|
248
|
-
source_type: str,
|
|
249
|
-
return_raw: bool,
|
|
250
|
-
) -> pd.DataFrame:
|
|
247
|
+
def get_di(trade_date: Timestamp, source_type: str, return_raw: bool) -> DataFrame:
|
|
251
248
|
zip_file = get_file_from_url(trade_date, source_type)
|
|
252
249
|
|
|
253
250
|
xml_file = extract_xml_from_zip(zip_file)
|
|
@@ -263,6 +260,8 @@ def get_di(
|
|
|
263
260
|
df_di = filter_and_order_pr_df(df_raw)
|
|
264
261
|
elif source_type == "b3s":
|
|
265
262
|
df_di = filter_and_order_sprd_df(df_raw)
|
|
263
|
+
else:
|
|
264
|
+
raise ValueError("Invalid source type. Must be 'b3' or 'b3s'.")
|
|
266
265
|
|
|
267
266
|
# Process and transform data
|
|
268
267
|
df_di = process_di_df(df_di)
|
|
@@ -273,7 +272,7 @@ def get_di(
|
|
|
273
272
|
return df_di
|
|
274
273
|
|
|
275
274
|
|
|
276
|
-
def read_di(file_path: Path, return_raw: bool = False) ->
|
|
275
|
+
def read_di(file_path: Path, return_raw: bool = False) -> DataFrame:
|
|
277
276
|
if file_path:
|
|
278
277
|
if file_path.exists():
|
|
279
278
|
content = file_path.read_bytes()
|
pyield-0.3.6/pyield/__about__.py
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.3.6"
|
pyield-0.3.6/pyield/__init__.py
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|