fix: 修复涨跌幅获取
This commit is contained in:
@@ -469,21 +469,12 @@ function handleStreamUpdate(data: StreamAnalysisUpdate) {
|
||||
|
||||
// 确保所有数值类型的字段都有默认值
|
||||
stock.price = data.price ?? stock.price ?? undefined;
|
||||
stock.price_change = data.price_change ?? stock.price_change ?? undefined;
|
||||
// 使用change_percent作为涨跌幅
|
||||
stock.price_change = data.price_change_value ?? data.price_change ?? stock.price_change ?? undefined;
|
||||
stock.changePercent = data.change_percent ?? stock.changePercent ?? undefined;
|
||||
stock.marketValue = data.market_value ?? stock.marketValue ?? undefined;
|
||||
stock.score = data.score ?? stock.score ?? undefined;
|
||||
stock.rsi = data.rsi ?? stock.rsi ?? undefined;
|
||||
|
||||
// 如果没有change_percent但有price_change和price,尝试计算changePercent
|
||||
if (stock.changePercent === undefined && stock.price_change !== undefined && stock.price !== undefined) {
|
||||
const basePrice = stock.price - stock.price_change;
|
||||
if (basePrice !== 0) {
|
||||
stock.changePercent = (stock.price_change / basePrice) * 100;
|
||||
}
|
||||
}
|
||||
|
||||
// 更新分析状态
|
||||
if (data.status) {
|
||||
stock.analysisStatus = data.status;
|
||||
|
||||
@@ -211,23 +211,11 @@ function highlightKeywords(html: string): string {
|
||||
return html;
|
||||
}
|
||||
|
||||
// 计算涨跌幅
|
||||
// 获取涨跌幅
|
||||
const calculatedChangePercent = computed(() => {
|
||||
// 如果已有changePercent,则直接使用
|
||||
if (props.stock.changePercent !== undefined) {
|
||||
return props.stock.changePercent;
|
||||
}
|
||||
|
||||
// 如果有price_change和price,则计算涨跌幅
|
||||
if (props.stock.price_change !== undefined && props.stock.price !== undefined) {
|
||||
// 计算涨跌幅百分比 = (价格变动 / (当前价格 - 价格变动)) * 100
|
||||
const basePrice = props.stock.price - props.stock.price_change;
|
||||
if (basePrice !== 0) {
|
||||
return (props.stock.price_change / basePrice) * 100;
|
||||
}
|
||||
}
|
||||
|
||||
// 无法计算则返回undefined
|
||||
return undefined;
|
||||
});
|
||||
|
||||
|
||||
@@ -96,6 +96,7 @@ export interface StreamAnalysisUpdate {
|
||||
name?: string;
|
||||
price?: number;
|
||||
change_percent?: number;
|
||||
price_change_value?: number;
|
||||
market_value?: number;
|
||||
score?: number;
|
||||
recommendation?: string;
|
||||
|
||||
@@ -95,8 +95,15 @@ class StockAnalyzerService:
|
||||
latest_data = df_with_indicators.iloc[-1]
|
||||
previous_data = df_with_indicators.iloc[-2] if len(df_with_indicators) > 1 else latest_data
|
||||
|
||||
# 计算价格变化百分比
|
||||
price_change = ((latest_data['Close'] - previous_data['Close']) / previous_data['Close']) * 100
|
||||
# 价格变动绝对值
|
||||
price_change_value = latest_data['Close'] - previous_data['Close']
|
||||
|
||||
# 优先使用原始数据中的涨跌幅(Change_pct)
|
||||
change_percent = latest_data.get('Change_pct')
|
||||
|
||||
# 如果原始数据中没有涨跌幅,才进行计算
|
||||
if change_percent is None and previous_data['Close'] != 0:
|
||||
change_percent = (price_change_value / previous_data['Close']) * 100
|
||||
|
||||
# 确定MA趋势
|
||||
ma_short = latest_data.get('MA5', 0)
|
||||
@@ -142,7 +149,9 @@ class StockAnalyzerService:
|
||||
"analysis_date": analysis_date,
|
||||
"score": score,
|
||||
"price": latest_data['Close'],
|
||||
"price_change": price_change,
|
||||
"price_change_value": price_change_value, # 价格变动绝对值
|
||||
"price_change": change_percent, # 兼容旧版前端,传递涨跌幅
|
||||
"change_percent": change_percent, # 涨跌幅百分比,新字段
|
||||
"ma_trend": ma_trend,
|
||||
"rsi": latest_data.get('RSI', 0),
|
||||
"macd_signal": macd_signal,
|
||||
@@ -220,6 +229,13 @@ class StockAnalyzerService:
|
||||
if df is not None and len(df) > 0:
|
||||
# 获取最新数据
|
||||
latest_data = df.iloc[-1]
|
||||
previous_data = df.iloc[-2] if len(df) > 1 else latest_data
|
||||
|
||||
# 价格变动绝对值
|
||||
price_change_value = latest_data['Close'] - previous_data['Close']
|
||||
|
||||
# 获取涨跌幅
|
||||
change_percent = latest_data.get('Change_pct')
|
||||
|
||||
# 发送股票基本信息和评分
|
||||
yield json.dumps({
|
||||
@@ -227,7 +243,9 @@ class StockAnalyzerService:
|
||||
"score": score,
|
||||
"recommendation": rec,
|
||||
"price": float(latest_data.get('Close', 0)),
|
||||
"price_change": float(latest_data.get('Change', 0)),
|
||||
"price_change_value": float(price_change_value), # 价格变动绝对值
|
||||
"price_change": change_percent, # 兼容旧版前端,传递涨跌幅
|
||||
"change_percent": change_percent, # 涨跌幅百分比,新字段
|
||||
"rsi": float(latest_data.get('RSI', 0)) if 'RSI' in latest_data else None,
|
||||
"ma_trend": "UP" if latest_data.get('MA5', 0) > latest_data.get('MA20', 0) else "DOWN",
|
||||
"macd_signal": "BUY" if latest_data.get('MACD', 0) > latest_data.get('MACD_Signal', 0) else "SELL",
|
||||
|
||||
Reference in New Issue
Block a user