修改限速
This commit is contained in:
@@ -85,12 +85,36 @@ async def _fetch_symbol_stats(
|
||||
return None
|
||||
|
||||
|
||||
async def _pick_candidate_symbols(symbols: list[str]) -> list[str]:
|
||||
"""用 24h ticker 成交额预筛,避免对全市场并发拉 K 线触发 418 封禁。"""
|
||||
try:
|
||||
tickers = await binance_client.get_24hr_tickers()
|
||||
vol_map = {
|
||||
t["symbol"]: float(t.get("quoteVolume", 0) or 0)
|
||||
for t in tickers
|
||||
if t.get("symbol") in symbols
|
||||
}
|
||||
ranked = sorted(symbols, key=lambda s: vol_map.get(s, 0.0), reverse=True)
|
||||
pool = min(settings.candidate_pool, len(ranked))
|
||||
picked = ranked[:pool]
|
||||
logger.info(
|
||||
"Candidate pool: %d / %d symbols (by 24h quoteVolume)",
|
||||
len(picked),
|
||||
len(symbols),
|
||||
)
|
||||
return picked
|
||||
except Exception as e:
|
||||
logger.warning("24hr ticker prescreen failed, using full list: %s", e)
|
||||
return symbols
|
||||
|
||||
|
||||
async def aggregate_period(
|
||||
start: datetime,
|
||||
end: datetime,
|
||||
use_live_prices: bool = False,
|
||||
) -> list[dict]:
|
||||
symbols = await binance_client.get_usdt_perpetual_symbols()
|
||||
candidates = await _pick_candidate_symbols(symbols)
|
||||
start_ms = to_ms(start)
|
||||
end_ms = to_ms(end)
|
||||
|
||||
@@ -103,8 +127,15 @@ async def aggregate_period(
|
||||
|
||||
sem = asyncio.Semaphore(settings.max_concurrency)
|
||||
tasks = [
|
||||
_fetch_symbol_stats(s, start_ms, end_ms, prices, sem) for s in symbols
|
||||
_fetch_symbol_stats(s, start_ms, end_ms, prices, sem) for s in candidates
|
||||
]
|
||||
logger.info(
|
||||
"Aggregating period %s ~ %s (%d symbols, concurrency=%d)",
|
||||
start.isoformat(),
|
||||
end.isoformat(),
|
||||
len(candidates),
|
||||
settings.max_concurrency,
|
||||
)
|
||||
results = await asyncio.gather(*tasks)
|
||||
stats = [r for r in results if r is not None and r.quote_volume > 0]
|
||||
stats.sort(key=lambda x: x.quote_volume, reverse=True)
|
||||
|
||||
Reference in New Issue
Block a user