tapebasis 1.0.0__py3-none-any.whl

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.
Files changed (114) hide show
  1. tapebasis/__init__.py +32 -0
  2. tapebasis/bb_access.py +597 -0
  3. tapebasis/blocking_pool.py +101 -0
  4. tapebasis/catalog.py +418 -0
  5. tapebasis/freshness.py +248 -0
  6. tapebasis/fv_access.py +190 -0
  7. tapebasis/health.py +806 -0
  8. tapebasis/http_pool.py +157 -0
  9. tapebasis/merge_helpers.py +466 -0
  10. tapebasis/providers/finviz/core/__init__.py +1 -0
  11. tapebasis/providers/finviz/core/errors.py +136 -0
  12. tapebasis/providers/finviz/core/services/__init__.py +1 -0
  13. tapebasis/providers/finviz/core/services/chart_service.py +314 -0
  14. tapebasis/providers/finviz/core/services/filings_service.py +125 -0
  15. tapebasis/providers/finviz/core/services/finviz_client.py +393 -0
  16. tapebasis/providers/finviz/core/services/html_utils.py +426 -0
  17. tapebasis/providers/finviz/core/services/insider_service.py +202 -0
  18. tapebasis/providers/finviz/core/services/market_service.py +176 -0
  19. tapebasis/providers/finviz/core/services/news_service.py +95 -0
  20. tapebasis/providers/finviz/core/services/quote_service.py +527 -0
  21. tapebasis/providers/finviz/core/services/screener_service.py +242 -0
  22. tapebasis/providers/finviz/core/utils/__init__.py +1 -0
  23. tapebasis/providers/finviz/core/utils/validators.py +202 -0
  24. tapebasis/providers/tradingview/coinlist/ace.txt +98 -0
  25. tapebasis/providers/tradingview/coinlist/all.txt +1948 -0
  26. tapebasis/providers/tradingview/coinlist/ams.txt +108 -0
  27. tapebasis/providers/tradingview/coinlist/asx.txt +200 -0
  28. tapebasis/providers/tradingview/coinlist/binance.txt +365 -0
  29. tapebasis/providers/tradingview/coinlist/bist.txt +478 -0
  30. tapebasis/providers/tradingview/coinlist/bitfinex.txt +176 -0
  31. tapebasis/providers/tradingview/coinlist/bitget.txt +266 -0
  32. tapebasis/providers/tradingview/coinlist/bme.txt +307 -0
  33. tapebasis/providers/tradingview/coinlist/bru.txt +110 -0
  34. tapebasis/providers/tradingview/coinlist/bse.txt +4770 -0
  35. tapebasis/providers/tradingview/coinlist/bursa.txt +101 -0
  36. tapebasis/providers/tradingview/coinlist/bybit.txt +257 -0
  37. tapebasis/providers/tradingview/coinlist/chn.txt +194 -0
  38. tapebasis/providers/tradingview/coinlist/coinbase.txt +241 -0
  39. tapebasis/providers/tradingview/coinlist/egx.txt +292 -0
  40. tapebasis/providers/tradingview/coinlist/epa.txt +612 -0
  41. tapebasis/providers/tradingview/coinlist/fwb.txt +2441 -0
  42. tapebasis/providers/tradingview/coinlist/gateio.txt +809 -0
  43. tapebasis/providers/tradingview/coinlist/hk.txt +107 -0
  44. tapebasis/providers/tradingview/coinlist/hkex.txt +107 -0
  45. tapebasis/providers/tradingview/coinlist/hsi.txt +107 -0
  46. tapebasis/providers/tradingview/coinlist/huobi.txt +1091 -0
  47. tapebasis/providers/tradingview/coinlist/idx.txt +844 -0
  48. tapebasis/providers/tradingview/coinlist/klse.txt +101 -0
  49. tapebasis/providers/tradingview/coinlist/kucoin.txt +1006 -0
  50. tapebasis/providers/tradingview/coinlist/leap.txt +95 -0
  51. tapebasis/providers/tradingview/coinlist/lis.txt +36 -0
  52. tapebasis/providers/tradingview/coinlist/lse.txt +2841 -0
  53. tapebasis/providers/tradingview/coinlist/mexc.txt +420 -0
  54. tapebasis/providers/tradingview/coinlist/mil.txt +1392 -0
  55. tapebasis/providers/tradingview/coinlist/myx.txt +101 -0
  56. tapebasis/providers/tradingview/coinlist/nasdaq.txt +4997 -0
  57. tapebasis/providers/tradingview/coinlist/nse.txt +3191 -0
  58. tapebasis/providers/tradingview/coinlist/nyse.txt +2744 -0
  59. tapebasis/providers/tradingview/coinlist/okx.txt +330 -0
  60. tapebasis/providers/tradingview/coinlist/six.txt +381 -0
  61. tapebasis/providers/tradingview/coinlist/sse.txt +103 -0
  62. tapebasis/providers/tradingview/coinlist/szse.txt +91 -0
  63. tapebasis/providers/tradingview/coinlist/tpex.txt +1339 -0
  64. tapebasis/providers/tradingview/coinlist/tsx.txt +1087 -0
  65. tapebasis/providers/tradingview/coinlist/tsxv.txt +1332 -0
  66. tapebasis/providers/tradingview/coinlist/twse.txt +1340 -0
  67. tapebasis/providers/tradingview/coinlist/xetra.txt +1412 -0
  68. tapebasis/providers/tradingview/core/data/__init__.py +0 -0
  69. tapebasis/providers/tradingview/core/data/egx_indices.py +309 -0
  70. tapebasis/providers/tradingview/core/data/egx_sectors.py +491 -0
  71. tapebasis/providers/tradingview/core/errors.py +211 -0
  72. tapebasis/providers/tradingview/core/services/backtest_service.py +1024 -0
  73. tapebasis/providers/tradingview/core/services/bitcoin_market_service.py +129 -0
  74. tapebasis/providers/tradingview/core/services/coinlist.py +102 -0
  75. tapebasis/providers/tradingview/core/services/egx_service.py +1199 -0
  76. tapebasis/providers/tradingview/core/services/extended_hours_service.py +256 -0
  77. tapebasis/providers/tradingview/core/services/futures_service.py +219 -0
  78. tapebasis/providers/tradingview/core/services/indicators.py +1696 -0
  79. tapebasis/providers/tradingview/core/services/indicators_calc.py +316 -0
  80. tapebasis/providers/tradingview/core/services/marketaux_service.py +340 -0
  81. tapebasis/providers/tradingview/core/services/multi_agent_service.py +238 -0
  82. tapebasis/providers/tradingview/core/services/options_service.py +390 -0
  83. tapebasis/providers/tradingview/core/services/scanner_service.py +430 -0
  84. tapebasis/providers/tradingview/core/services/screener_provider.py +794 -0
  85. tapebasis/providers/tradingview/core/services/screener_service.py +1251 -0
  86. tapebasis/providers/tradingview/core/services/stock_screener_service.py +227 -0
  87. tapebasis/providers/tradingview/core/types.py +86 -0
  88. tapebasis/providers/tradingview/core/utils/validators.py +376 -0
  89. tapebasis/providers/tradingview/native.py +514 -0
  90. tapebasis/providers/tradingview/quote_socket.py +231 -0
  91. tapebasis/providers_state.py +353 -0
  92. tapebasis/ratelimit.py +285 -0
  93. tapebasis/reconcile.py +1287 -0
  94. tapebasis/sa_access.py +269 -0
  95. tapebasis/sentiment.py +272 -0
  96. tapebasis/server.py +226 -0
  97. tapebasis/sessions.py +477 -0
  98. tapebasis/singleflight.py +93 -0
  99. tapebasis/symbols.py +1338 -0
  100. tapebasis/tools_bloomberg.py +176 -0
  101. tapebasis/tools_finviz.py +307 -0
  102. tapebasis/tools_health.py +158 -0
  103. tapebasis/tools_market.py +3044 -0
  104. tapebasis/tools_tradingview.py +865 -0
  105. tapebasis/tv_access.py +298 -0
  106. tapebasis/utils.py +85 -0
  107. tapebasis/yh_access.py +734 -0
  108. tapebasis-1.0.0.dist-info/METADATA +777 -0
  109. tapebasis-1.0.0.dist-info/RECORD +114 -0
  110. tapebasis-1.0.0.dist-info/WHEEL +5 -0
  111. tapebasis-1.0.0.dist-info/entry_points.txt +2 -0
  112. tapebasis-1.0.0.dist-info/licenses/LICENSE +21 -0
  113. tapebasis-1.0.0.dist-info/licenses/NOTICE +90 -0
  114. tapebasis-1.0.0.dist-info/top_level.txt +1 -0
tapebasis/__init__.py ADDED
@@ -0,0 +1,32 @@
1
+ """tapebasis: one MCP server, multiple market-data providers, cross-verified.
2
+
3
+ The server fans every overlapping request out to every upstream provider that
4
+ can serve it (TradingView, Finviz, Yahoo Finance, StockAnalysis and Bloomberg),
5
+ compares the answers, and labels each response with exactly one
6
+ :data:`data_provider` value:
7
+
8
+ ``ALL``
9
+ Every provider that was asked answered. This is a statement about
10
+ *coverage*, not about agreement: agreement is reported separately in
11
+ ``comparisons`` and summarised in ``notice``.
12
+ ``TRADINGVIEW`` / ``FINVIZ`` / ``YAHOO`` / ``SA`` / ``BLOOMBERG``
13
+ Only that provider produced data. Served, but explicitly single-source.
14
+ Bloomberg's quote pages are exchange-delayed, so its payload carries the
15
+ delay and a print time derived from the intraday series. StockAnalysis
16
+ covers US equities and ETFs only, and serves the same consolidated tape
17
+ Yahoo does, so that pair is weak corroboration.
18
+ ``NONE``
19
+ No provider produced data.
20
+
21
+ Each provider is fetched natively and independently: a leg that quietly proxied
22
+ another provider would be worthless as a cross-check. Symbols are resolved per
23
+ provider dynamically, and every provider call is paced by a shared rate limiter.
24
+
25
+ See :mod:`tapebasis.reconcile` for the fan-out/comparison engine,
26
+ :mod:`tapebasis.ratelimit` for pacing, and :mod:`tapebasis.health` for the
27
+ upstream schema-drift monitor.
28
+ """
29
+
30
+ __version__ = "1.0.0"
31
+
32
+ __all__ = ["__version__"]
tapebasis/bb_access.py ADDED
@@ -0,0 +1,597 @@
1
+ """Typed access to bloomberg.com's public quote pages.
2
+
3
+ Bloomberg is a reconciliation leg: ``stock_quote`` and ``stock_prices`` ask it
4
+ in parallel with every other provider, its readings appear in
5
+ ``comparisons``, and it contributes to ``ALL``. Three properties of the feed are
6
+ handled here rather than hidden by a tolerance:
7
+
8
+ * its equity and index quotes are delayed (``exchangeDelay`` is
9
+ ``15 Minutes``), so the print time is reported truthfully and the reconciler
10
+ treats a pair that far apart as *not comparable* instead of as a disagreement,
11
+ * its static fields are penny-identical to Yahoo's, so agreement between those
12
+ two is weak corroboration, not two independent witnesses; the docs say so,
13
+ * ``lastUpdate`` is a session sentinel (``...T01:00:00Z``), not a print time.
14
+ Using it would make a stale page look newer than a live print.
15
+
16
+ Access is not cookie-based. The site gates plain HTTP clients on the TLS
17
+ fingerprint, so ``curl_cffi`` with a specific browser impersonation is required;
18
+ ``httpx`` gets a 403 robot page no matter what headers it sends. That is also
19
+ why the health monitor cannot probe this provider over httpx: a probe that
20
+ cannot reach the page would report a false outage. The tool surfaces its own
21
+ failures instead.
22
+
23
+ Two paths exist and only one is used:
24
+
25
+ ``/bazaar/api/quote/...`` burns after roughly ten rapid requests and then
26
+ answers 403 for many minutes, even while the HTML page
27
+ still answers 200 from the same session.
28
+ ``/quote/<id>`` the HTML page, which is a strict superset: the whole
29
+ quote object is embedded in ``__NEXT_DATA__``.
30
+
31
+ So every request goes through the HTML page, paced slowly by
32
+ :mod:`tapebasis.ratelimit`, with a short TTL cache to absorb duplicate asks.
33
+
34
+ The ``curl_cffi`` session is pooled per event loop through
35
+ :mod:`tapebasis.http_pool`. Measured against the live page, a fresh session cost
36
+ 325-725 ms per fetch against 176 ms on a reused one, and the reuse changes
37
+ nothing about the TLS fingerprint that gets past the wall.
38
+ """
39
+ from __future__ import annotations
40
+
41
+ import asyncio
42
+ import json
43
+ import os
44
+ import re
45
+ import weakref
46
+ from typing import Any
47
+
48
+ from .providers_state import NoDataError, ProviderError
49
+ from .ratelimit import RateLimited, call, limiter
50
+ from .symbols import Resolution
51
+
52
+ PROVIDER = "BLOOMBERG"
53
+ _POOL = "bloomberg"
54
+
55
+ _BASE = "https://www.bloomberg.com"
56
+ #: Recorded from live traffic: this version's fingerprint is accepted, while
57
+ #: the newest ``chrome`` alias is not. Overridable for a deployment that needs
58
+ #: to track a different accepted version.
59
+ _IMPERSONATE = os.environ.get("TAPEBASIS_BLOOMBERG_IMPERSONATE", "chrome124")
60
+ _TIMEOUT = float(os.environ.get("TAPEBASIS_BLOOMBERG_TIMEOUT", "30"))
61
+ #: The page is ~460 KB of HTML; 60 s absorbs an agent asking twice in one turn
62
+ #: without ever serving a materially stale price.
63
+ _CACHE_TTL = float(os.environ.get("TAPEBASIS_BLOOMBERG_CACHE_TTL", "60"))
64
+
65
+ _NEXT_DATA = re.compile(
66
+ r'<script[^>]*id="__NEXT_DATA__"[^>]*>(?P<body>.*?)</script>',
67
+ re.S,
68
+ )
69
+
70
+ #: Suffixes that mean "this already is a Bloomberg id", not "EXCHANGE:TICKER".
71
+ _SUFFIXES = {"US", "IND", "CUR", "COM", "GOV", "INDEX", "EQ"}
72
+
73
+ #: Extra spellings Bloomberg resolves mechanically but the instrument registry
74
+ #: has no row for (platinum, palladium, Nasdaq-100, foreign indices, bare
75
+ #: commodity roots). The registry rows themselves (indices, commodities, FX
76
+ #: pairs) are answered from :data:`tapebasis.symbols.INSTRUMENTS`, which is the
77
+ #: single home for every spelling; this table only holds what no registry row
78
+ #: covers.
79
+ _ALIASES: dict[str, str] = {
80
+ # Foreign equity indices (no registry row: this server reports US majors)
81
+ "DAX": "DAX:IND",
82
+ "FTSE": "UKX:IND",
83
+ "UKX": "UKX:IND",
84
+ "NKY": "NKY:IND",
85
+ "NIKKEI": "NKY:IND",
86
+ "HSI": "HSI:IND",
87
+ # Nasdaq-100 is a registry row; platinum/palladium spot pairs are not
88
+ "XPTUSD": "XPTUSD:CUR",
89
+ "XPDUSD": "XPDUSD:CUR",
90
+ # Bare commodity roots (``GC1!`` is the registry row; ``GC`` alone is not)
91
+ "GC": "GC1:COM",
92
+ "SI": "SI1:COM",
93
+ "CL": "CL1:COM",
94
+ "BZ": "BZ1:COM",
95
+ "NG": "NG1:COM",
96
+ "HG": "HG1:COM",
97
+ # Crypto pairs quoted as currencies
98
+ "XBTUSD": "XBTUSD:CUR",
99
+ "BTC": "XBTUSD:CUR",
100
+ "BTCUSD": "XBTUSD:CUR",
101
+ }
102
+
103
+
104
+ def _registry_bloomberg_id(symbol: str) -> str | None:
105
+ """Bloomberg's id for *symbol* from the instrument registry, or ``None``.
106
+
107
+ Imports lazily so :mod:`tapebasis.symbols` (which imports nothing from
108
+ this module) stays free of a cycle.
109
+ """
110
+ from .symbols import instrument_row
111
+
112
+ row = instrument_row(symbol)
113
+ if row is None:
114
+ return None
115
+ bloomberg = row.get("bloomberg")
116
+ return str(bloomberg) if bloomberg else None
117
+
118
+
119
+ def _registry_declines(symbol: str) -> str | None:
120
+ """The registry's stated reason Bloomberg does not carry *symbol*, if any.
121
+
122
+ A registry row with an empty Bloomberg cell is a boundary the instrument
123
+ itself declares ("Bloomberg's quote page carries no WTI spot series; its WTI
124
+ generic future is CL1:COM, a different instrument, not substituted"). That
125
+ is not a resolution failure to be papered over with a mechanical guess: the
126
+ guess lands on an unrelated company. ``WTI`` mechanically becomes ``WTI:US``,
127
+ which is W&T Offshore Inc at 2.89, and ``USOIL`` becomes ``USOIL:US``, which
128
+ answers a robot page.
129
+
130
+ Returns:
131
+ The reason, or ``None`` when the registry has no row or carries it.
132
+ """
133
+ from .symbols import instrument_row
134
+
135
+ row = instrument_row(symbol)
136
+ if row is None or row.get("bloomberg") is not None:
137
+ return None
138
+ return str(
139
+ row.get("bloomberg_note") or "not carried by bloomberg.com"
140
+ )
141
+
142
+
143
+ def resolve_bloomberg(symbol: str) -> Resolution:
144
+ """Bloomberg's identity for *symbol*, as the one place it is decided.
145
+
146
+ Every path that asks Bloomberg goes through this function, so the id a
147
+ response *reports* and the id it *fetched* cannot disagree. That was a real
148
+ bug: ``tools_market`` resolved through the instrument registry (which says
149
+ ``WTI`` has no Bloomberg spot series) while :func:`to_bloomberg` guessed
150
+ ``WTI:US``, and the single-provider tool priced W&T Offshore Inc at 2.89 for
151
+ a caller who asked for crude.
152
+
153
+ The order is:
154
+
155
+ 1. a registry row that carries the instrument wins,
156
+ 2. a registry row that declines it is a stated boundary, not a failure,
157
+ 3. the alias table and the mechanical rule fill in what the registry does
158
+ not describe.
159
+
160
+ Args:
161
+ symbol: Anything the instrument is commonly called, or an id already in
162
+ Bloomberg's spelling.
163
+
164
+ Returns:
165
+ A :class:`tapebasis.symbols.Resolution` whose ``symbol`` is the quote id
166
+ to fetch, or ``None`` with ``note`` stating why Bloomberg does not
167
+ carry the instrument.
168
+ """
169
+ query = (symbol or "").strip().upper().replace(" ", "")
170
+ declined = _registry_declines(query)
171
+ if declined is not None:
172
+ return Resolution(
173
+ query=symbol,
174
+ provider=PROVIDER,
175
+ symbol=None,
176
+ method="none",
177
+ note=declined,
178
+ )
179
+ try:
180
+ bloomberg_id = to_bloomberg(symbol)
181
+ except NoDataError as exc:
182
+ return Resolution(
183
+ query=symbol,
184
+ provider=PROVIDER,
185
+ symbol=None,
186
+ method="none",
187
+ note=exc.message,
188
+ )
189
+ return Resolution(
190
+ query=symbol,
191
+ provider=PROVIDER,
192
+ symbol=bloomberg_id,
193
+ venue=bloomberg_id.rpartition(":")[2],
194
+ method="mechanical",
195
+ confidence=0.8,
196
+ )
197
+
198
+
199
+ def to_bloomberg(symbol: str) -> str:
200
+ """Resolve a caller's symbol to a Bloomberg quote id.
201
+
202
+ Bloomberg spells the same instrument differently from every other provider
203
+ (``AAPL:US``, ``SPX:IND``, ``EURUSD:CUR``, ``CL1:COM``), and its quote URL
204
+ returns a 403 robot page rather than a 404 for an id it does not like. So
205
+ resolution is the instrument registry plus a small alias table plus a
206
+ mechanical rule, never a search:
207
+
208
+ * a registry instrument wins (``SPX`` -> ``SPX:IND``, ``XAUUSD`` ->
209
+ ``XAUUSD:CUR``, ``CL1!`` -> ``CL1:COM``),
210
+ * a registry instrument Bloomberg does not carry is refused with its stated
211
+ reason (``WTI`` is a spot series with no Bloomberg page; ``WTI:US`` is
212
+ W&T Offshore Inc, a different company),
213
+ * a known alias wins (``DAX`` -> ``DAX:IND``),
214
+ * an id that already carries a Bloomberg suffix is kept (``BRK/B:US``),
215
+ * ``EXCHANGE:TICKER`` drops the exchange (``NASDAQ:AAPL`` -> ``AAPL:US``),
216
+ * a six-letter currency pair becomes ``PAIR:CUR`` (``GBPUSD`` -> ``GBPUSD:CUR``),
217
+ * anything else is a US equity (``BRK.B`` -> ``BRK/B:US``).
218
+
219
+ Prefer :func:`resolve_bloomberg`: it is the identity decision every caller
220
+ should make, and it reports *why* an instrument is not carried instead of
221
+ raising. This function stays for callers that only need the id, and it
222
+ raises ``NoDataError`` where the registry declines an instrument rather than
223
+ guessing a ticker that belongs to something else.
224
+
225
+ Args:
226
+ symbol: The instrument as the caller spelled it.
227
+
228
+ Returns:
229
+ Bloomberg's quote id, e.g. ``AAPL:US``.
230
+
231
+ Raises:
232
+ NoDataError: The symbol is empty, has no ticker part, or names an
233
+ instrument the registry states Bloomberg does not carry.
234
+ """
235
+ raw = (symbol or "").strip()
236
+ if not raw:
237
+ raise NoDataError(PROVIDER, "empty symbol")
238
+
239
+ upper = raw.upper().replace(" ", "")
240
+ # Registry first: one canonical home for every instrument spelling.
241
+ registry_id = _registry_bloomberg_id(upper)
242
+ if registry_id:
243
+ return registry_id
244
+ declined = _registry_declines(upper)
245
+ if declined is not None:
246
+ raise NoDataError(PROVIDER, declined)
247
+ alias = _ALIASES.get(upper)
248
+ if alias:
249
+ return alias
250
+
251
+ if ":" in upper:
252
+ head, _, suffix = upper.rpartition(":")
253
+ if suffix in _SUFFIXES:
254
+ # Already a Bloomberg id; normalise the class suffix and keep the
255
+ # rest verbatim, including a share-class slash.
256
+ return f"{head}:{suffix}"
257
+ # EXCHANGE:TICKER from another provider's vocabulary: keep the ticker.
258
+ upper = suffix
259
+
260
+ if not upper:
261
+ raise NoDataError(PROVIDER, f"{symbol!r} has no ticker part")
262
+
263
+ compact = upper.replace("/", "").replace(".", "")
264
+ if len(compact) == 6 and compact.isalpha():
265
+ return f"{compact}:CUR"
266
+
267
+ if upper.endswith(":US") or upper.endswith(":CUR"):
268
+ return upper
269
+ return f"{upper.replace('.', '/')}:US"
270
+
271
+
272
+ # ── transport ───────────────────────────────────────────────────────────────
273
+
274
+
275
+ def _client() -> Any:
276
+ """Import ``curl_cffi`` lazily, with an actionable error when it is absent."""
277
+ try:
278
+ from curl_cffi.requests import AsyncSession
279
+ except Exception as exc: # noqa: BLE001 - the package is the only requirement
280
+ raise ProviderError(
281
+ PROVIDER,
282
+ "curl_cffi is not installed. Bloomberg gates plain HTTP clients on the "
283
+ "TLS fingerprint, so this provider needs curl_cffi (pip install curl_cffi)",
284
+ code="NOT_CONFIGURED",
285
+ ) from exc
286
+ return AsyncSession
287
+
288
+
289
+ #: One pooled ``curl_cffi`` session per event loop. The loop key is weak, so a
290
+ #: closed loop's session goes with it: the hermetic suite runs one loop per test
291
+ #: and must never inherit a socket from another test.
292
+ _SESSIONS: "weakref.WeakKeyDictionary[asyncio.AbstractEventLoop, Any]" = (
293
+ weakref.WeakKeyDictionary()
294
+ )
295
+
296
+
297
+ def _session() -> Any:
298
+ """The pooled ``curl_cffi`` session for the running loop, created on first use.
299
+
300
+ The session carries the browser impersonation that gets past bloomberg.com's
301
+ bot wall, so it is created with the same options a per-request session was.
302
+
303
+ Returns:
304
+ A reusable ``curl_cffi`` ``AsyncSession``. Callers must not close it;
305
+ :func:`aclose` does that when the server shuts down.
306
+ """
307
+ AsyncSession = _client()
308
+ loop = asyncio.get_running_loop()
309
+ session = _SESSIONS.get(loop)
310
+ if session is None:
311
+ session = AsyncSession(
312
+ impersonate=_IMPERSONATE,
313
+ timeout=_TIMEOUT,
314
+ headers={
315
+ "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
316
+ "Accept-Language": "en-US,en;q=0.9",
317
+ },
318
+ )
319
+ _SESSIONS[loop] = session
320
+ return session
321
+
322
+
323
+ async def aclose() -> None:
324
+ """Close the pooled session of the running loop, if there is one."""
325
+ try:
326
+ loop = asyncio.get_running_loop()
327
+ except RuntimeError:
328
+ return
329
+ session = _SESSIONS.pop(loop, None)
330
+ if session is None:
331
+ return
332
+ try:
333
+ await session.close()
334
+ except Exception: # noqa: BLE001 - shutdown must not fail on a dead session
335
+ pass
336
+
337
+
338
+ async def _fetch_html(path: str, cache_key: str) -> str:
339
+ """Fetch one page under the provider limiter, with a short TTL cache."""
340
+ paced = limiter(PROVIDER)
341
+ cached = paced.cache_get(cache_key, _CACHE_TTL)
342
+ if isinstance(cached, str):
343
+ return cached
344
+
345
+ async def _once() -> str:
346
+ session = _session()
347
+ response = await session.get(f"{_BASE}{path}")
348
+ status = response.status_code
349
+ body = response.text
350
+ if status == 429:
351
+ raise RateLimited(PROVIDER, "HTTP 429 from bloomberg.com")
352
+ if status in (401, 403):
353
+ # The bot wall, not a missing instrument: back off, so a caller that
354
+ # keeps asking does not deepen the block.
355
+ paced.note_rate_limited()
356
+ raise ProviderError(
357
+ PROVIDER,
358
+ f"bloomberg.com refused the request with HTTP {status}. This is the "
359
+ "anti-bot wall, usually because too many requests arrived too fast; "
360
+ "the provider is cooling down",
361
+ code="UPSTREAM_BLOCKED",
362
+ )
363
+ if status == 404:
364
+ raise NoDataError(PROVIDER, f"bloomberg.com has no quote page for {path}")
365
+ if status != 200:
366
+ raise ProviderError(
367
+ PROVIDER,
368
+ f"bloomberg.com answered HTTP {status}",
369
+ code="UPSTREAM_ERROR",
370
+ )
371
+ paced.cache_set(cache_key, body)
372
+ return body
373
+
374
+ return await call(PROVIDER, _once, attempts=1)
375
+
376
+
377
+ def _payload(html: str) -> dict[str, Any]:
378
+ """Decode ``__NEXT_DATA__`` out of a rendered quote page."""
379
+ match = _NEXT_DATA.search(html)
380
+ if not match:
381
+ raise ProviderError(
382
+ PROVIDER,
383
+ "quote page no longer contains __NEXT_DATA__; the page was "
384
+ "restructured or an interstitial was served",
385
+ code="SCHEMA_DRIFT",
386
+ )
387
+ try:
388
+ return json.loads(match.group("body"))
389
+ except ValueError as exc:
390
+ raise ProviderError(
391
+ PROVIDER,
392
+ f"__NEXT_DATA__ is not valid JSON: {exc}",
393
+ code="SCHEMA_DRIFT",
394
+ ) from exc
395
+
396
+
397
+ # ── shaping ─────────────────────────────────────────────────────────────────
398
+
399
+
400
+ def _number(value: Any) -> float | None:
401
+ """Parse Bloomberg's formatted numbers (``"89,637,749.00"``, ``"1.1230"``)."""
402
+ if value is None or isinstance(value, bool):
403
+ return None
404
+ if isinstance(value, (int, float)):
405
+ return float(value)
406
+ if isinstance(value, str):
407
+ cleaned = value.replace(",", "").replace("%", "").strip()
408
+ if not cleaned:
409
+ return None
410
+ try:
411
+ return float(cleaned)
412
+ except ValueError:
413
+ return None
414
+ return None
415
+
416
+
417
+ def _pair(value: Any) -> list[float] | None:
418
+ if not isinstance(value, (list, tuple)) or len(value) != 2:
419
+ return None
420
+ low, high = _number(value[0]), _number(value[1])
421
+ if low is None or high is None:
422
+ return None
423
+ return [low, high]
424
+
425
+
426
+ def _print_time(payload: dict[str, Any], price: float | None) -> tuple[str | None, str | None]:
427
+ """The real print time, taken from the intraday series, never ``lastUpdate``.
428
+
429
+ ``lastUpdate`` on an equity page is a session sentinel such as
430
+ ``2026-11-05T01:00:00.000Z`` while the actual print is hours earlier, so
431
+ using it would make a stale page win a freshness comparison against a live
432
+ one. The last intraday point is the print, and it is only trusted when its
433
+ value matches the quoted price: if the series ends somewhere else, its
434
+ timestamp describes a different number.
435
+
436
+ Returns ``(iso_timestamp, reason_when_missing)``.
437
+ """
438
+ series = payload.get("priceMovements1Day")
439
+ if not isinstance(series, list) or not series:
440
+ return None, "the page carries no intraday series, so the print time is unknown"
441
+ last = series[-1] if isinstance(series[-1], dict) else None
442
+ if not last or not last.get("dateTime"):
443
+ return None, "the last intraday point carries no timestamp"
444
+ last_value = _number(last.get("value"))
445
+ if price is not None and last_value is not None:
446
+ tolerance = max(abs(price) * 0.001, 1e-9)
447
+ if abs(last_value - price) > tolerance:
448
+ return (
449
+ None,
450
+ "the intraday series ends at "
451
+ f"{last_value} while the quoted price is {price}, so the series "
452
+ "timestamp does not describe the quoted print",
453
+ )
454
+ return str(last["dateTime"]), None
455
+
456
+
457
+ def shape_quote(payload: dict[str, Any], *, requested: str) -> dict[str, Any]:
458
+ """Normalise Bloomberg's quote object into this server's vocabulary."""
459
+ price = _number(payload.get("price"))
460
+ if price is None:
461
+ raise NoDataError(
462
+ PROVIDER,
463
+ f"bloomberg.com has a page for {payload.get('id') or requested} but no "
464
+ "price for it",
465
+ )
466
+ print_time, print_time_reason = _print_time(payload, price)
467
+ percentage = _number(payload.get("percentChange1Day"))
468
+ return {
469
+ "symbol": payload.get("id") or requested,
470
+ "requested": requested,
471
+ "name": payload.get("name") or payload.get("shortName"),
472
+ "price": price,
473
+ "change_percent": percentage,
474
+ "previous_close": _number(payload.get("prevClose")),
475
+ "currency": payload.get("issuedCurrency"),
476
+ "exchange": payload.get("primaryExchange"),
477
+ "day_range": _pair(payload.get("dayRange")),
478
+ "week_52_range": _pair(payload.get("fiftyTwoWeekRange")),
479
+ "volume": _number(payload.get("volume")),
480
+ "pe_ratio": _number(payload.get("priceEarningsRatio")),
481
+ "eps": _number(payload.get("earningsPerShare")),
482
+ "market_status": payload.get("marketStatus"),
483
+ "is_open": payload.get("isOpen"),
484
+ "is_after_hours": payload.get("isAfterHours"),
485
+ "is_exchange_delayed": payload.get("isExchangeDelayed"),
486
+ "exchange_delay": payload.get("exchangeDelay"),
487
+ "trading_date": payload.get("tradingDate"),
488
+ "trading_day_close": payload.get("tradingDayClose"),
489
+ "print_time": print_time,
490
+ "print_time_source": "priceMovements1Day[-1].dateTime" if print_time else None,
491
+ "print_time_missing_reason": print_time_reason,
492
+ # Kept visible, never used as the print time: see ``_print_time``.
493
+ "provider_last_update": payload.get("lastUpdate"),
494
+ }
495
+
496
+
497
+ _BOARD_GROUPS: tuple[tuple[str, str], ...] = (
498
+ ("IND", "indices"),
499
+ ("GOV", "government_yields"),
500
+ ("COM", "commodities"),
501
+ ("CUR", "fx"),
502
+ )
503
+
504
+
505
+ def shape_board(payload: dict[str, Any]) -> dict[str, Any]:
506
+ """Group Bloomberg's market board (``tickerbar``) by instrument class.
507
+
508
+ The board is where Bloomberg earns its place: it carries the government
509
+ benchmark curve, which no other provider here publishes. It also carries
510
+ index levels, commodities and FX, kept as context. The board has no
511
+ per-instrument print time, so nothing here can be aged or ordered, and the
512
+ response says so instead of implying a live tape.
513
+ """
514
+ rows = payload.get("tickerbar")
515
+ if not isinstance(rows, list) or not rows:
516
+ raise NoDataError(PROVIDER, "the market board carried no tickerbar rows")
517
+
518
+ groups: dict[str, list[dict[str, Any]]] = {name: [] for _, name in _BOARD_GROUPS}
519
+ for row in rows:
520
+ if not isinstance(row, dict):
521
+ continue
522
+ ticker = str(row.get("ticker") or "")
523
+ _, _, suffix = ticker.rpartition(":")
524
+ for class_suffix, group_name in _BOARD_GROUPS:
525
+ if suffix == class_suffix:
526
+ groups[group_name].append(
527
+ {
528
+ "symbol": ticker,
529
+ "name": row.get("name"),
530
+ "price": _number(row.get("price")),
531
+ "change_percent": _number(row.get("percentChange1Day")),
532
+ }
533
+ )
534
+ break
535
+ if not any(groups.values()):
536
+ raise NoDataError(PROVIDER, "the market board carried no usable rows")
537
+ return groups
538
+
539
+
540
+ # ── public accessors ────────────────────────────────────────────────────────
541
+
542
+
543
+ async def quote(symbol: str) -> tuple[dict[str, Any], str]:
544
+ """Bloomberg's quote page for *symbol*, shaped and labelled.
545
+
546
+ Identity comes from :func:`resolve_bloomberg`, the same function the
547
+ cross-checked tools use, so the id reported in ``resolutions`` is always the
548
+ id that was fetched. An instrument the registry states Bloomberg does not
549
+ carry is a ``NoDataError`` with that reason, never a mechanical guess that
550
+ lands on an unrelated company.
551
+
552
+ Args:
553
+ symbol: The instrument as the caller spelled it.
554
+
555
+ Returns:
556
+ The shaped payload plus the upstream URL it came from.
557
+
558
+ Raises:
559
+ NoDataError: Bloomberg does not carry this instrument, or the page
560
+ exists but has no quote object.
561
+ """
562
+ resolution = resolve_bloomberg(symbol)
563
+ if not resolution.found:
564
+ raise NoDataError(
565
+ PROVIDER,
566
+ resolution.note or f"bloomberg.com does not carry {symbol!r}",
567
+ )
568
+ bloomberg_id = resolution.require()
569
+ path = f"/quote/{bloomberg_id}"
570
+ html = await _fetch_html(path, f"quote:{bloomberg_id}")
571
+ payload = _payload(html).get("props", {}).get("pageProps", {})
572
+ quote_payload = payload.get("quote")
573
+ if not isinstance(quote_payload, dict):
574
+ raise NoDataError(
575
+ PROVIDER,
576
+ f"bloomberg.com has no quote for {bloomberg_id}",
577
+ )
578
+ return shape_quote(quote_payload, requested=symbol), f"{_BASE}{path}"
579
+
580
+
581
+ async def board() -> tuple[dict[str, Any], str]:
582
+ """Bloomberg's market board (indices, government yields, commodities, FX)."""
583
+ path = "/markets/rates-bonds"
584
+ html = await _fetch_html(path, "board:markets")
585
+ payload = _payload(html).get("props", {}).get("pageProps", {})
586
+ return shape_board(payload), f"{_BASE}{path}"
587
+
588
+
589
+ __all__ = [
590
+ "PROVIDER",
591
+ "board",
592
+ "quote",
593
+ "resolve_bloomberg",
594
+ "shape_board",
595
+ "shape_quote",
596
+ "to_bloomberg",
597
+ ]