PYield 0.31.4__tar.gz → 0.32.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyield-0.31.4 → pyield-0.32.0}/PKG-INFO +1 -1
- pyield-0.32.0/pyield/__about__.py +1 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/__init__.py +2 -2
- {pyield-0.31.4 → pyield-0.32.0}/pyield/anbima/tpf.py +2 -2
- {pyield-0.31.4 → pyield-0.32.0}/pyield/b3/di.py +34 -24
- pyield-0.32.0/pyield/data_cache.py +184 -0
- pyield-0.31.4/pyield/forwards.py → pyield-0.32.0/pyield/forward.py +1 -1
- pyield-0.32.0/pyield/retry.py +56 -0
- pyield-0.31.4/pyield/__about__.py +0 -1
- pyield-0.31.4/pyield/data_cache.py +0 -73
- pyield-0.31.4/pyield/retry.py +0 -39
- {pyield-0.31.4 → pyield-0.32.0}/.gitignore +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/LICENSE +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/README.md +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/anbima/__init__.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/anbima/ettj.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/anbima/ima.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/anbima/imaq.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/anbima/ipca.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/b3/__init__.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/b3/di_over.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/b3/futures/__init__.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/b3/futures/common.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/b3/futures/historical.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/b3/futures/intraday.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/b3/futures/xml.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/bc/__init__.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/bc/auction.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/bc/bcdata.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/bc/ptax.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/bc/repo.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/bc/trade.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/bc/vna.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/bday.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/conftest.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/date_converter.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/holidays/__init__.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/holidays/br_holidays_new.txt +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/holidays/br_holidays_old.txt +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/holidays/brholidays.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/ibge/__init__.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/ibge/ipca.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/interpolator.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/py.typed +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/tpf/__init__.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/tpf/lft.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/tpf/ltn.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/tpf/ntnb.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/tpf/ntnc.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/tpf/ntnf.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyield/tpf/tools.py +0 -0
- {pyield-0.31.4 → pyield-0.32.0}/pyproject.toml +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
__version__ = "0.32.0"
|
|
@@ -4,7 +4,7 @@ from pyield import anbima, b3, bc, bday, ibge
|
|
|
4
4
|
from pyield.__about__ import __version__
|
|
5
5
|
from pyield.b3 import futures
|
|
6
6
|
from pyield.b3.di import DIFutures
|
|
7
|
-
from pyield.
|
|
7
|
+
from pyield.forward import forwards
|
|
8
8
|
from pyield.interpolator import Interpolator
|
|
9
9
|
from pyield.tpf import lft, ltn, ntnb, ntnc, ntnf
|
|
10
10
|
|
|
@@ -13,7 +13,7 @@ __all__ = [
|
|
|
13
13
|
"anbima",
|
|
14
14
|
"bday",
|
|
15
15
|
"DIFutures",
|
|
16
|
-
"
|
|
16
|
+
"forwards",
|
|
17
17
|
"futures",
|
|
18
18
|
"ibge",
|
|
19
19
|
"Interpolator",
|
|
@@ -5,7 +5,7 @@ from urllib.error import HTTPError
|
|
|
5
5
|
import pandas as pd
|
|
6
6
|
|
|
7
7
|
from pyield import bday
|
|
8
|
-
from pyield.data_cache import
|
|
8
|
+
from pyield.data_cache import get_tpf_dataset
|
|
9
9
|
from pyield.date_converter import DateScalar, convert_input_dates
|
|
10
10
|
from pyield.retry import default_retry
|
|
11
11
|
|
|
@@ -154,7 +154,7 @@ def tpf_data(
|
|
|
154
154
|
- Price: The price (PU) of the bond.
|
|
155
155
|
"""
|
|
156
156
|
|
|
157
|
-
df =
|
|
157
|
+
df = get_tpf_dataset()
|
|
158
158
|
date = convert_input_dates(date)
|
|
159
159
|
df = df.query("ReferenceDate == @date").reset_index(drop=True)
|
|
160
160
|
|
|
@@ -1,15 +1,19 @@
|
|
|
1
1
|
import logging
|
|
2
|
+
from datetime import datetime as dt
|
|
3
|
+
from zoneinfo import ZoneInfo
|
|
2
4
|
|
|
3
5
|
import pandas as pd
|
|
4
6
|
|
|
5
7
|
import pyield.date_converter as dc
|
|
6
|
-
import pyield.
|
|
8
|
+
import pyield.forward as fw
|
|
7
9
|
from pyield import b3, bday, interpolator
|
|
8
|
-
from pyield.data_cache import
|
|
10
|
+
from pyield.data_cache import get_di_dataset, get_tpf_dataset
|
|
9
11
|
from pyield.date_converter import DateArray, DateScalar
|
|
10
12
|
|
|
11
13
|
logger = logging.getLogger(__name__)
|
|
12
14
|
|
|
15
|
+
TIMEZONE_BZ = ZoneInfo("America/Sao_Paulo")
|
|
16
|
+
|
|
13
17
|
|
|
14
18
|
class DIFutures:
|
|
15
19
|
"""
|
|
@@ -50,18 +54,6 @@ class DIFutures:
|
|
|
50
54
|
4 2024-10-16 2025-03-01 0.1157 0.12343
|
|
51
55
|
"""
|
|
52
56
|
|
|
53
|
-
historical_dates = (
|
|
54
|
-
get_di_dataset()
|
|
55
|
-
.drop_duplicates(subset=["TradeDate"])["TradeDate"]
|
|
56
|
-
.sort_values(ascending=True)
|
|
57
|
-
.reset_index(drop=True)
|
|
58
|
-
)
|
|
59
|
-
"""
|
|
60
|
-
pd.Series: Sorted series of unique trade dates available in the DI dataset.
|
|
61
|
-
It does not include the intraday date. It can be used to check for available
|
|
62
|
-
historical data.
|
|
63
|
-
"""
|
|
64
|
-
|
|
65
57
|
def __init__(
|
|
66
58
|
self,
|
|
67
59
|
date: DateScalar | None = None,
|
|
@@ -71,10 +63,6 @@ class DIFutures:
|
|
|
71
63
|
):
|
|
72
64
|
"""
|
|
73
65
|
Initialize the DIFutures instance with the specified parameters.
|
|
74
|
-
|
|
75
|
-
Note:
|
|
76
|
-
The DI contract DataFrame is not loaded during initialization. It will be
|
|
77
|
-
automatically loaded the first time the df() method is accessed.
|
|
78
66
|
"""
|
|
79
67
|
self._df = pd.DataFrame()
|
|
80
68
|
self._dirty = True # Attribute to track if the df needs updating
|
|
@@ -84,6 +72,25 @@ class DIFutures:
|
|
|
84
72
|
self.pre_filter = pre_filter
|
|
85
73
|
self.all_columns = all_columns
|
|
86
74
|
|
|
75
|
+
@staticmethod
|
|
76
|
+
def _get_historical_dates():
|
|
77
|
+
"""
|
|
78
|
+
Returns a sorted series of unique trade dates available in the DI dataset.
|
|
79
|
+
"""
|
|
80
|
+
return (
|
|
81
|
+
get_di_dataset()
|
|
82
|
+
.drop_duplicates(subset=["TradeDate"])["TradeDate"]
|
|
83
|
+
.sort_values(ascending=True)
|
|
84
|
+
.reset_index(drop=True)
|
|
85
|
+
)
|
|
86
|
+
|
|
87
|
+
@property
|
|
88
|
+
def historical_dates(self):
|
|
89
|
+
"""
|
|
90
|
+
pd.Series: Sorted series of unique trade dates available in the DI dataset.
|
|
91
|
+
"""
|
|
92
|
+
return self._get_historical_dates()
|
|
93
|
+
|
|
87
94
|
@property
|
|
88
95
|
def df(self) -> pd.DataFrame:
|
|
89
96
|
"""
|
|
@@ -129,7 +136,7 @@ class DIFutures:
|
|
|
129
136
|
|
|
130
137
|
if self._pre_filter:
|
|
131
138
|
df_pre = (
|
|
132
|
-
|
|
139
|
+
get_tpf_dataset()
|
|
133
140
|
.query("BondType in ['LTN', 'NTN-F']")
|
|
134
141
|
.query("ReferenceDate == @self._date")[
|
|
135
142
|
["ReferenceDate", "MaturityDate"]
|
|
@@ -197,7 +204,7 @@ class DIFutures:
|
|
|
197
204
|
if df.empty:
|
|
198
205
|
return pd.DataFrame()
|
|
199
206
|
|
|
200
|
-
df["ForwardRate"] =
|
|
207
|
+
df["ForwardRate"] = fw.forwards(
|
|
201
208
|
business_days=bday.count(df["TradeDate"], df["ExpirationDate"]),
|
|
202
209
|
zero_rates=df["SettlementRate"],
|
|
203
210
|
groupby_dates=df["TradeDate"],
|
|
@@ -363,10 +370,13 @@ class DIFutures:
|
|
|
363
370
|
def date(self, value: DateScalar | None):
|
|
364
371
|
if value:
|
|
365
372
|
self._date = dc.convert_input_dates(value)
|
|
366
|
-
bz_today =
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
373
|
+
bz_today = dt.now(TIMEZONE_BZ).date()
|
|
374
|
+
if self._date.date() > bz_today:
|
|
375
|
+
raise ValueError(
|
|
376
|
+
f"DI date ({self._date}) after current date {bz_today}"
|
|
377
|
+
)
|
|
378
|
+
else:
|
|
379
|
+
self._date = None
|
|
370
380
|
self._dirty = True
|
|
371
381
|
|
|
372
382
|
@property
|
|
@@ -0,0 +1,184 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Módulo para carregamento e cache de dados financeiros.
|
|
3
|
+
Fornece acesso a dados DI e TPF com atualizações automáticas.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
import logging
|
|
7
|
+
from functools import wraps
|
|
8
|
+
from typing import Any, Dict
|
|
9
|
+
from zoneinfo import ZoneInfo
|
|
10
|
+
|
|
11
|
+
import pandas as pd
|
|
12
|
+
import requests
|
|
13
|
+
|
|
14
|
+
from pyield import bday
|
|
15
|
+
|
|
16
|
+
# Configuração de logging
|
|
17
|
+
logger = logging.getLogger(__name__)
|
|
18
|
+
|
|
19
|
+
# Configurações
|
|
20
|
+
TIMEZONE_BZ = ZoneInfo("America/Sao_Paulo")
|
|
21
|
+
|
|
22
|
+
GIT_URL = "https://raw.githubusercontent.com/crdcj/pyield-data/main"
|
|
23
|
+
DI_URL = f"{GIT_URL}/data/b3_di.pkl.gz"
|
|
24
|
+
TPF_URL = f"{GIT_URL}/data/anbima_tpf.pkl.gz"
|
|
25
|
+
|
|
26
|
+
# Configuração de datasets
|
|
27
|
+
DATASETS_CONFIG = {
|
|
28
|
+
"di": {"url": f"{DI_URL}", "date_column": "TradeDate"},
|
|
29
|
+
"tpf": {"url": f"{TPF_URL}", "date_column": "ReferenceDate"},
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def singleton(cls):
|
|
34
|
+
"""Decorator para implementar o padrão Singleton."""
|
|
35
|
+
instances = {}
|
|
36
|
+
|
|
37
|
+
@wraps(cls)
|
|
38
|
+
def get_instance(*args, **kwargs):
|
|
39
|
+
if cls not in instances:
|
|
40
|
+
instances[cls] = cls(*args, **kwargs)
|
|
41
|
+
return instances[cls]
|
|
42
|
+
|
|
43
|
+
return get_instance
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
@singleton
|
|
47
|
+
class DataCache:
|
|
48
|
+
"""
|
|
49
|
+
Classe para gerenciar o cache de datasets financeiros.
|
|
50
|
+
Implementa o padrão Singleton para garantir uma única instância.
|
|
51
|
+
"""
|
|
52
|
+
|
|
53
|
+
def __init__(self):
|
|
54
|
+
"""Inicializa o cache de dados vazio."""
|
|
55
|
+
self._datasets: Dict[str, Dict[str, Any]] = {}
|
|
56
|
+
|
|
57
|
+
# Inicializar configurações de datasets
|
|
58
|
+
for key, config in DATASETS_CONFIG.items():
|
|
59
|
+
self._datasets[key] = {
|
|
60
|
+
"url": config["url"],
|
|
61
|
+
"date_column": config["date_column"],
|
|
62
|
+
"df": None,
|
|
63
|
+
"last_date": None,
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
def _should_update_dataset(self, key: str) -> bool:
|
|
67
|
+
"""
|
|
68
|
+
Verifica se o dataset precisa ser atualizado.
|
|
69
|
+
|
|
70
|
+
Args:
|
|
71
|
+
key: Identificador do dataset
|
|
72
|
+
|
|
73
|
+
Returns:
|
|
74
|
+
True se o dataset precisar ser atualizado, False caso contrário
|
|
75
|
+
"""
|
|
76
|
+
data_info = self._datasets[key]
|
|
77
|
+
last_date_in_dataset = data_info["last_date"]
|
|
78
|
+
|
|
79
|
+
# Se nunca foi carregado, deve atualizar
|
|
80
|
+
if last_date_in_dataset is None:
|
|
81
|
+
return True
|
|
82
|
+
|
|
83
|
+
bz_last_bday = bday.last_business_day().date()
|
|
84
|
+
n_bdays = bday.count(last_date_in_dataset, bz_last_bday)
|
|
85
|
+
|
|
86
|
+
# Atualizar se houver mais de 1 dia útil de diferença
|
|
87
|
+
return n_bdays > 1 # noqa
|
|
88
|
+
|
|
89
|
+
def _load_dataset(self, key: str) -> None:
|
|
90
|
+
"""
|
|
91
|
+
Carrega ou atualiza um dataset.
|
|
92
|
+
|
|
93
|
+
Args:
|
|
94
|
+
key: Identificador do dataset
|
|
95
|
+
|
|
96
|
+
Raises:
|
|
97
|
+
requests.RequestException: Se ocorrer erro ao fazer download dos dados
|
|
98
|
+
ValueError: Se o formato dos dados for inválido
|
|
99
|
+
"""
|
|
100
|
+
dataset_info = self._datasets[key]
|
|
101
|
+
|
|
102
|
+
try:
|
|
103
|
+
logger.info(f"Carregando dataset {key} de {dataset_info['url']}")
|
|
104
|
+
df = pd.read_pickle(dataset_info["url"])
|
|
105
|
+
|
|
106
|
+
# Validar se o DataFrame tem a coluna de data esperada
|
|
107
|
+
date_column = dataset_info["date_column"]
|
|
108
|
+
if date_column not in df.columns:
|
|
109
|
+
raise ValueError(
|
|
110
|
+
f"Coluna de data '{date_column}' não encontrada no dataset '{key}'"
|
|
111
|
+
)
|
|
112
|
+
|
|
113
|
+
dataset_info["df"] = df
|
|
114
|
+
dataset_info["last_date"] = df[date_column].max().date()
|
|
115
|
+
|
|
116
|
+
logger.info(
|
|
117
|
+
f"Dataset {key} atualizado. Última data: {dataset_info['last_date']}"
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
except requests.RequestException as e:
|
|
121
|
+
logger.error(f"Erro ao fazer download do dataset {key}: {str(e)}")
|
|
122
|
+
raise
|
|
123
|
+
except Exception as e:
|
|
124
|
+
logger.error(f"Erro ao processar dataset {key}: {str(e)}")
|
|
125
|
+
raise
|
|
126
|
+
|
|
127
|
+
def get_dataframe(self, key: str) -> pd.DataFrame:
|
|
128
|
+
"""
|
|
129
|
+
Obtém um DataFrame, carregando-o ou atualizando-o se necessário.
|
|
130
|
+
|
|
131
|
+
Args:
|
|
132
|
+
key: Identificador do dataset
|
|
133
|
+
|
|
134
|
+
Returns:
|
|
135
|
+
DataFrame com os dados solicitados
|
|
136
|
+
|
|
137
|
+
Raises:
|
|
138
|
+
ValueError: Se o dataset não estiver configurado
|
|
139
|
+
Exception: Se ocorrer erro ao carregar os dados
|
|
140
|
+
"""
|
|
141
|
+
dataset_info = self._datasets.get(key)
|
|
142
|
+
|
|
143
|
+
if dataset_info is None:
|
|
144
|
+
logger.error(f"Dataset '{key}' não está configurado")
|
|
145
|
+
raise ValueError(f"Dataset '{key}' não está configurado")
|
|
146
|
+
|
|
147
|
+
try:
|
|
148
|
+
if dataset_info["df"] is None or self._should_update_dataset(key):
|
|
149
|
+
self._load_dataset(key)
|
|
150
|
+
|
|
151
|
+
return dataset_info["df"].copy()
|
|
152
|
+
except Exception as e:
|
|
153
|
+
logger.error(f"Erro ao obter dataset {key}: {str(e)}")
|
|
154
|
+
raise e
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
# Instância única da classe para uso interno da biblioteca
|
|
158
|
+
_data_cache = DataCache()
|
|
159
|
+
|
|
160
|
+
|
|
161
|
+
def get_di_dataset() -> pd.DataFrame:
|
|
162
|
+
"""
|
|
163
|
+
Obtém o dataset de DI (Depósito Interfinanceiro).
|
|
164
|
+
|
|
165
|
+
Returns:
|
|
166
|
+
DataFrame com os dados de DI
|
|
167
|
+
|
|
168
|
+
Raises:
|
|
169
|
+
Exception: Se ocorrer erro ao carregar os dados
|
|
170
|
+
"""
|
|
171
|
+
return _data_cache.get_dataframe("di")
|
|
172
|
+
|
|
173
|
+
|
|
174
|
+
def get_tpf_dataset() -> pd.DataFrame:
|
|
175
|
+
"""
|
|
176
|
+
Obtém o dataset de TPF (Títulos Públicos Federais).
|
|
177
|
+
|
|
178
|
+
Returns:
|
|
179
|
+
DataFrame com os dados de TPF
|
|
180
|
+
|
|
181
|
+
Raises:
|
|
182
|
+
Exception: Se ocorrer erro ao carregar os dados
|
|
183
|
+
"""
|
|
184
|
+
return _data_cache.get_dataframe("tpf")
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import time
|
|
3
|
+
from urllib.error import HTTPError, URLError
|
|
4
|
+
|
|
5
|
+
import pandas as pd
|
|
6
|
+
import tenacity
|
|
7
|
+
|
|
8
|
+
logger = logging.getLogger(__name__)
|
|
9
|
+
|
|
10
|
+
# Default timeout values for HTTP requests (connect_timeout, read_timeout)
|
|
11
|
+
DEFAULT_TIMEOUT = (2, 10)
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def retry_if_recoverable(exception):
|
|
15
|
+
"""
|
|
16
|
+
Determine if the exception is worth retrying.
|
|
17
|
+
"""
|
|
18
|
+
if isinstance(exception, HTTPError):
|
|
19
|
+
# Don't retry for 404 errors
|
|
20
|
+
if exception.code == 404: # noqa
|
|
21
|
+
logger.info("Resource not found (404), not retrying.")
|
|
22
|
+
return False
|
|
23
|
+
|
|
24
|
+
# Don't retry for 504 Gateway Timeout after first attempt
|
|
25
|
+
# These often indicate longer-term issues with API
|
|
26
|
+
if exception.code == 504: # noqa
|
|
27
|
+
logger.warning(
|
|
28
|
+
"Gateway Timeout (504) from API - likely service degradation."
|
|
29
|
+
)
|
|
30
|
+
# Check if we've already retried for this 504
|
|
31
|
+
retry_state = getattr(retry_if_recoverable, "retry_for_504", 0)
|
|
32
|
+
if retry_state >= 1:
|
|
33
|
+
logger.error("Multiple 504 errors - BC API appears to be unavailable.")
|
|
34
|
+
return False
|
|
35
|
+
# Allow just one retry for 504 with a longer wait
|
|
36
|
+
retry_if_recoverable.retry_for_504 = retry_state + 1
|
|
37
|
+
time.sleep(2) # Immediate wait before retry
|
|
38
|
+
return True
|
|
39
|
+
|
|
40
|
+
# Reset the 504 counter for other types of errors
|
|
41
|
+
retry_if_recoverable.retry_for_504 = 0
|
|
42
|
+
|
|
43
|
+
# Retry for other HTTP errors, URLErrors, and parsing errors
|
|
44
|
+
return isinstance(
|
|
45
|
+
exception,
|
|
46
|
+
(HTTPError, URLError, pd.errors.EmptyDataError, pd.errors.ParserError),
|
|
47
|
+
)
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
# Custom retry configuration that handles parsing errors
|
|
51
|
+
default_retry = tenacity.retry(
|
|
52
|
+
retry=tenacity.retry_if_exception(retry_if_recoverable),
|
|
53
|
+
stop=(tenacity.stop_after_attempt(3) | tenacity.stop_after_delay(15)),
|
|
54
|
+
wait=tenacity.wait_exponential(multiplier=0.5, min=0.5, max=2),
|
|
55
|
+
before_sleep=tenacity.before_sleep_log(logger, logging.WARNING),
|
|
56
|
+
)
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
__version__ = "0.31.4"
|
|
@@ -1,73 +0,0 @@
|
|
|
1
|
-
from datetime import datetime as dt
|
|
2
|
-
from zoneinfo import ZoneInfo
|
|
3
|
-
|
|
4
|
-
import pandas as pd
|
|
5
|
-
|
|
6
|
-
from pyield import bday
|
|
7
|
-
|
|
8
|
-
TIMEZONE_BZ = ZoneInfo("America/Sao_Paulo")
|
|
9
|
-
|
|
10
|
-
UPDATE_HOUR = 21
|
|
11
|
-
GIT_URL = "https://raw.githubusercontent.com/crdcj/pyield-data/main"
|
|
12
|
-
|
|
13
|
-
DI_FILE = "data/b3_di.pkl.gz"
|
|
14
|
-
DI_URL = f"{GIT_URL}/{DI_FILE}"
|
|
15
|
-
|
|
16
|
-
ANBIMA_FILE = "data/anbima_tpf.pkl.gz"
|
|
17
|
-
ANBIMA_URL = f"{GIT_URL}/{ANBIMA_FILE}"
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
class DataCache:
|
|
21
|
-
_instance = None
|
|
22
|
-
|
|
23
|
-
def __new__(cls):
|
|
24
|
-
if cls._instance is None:
|
|
25
|
-
cls._instance = super(DataCache, cls).__new__(cls)
|
|
26
|
-
return cls._instance
|
|
27
|
-
|
|
28
|
-
def __init__(self):
|
|
29
|
-
self._datasets = {
|
|
30
|
-
"di": {"url": DI_URL, "df": None, "last_date": None},
|
|
31
|
-
"tpf": {"url": ANBIMA_URL, "df": None, "last_date": None},
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
def _should_update_dataset(self, key) -> bool:
|
|
35
|
-
data_info = self._datasets[key]
|
|
36
|
-
last_date_in_dataset = data_info["last_date"]
|
|
37
|
-
bz_now = dt.now(TIMEZONE_BZ)
|
|
38
|
-
bz_hour = bz_now.hour
|
|
39
|
-
bz_today = bz_now.date()
|
|
40
|
-
bz_last_bday = bday.offset(bz_today, 0, roll="backward").date()
|
|
41
|
-
condition1 = bz_hour >= UPDATE_HOUR
|
|
42
|
-
condition2 = bz_last_bday != last_date_in_dataset
|
|
43
|
-
return condition1 and condition2
|
|
44
|
-
|
|
45
|
-
def _load_dataset(self, key: str):
|
|
46
|
-
dataset_info = self._datasets[key]
|
|
47
|
-
df = pd.read_pickle(dataset_info["url"])
|
|
48
|
-
dataset_info["df"] = df
|
|
49
|
-
if key == "di":
|
|
50
|
-
dataset_info["last_date"] = df["TradeDate"].max().date()
|
|
51
|
-
elif key == "tpf":
|
|
52
|
-
dataset_info["last_date"] = df["ReferenceDate"].max().date()
|
|
53
|
-
|
|
54
|
-
def get_dataframe(self, key: str) -> pd.DataFrame:
|
|
55
|
-
dataset_info = self._datasets.get(key)
|
|
56
|
-
if dataset_info is None:
|
|
57
|
-
raise ValueError(f"Dataset '{key}' não está configurado.")
|
|
58
|
-
if dataset_info["df"] is None or self._should_update_dataset(key):
|
|
59
|
-
self._load_dataset(key)
|
|
60
|
-
return dataset_info["df"].copy()
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
# Instância única da classe para uso interno da biblioteca
|
|
64
|
-
_data_cache = DataCache()
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
# Funções de acesso para os módulos
|
|
68
|
-
def get_di_dataset():
|
|
69
|
-
return _data_cache.get_dataframe("di")
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
def get_anbima_dataset():
|
|
73
|
-
return _data_cache.get_dataframe("tpf")
|
pyield-0.31.4/pyield/retry.py
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import logging
|
|
2
|
-
from urllib.error import HTTPError, URLError
|
|
3
|
-
|
|
4
|
-
import pandas as pd
|
|
5
|
-
import tenacity
|
|
6
|
-
|
|
7
|
-
logger = logging.getLogger(__name__)
|
|
8
|
-
|
|
9
|
-
# Default timeout values for HTTP requests (connect_timeout, read_timeout)
|
|
10
|
-
DEFAULT_TIMEOUT = (3.05, 30)
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
def retry_if_not_404(exception):
|
|
14
|
-
"""
|
|
15
|
-
Retry if the exception is not a 404 error.
|
|
16
|
-
|
|
17
|
-
Args:
|
|
18
|
-
exception: The exception that was raised
|
|
19
|
-
|
|
20
|
-
Returns:
|
|
21
|
-
Boolean indicating whether to retry
|
|
22
|
-
"""
|
|
23
|
-
# Don't retry for 404 errors (resource doesn't exist)
|
|
24
|
-
if isinstance(exception, HTTPError) and exception.code == 404: # noqa
|
|
25
|
-
logger.info("Resource not found (404), not retrying.")
|
|
26
|
-
return False
|
|
27
|
-
# Retry for parsing errors and other connection errors
|
|
28
|
-
return isinstance(
|
|
29
|
-
exception, (URLError, pd.errors.EmptyDataError, pd.errors.ParserError)
|
|
30
|
-
)
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
# Custom retry configuration that handles parsing errors
|
|
34
|
-
default_retry = tenacity.retry(
|
|
35
|
-
retry=tenacity.retry_if_exception(retry_if_not_404),
|
|
36
|
-
stop=tenacity.stop_after_attempt(5),
|
|
37
|
-
wait=tenacity.wait_exponential(multiplier=1, min=1, max=10),
|
|
38
|
-
before_sleep=tenacity.before_sleep_log(logger, logging.WARNING),
|
|
39
|
-
)
|
|
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
|