diff --git a/backend/api/routes/recommendations.py b/backend/api/routes/recommendations.py index f34198c..6b3be9b 100644 --- a/backend/api/routes/recommendations.py +++ b/backend/api/routes/recommendations.py @@ -55,6 +55,54 @@ async def get_recommendations( end_date=end_dt ) + # 如果是获取有效推荐,尝试更新实时价格 + if status == 'active' or (status is None and len(recommendations) > 0): + try: + import sys + from pathlib import Path + current_file = Path(__file__) + backend_path = current_file.parent.parent.parent + project_root = backend_path.parent + trading_system_path = project_root / 'trading_system' + + if trading_system_path.exists(): + sys.path.insert(0, str(trading_system_path)) + from binance_client import BinanceClient + import config + + # 创建客户端实例(不需要连接,只需要访问价格缓存) + client = BinanceClient( + api_key=config.BINANCE_API_KEY, + api_secret=config.BINANCE_API_SECRET, + testnet=config.USE_TESTNET + ) + + # 更新推荐中的实时价格和涨跌幅 + for rec in recommendations: + symbol = rec.get('symbol') + if symbol: + # 尝试从WebSocket缓存获取实时价格 + realtime_price = client.get_realtime_price(symbol) + if realtime_price is not None: + # 更新价格 + old_price = rec.get('current_price', 0) + rec['current_price'] = realtime_price + + # 计算新的涨跌幅 + if old_price > 0: + change_percent = ((realtime_price - old_price) / old_price) * 100 + rec['change_percent'] = round(change_percent, 4) + rec['price_updated'] = True # 标记价格已更新 + else: + rec['price_updated'] = False + else: + rec['price_updated'] = False + else: + rec['price_updated'] = False + except Exception as price_update_error: + # 如果价格更新失败,不影响返回推荐列表 + logger.debug(f"更新推荐实时价格失败(不影响返回): {price_update_error}") + return { "success": True, "count": len(recommendations), @@ -69,9 +117,58 @@ async def get_recommendations( async def get_active_recommendations(): """ 获取当前有效的推荐(未过期、未执行、未取消) + 使用WebSocket实时价格更新推荐中的价格信息 """ try: recommendations = TradeRecommendation.get_active() + + # 尝试从WebSocket缓存获取实时价格更新推荐中的价格 + try: + import sys + from pathlib import Path + current_file = Path(__file__) + backend_path = current_file.parent.parent.parent + project_root = backend_path.parent + trading_system_path = project_root / 'trading_system' + + if trading_system_path.exists(): + sys.path.insert(0, str(trading_system_path)) + from binance_client import BinanceClient + import config + + # 创建客户端实例(不需要连接,只需要访问价格缓存) + client = BinanceClient( + api_key=config.BINANCE_API_KEY, + api_secret=config.BINANCE_API_SECRET, + testnet=config.USE_TESTNET + ) + + # 更新推荐中的实时价格和涨跌幅 + for rec in recommendations: + symbol = rec.get('symbol') + if symbol: + # 尝试从WebSocket缓存获取实时价格 + realtime_price = client.get_realtime_price(symbol) + if realtime_price is not None: + # 更新价格 + old_price = rec.get('current_price', 0) + rec['current_price'] = realtime_price + + # 计算新的涨跌幅 + if old_price > 0: + change_percent = ((realtime_price - old_price) / old_price) * 100 + rec['change_percent'] = round(change_percent, 4) + rec['price_updated'] = True # 标记价格已更新 + else: + rec['price_updated'] = False + else: + rec['price_updated'] = False + else: + rec['price_updated'] = False + except Exception as price_update_error: + # 如果价格更新失败,不影响返回推荐列表 + logger.debug(f"更新推荐实时价格失败(不影响返回): {price_update_error}") + return { "success": True, "count": len(recommendations), diff --git a/frontend/src/components/Recommendations.jsx b/frontend/src/components/Recommendations.jsx index a15192b..c6cbdfc 100644 --- a/frontend/src/components/Recommendations.jsx +++ b/frontend/src/components/Recommendations.jsx @@ -13,6 +13,18 @@ function Recommendations() { useEffect(() => { loadRecommendations() + + // 如果是查看有效推荐,每10秒自动刷新一次(获取实时价格) + let interval = null + if (statusFilter === 'active') { + interval = setInterval(loadRecommendations, 10000) // 每10秒刷新 + } + + return () => { + if (interval) { + clearInterval(interval) + } + } }, [statusFilter, directionFilter]) const loadRecommendations = async () => { @@ -208,9 +220,14 @@ function Recommendations() {