Skip to content

"Vibe-Trading: Your Personal Trading Agent"

fix(loaders): make the stooq challenge latch stop probing, not just logging

cgycoreya2a5f950a011
diff --git a/agent/backtest/loaders/stooq_loader.py b/agent/backtest/loaders/stooq_loader.py
index 778c32d40..801f80570 100644
--- a/agent/backtest/loaders/stooq_loader.py
+++ b/agent/backtest/loaders/stooq_loader.py
@@ -41,7 +41,10 @@ _DEFAULT_MIN_INTERVAL_S = 0.6
# Stooq currently answers non-browser clients with a JavaScript proof-of-work
# challenge page (HTTP 200, HTML body) instead of CSV. Without detection the
# loader parses it as "no data" and the fallback chain slides past a source
-# that never serves, with nothing in the run log to show for it.
+# that never serves, with nothing in the run log to show for it. The flag is
+# the process-wide latch behind that warning: once the challenge is seen, no
+# further symbol is probed, because a second request can only be answered the
+# same way.
_challenge_warned = False
@@ -160,6 +163,13 @@ class DataLoader:
self, code: str, start_date: str, end_date: str,
) -> Optional[pd.DataFrame]:
"""Fetch and parse one symbol's CSV; ``None`` when Stooq has no data."""
+ global _challenge_warned
+ if _challenge_warned:
+ # Latched earlier in this process: the warning already told the
+ # operator the source is unavailable, so spending a throttled
+ # request per remaining symbol (0.6s apart) only delays the chain's
+ # move to the next source. A new process re-probes.
+ return None
params = {
"s": map_symbol(code),
"d1": _compact_date(start_date),
@@ -174,7 +184,6 @@ class DataLoader:
)
response.raise_for_status()
if _looks_like_challenge_page(response.text):
- global _challenge_warned
if not _challenge_warned:
logger.warning(
"stooq is serving an anti-bot challenge page instead of CSV "
diff --git a/agent/tests/test_stooq_loader.py b/agent/tests/test_stooq_loader.py
index 3954a8bca..497aa5db0 100644
--- a/agent/tests/test_stooq_loader.py
+++ b/agent/tests/test_stooq_loader.py
@@ -207,6 +207,38 @@ class TestChallengePageDetection:
warnings = [r.message for r in caplog.records if "anti-bot challenge" in r.message]
assert len(warnings) == 1 # two symbols, one warning
+ def test_challenge_latch_stops_probing_later_symbols(self, monkeypatch, caplog):
+ """Once challenged, later symbols must not pay another request.
+
+ The warning says the source is "unavailable for the rest of this
+ process", but the latch only gated the log line: a batched US fallback
+ probe still spent one throttled request per missing symbol on an
+ endpoint that can only answer with the challenge page.
+ """
+ monkeypatch.setattr(stooq_loader, "_challenge_warned", False)
+ probed: List[str] = []
+
+ def fake_get(url, **kwargs):
+ probed.append(kwargs["params"]["s"])
+ return _FakeResponse(text=self._CHALLENGE_HTML)
+
+ monkeypatch.setattr(stooq_loader, "throttled_get", fake_get)
+ loader = stooq_loader.DataLoader()
+
+ with caplog.at_level(logging.WARNING, logger="backtest.loaders.stooq_loader"):
+ first = loader.fetch(
+ ["AAPL.US", "MSFT.US", "NVDA.US"], "2024-01-01", "2024-01-31",
+ )
+
+ assert first == {}
+ # One probe establishes the challenge; the other two symbols are not
+ # spent against a source the same process already knows cannot serve.
+ assert probed == ["aapl.us"]
+
+ # A later batch in the same process is answered without any request.
+ assert loader.fetch(["AMZN.US"], "2024-01-01", "2024-01-31") == {}
+ assert probed == ["aapl.us"]
+
def test_challenge_page_does_not_reach_csv_parser(self, monkeypatch):
monkeypatch.setattr(stooq_loader, "_challenge_warned", True) # latch already set
monkeypatch.setattr(