feat(config, strategy): 增强多账号错峰扫描逻辑
在 `config.py` 中新增随机延迟配置,允许在多账号环境下实现更灵活的错峰扫描策略。更新了 `strategy.py` 中的相关逻辑,支持随机延迟与固定步长延迟两种模式,提升了系统在低配服务器上的性能与稳定性。此改进有助于优化资源管理与并发处理能力。
This commit is contained in:
parent
3bfbafbab2
commit
a371e50a3e
|
|
@ -220,8 +220,11 @@ DEFAULT_TRADING_CONFIG = {
|
|||
'SCAN_LIMIT_KLINE_SUBSCRIBE': True, # 限制 K 线订阅:只在缓存完全没有数据时才订阅,避免订阅过多导致负载上升
|
||||
# 多账号/低配服务器(如 2 CPU 4G):降低并发与错峰扫描,避免 CPU 打满
|
||||
'SCAN_CONCURRENT_SYMBOLS': 2, # 扫描时同时分析多少个交易对(2 CPU 4G 多账号建议 2,单账号可 3~5)
|
||||
'SCAN_STAGGER_BY_ACCOUNT': True, # 多账号时按 account_id 错峰首次扫描,避免多进程同时扫
|
||||
'SCAN_STAGGER_SEC': 60, # 每多一个账号延迟的秒数(account_id-1 * 此值),如 4 账号则 0/60/120/180 秒后开扫
|
||||
'SCAN_STAGGER_BY_ACCOUNT': True, # 多账号时错峰首次扫描,避免多进程同时扫
|
||||
'SCAN_STAGGER_RANDOM': True, # True=随机延迟(更分散),False=按 account_id 固定步长延迟
|
||||
'SCAN_STAGGER_MIN_SEC': 10, # 随机延迟下限(秒)
|
||||
'SCAN_STAGGER_MAX_SEC': 120, # 随机延迟上限(秒)
|
||||
'SCAN_STAGGER_SEC': 60, # 非随机模式下每账号步长(account_id-1 * 此值)
|
||||
'KLINE_INTERVAL': '1h',
|
||||
'PRIMARY_INTERVAL': '4h', # 主周期4小时,过滤噪音
|
||||
'CONFIRM_INTERVAL': '1d', # 确认周期日线,看大趋势
|
||||
|
|
|
|||
|
|
@ -78,12 +78,22 @@ class TradingStrategy:
|
|||
|
||||
# 多账号错峰:避免多进程同时扫描导致 CPU 打满(2 CPU 4G 等低配服务器)
|
||||
import os
|
||||
import random
|
||||
stagger_enabled = config.TRADING_CONFIG.get('SCAN_STAGGER_BY_ACCOUNT', False)
|
||||
stagger_sec = int(config.TRADING_CONFIG.get('SCAN_STAGGER_SEC', 60) or 60)
|
||||
stagger_random = config.TRADING_CONFIG.get('SCAN_STAGGER_RANDOM', True)
|
||||
account_id = int(os.getenv('ATS_ACCOUNT_ID') or os.getenv('ACCOUNT_ID') or 1)
|
||||
if stagger_enabled and account_id > 1 and stagger_sec > 0:
|
||||
delay = (account_id - 1) * stagger_sec
|
||||
if stagger_enabled:
|
||||
if stagger_random:
|
||||
min_sec = max(0, int(config.TRADING_CONFIG.get('SCAN_STAGGER_MIN_SEC', 10) or 10))
|
||||
max_sec = max(min_sec, int(config.TRADING_CONFIG.get('SCAN_STAGGER_MAX_SEC', 120) or 120))
|
||||
delay = random.randint(min_sec, max_sec)
|
||||
logger.info(f"多账号错峰:account_id={account_id},随机延迟 {delay} 秒后开始首次扫描(范围 {min_sec}~{max_sec}s)")
|
||||
else:
|
||||
stagger_sec = int(config.TRADING_CONFIG.get('SCAN_STAGGER_SEC', 60) or 60)
|
||||
delay = (account_id - 1) * stagger_sec if account_id > 1 else 0
|
||||
if delay > 0:
|
||||
logger.info(f"多账号错峰:account_id={account_id},延迟 {delay} 秒后开始首次扫描")
|
||||
if delay > 0:
|
||||
await asyncio.sleep(delay)
|
||||
|
||||
try:
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user