update pay ui
This commit is contained in:
88
app.py
88
app.py
@@ -6519,6 +6519,22 @@ def get_batch_kline_data():
|
||||
results = {}
|
||||
|
||||
if chart_type == 'timeline':
|
||||
# 批量获取昨收价(从 MySQL ea_trade 表)
|
||||
prev_close_map = {}
|
||||
target_date_str = target_date.strftime('%Y%m%d')
|
||||
with engine.connect() as conn:
|
||||
base_codes = list(set([code.split('.')[0] for code in codes]))
|
||||
if base_codes:
|
||||
placeholders = ','.join([f':code{i}' for i in range(len(base_codes))])
|
||||
params = {f'code{i}': code for i, code in enumerate(base_codes)}
|
||||
params['trade_date'] = target_date_str
|
||||
result = conn.execute(text(f"""
|
||||
SELECT SECCODE, F007N FROM ea_trade
|
||||
WHERE SECCODE IN ({placeholders}) AND TRADEDATE = :trade_date AND F007N > 0
|
||||
"""), params).fetchall()
|
||||
for row in result:
|
||||
prev_close_map[row[0]] = float(row[1])
|
||||
|
||||
# 批量查询分时数据(使用标准化代码查询 ClickHouse)
|
||||
batch_data = client.execute("""
|
||||
SELECT code, timestamp, close, volume
|
||||
@@ -6532,16 +6548,35 @@ def get_batch_kline_data():
|
||||
'end': end_time
|
||||
})
|
||||
|
||||
# 按股票代码分组(标准化代码 -> 数据列表)
|
||||
# 按股票代码分组,同时计算均价和涨跌幅
|
||||
stock_data = {}
|
||||
stock_accum = {} # 用于计算均价的累计值
|
||||
for row in batch_data:
|
||||
norm_code = row[0]
|
||||
base_code = norm_code.split('.')[0]
|
||||
price = float(row[2])
|
||||
volume = float(row[3])
|
||||
|
||||
if norm_code not in stock_data:
|
||||
stock_data[norm_code] = []
|
||||
stock_accum[norm_code] = {'total_amount': 0, 'total_volume': 0}
|
||||
|
||||
# 累计计算均价
|
||||
stock_accum[norm_code]['total_amount'] += price * volume
|
||||
stock_accum[norm_code]['total_volume'] += volume
|
||||
total_vol = stock_accum[norm_code]['total_volume']
|
||||
avg_price = stock_accum[norm_code]['total_amount'] / total_vol if total_vol > 0 else price
|
||||
|
||||
# 计算涨跌幅
|
||||
prev_close = prev_close_map.get(base_code)
|
||||
change_percent = ((price - prev_close) / prev_close * 100) if prev_close and prev_close > 0 else 0
|
||||
|
||||
stock_data[norm_code].append({
|
||||
'time': row[1].strftime('%H:%M'),
|
||||
'price': float(row[2]),
|
||||
'volume': float(row[3])
|
||||
'price': price,
|
||||
'avg_price': round(avg_price, 2),
|
||||
'volume': volume,
|
||||
'change_percent': round(change_percent, 2)
|
||||
})
|
||||
|
||||
# 组装结果(使用原始代码作为 key 返回)
|
||||
@@ -6550,13 +6585,15 @@ def get_batch_kline_data():
|
||||
base_code = orig_code.split('.')[0]
|
||||
stock_name = stock_names.get(base_code, f'股票{base_code}')
|
||||
data_list = stock_data.get(norm_code, [])
|
||||
prev_close = prev_close_map.get(base_code)
|
||||
|
||||
results[orig_code] = {
|
||||
'code': orig_code,
|
||||
'name': stock_name,
|
||||
'data': data_list,
|
||||
'trade_date': target_date.strftime('%Y-%m-%d'),
|
||||
'type': 'timeline'
|
||||
'type': 'timeline',
|
||||
'prev_close': prev_close
|
||||
}
|
||||
|
||||
elif chart_type == 'daily':
|
||||
@@ -7634,23 +7671,36 @@ def get_timeline_data(stock_code, event_datetime, stock_name):
|
||||
'type': 'timeline'
|
||||
})
|
||||
|
||||
# 获取昨收盘价
|
||||
prev_close_query = """
|
||||
SELECT close
|
||||
FROM stock_minute
|
||||
WHERE code = %(code)s
|
||||
AND timestamp \
|
||||
< %(start)s
|
||||
ORDER BY timestamp DESC
|
||||
LIMIT 1 \
|
||||
"""
|
||||
# 获取昨收盘价 - 优先从 MySQL ea_trade 表获取(更可靠)
|
||||
prev_close = None
|
||||
base_code = stock_code.split('.')[0]
|
||||
target_date_str = target_date.strftime('%Y%m%d')
|
||||
|
||||
prev_close_result = client.execute(prev_close_query, {
|
||||
'code': stock_code,
|
||||
'start': datetime.combine(target_date, dt_time(9, 30))
|
||||
})
|
||||
try:
|
||||
with engine.connect() as conn:
|
||||
# F007N 是昨收价字段
|
||||
result = conn.execute(text("""
|
||||
SELECT F007N FROM ea_trade
|
||||
WHERE SECCODE = :code AND TRADEDATE = :trade_date AND F007N > 0
|
||||
"""), {'code': base_code, 'trade_date': target_date_str}).fetchone()
|
||||
if result and result[0]:
|
||||
prev_close = float(result[0])
|
||||
except Exception as e:
|
||||
logger.warning(f"从 ea_trade 获取昨收价失败: {e}")
|
||||
|
||||
prev_close = float(prev_close_result[0][0]) if prev_close_result else None
|
||||
# 如果 MySQL 没有数据,回退到 ClickHouse
|
||||
if prev_close is None:
|
||||
prev_close_query = """
|
||||
SELECT close FROM stock_minute
|
||||
WHERE code = %(code)s AND timestamp < %(start)s
|
||||
ORDER BY timestamp DESC LIMIT 1
|
||||
"""
|
||||
prev_close_result = client.execute(prev_close_query, {
|
||||
'code': stock_code,
|
||||
'start': datetime.combine(target_date, dt_time(9, 30))
|
||||
})
|
||||
if prev_close_result:
|
||||
prev_close = float(prev_close_result[0][0])
|
||||
|
||||
data = client.execute(
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user