|
|
|
|
@ -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 "高",
|
|
|
|
|
|