Fix amp-stats long-range candles via OKX history endpoints.
Recent candles cap near 60d; continue with history-index/history candles and color profit green/red. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -191,6 +191,54 @@ class AmpStatsLibTests(unittest.TestCase):
|
||||
self.assertEqual(reframed["rows"][0]["profit"], -2)
|
||||
self.assertIn("收益", build_export_csv(reframed))
|
||||
|
||||
def test_fetch_switches_to_history_endpoint(self):
|
||||
"""近期接口到头后应切 history 续拉."""
|
||||
from lib.hub.amp_stats_lib import fetch_okx_candles
|
||||
|
||||
calls: list[str] = []
|
||||
|
||||
class FakeResp:
|
||||
def __init__(self, data):
|
||||
self._data = data
|
||||
|
||||
def raise_for_status(self):
|
||||
return None
|
||||
|
||||
def json(self):
|
||||
return {"code": "0", "data": self._data}
|
||||
|
||||
class FakeClient:
|
||||
def get(self, url, params=None):
|
||||
calls.append(url)
|
||||
after = (params or {}).get("after")
|
||||
# recent: only 2 pages then empty; history continues
|
||||
if "history" not in url:
|
||||
if after is None:
|
||||
return FakeResp([["2000", "1", "2", "0.5", "1.5"], ["1900", "1", "2", "0.5", "1.5"]])
|
||||
if after == "1900":
|
||||
return FakeResp([]) # recent exhausted
|
||||
return FakeResp([])
|
||||
# history
|
||||
if after == "1900":
|
||||
return FakeResp([["1800", "1", "2", "0.5", "1.5"], ["1000", "1", "2", "0.5", "1.5"]])
|
||||
return FakeResp([])
|
||||
|
||||
def close(self):
|
||||
return None
|
||||
|
||||
bars = fetch_okx_candles(
|
||||
url="https://www.okx.com/api/v5/market/index-candles",
|
||||
history_url="https://www.okx.com/api/v5/market/history-index-candles",
|
||||
inst_id="ETH-USD",
|
||||
since_ms=1000,
|
||||
until_ms=3000,
|
||||
client=FakeClient(),
|
||||
max_pages=10,
|
||||
)
|
||||
self.assertTrue(any("history-index-candles" in u for u in calls))
|
||||
self.assertGreaterEqual(len(bars), 3)
|
||||
self.assertEqual(bars[0]["ts"], 1000)
|
||||
|
||||
def test_compute_with_mock_fetch(self):
|
||||
now = datetime(2026, 7, 22, 18, 0, tzinfo=TZ)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user