PYield 0.7.8__tar.gz → 0.7.10__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.7.8 → pyield-0.7.10}/PKG-INFO +2 -2
- {pyield-0.7.8 → pyield-0.7.10}/README.md +1 -1
- pyield-0.7.10/pyield/__about__.py +1 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/__init__.py +6 -2
- {pyield-0.7.8 → pyield-0.7.10}/pyield/data_access.py +53 -27
- pyield-0.7.10/pyield/futures/__init__.py +4 -0
- pyield-0.7.8/pyield/futures/historical/common.py → pyield-0.7.10/pyield/futures/historical.py +91 -12
- pyield-0.7.8/pyield/futures/intraday/trading_data.py → pyield-0.7.10/pyield/futures/intraday.py +65 -13
- pyield-0.7.10/pyield/futures/xml.py +261 -0
- pyield-0.7.10/pyield/spreads.py +56 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/treasuries.py +0 -52
- {pyield-0.7.8 → pyield-0.7.10}/pyproject.toml +1 -1
- pyield-0.7.10/tests/data/SPRD231222.zip +0 -0
- pyield-0.7.10/tests/data/SPRD240426.zip +0 -0
- pyield-0.7.8/pyield/__about__.py +0 -1
- pyield-0.7.8/pyield/data_analysis.py +0 -33
- pyield-0.7.8/pyield/futures/historical/__init__.py +0 -6
- pyield-0.7.8/pyield/futures/historical/dap.py +0 -62
- pyield-0.7.8/pyield/futures/historical/ddi.py +0 -80
- pyield-0.7.8/pyield/futures/historical/di.py +0 -91
- pyield-0.7.8/pyield/futures/historical/di_xml.py +0 -349
- pyield-0.7.8/pyield/futures/historical/frc.py +0 -60
- pyield-0.7.8/pyield/futures/intraday/__init__.py +0 -3
- pyield-0.7.8/tests/__init__.py +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/LICENSE +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/bday.py +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/holidays/__init__.py +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/holidays/br_holidays_new.txt +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/holidays/br_holidays_old.txt +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/holidays/core.py +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/indicators.py +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/projections.py +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/py.typed +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/pyield/utils.py +0 -0
- {pyield-0.7.8/pyield/futures → pyield-0.7.10/tests}/__init__.py +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/tests/test_bday.py +0 -0
- {pyield-0.7.8 → pyield-0.7.10}/tests/test_futures.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.1
|
|
2
2
|
Name: PYield
|
|
3
|
-
Version: 0.7.
|
|
3
|
+
Version: 0.7.10
|
|
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>
|
|
@@ -101,7 +101,7 @@ dtype: datetime64[ns]
|
|
|
101
101
|
```python
|
|
102
102
|
# Fetch current DI Futures data from B3 (15 minutes delay)
|
|
103
103
|
>>> yd.fetch_asset(asset_code="DI1")
|
|
104
|
-
|
|
104
|
+
TradeTime TickerSymbol ExpirationDate BDaysToExp ... MaxRate LastAskRate LastBidRate CurrentRate
|
|
105
105
|
2024-04-21 13:37:39 DI1K24 2024-05-02 7 ... 0.10660 0.10652 0.10660 0.10660
|
|
106
106
|
2024-04-21 13:37:39 DI1M24 2024-06-03 28 ... 0.10518 0.10510 0.10516 0.10518
|
|
107
107
|
2024-04-21 13:37:39 DI1N24 2024-07-01 48 ... 0.10480 0.10456 0.10462 0.10460
|
|
@@ -58,7 +58,7 @@ dtype: datetime64[ns]
|
|
|
58
58
|
```python
|
|
59
59
|
# Fetch current DI Futures data from B3 (15 minutes delay)
|
|
60
60
|
>>> yd.fetch_asset(asset_code="DI1")
|
|
61
|
-
|
|
61
|
+
TradeTime TickerSymbol ExpirationDate BDaysToExp ... MaxRate LastAskRate LastBidRate CurrentRate
|
|
62
62
|
2024-04-21 13:37:39 DI1K24 2024-05-02 7 ... 0.10660 0.10652 0.10660 0.10660
|
|
63
63
|
2024-04-21 13:37:39 DI1M24 2024-06-03 28 ... 0.10518 0.10510 0.10516 0.10518
|
|
64
64
|
2024-04-21 13:37:39 DI1N24 2024-07-01 48 ... 0.10480 0.10456 0.10462 0.10460
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.7.10"
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
from .__about__ import __version__
|
|
2
2
|
from .bday import count_bdays, generate_bdays, is_bday, offset_bdays
|
|
3
|
-
from .data_access import
|
|
4
|
-
|
|
3
|
+
from .data_access import (
|
|
4
|
+
calculate_spreads,
|
|
5
|
+
fetch_asset,
|
|
6
|
+
fetch_indicator,
|
|
7
|
+
fetch_projection,
|
|
8
|
+
)
|
|
5
9
|
|
|
6
10
|
__all__ = [
|
|
7
11
|
"__version__",
|
|
@@ -1,17 +1,15 @@
|
|
|
1
1
|
import pandas as pd
|
|
2
2
|
|
|
3
|
+
from . import futures as ft
|
|
3
4
|
from . import indicators as it
|
|
4
5
|
from . import projections as pr
|
|
6
|
+
from . import spreads as sp
|
|
5
7
|
from . import treasuries as tr
|
|
6
|
-
from .futures import historical as fh
|
|
7
|
-
from .futures import intraday as fi
|
|
8
8
|
from .utils import _normalize_date
|
|
9
9
|
|
|
10
10
|
|
|
11
11
|
def fetch_asset(
|
|
12
|
-
asset_code: str,
|
|
13
|
-
reference_date: str | pd.Timestamp | None = None,
|
|
14
|
-
**kwargs,
|
|
12
|
+
asset_code: str, reference_date: str | pd.Timestamp | None = None
|
|
15
13
|
) -> pd.DataFrame:
|
|
16
14
|
"""
|
|
17
15
|
Fetches data for a specified asset based on type and reference date.
|
|
@@ -25,7 +23,11 @@ def fetch_asset(
|
|
|
25
23
|
- "DI1": One-day Interbank Deposit Futures (Futuro de DI) from B3.
|
|
26
24
|
- "DDI": DI x U.S. Dollar Spread Futures (Futuro de Cupom Cambial) from B3.
|
|
27
25
|
- "FRC": Forward Rate Agreement (FRA) from B3.
|
|
28
|
-
- "DAP": DI x IPCA Spread Futures
|
|
26
|
+
- "DAP": DI x IPCA Spread Futures.
|
|
27
|
+
- "DOL": U.S. Dollar Futures from B3.
|
|
28
|
+
- "WDO": Mini U.S. Dollar Futures from B3.
|
|
29
|
+
- "IND": Ibovespa Futures from B3.
|
|
30
|
+
- "WIN": Mini Ibovespa Futures from B3.
|
|
29
31
|
reference_date (str | pd.Timestamp | None): The reference date for which data is
|
|
30
32
|
fetched. Defaults to the last business day if None.
|
|
31
33
|
**kwargs: Additional keyword arguments, specifically:
|
|
@@ -42,31 +44,26 @@ def fetch_asset(
|
|
|
42
44
|
>>> fetch_asset('TRB', '2023-04-01')
|
|
43
45
|
>>> fetch_asset('DI1', '2023-04-01', return_raw=True)
|
|
44
46
|
"""
|
|
45
|
-
|
|
47
|
+
SUPPORTED_BONDS = ["LTN", "LFT", "NTN-F", "NTN-B"]
|
|
48
|
+
SUPPORTED_FUTURES = ["DI1", "DDI", "FRC", "DAP", "DOL", "WDO", "IND", "WIN"]
|
|
49
|
+
|
|
46
50
|
normalized_date = _normalize_date(reference_date)
|
|
47
51
|
|
|
48
52
|
today = pd.Timestamp.today().normalize()
|
|
49
53
|
if normalized_date == today:
|
|
50
|
-
return
|
|
54
|
+
return ft.fetch_intraday_df(future_code=asset_code.upper())
|
|
51
55
|
|
|
52
|
-
if asset_code.
|
|
53
|
-
return tr.fetch_bonds(reference_date=normalized_date
|
|
56
|
+
if asset_code.upper() == "TRB":
|
|
57
|
+
return tr.fetch_bonds(reference_date=normalized_date)
|
|
54
58
|
|
|
55
|
-
if asset_code.
|
|
59
|
+
if asset_code.upper() in SUPPORTED_BONDS:
|
|
56
60
|
df = tr.fetch_bonds(reference_date=normalized_date)
|
|
57
61
|
return df.query(f"BondType == '{asset_code.upper()}'")
|
|
58
62
|
|
|
59
|
-
if asset_code.
|
|
60
|
-
return
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
return fh.fetch_ddi(trade_date=normalized_date, return_raw=return_raw)
|
|
64
|
-
|
|
65
|
-
if asset_code.lower() == "frc":
|
|
66
|
-
return fh.fetch_frc(trade_date=normalized_date, return_raw=return_raw)
|
|
67
|
-
|
|
68
|
-
if asset_code.lower() == "dap":
|
|
69
|
-
return fh.fetch_dap(trade_date=normalized_date, return_raw=return_raw)
|
|
63
|
+
if asset_code.upper() in SUPPORTED_FUTURES:
|
|
64
|
+
return ft.fetch_historical_df(
|
|
65
|
+
asset_code=asset_code.upper(), trade_date=normalized_date
|
|
66
|
+
)
|
|
70
67
|
|
|
71
68
|
raise ValueError("Asset type not supported.")
|
|
72
69
|
|
|
@@ -107,13 +104,13 @@ def fetch_indicator(
|
|
|
107
104
|
"""
|
|
108
105
|
normalized_date = _normalize_date(reference_date)
|
|
109
106
|
|
|
110
|
-
if indicator_code.
|
|
107
|
+
if indicator_code.upper() == "SELIC":
|
|
111
108
|
return it.fetch_selic_target(reference_date=normalized_date)
|
|
112
|
-
elif indicator_code.
|
|
109
|
+
elif indicator_code.upper() == "IPCA":
|
|
113
110
|
return it.fetch_ipca_mr(reference_date=normalized_date)
|
|
114
|
-
elif indicator_code.
|
|
111
|
+
elif indicator_code.upper() == "DI":
|
|
115
112
|
return it.fetch_di(reference_date=normalized_date)
|
|
116
|
-
elif indicator_code.
|
|
113
|
+
elif indicator_code.upper() == "VNA_LFT":
|
|
117
114
|
return it.fetch_vna_selic(reference_date=normalized_date)
|
|
118
115
|
else:
|
|
119
116
|
raise ValueError("Indicator type not supported.")
|
|
@@ -150,7 +147,36 @@ def fetch_projection(projection_code: str) -> pr.IndicatorProjection:
|
|
|
150
147
|
)
|
|
151
148
|
"""
|
|
152
149
|
|
|
153
|
-
if projection_code.
|
|
150
|
+
if projection_code.upper() == "IPCA_CM":
|
|
154
151
|
return pr.fetch_current_month_ipca_projection()
|
|
155
152
|
else:
|
|
156
153
|
raise ValueError("Projection type not supported.")
|
|
154
|
+
|
|
155
|
+
|
|
156
|
+
def calculate_spreads(
|
|
157
|
+
spread_type: str, reference_date: str | pd.Timestamp | None = None
|
|
158
|
+
) -> pd.DataFrame:
|
|
159
|
+
"""
|
|
160
|
+
Calculates spreads between assets based on the specified spread type.
|
|
161
|
+
If no reference date is provided, the function uses the previous business day.
|
|
162
|
+
|
|
163
|
+
Parameters:
|
|
164
|
+
spread_type (str): The type of spread to calculate. Available options are:
|
|
165
|
+
- "DI_vs_PRE": the spread between DI Futures and Treasury Pre-Fixed bonds.
|
|
166
|
+
reference_date (str | pd.Timestamp, optional): The reference date for the
|
|
167
|
+
spread calculation. If None or not provided, defaults to the previous
|
|
168
|
+
business day according to the Brazilian calendar.
|
|
169
|
+
|
|
170
|
+
Returns:
|
|
171
|
+
pd.DataFrame: A DataFrame containing the calculated spread in basis points.
|
|
172
|
+
The data is sorted by asset type and maturity/expiration date.
|
|
173
|
+
|
|
174
|
+
Raises:
|
|
175
|
+
ValueError: If an invalid spread type is provided.
|
|
176
|
+
"""
|
|
177
|
+
# Normalize the reference date
|
|
178
|
+
normalized_date = _normalize_date(reference_date)
|
|
179
|
+
if spread_type.upper() == "DI_VS_PRE":
|
|
180
|
+
return sp.calculate_di_spreads(normalized_date)
|
|
181
|
+
else:
|
|
182
|
+
raise ValueError("Invalid spread type.")
|
pyield-0.7.8/pyield/futures/historical/common.py → pyield-0.7.10/pyield/futures/historical.py
RENAMED
|
@@ -3,7 +3,18 @@ import io
|
|
|
3
3
|
import pandas as pd
|
|
4
4
|
import requests
|
|
5
5
|
|
|
6
|
-
from
|
|
6
|
+
from .. import bday
|
|
7
|
+
|
|
8
|
+
COUNT_CONVENTIONS = {
|
|
9
|
+
"DAP": 252,
|
|
10
|
+
"DI1": 252,
|
|
11
|
+
"DDI": 360,
|
|
12
|
+
"FRC": None,
|
|
13
|
+
"DOL": None,
|
|
14
|
+
"WDO": None,
|
|
15
|
+
"IND": None,
|
|
16
|
+
"WIN": None,
|
|
17
|
+
}
|
|
7
18
|
|
|
8
19
|
|
|
9
20
|
def get_expiration_date(expiration_code: str) -> pd.Timestamp:
|
|
@@ -141,12 +152,17 @@ def convert_prices_to_rates(
|
|
|
141
152
|
prices (pd.Series): The futures prices to be converted.
|
|
142
153
|
days_to_expiration (pd.Series): The number of days to expiration for each price.
|
|
143
154
|
count_convention (int): The count convention for the DI futures contract.
|
|
144
|
-
|
|
155
|
+
Can be 252 business days or 360 calendar days.
|
|
145
156
|
|
|
146
157
|
Returns:
|
|
147
158
|
pd.Series: A pd.Series containing the futures rates.
|
|
148
159
|
"""
|
|
149
|
-
|
|
160
|
+
if count_convention == 252:
|
|
161
|
+
rates = (100_000 / prices) ** (252 / days_to_expiration) - 1
|
|
162
|
+
elif count_convention == 360:
|
|
163
|
+
rates = (100_000 / prices - 1) * (360 / days_to_expiration)
|
|
164
|
+
else:
|
|
165
|
+
raise ValueError("Invalid count_convention. Must be 252 or 360.")
|
|
150
166
|
|
|
151
167
|
# Round to 5 (3 in %) dec. places (contract's current max. precision)
|
|
152
168
|
return rates.round(5)
|
|
@@ -169,12 +185,12 @@ def fetch_raw_df(asset_code: str, trade_date: pd.Timestamp) -> pd.DataFrame:
|
|
|
169
185
|
r = requests.get(url)
|
|
170
186
|
|
|
171
187
|
text = r.text
|
|
172
|
-
if "
|
|
188
|
+
if "VENCTO" not in text:
|
|
173
189
|
return pd.DataFrame()
|
|
174
190
|
|
|
175
191
|
df = pd.read_html(
|
|
176
192
|
io.StringIO(text),
|
|
177
|
-
match="
|
|
193
|
+
match="VENCTO",
|
|
178
194
|
header=1,
|
|
179
195
|
thousands=".",
|
|
180
196
|
decimal=",",
|
|
@@ -198,6 +214,16 @@ def fetch_raw_df(asset_code: str, trade_date: pd.Timestamp) -> pd.DataFrame:
|
|
|
198
214
|
return df
|
|
199
215
|
|
|
200
216
|
|
|
217
|
+
def _adjust_older_contracts_rates(df: pd.DataFrame, rate_cols: list) -> pd.DataFrame:
|
|
218
|
+
for col in rate_cols:
|
|
219
|
+
df[col] = convert_prices_to_rates(df[col], df["BDaysToExp"], 252)
|
|
220
|
+
|
|
221
|
+
# Invert low and high prices
|
|
222
|
+
df["MinRate"], df["MaxRate"] = df["MaxRate"], df["MinRate"]
|
|
223
|
+
|
|
224
|
+
return df
|
|
225
|
+
|
|
226
|
+
|
|
201
227
|
def rename_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
202
228
|
all_columns = {
|
|
203
229
|
"VENCTO": "ExpirationCode",
|
|
@@ -213,7 +239,7 @@ def rename_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
213
239
|
"PREÇO MÍN.": "MinRate",
|
|
214
240
|
"PREÇO MÉD.": "AvgRate",
|
|
215
241
|
"PREÇO MÁX.": "MaxRate",
|
|
216
|
-
"PREÇO ABERTU.": "
|
|
242
|
+
"PREÇO ABERTU.": "OpenRate",
|
|
217
243
|
"ÚLT. PREÇO": "CloseRate",
|
|
218
244
|
"VAR. PTOS.": "PointsVariation",
|
|
219
245
|
# Attention: bid/ask rates are inverted
|
|
@@ -224,11 +250,10 @@ def rename_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
224
250
|
return df.rename(columns=rename_dict)
|
|
225
251
|
|
|
226
252
|
|
|
227
|
-
def
|
|
228
|
-
|
|
253
|
+
def process_df(
|
|
254
|
+
input_df: pd.DataFrame, trade_date: pd.Timestamp, asset_code: str
|
|
229
255
|
) -> pd.DataFrame:
|
|
230
|
-
df =
|
|
231
|
-
|
|
256
|
+
df = input_df.copy()
|
|
232
257
|
df["TradeDate"] = trade_date
|
|
233
258
|
# Convert to datetime64[ns] since it is pandas default type for timestamps
|
|
234
259
|
df["TradeDate"] = df["TradeDate"].astype("datetime64[ns]")
|
|
@@ -243,6 +268,14 @@ def process_raw_df(
|
|
|
243
268
|
else:
|
|
244
269
|
df["ExpirationDate"] = df["ExpirationCode"].apply(get_expiration_date)
|
|
245
270
|
|
|
271
|
+
df["DaysToExp"] = (df["ExpirationDate"] - trade_date).dt.days
|
|
272
|
+
# Convert to nullable integer, since it is the default type in the library
|
|
273
|
+
df["DaysToExp"] = df["DaysToExp"].astype(pd.Int64Dtype())
|
|
274
|
+
# Remove expired contracts
|
|
275
|
+
df.query("DaysToExp > 0", inplace=True)
|
|
276
|
+
|
|
277
|
+
df["BDaysToExp"] = bday.count_bdays(trade_date, df["ExpirationDate"])
|
|
278
|
+
|
|
246
279
|
# Columns where 0 means NaN
|
|
247
280
|
cols_with_nan = [col for col in df.columns if "Rate" in col]
|
|
248
281
|
if "SettlementPrice" in df.columns:
|
|
@@ -250,10 +283,31 @@ def process_raw_df(
|
|
|
250
283
|
# Replace 0 with NaN in these columns
|
|
251
284
|
df[cols_with_nan] = df[cols_with_nan].replace(0, pd.NA)
|
|
252
285
|
|
|
286
|
+
rate_cols = [col for col in df.columns if "Rate" in col]
|
|
287
|
+
# Prior to 17/01/2002 (inclusive), DI prices were not converted to rates
|
|
288
|
+
if trade_date <= pd.Timestamp("2002-01-17") and asset_code == "DI1":
|
|
289
|
+
df = _adjust_older_contracts_rates(df, rate_cols)
|
|
290
|
+
else:
|
|
291
|
+
# Remove % and round to 5 (3 in %) dec. places in rate columns
|
|
292
|
+
df[rate_cols] = df[rate_cols].div(100).round(5)
|
|
293
|
+
|
|
294
|
+
if COUNT_CONVENTIONS[asset_code] == 252:
|
|
295
|
+
df["SettlementRate"] = convert_prices_to_rates(
|
|
296
|
+
prices=df["SettlementPrice"],
|
|
297
|
+
days_to_expiration=df["BDaysToExp"],
|
|
298
|
+
count_convention=252,
|
|
299
|
+
)
|
|
300
|
+
elif COUNT_CONVENTIONS[asset_code] == 360:
|
|
301
|
+
df["SettlementRate"] = convert_prices_to_rates(
|
|
302
|
+
prices=df["SettlementPrice"],
|
|
303
|
+
days_to_expiration=df["DaysToExp"],
|
|
304
|
+
count_convention=360,
|
|
305
|
+
)
|
|
306
|
+
|
|
253
307
|
return df
|
|
254
308
|
|
|
255
309
|
|
|
256
|
-
def
|
|
310
|
+
def select_and_reorder_columns(df: pd.DataFrame):
|
|
257
311
|
all_columns = [
|
|
258
312
|
"TradeDate",
|
|
259
313
|
"TickerSymbol",
|
|
@@ -269,7 +323,7 @@ def reorder_columns(df: pd.DataFrame):
|
|
|
269
323
|
"FinancialVolume",
|
|
270
324
|
"SettlementPrice",
|
|
271
325
|
"SettlementRate",
|
|
272
|
-
"
|
|
326
|
+
"OpenRate",
|
|
273
327
|
"MinRate",
|
|
274
328
|
"AvgRate",
|
|
275
329
|
"MaxRate",
|
|
@@ -279,3 +333,28 @@ def reorder_columns(df: pd.DataFrame):
|
|
|
279
333
|
]
|
|
280
334
|
reordered_columns = [col for col in all_columns if col in df.columns]
|
|
281
335
|
return df[reordered_columns]
|
|
336
|
+
|
|
337
|
+
|
|
338
|
+
def fetch_historical_df(asset_code: str, trade_date: pd.Timestamp) -> pd.DataFrame:
|
|
339
|
+
"""
|
|
340
|
+
Fetchs the futures data for a given date from B3.
|
|
341
|
+
|
|
342
|
+
This function fetches and processes the futures data from B3 for a specific
|
|
343
|
+
trade date. It's the primary external interface for accessing futures data.
|
|
344
|
+
|
|
345
|
+
Args:
|
|
346
|
+
asset_code (str): The asset code to fetch the futures data.
|
|
347
|
+
trade_date (pd.Timestamp): The trade date to fetch the futures data.
|
|
348
|
+
count_convention (int): The count convention for the DI futures contract.
|
|
349
|
+
Can be 252 business days or 360 calendar days.
|
|
350
|
+
|
|
351
|
+
Returns:
|
|
352
|
+
pd.DataFrame: A Pandas pd.DataFrame containing processed futures data.
|
|
353
|
+
"""
|
|
354
|
+
df_raw = fetch_raw_df(asset_code=asset_code, trade_date=trade_date)
|
|
355
|
+
if df_raw.empty:
|
|
356
|
+
return df_raw
|
|
357
|
+
df = rename_columns(df_raw)
|
|
358
|
+
df = process_df(df, trade_date, asset_code)
|
|
359
|
+
df = select_and_reorder_columns(df)
|
|
360
|
+
return df
|
pyield-0.7.8/pyield/futures/intraday/trading_data.py → pyield-0.7.10/pyield/futures/intraday.py
RENAMED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import pandas as pd
|
|
2
2
|
import requests
|
|
3
3
|
|
|
4
|
+
from .. import bday
|
|
5
|
+
|
|
4
6
|
|
|
5
7
|
def _fetch_raw_df(future_code: str) -> pd.DataFrame:
|
|
6
8
|
"""
|
|
@@ -50,7 +52,7 @@ def _fetch_raw_df(future_code: str) -> pd.DataFrame:
|
|
|
50
52
|
now = pd.Timestamp.now().round("s")
|
|
51
53
|
# Subtract 15 minutes from the current time to account for API delay
|
|
52
54
|
trade_ts = now - pd.Timedelta(minutes=15)
|
|
53
|
-
df["
|
|
55
|
+
df["TradeTime"] = trade_ts
|
|
54
56
|
|
|
55
57
|
# Convert DataFrame to use nullable data types for better type consistency
|
|
56
58
|
df = df.convert_dtypes(dtype_backend="numpy_nullable")
|
|
@@ -58,15 +60,19 @@ def _fetch_raw_df(future_code: str) -> pd.DataFrame:
|
|
|
58
60
|
return df
|
|
59
61
|
|
|
60
62
|
|
|
61
|
-
def
|
|
62
|
-
|
|
63
|
+
def _rename_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
64
|
+
"""
|
|
65
|
+
Rename the columns of a DataFrame containing the futures data.
|
|
66
|
+
|
|
67
|
+
Args:
|
|
68
|
+
df (pd.DataFrame): A DataFrame containing futures data.
|
|
63
69
|
|
|
64
|
-
|
|
70
|
+
Returns:
|
|
71
|
+
pd.DataFrame: A DataFrame with the columns renamed.
|
|
72
|
+
"""
|
|
65
73
|
all_columns = {
|
|
66
|
-
"TradeTimestamp": "TradeTimestamp",
|
|
67
74
|
"symb": "TickerSymbol",
|
|
68
75
|
"mtrtyCode": "ExpirationDate",
|
|
69
|
-
"BDaysToExp": "BDaysToExp",
|
|
70
76
|
"opnCtrcts": "OpenContracts",
|
|
71
77
|
"tradQty": "TradeCount",
|
|
72
78
|
"traddCtrctsQty": "TradeVolume",
|
|
@@ -85,23 +91,69 @@ def _process_raw_df(raw_df: pd.DataFrame) -> pd.DataFrame:
|
|
|
85
91
|
# Check which columns are present in the DataFrame before renaming
|
|
86
92
|
rename_dict = {c: all_columns[c] for c in all_columns if c in df.columns}
|
|
87
93
|
df = df.rename(columns=rename_dict)
|
|
94
|
+
return df
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _process_df(raw_df: pd.DataFrame) -> pd.DataFrame:
|
|
98
|
+
df = raw_df.copy()
|
|
99
|
+
|
|
100
|
+
df["BDaysToExp"] = bday.count_bdays(df["TradeTime"], df["ExpirationDate"])
|
|
88
101
|
|
|
89
|
-
|
|
102
|
+
df["DaysToExp"] = (df["ExpirationDate"] - df["TradeTime"]).dt.days
|
|
103
|
+
# Convert to nullable integer, since it is the default type in the library
|
|
104
|
+
df["DaysToExp"] = df["DaysToExp"].astype(pd.Int64Dtype())
|
|
90
105
|
|
|
91
106
|
# Remove percentage in all rate columns
|
|
92
107
|
rate_cols = [col for col in df.columns if "Rate" in col]
|
|
93
|
-
df[rate_cols] = df[rate_cols]
|
|
108
|
+
df[rate_cols] = df[rate_cols].div(100).round(5)
|
|
109
|
+
|
|
110
|
+
return df
|
|
111
|
+
|
|
94
112
|
|
|
95
|
-
|
|
96
|
-
|
|
113
|
+
def _select_and_reorder_columns(df: pd.DataFrame) -> pd.DataFrame:
|
|
114
|
+
"""
|
|
115
|
+
Select and reorder columns in the DataFrame.
|
|
97
116
|
|
|
117
|
+
Args:
|
|
118
|
+
df (pd.DataFrame): A DataFrame containing futures data.
|
|
98
119
|
|
|
99
|
-
|
|
120
|
+
Returns:
|
|
121
|
+
pd.DataFrame: A DataFrame with the columns selected and reordered.
|
|
100
122
|
"""
|
|
101
|
-
|
|
123
|
+
columns = [
|
|
124
|
+
"TradeTime",
|
|
125
|
+
"TickerSymbol",
|
|
126
|
+
"ExpirationDate",
|
|
127
|
+
"BDaysToExp",
|
|
128
|
+
"OpenContracts",
|
|
129
|
+
"TradeCount",
|
|
130
|
+
"TradeVolume",
|
|
131
|
+
"FinancialVolume",
|
|
132
|
+
"PrevSettlementRate",
|
|
133
|
+
"MinLimitRate",
|
|
134
|
+
"MaxLimitRate",
|
|
135
|
+
"OpenRate",
|
|
136
|
+
"MinRate",
|
|
137
|
+
"AvgRate",
|
|
138
|
+
"MaxRate",
|
|
139
|
+
"CurrentAskRate",
|
|
140
|
+
"CurrentBidRate",
|
|
141
|
+
"CurrentRate",
|
|
142
|
+
]
|
|
143
|
+
return df[columns]
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
def fetch_intraday_df(future_code: str) -> pd.DataFrame:
|
|
147
|
+
"""
|
|
148
|
+
Fetch the latest futures data from B3.
|
|
102
149
|
|
|
103
150
|
Returns:
|
|
104
151
|
pd.DataFrame: A Pandas pd.DataFrame containing the latest DI futures data.
|
|
105
152
|
"""
|
|
106
153
|
raw_df = _fetch_raw_df(future_code)
|
|
107
|
-
|
|
154
|
+
if raw_df.empty:
|
|
155
|
+
return raw_df
|
|
156
|
+
df = _rename_columns(raw_df)
|
|
157
|
+
df = _process_df(df)
|
|
158
|
+
df = _select_and_reorder_columns(df)
|
|
159
|
+
return df
|