luxorasap 0.0.2__tar.gz → 0.1.1__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.
- {luxorasap-0.0.2 → luxorasap-0.1.1}/PKG-INFO +13 -3
- {luxorasap-0.0.2 → luxorasap-0.1.1}/pyproject.toml +8 -4
- {luxorasap-0.0.2 → luxorasap-0.1.1}/src/luxorasap/__init__.py +1 -1
- luxorasap-0.1.1/src/luxorasap/btgapi/__init__.py +16 -0
- luxorasap-0.1.1/src/luxorasap/btgapi/auth.py +57 -0
- luxorasap-0.1.1/src/luxorasap/btgapi/reports.py +188 -0
- luxorasap-0.1.1/src/luxorasap/btgapi/trades.py +181 -0
- {luxorasap-0.0.2 → luxorasap-0.1.1}/src/luxorasap/datareader/core.py +24 -120
- luxorasap-0.1.1/src/luxorasap/ingest/__init__.py +23 -0
- luxorasap-0.1.1/src/luxorasap/ingest/cloud/__init__.py +54 -0
- luxorasap-0.1.1/src/luxorasap/ingest/legacy_local/dataloader.py +280 -0
- luxorasap-0.1.1/src/luxorasap/utils/dataframe/__init__.py +4 -0
- luxorasap-0.1.1/src/luxorasap/utils/dataframe/reader.py +19 -0
- luxorasap-0.1.1/src/luxorasap/utils/dataframe/transforms.py +52 -0
- luxorasap-0.1.1/src/luxorasap/utils/storage/__init__.py +2 -0
- luxorasap-0.1.1/src/luxorasap/utils/storage/blob.py +95 -0
- {luxorasap-0.0.2 → luxorasap-0.1.1}/src/luxorasap.egg-info/PKG-INFO +13 -3
- luxorasap-0.1.1/src/luxorasap.egg-info/SOURCES.txt +32 -0
- {luxorasap-0.0.2 → luxorasap-0.1.1}/src/luxorasap.egg-info/requires.txt +14 -2
- luxorasap-0.1.1/tests/test_btgapi_auth.py +17 -0
- luxorasap-0.1.1/tests/test_btgapi_reports.py +62 -0
- luxorasap-0.1.1/tests/test_btgapi_trades.py +37 -0
- luxorasap-0.1.1/tests/test_ingest_cloud.py +30 -0
- luxorasap-0.1.1/tests/test_ingest_legacy_local.py +21 -0
- luxorasap-0.1.1/tests/test_utils_dataframe.py +8 -0
- luxorasap-0.1.1/tests/test_utils_storage.py +9 -0
- luxorasap-0.0.2/src/luxorasap/utils/__init__.py +0 -0
- luxorasap-0.0.2/src/luxorasap.egg-info/SOURCES.txt +0 -14
- {luxorasap-0.0.2 → luxorasap-0.1.1}/README.md +0 -0
- {luxorasap-0.0.2 → luxorasap-0.1.1}/setup.cfg +0 -0
- {luxorasap-0.0.2 → luxorasap-0.1.1}/src/luxorasap/datareader/__init__.py +0 -0
- {luxorasap-0.0.2/src/luxorasap/ingest → luxorasap-0.1.1/src/luxorasap/utils}/__init__.py +0 -0
- {luxorasap-0.0.2 → luxorasap-0.1.1}/src/luxorasap.egg-info/dependency_links.txt +0 -0
- {luxorasap-0.0.2 → luxorasap-0.1.1}/src/luxorasap.egg-info/entry_points.txt +0 -0
- {luxorasap-0.0.2 → luxorasap-0.1.1}/src/luxorasap.egg-info/top_level.txt +0 -0
- {luxorasap-0.0.2 → luxorasap-0.1.1}/tests/test_datareader.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: luxorasap
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.1.1
|
|
4
4
|
Summary: Luxor’s unified toolbox for data ingestion, querying and analytics.
|
|
5
5
|
Author-email: Luxor Group <backoffice@luxor.com.br>
|
|
6
6
|
License: Proprietary – All rights reserved
|
|
@@ -18,15 +18,25 @@ Requires-Dist: pyarrow>=15.0
|
|
|
18
18
|
Requires-Dist: requests>=2.32
|
|
19
19
|
Requires-Dist: pydantic>=2.7
|
|
20
20
|
Requires-Dist: scipy>=1.13
|
|
21
|
+
Requires-Dist: openpyxl
|
|
22
|
+
Provides-Extra: storage
|
|
23
|
+
Requires-Dist: azure-storage-blob>=12.19; extra == "storage"
|
|
24
|
+
Requires-Dist: pyarrow>=15.0; extra == "storage"
|
|
25
|
+
Provides-Extra: dataframe
|
|
26
|
+
Requires-Dist: pandas>=2.2; extra == "dataframe"
|
|
21
27
|
Provides-Extra: datareader
|
|
22
|
-
Requires-Dist:
|
|
28
|
+
Requires-Dist: luxorasap[dataframe,storage]; extra == "datareader"
|
|
29
|
+
Requires-Dist: numpy>=1.25; extra == "datareader"
|
|
30
|
+
Requires-Dist: scipy>=1.13; extra == "datareader"
|
|
23
31
|
Provides-Extra: ingest
|
|
24
|
-
Requires-Dist:
|
|
32
|
+
Requires-Dist: luxorasap[dataframe,storage]; extra == "ingest"
|
|
33
|
+
Requires-Dist: pandas>=2.2; extra == "ingest"
|
|
25
34
|
Provides-Extra: btgapi
|
|
26
35
|
Requires-Dist: requests>=2.32; extra == "btgapi"
|
|
27
36
|
Requires-Dist: pydantic>=2.7; extra == "btgapi"
|
|
28
37
|
Provides-Extra: dev
|
|
29
38
|
Requires-Dist: pytest>=8.2; extra == "dev"
|
|
39
|
+
Requires-Dist: requests-mock>=1.11; extra == "dev"
|
|
30
40
|
Requires-Dist: black>=24.4.0; extra == "dev"
|
|
31
41
|
Requires-Dist: isort>=5.13; extra == "dev"
|
|
32
42
|
Requires-Dist: bumpver>=2024.3; extra == "dev"
|
|
@@ -10,7 +10,7 @@ build-backend = "setuptools.build_meta"
|
|
|
10
10
|
#############################
|
|
11
11
|
[project]
|
|
12
12
|
name = "luxorasap"
|
|
13
|
-
version = "0.
|
|
13
|
+
version = "0.1.1"
|
|
14
14
|
description = "Luxor’s unified toolbox for data ingestion, querying and analytics."
|
|
15
15
|
readme = "README.md"
|
|
16
16
|
requires-python = ">=3.9"
|
|
@@ -36,15 +36,19 @@ dependencies = [
|
|
|
36
36
|
"requests>=2.32", # usado por btgapi
|
|
37
37
|
"pydantic>=2.7", # usado por btgapi
|
|
38
38
|
"scipy>=1.13",
|
|
39
|
+
"openpyxl"
|
|
39
40
|
]
|
|
40
41
|
|
|
41
42
|
[project.optional-dependencies]
|
|
42
43
|
# mantém para uso futuro ou para quem queira instalação granular
|
|
43
|
-
|
|
44
|
-
|
|
44
|
+
storage = ["azure-storage-blob>=12.19", "pyarrow>=15.0"]
|
|
45
|
+
dataframe = ["pandas>=2.2"]
|
|
46
|
+
datareader = ["luxorasap[storage,dataframe]", "numpy>=1.25", "scipy>=1.13"]
|
|
47
|
+
ingest = ["luxorasap[storage,dataframe]", "pandas>=2.2"]
|
|
45
48
|
btgapi = ["requests>=2.32", "pydantic>=2.7"]
|
|
46
49
|
dev = [
|
|
47
50
|
"pytest>=8.2",
|
|
51
|
+
"requests-mock>=1.11",
|
|
48
52
|
"black>=24.4.0",
|
|
49
53
|
"isort>=5.13",
|
|
50
54
|
"bumpver>=2024.3",
|
|
@@ -70,7 +74,7 @@ exclude = ["tests*"]
|
|
|
70
74
|
# bumpver (sem-ver)
|
|
71
75
|
#############################
|
|
72
76
|
[tool.bumpver]
|
|
73
|
-
current_version = "0.
|
|
77
|
+
current_version = "0.1.1"
|
|
74
78
|
version_pattern = "MAJOR.MINOR.PATCH"
|
|
75
79
|
|
|
76
80
|
# regex explícito – obrigatório no bumpver 2024+
|
|
@@ -13,7 +13,7 @@ from types import ModuleType
|
|
|
13
13
|
try:
|
|
14
14
|
__version__: str = metadata.version(__name__)
|
|
15
15
|
except metadata.PackageNotFoundError: # editable install
|
|
16
|
-
__version__ = "0.
|
|
16
|
+
__version__ = "0.1.1"
|
|
17
17
|
|
|
18
18
|
# ─── Lazy loader ─────────────────────────────────────────────────
|
|
19
19
|
def __getattr__(name: str) -> ModuleType:
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"""Wrapper para as APIs do BTG Pactual."""
|
|
2
|
+
|
|
3
|
+
from .auth import get_access_token, BTGApiError
|
|
4
|
+
from .reports import request_portfolio, await_report_ticket_result, process_zip_to_dfs, request_investors_transactions_report
|
|
5
|
+
from .trades import submit_offshore_equity_trades, await_transaction_ticket_result
|
|
6
|
+
|
|
7
|
+
__all__ = [
|
|
8
|
+
"BTGApiError",
|
|
9
|
+
"get_access_token",
|
|
10
|
+
"request_portfolio",
|
|
11
|
+
"await_report_ticket_result",
|
|
12
|
+
"submit_offshore_equity_trades",
|
|
13
|
+
"await_transaction_ticket_result",
|
|
14
|
+
"process_zip_to_dfs",
|
|
15
|
+
"request_investors_transactions_report"
|
|
16
|
+
]
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import os
|
|
2
|
+
import requests
|
|
3
|
+
from dotenv import load_dotenv
|
|
4
|
+
from loguru import logger
|
|
5
|
+
|
|
6
|
+
__all__ = ["BTGApiError", "get_access_token"]
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class BTGApiError(Exception):
|
|
10
|
+
"""Erro genérico da API do BTG."""
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def get_access_token(*, client_id=None, client_secret=None, test_env: bool = True,
|
|
14
|
+
timeout: int = 20) -> str:
|
|
15
|
+
"""Obtém JWT válido por ~1 h para autenticação nas APIs BTG.
|
|
16
|
+
Args:
|
|
17
|
+
client_id: ID do cliente (opcional, lê de env var se None).
|
|
18
|
+
client_secret: Segredo do cliente (opcional, lê de env var se None).
|
|
19
|
+
test_env: Ambiente de teste (True) ou produção (False).
|
|
20
|
+
timeout: Timeout da requisição em segundos.
|
|
21
|
+
|
|
22
|
+
Returns:
|
|
23
|
+
Token de acesso.
|
|
24
|
+
|
|
25
|
+
Raises:
|
|
26
|
+
BTGApiError: Se as credenciais não estiverem disponíveis ou a requisição falhar.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
if not client_id or not client_secret:
|
|
30
|
+
load_dotenv()
|
|
31
|
+
client_id = os.getenv("BTG_CLIENT_ID")
|
|
32
|
+
client_secret = os.getenv("BTG_CLIENT_SECRET")
|
|
33
|
+
if not client_id or not client_secret:
|
|
34
|
+
raise BTGApiError("BTG_CLIENT_ID ou BTG_CLIENT_SECRET não definidos no ambiente")
|
|
35
|
+
|
|
36
|
+
url = (
|
|
37
|
+
"https://funds-uat.btgpactual.com/connect/token"
|
|
38
|
+
if test_env
|
|
39
|
+
else "https://funds.btgpactual.com/connect/token"
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
resp = requests.post(
|
|
43
|
+
url,
|
|
44
|
+
headers={"Content-Type": "application/x-www-form-urlencoded"},
|
|
45
|
+
data={
|
|
46
|
+
"grant_type": "client_credentials",
|
|
47
|
+
"client_id": client_id,
|
|
48
|
+
"client_secret": client_secret,
|
|
49
|
+
},
|
|
50
|
+
timeout=timeout,
|
|
51
|
+
)
|
|
52
|
+
|
|
53
|
+
if resp.ok:
|
|
54
|
+
token = resp.json().get("access_token")
|
|
55
|
+
logger.debug("Token BTG obtido (len=%s)", len(token) if token else "None")
|
|
56
|
+
return token or ""
|
|
57
|
+
raise BTGApiError(f"Falha ao autenticar: HTTP {resp.status_code} – {resp.text}")
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import datetime as dt
|
|
2
|
+
import io
|
|
3
|
+
import json
|
|
4
|
+
import time
|
|
5
|
+
import zipfile
|
|
6
|
+
from typing import Optional, Dict
|
|
7
|
+
import pandas as pd
|
|
8
|
+
import requests
|
|
9
|
+
from loguru import logger
|
|
10
|
+
|
|
11
|
+
from .auth import BTGApiError
|
|
12
|
+
from luxorasap.utils.dataframe import read_bytes
|
|
13
|
+
|
|
14
|
+
__all__ = [
|
|
15
|
+
"request_portfolio",
|
|
16
|
+
"check_report_ticket",
|
|
17
|
+
"await_report_ticket_result",
|
|
18
|
+
"process_zip_to_dfs",
|
|
19
|
+
"request_investors_transactions_report"
|
|
20
|
+
]
|
|
21
|
+
|
|
22
|
+
_REPORT_ENDPOINT = "https://funds.btgpactual.com/reports/Portfolio"
|
|
23
|
+
_TICKET_ENDPOINT = "https://funds.btgpactual.com/reports/Ticket"
|
|
24
|
+
_INVESTOR_TX_ENDPOINT = (
|
|
25
|
+
"https://funds.btgpactual.com/reports/RTA/InvestorTransactionsFileReport"
|
|
26
|
+
)
|
|
27
|
+
_REPORT_TYPES = {"excel": 10, "xml5": 81, "pdf": 2}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def request_portfolio(token: str, fund_name: str, start_date: dt.date, end_date: dt.date,
|
|
31
|
+
format: str = "excel") -> str:
|
|
32
|
+
"""Envia requisição de carteira; retorna *ticket*.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
token: Token de autenticação.
|
|
36
|
+
fund_name: Nome do fundo.
|
|
37
|
+
start_date: Data de início.
|
|
38
|
+
end_date: Data de fim.
|
|
39
|
+
format: Formato do relatório ("excel", "xml5", "pdf").
|
|
40
|
+
|
|
41
|
+
Returns:
|
|
42
|
+
Ticket da requisição.
|
|
43
|
+
"""
|
|
44
|
+
body = {
|
|
45
|
+
"contract": {
|
|
46
|
+
"startDate": f"{start_date}T00:00:00Z",
|
|
47
|
+
"endDate": f"{end_date}T00:00:00Z",
|
|
48
|
+
"typeReport": _REPORT_TYPES[format],
|
|
49
|
+
"fundName": fund_name,
|
|
50
|
+
},
|
|
51
|
+
"pageSize": 100,
|
|
52
|
+
"webhookEndpoint": "string",
|
|
53
|
+
}
|
|
54
|
+
r = requests.post(
|
|
55
|
+
_REPORT_ENDPOINT,
|
|
56
|
+
headers={"X-SecureConnect-Token": token, "Content-Type": "application/json"},
|
|
57
|
+
json=body,
|
|
58
|
+
timeout=30,
|
|
59
|
+
)
|
|
60
|
+
if r.ok:
|
|
61
|
+
return r.json()["ticket"]
|
|
62
|
+
raise BTGApiError(f"Erro ao solicitar relatório: {r.status_code} – {r.text}")
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _download_url(download_url: str) -> bytes:
|
|
66
|
+
r = requests.get(download_url, timeout=60)
|
|
67
|
+
if r.ok:
|
|
68
|
+
return r.content
|
|
69
|
+
raise BTGApiError(f"Falha no download: {r.status_code} – {r.text}")
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
def check_report_ticket(token: str, ticket: str, *, page: Optional[int] = None) -> bytes:
|
|
73
|
+
"""Consulta único ticket; devolve bytes se pronto, lança BTGApiError caso contrário."""
|
|
74
|
+
|
|
75
|
+
params = {"ticketId": ticket}
|
|
76
|
+
if page is not None:
|
|
77
|
+
params["pageNumber"] = str(page)
|
|
78
|
+
|
|
79
|
+
r = requests.get(
|
|
80
|
+
_TICKET_ENDPOINT,
|
|
81
|
+
params=params,
|
|
82
|
+
headers={"X-SecureConnect-Token": token},
|
|
83
|
+
timeout=30,
|
|
84
|
+
)
|
|
85
|
+
# 1. Se resposta é ZIP direto → retornamos conteúdo
|
|
86
|
+
try:
|
|
87
|
+
payload = r.json()
|
|
88
|
+
except json.JSONDecodeError:
|
|
89
|
+
if r.ok:
|
|
90
|
+
return r.content
|
|
91
|
+
raise BTGApiError(f"Resposta inesperada: {r.status_code} – {r.text}")
|
|
92
|
+
|
|
93
|
+
# 2. Caso contrário tenta decodificar JSON
|
|
94
|
+
|
|
95
|
+
result = payload.get("result")
|
|
96
|
+
if result == "Processando":
|
|
97
|
+
raise BTGApiError("Processando")
|
|
98
|
+
|
|
99
|
+
# 3. Quando pronto, result é JSON string com UrlDownload
|
|
100
|
+
if isinstance(result, str):
|
|
101
|
+
try:
|
|
102
|
+
info: Dict[str, str] = json.loads(result)
|
|
103
|
+
url = info["UrlDownload"]
|
|
104
|
+
return _download_url(url)
|
|
105
|
+
except Exception as exc:
|
|
106
|
+
raise BTGApiError(f"Falha ao interpretar result: {exc}") from exc
|
|
107
|
+
|
|
108
|
+
raise BTGApiError("Formato de resposta desconhecido")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def await_report_ticket_result(token: str, ticket: str, *, attempts: int = 10,
|
|
112
|
+
interval: int = 15) -> bytes:
|
|
113
|
+
"""Espera até que o relatório esteja pronto e devolve conteúdo binário.
|
|
114
|
+
|
|
115
|
+
Args:
|
|
116
|
+
token: Token de autenticação.
|
|
117
|
+
ticket: Ticket da requisição.
|
|
118
|
+
attempts: Número de tentativas.
|
|
119
|
+
interval: Intervalo entre tentativas em segundos.
|
|
120
|
+
|
|
121
|
+
Returns:
|
|
122
|
+
Conteúdo binário do relatório (arquivo ZIP).
|
|
123
|
+
|
|
124
|
+
Raises:
|
|
125
|
+
BTGApiError: Se o relatório não ficar pronto ou falhar.
|
|
126
|
+
"""
|
|
127
|
+
for i in range(attempts):
|
|
128
|
+
try:
|
|
129
|
+
return check_report_ticket(token, ticket)
|
|
130
|
+
except BTGApiError as err:
|
|
131
|
+
if "Processando" in str(err):
|
|
132
|
+
logger.debug("Ticket %s pendente (%d/%d)", ticket, i + 1, attempts)
|
|
133
|
+
time.sleep(interval)
|
|
134
|
+
continue
|
|
135
|
+
raise
|
|
136
|
+
raise BTGApiError("Relatório não ficou pronto no tempo limite")
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def process_zip_to_dfs(zip_bytes: bytes) -> dict[str, pd.DataFrame]:
|
|
140
|
+
"""Extrai todos os arquivos do ZIP e devolve DataFrames por nome."""
|
|
141
|
+
out: dict[str, pd.DataFrame] = {}
|
|
142
|
+
with zipfile.ZipFile(io.BytesIO(zip_bytes)) as zf:
|
|
143
|
+
for name in zf.namelist():
|
|
144
|
+
if name.endswith("/"):
|
|
145
|
+
continue
|
|
146
|
+
out[name] = read_bytes(zf.read(name), filename=name)
|
|
147
|
+
|
|
148
|
+
return out
|
|
149
|
+
|
|
150
|
+
|
|
151
|
+
def request_investors_transactions_report( token: str, query_date: dt.date, *,
|
|
152
|
+
distributors: list[str] | None = None, fund_names: list[str] | None = None,
|
|
153
|
+
consolidate_by_account: bool = True, page_size: int = 100) -> str:
|
|
154
|
+
"""
|
|
155
|
+
Gera um ticket para o relatório de transações de cotistas (RTA).
|
|
156
|
+
Args:
|
|
157
|
+
token: Token de autenticação.
|
|
158
|
+
query_date: Data da consulta.
|
|
159
|
+
distributors: Lista de nomes de distribuidores (opcional).
|
|
160
|
+
fund_names: Lista de nomes de fundos (opcional).
|
|
161
|
+
consolidate_by_account: Consolidar por conta (default True).
|
|
162
|
+
page_size: Tamanho da página (default 100).
|
|
163
|
+
|
|
164
|
+
Retorna *ticket* (string) a ser usado em `await_report_ticket_result`.
|
|
165
|
+
"""
|
|
166
|
+
body = {
|
|
167
|
+
"contract": {
|
|
168
|
+
"distributors": distributors or [],
|
|
169
|
+
"queryDate": f"{query_date.isoformat()}T00:00:00Z",
|
|
170
|
+
"accountNumber": "",
|
|
171
|
+
"consolidateByAccount": str(consolidate_by_account).lower(),
|
|
172
|
+
"fundNames": fund_names or [],
|
|
173
|
+
},
|
|
174
|
+
"pageSize": page_size,
|
|
175
|
+
"webhookEndpoint": "string",
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
r = requests.post(
|
|
179
|
+
_INVESTOR_TX_ENDPOINT,
|
|
180
|
+
headers={"X-SecureConnect-Token": token, "Content-Type": "application/json"},
|
|
181
|
+
json=body,
|
|
182
|
+
timeout=30,
|
|
183
|
+
)
|
|
184
|
+
if r.ok:
|
|
185
|
+
return r.json()["ticket"]
|
|
186
|
+
raise BTGApiError(
|
|
187
|
+
f"Erro InvestorTransactionsFileReport: {r.status_code} – {r.text}"
|
|
188
|
+
)
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import time
|
|
2
|
+
from typing import List, Dict
|
|
3
|
+
|
|
4
|
+
import pandas as pd
|
|
5
|
+
import requests
|
|
6
|
+
from loguru import logger
|
|
7
|
+
|
|
8
|
+
from .auth import BTGApiError
|
|
9
|
+
|
|
10
|
+
__all__ = [
|
|
11
|
+
"submit_offshore_equity_trades",
|
|
12
|
+
"get_submitted_transactions",
|
|
13
|
+
"await_transaction_ticket_result",
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
_EP_SUBMIT_TEST = "https://funds-uat.btgpactual.com/offshore/TradeOffShore/Equity"
|
|
17
|
+
_EP_SUBMIT_PROD = "https://funds.btgpactual.com/offshore/TradeOffShore/Equity"
|
|
18
|
+
_EP_TICKET_TEST = "https://funds-uat.btgpactual.com/offshore/Ticket"
|
|
19
|
+
_EP_TICKET_PROD = "https://funds.btgpactual.com/offshore/Ticket"
|
|
20
|
+
|
|
21
|
+
_MARKET_IDS = {
|
|
22
|
+
"equity": 20,
|
|
23
|
+
"future": 22,
|
|
24
|
+
"bonds": 24,
|
|
25
|
+
"repo": 28,
|
|
26
|
+
"portfolio_swap": 29,
|
|
27
|
+
"interest_rate_swap": 30,
|
|
28
|
+
"performance_swap": 31,
|
|
29
|
+
"variance_swap": 32,
|
|
30
|
+
"equity_option": 33,
|
|
31
|
+
"future_option": 34,
|
|
32
|
+
"fx_option_vanilla": 35,
|
|
33
|
+
"fx_option_barrier": 36,
|
|
34
|
+
"fx": 25,
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def submit_offshore_equity_trades(token: str, trades: list[dict], *, test_env: bool = True) -> str:
|
|
39
|
+
"""
|
|
40
|
+
Submete lista de trades de Equity Offshore para a API do BTG.
|
|
41
|
+
|
|
42
|
+
Args:
|
|
43
|
+
token: Token de autenticação.
|
|
44
|
+
trades: Lista de dicionários representando os trades. Cada dict deve ter
|
|
45
|
+
a estrutura esperada pela API. Modelo:
|
|
46
|
+
[{
|
|
47
|
+
"currency": "USD",
|
|
48
|
+
"price": "60.12",
|
|
49
|
+
"productCodeValue": "...",
|
|
50
|
+
"glAccount": "...",
|
|
51
|
+
"primeBroker": "...",
|
|
52
|
+
"side": "Buy",
|
|
53
|
+
"tradeQuantity": "1000",
|
|
54
|
+
"commissionAmount": "12.50",
|
|
55
|
+
"settlementCurrency": "USD",
|
|
56
|
+
"fXRate": "1.0",
|
|
57
|
+
"externalReference": "TRADE-001",
|
|
58
|
+
"counterparty": "...",
|
|
59
|
+
"fundNickname": "my_fund",
|
|
60
|
+
"orderIdentification": "...",
|
|
61
|
+
"book": "some_book",
|
|
62
|
+
"tradeDate": "2025-02-19T16:03:53.596Z"
|
|
63
|
+
}]
|
|
64
|
+
|
|
65
|
+
test_env: Ambiente de teste (True) ou produção (False).
|
|
66
|
+
|
|
67
|
+
Returns:
|
|
68
|
+
Ticket da requisição.
|
|
69
|
+
|
|
70
|
+
Raises:
|
|
71
|
+
BTGApiError: Se a requisição falhar.
|
|
72
|
+
|
|
73
|
+
"""
|
|
74
|
+
url = _EP_SUBMIT_TEST if test_env else _EP_SUBMIT_PROD
|
|
75
|
+
r = requests.post(
|
|
76
|
+
url,
|
|
77
|
+
headers={
|
|
78
|
+
"X-SecureConnect-Token": token,
|
|
79
|
+
"Content-Type": "application/json-patch+json",
|
|
80
|
+
},
|
|
81
|
+
json={"results": trades},
|
|
82
|
+
timeout=30,
|
|
83
|
+
)
|
|
84
|
+
if r.status_code in (200, 201):
|
|
85
|
+
ticket = r.json()["ticket"]
|
|
86
|
+
logger.debug("Trades submetidos, ticket %s", ticket)
|
|
87
|
+
return ticket
|
|
88
|
+
raise BTGApiError(f"Falha no submit: {r.status_code} – {r.text}")
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def get_submitted_transactions(token: str, *, ticket_id: str = "", start_date: str = "",
|
|
92
|
+
end_date: str = "", market: str = "", test_env: bool = True) -> Dict:
|
|
93
|
+
"""Consulta status detalhado de ticket ou filtro de datas/mercado.
|
|
94
|
+
Args:
|
|
95
|
+
token: Token de autenticação.
|
|
96
|
+
ticket_id: ID do ticket (opcional).
|
|
97
|
+
start_date: Data de início (opcional, formato YYYY-MM-DD).
|
|
98
|
+
end_date: Data de fim (opcional, formato YYYY-MM-DD).
|
|
99
|
+
market: Mercado (opcional. Valores válidos: "equity", "future", "bonds", "repo",
|
|
100
|
+
"portfolio_swap", "interest_rate_swap", "performance_swap", "variance_swap",
|
|
101
|
+
"equity_option", "future_option", "fx_option_vanilla", "fx_option_barrier", "fx"]).
|
|
102
|
+
test_env: Ambiente de teste (True) ou produção (False).
|
|
103
|
+
|
|
104
|
+
Returns:
|
|
105
|
+
Dicionário com os dados da resposta.
|
|
106
|
+
|
|
107
|
+
Raises:
|
|
108
|
+
BTGApiError: Se a requisição falhar ou a resposta for inválida.
|
|
109
|
+
|
|
110
|
+
"""
|
|
111
|
+
|
|
112
|
+
base_url = _EP_TICKET_TEST if test_env else _EP_TICKET_PROD
|
|
113
|
+
|
|
114
|
+
if ticket_id:
|
|
115
|
+
params = {"Ticket": ticket_id, "Detailed": "true"}
|
|
116
|
+
elif start_date and end_date and market:
|
|
117
|
+
params = {
|
|
118
|
+
"StartDate": start_date,
|
|
119
|
+
"EndDate": end_date,
|
|
120
|
+
"Market": _MARKET_IDS.get(market.lower(), market),
|
|
121
|
+
"Detailed": "true",
|
|
122
|
+
}
|
|
123
|
+
else:
|
|
124
|
+
raise BTGApiError("Forneça ticket_id OU start_date+end_date+market")
|
|
125
|
+
|
|
126
|
+
r = requests.get(
|
|
127
|
+
base_url,
|
|
128
|
+
headers={"X-SecureConnect-Token": token},
|
|
129
|
+
params=params,
|
|
130
|
+
timeout=30,
|
|
131
|
+
)
|
|
132
|
+
try:
|
|
133
|
+
return r.json()
|
|
134
|
+
except Exception as exc:
|
|
135
|
+
raise BTGApiError(f"Resposta inválida: {r.status_code}") from exc
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def await_transaction_ticket_result( token: str, ticket_id: str, *, attempts: int = 10,
|
|
139
|
+
interval: int = 30, test_env: bool = True) -> pd.DataFrame:
|
|
140
|
+
"""Espera a conclusão do ticket e devolve DataFrame com metadados.
|
|
141
|
+
Args:
|
|
142
|
+
token: Token de autenticação.
|
|
143
|
+
ticket_id: ID do ticket.
|
|
144
|
+
attempts: Número de tentativas.
|
|
145
|
+
interval: Intervalo entre tentativas em segundos.
|
|
146
|
+
test_env: Ambiente de teste (True) ou produção (False).
|
|
147
|
+
|
|
148
|
+
Returns:
|
|
149
|
+
DataFrame com o status detalhado das transações.
|
|
150
|
+
|
|
151
|
+
Raises:
|
|
152
|
+
BTGApiError: Se o ticket não for finalizado ou falhar.
|
|
153
|
+
|
|
154
|
+
"""
|
|
155
|
+
|
|
156
|
+
cols = ["Status", "Ticket", "TradeId", "Env", "Msg"]
|
|
157
|
+
results = pd.DataFrame(columns=cols)
|
|
158
|
+
|
|
159
|
+
for i in range(attempts):
|
|
160
|
+
data = get_submitted_transactions(token, ticket_id=ticket_id, test_env=test_env)
|
|
161
|
+
trades_info = data["trades"]
|
|
162
|
+
ticket_status = trades_info[0]["Status"].lower()
|
|
163
|
+
|
|
164
|
+
# ambiente de produção pode ficar em pendente; aguardamos
|
|
165
|
+
if ticket_status == "pendente":
|
|
166
|
+
logger.debug("Ticket %s pendente (%d/%d)", ticket_id, i + 1, attempts)
|
|
167
|
+
time.sleep(interval)
|
|
168
|
+
continue
|
|
169
|
+
|
|
170
|
+
trades = trades_info[0]["Details"]["TicketDetalhesEquity"]
|
|
171
|
+
for tr in trades:
|
|
172
|
+
results.loc[len(results)] = [
|
|
173
|
+
tr["stateItemFile"].lower(),
|
|
174
|
+
ticket_id.lower(),
|
|
175
|
+
tr["externalReference"],
|
|
176
|
+
"test" if test_env else "prod",
|
|
177
|
+
tr["mensagens"],
|
|
178
|
+
]
|
|
179
|
+
return results
|
|
180
|
+
|
|
181
|
+
raise BTGApiError("Ticket não finalizado no tempo limite")
|