diff --git a/app/__pycache__/main.cpython-311.pyc b/app/__pycache__/main.cpython-311.pyc index b9577d7..deb0419 100644 Binary files a/app/__pycache__/main.cpython-311.pyc and b/app/__pycache__/main.cpython-311.pyc differ diff --git a/app/api/__pycache__/config.cpython-311.pyc b/app/api/__pycache__/config.cpython-311.pyc index 4b1535f..1e93bb5 100644 Binary files a/app/api/__pycache__/config.cpython-311.pyc and b/app/api/__pycache__/config.cpython-311.pyc differ diff --git a/app/api/__pycache__/futures_analysis.cpython-311.pyc b/app/api/__pycache__/futures_analysis.cpython-311.pyc index ba9fd48..b7939f6 100644 Binary files a/app/api/__pycache__/futures_analysis.cpython-311.pyc and b/app/api/__pycache__/futures_analysis.cpython-311.pyc differ diff --git a/app/api/config.py b/app/api/config.py index 4c349b6..27dec30 100644 --- a/app/api/config.py +++ b/app/api/config.py @@ -7,7 +7,7 @@ import shutil from pathlib import Path from typing import Optional -from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Body +from fastapi import APIRouter, Depends, HTTPException, UploadFile, File, Body, Request from fastapi.responses import JSONResponse from sqlalchemy.orm import Session from pydantic import BaseModel @@ -48,12 +48,12 @@ def get_config(): @router.post("/upload") -def upload_config( +async def upload_config( file: Optional[UploadFile] = File(None), - json_config: Optional[dict] = Body(None, embed=False), + request: Request = None, ): """ - 上传品种配置文件(JSON格式)。 + 上传品种配置文件(JSON格式)。 格式示例: { "futures": {"沪银": "AG2606", "沪金": "AU2606"}, @@ -64,12 +64,14 @@ def upload_config( try: if file: - content = file.file.read() + content = await file.read() data = json.loads(content) - elif json_config: - data = json_config else: - raise HTTPException(status_code=400, detail="请提供配置文件或JSON数据") + # 直接从请求体读取 JSON + body = await request.body() + if not body: + raise HTTPException(status_code=400, detail="请提供配置文件或JSON数据") + data = json.loads(body) if not isinstance(data, dict): raise HTTPException(status_code=400, detail="配置文件必须是 JSON 对象") diff --git a/app/api/futures_analysis.py b/app/api/futures_analysis.py index 24d4fc0..72db2e8 100644 --- a/app/api/futures_analysis.py +++ b/app/api/futures_analysis.py @@ -44,6 +44,9 @@ def get_futures_list(db: Session = Depends(get_db)): for name, symbol_code in futures_config.items(): cached = get_cached_data(db, symbol_code, "futures") if cached and cached.get("timeframes"): + # 优先使用缓存中的 current_price 字段 + current_price = cached.get("current_price") + all_candles = [] for period, candles in cached.get("timeframes", {}).items(): all_candles.extend(candles) @@ -55,22 +58,26 @@ def get_futures_list(db: Session = Depends(get_db)): high_price = float(latest_candle.get("high", 0)) low_price = float(latest_candle.get("low", 0)) - change = close_price - open_price + # 如果 current_price 为空,则使用收盘价 + if not current_price: + current_price = close_price + + change = current_price - open_price change_pct = (change / open_price * 100) if open_price > 0 else 0 futures_data.append({ "symbol": symbol_code, "name": name, - "price": close_price, + "price": current_price, "change": round(change, 2), "changePct": round(change_pct, 2), - "suggestion": _get_suggestion(close_price, open_price, change_pct), + "suggestion": _get_suggestion(current_price, open_price, change_pct), "suggestionType": "up" if change >= 0 else "down", "periods": _get_period_trends(all_candles), "successRate": _calc_success_rate(all_candles), "trendScore": _calc_trend_score(all_candles), - "resistance": round(2 * ((high_price + low_price + close_price) / 3) - low_price, 2), - "support": round(2 * ((high_price + low_price + close_price) / 3) - high_price, 2), + "resistance": round(2 * ((high_price + low_price + current_price) / 3) - low_price, 2), + "support": round(2 * ((high_price + low_price + current_price) / 3) - high_price, 2), "open": open_price, "high": high_price, "low": low_price, @@ -106,37 +113,46 @@ def get_futures_detail(symbol: str, db: Session = Depends(get_db)): if not cached: raise HTTPException(status_code=404, detail=f"未找到 {symbol} 的缓存数据") + # 优先使用缓存中的 current_price 字段 + current_price = cached.get("current_price") + + # 从所有K线数据中获取最新的K线用于计算指标 all_candles = [] for period, candles in cached.get("timeframes", {}).items(): all_candles.extend(candles) if not all_candles: raise HTTPException(status_code=404, detail=f"未找到 {symbol} 的K线数据") - + + # 如果 current_price 为空,则从K线数据中获取最新收盘价 + if not current_price: + current_price = float(all_candles[-1].get("close", 0)) + + # 使用最新一条K线数据计算指标 latest_candle = all_candles[-1] open_price = float(latest_candle.get("open", 0)) - close_price = float(latest_candle.get("close", 0)) + close_price = float(latest_candle.get("close", current_price)) high_price = float(latest_candle.get("high", 0)) low_price = float(latest_candle.get("low", 0)) - change = close_price - open_price + change = current_price - open_price change_pct = (change / open_price * 100) if open_price > 0 else 0 # Pivot Point 公式计算关键点位 - pp = (high_price + low_price + close_price) / 3 + pp = (high_price + low_price + current_price) / 3 r1 = round(2 * pp - low_price, 2) r2 = round(pp + (high_price - low_price), 2) s1 = round(2 * pp - high_price, 2) s2 = round(pp - (high_price - low_price), 2) - suggestion = _get_suggestion(close_price, open_price, change_pct) + suggestion = _get_suggestion(current_price, open_price, change_pct) suggestion_type = "up" if change >= 0 else "down" trend_score = _calc_trend_score(all_candles) data = { "symbol": symbol, "name": _get_futures_name(symbol), - "price": close_price, + "price": current_price, "change": round(change, 2), "changePct": round(change_pct, 2), "suggestion": suggestion, @@ -146,7 +162,7 @@ def get_futures_detail(symbol: str, db: Session = Depends(get_db)): "high": high_price, "low": low_price, "volume": sum(float(c.get("volume", 0)) for c in all_candles), - "entryPrice": round(close_price * 0.995, 2) if change >= 0 else round(close_price * 1.005, 2), + "entryPrice": round(current_price * 0.995, 2) if change >= 0 else round(current_price * 1.005, 2), "targetPrice": r1 if change >= 0 else s1, "stopLoss": s1 if change >= 0 else r1, "riskLevel": "低" if trend_score >= 80 else "中" if trend_score >= 60 else "高", diff --git a/app/services/__pycache__/cache.cpython-311.pyc b/app/services/__pycache__/cache.cpython-311.pyc index 1eccd6d..5333b96 100644 Binary files a/app/services/__pycache__/cache.cpython-311.pyc and b/app/services/__pycache__/cache.cpython-311.pyc differ diff --git a/app/static/index.html b/app/static/index.html index 9ac262e..6c155fb 100644 --- a/app/static/index.html +++ b/app/static/index.html @@ -1176,6 +1176,42 @@ + + +