增加历史记录中股票一键加入监测功能
This commit is contained in:
@@ -960,6 +960,13 @@ def display_history_records():
|
||||
st.session_state.viewing_record_id = record['id']
|
||||
|
||||
with col4:
|
||||
if st.button("➕ 监测", key=f"add_monitor_{record['id']}"):
|
||||
st.session_state.add_to_monitor_id = record['id']
|
||||
st.session_state.viewing_record_id = record['id']
|
||||
|
||||
# 删除按钮(新增一行)
|
||||
col5, _, _, _ = st.columns(4)
|
||||
with col5:
|
||||
if st.button("🗑️ 删除", key=f"delete_{record['id']}"):
|
||||
if db.delete_record(record['id']):
|
||||
st.success("✅ 记录已删除")
|
||||
@@ -971,6 +978,182 @@ def display_history_records():
|
||||
if 'viewing_record_id' in st.session_state:
|
||||
display_record_detail(st.session_state.viewing_record_id)
|
||||
|
||||
def display_add_to_monitor_dialog(record):
|
||||
"""显示加入监测的对话框"""
|
||||
st.markdown("---")
|
||||
st.subheader("➕ 加入监测")
|
||||
|
||||
final_decision = record['final_decision']
|
||||
|
||||
# 从final_decision中提取关键数据
|
||||
if isinstance(final_decision, dict):
|
||||
# 解析进场区间
|
||||
entry_range_str = final_decision.get('entry_range', 'N/A')
|
||||
entry_min = 0.0
|
||||
entry_max = 0.0
|
||||
|
||||
# 尝试解析进场区间字符串,支持多种格式
|
||||
if entry_range_str and entry_range_str != 'N/A':
|
||||
try:
|
||||
import re
|
||||
# 移除常见的前缀和单位
|
||||
clean_str = str(entry_range_str).replace('¥', '').replace('元', '').replace('$', '')
|
||||
# 使用正则表达式提取数字
|
||||
# 支持格式:10.5-12.0, 10.5 - 12.0, 10.5~12.0, 10.5至12.0 等
|
||||
numbers = re.findall(r'\d+\.?\d*', clean_str)
|
||||
if len(numbers) >= 2:
|
||||
entry_min = float(numbers[0])
|
||||
entry_max = float(numbers[1])
|
||||
except:
|
||||
# 如果解析失败,尝试用分隔符split
|
||||
try:
|
||||
clean_str = str(entry_range_str).replace('¥', '').replace('元', '').replace('$', '')
|
||||
# 尝试多种分隔符
|
||||
for sep in ['-', '~', '至', '到']:
|
||||
if sep in clean_str:
|
||||
parts = clean_str.split(sep)
|
||||
if len(parts) == 2:
|
||||
entry_min = float(parts[0].strip())
|
||||
entry_max = float(parts[1].strip())
|
||||
break
|
||||
except:
|
||||
pass
|
||||
|
||||
# 提取止盈和止损
|
||||
take_profit_str = final_decision.get('take_profit', 'N/A')
|
||||
stop_loss_str = final_decision.get('stop_loss', 'N/A')
|
||||
|
||||
take_profit = 0.0
|
||||
stop_loss = 0.0
|
||||
|
||||
# 解析止盈位
|
||||
if take_profit_str and take_profit_str != 'N/A':
|
||||
try:
|
||||
import re
|
||||
# 移除单位和符号
|
||||
clean_str = str(take_profit_str).replace('¥', '').replace('元', '').replace('$', '').strip()
|
||||
# 提取第一个数字
|
||||
numbers = re.findall(r'\d+\.?\d*', clean_str)
|
||||
if numbers:
|
||||
take_profit = float(numbers[0])
|
||||
except:
|
||||
pass
|
||||
|
||||
# 解析止损位
|
||||
if stop_loss_str and stop_loss_str != 'N/A':
|
||||
try:
|
||||
import re
|
||||
# 移除单位和符号
|
||||
clean_str = str(stop_loss_str).replace('¥', '').replace('元', '').replace('$', '').strip()
|
||||
# 提取第一个数字
|
||||
numbers = re.findall(r'\d+\.?\d*', clean_str)
|
||||
if numbers:
|
||||
stop_loss = float(numbers[0])
|
||||
except:
|
||||
pass
|
||||
|
||||
# 获取评级
|
||||
rating = final_decision.get('rating', '买入')
|
||||
|
||||
# 检查是否已经在监测列表中
|
||||
from monitor_db import monitor_db
|
||||
existing_stocks = monitor_db.get_monitored_stocks()
|
||||
is_duplicate = any(stock['symbol'] == record['symbol'] for stock in existing_stocks)
|
||||
|
||||
if is_duplicate:
|
||||
st.warning(f"⚠️ {record['symbol']} 已经在监测列表中。继续添加将创建重复监测项。")
|
||||
|
||||
st.info(f"""
|
||||
**从分析结果中提取的数据:**
|
||||
- 进场区间: {entry_min} - {entry_max}
|
||||
- 止盈位: {take_profit if take_profit > 0 else '未设置'}
|
||||
- 止损位: {stop_loss if stop_loss > 0 else '未设置'}
|
||||
- 投资评级: {rating}
|
||||
""")
|
||||
|
||||
# 显示表单供用户确认或修改
|
||||
with st.form(key=f"monitor_form_{record['id']}"):
|
||||
st.markdown("**请确认或修改监测参数:**")
|
||||
|
||||
col1, col2 = st.columns([1, 1])
|
||||
|
||||
with col1:
|
||||
st.subheader("🎯 关键位置")
|
||||
new_entry_min = st.number_input("进场区间最低价", value=float(entry_min), step=0.01, format="%.2f")
|
||||
new_entry_max = st.number_input("进场区间最高价", value=float(entry_max), step=0.01, format="%.2f")
|
||||
new_take_profit = st.number_input("止盈价位", value=float(take_profit), step=0.01, format="%.2f")
|
||||
new_stop_loss = st.number_input("止损价位", value=float(stop_loss), step=0.01, format="%.2f")
|
||||
|
||||
with col2:
|
||||
st.subheader("⚙️ 监测设置")
|
||||
check_interval = st.slider("监测间隔(分钟)", 5, 120, 30)
|
||||
notification_enabled = st.checkbox("启用通知", value=True)
|
||||
new_rating = st.selectbox("投资评级", ["买入", "持有", "卖出"],
|
||||
index=["买入", "持有", "卖出"].index(rating) if rating in ["买入", "持有", "卖出"] else 0)
|
||||
|
||||
col_a, col_b, col_c = st.columns(3)
|
||||
|
||||
with col_a:
|
||||
submit = st.form_submit_button("✅ 确认加入监测", type="primary", use_container_width=True)
|
||||
|
||||
with col_b:
|
||||
cancel = st.form_submit_button("❌ 取消", use_container_width=True)
|
||||
|
||||
if submit:
|
||||
if new_entry_min > 0 and new_entry_max > 0 and new_entry_max > new_entry_min:
|
||||
try:
|
||||
# 添加到监测数据库
|
||||
entry_range = {"min": new_entry_min, "max": new_entry_max}
|
||||
|
||||
stock_id = monitor_db.add_monitored_stock(
|
||||
symbol=record['symbol'],
|
||||
name=record['stock_name'],
|
||||
rating=new_rating,
|
||||
entry_range=entry_range,
|
||||
take_profit=new_take_profit if new_take_profit > 0 else None,
|
||||
stop_loss=new_stop_loss if new_stop_loss > 0 else None,
|
||||
check_interval=check_interval,
|
||||
notification_enabled=notification_enabled
|
||||
)
|
||||
|
||||
st.success(f"✅ 已成功将 {record['symbol']} 加入监测列表!")
|
||||
st.balloons()
|
||||
|
||||
# 立即更新一次价格
|
||||
from monitor_service import monitor_service
|
||||
monitor_service.manual_update_stock(stock_id)
|
||||
|
||||
# 清理session state并跳转到监测页面
|
||||
if 'add_to_monitor_id' in st.session_state:
|
||||
del st.session_state.add_to_monitor_id
|
||||
if 'viewing_record_id' in st.session_state:
|
||||
del st.session_state.viewing_record_id
|
||||
if 'show_history' in st.session_state:
|
||||
del st.session_state.show_history
|
||||
|
||||
# 设置跳转到监测页面
|
||||
st.session_state.show_monitor = True
|
||||
st.session_state.monitor_jump_highlight = record['symbol'] # 标记要高亮显示的股票
|
||||
|
||||
time.sleep(1.5)
|
||||
st.rerun()
|
||||
|
||||
except Exception as e:
|
||||
st.error(f"❌ 加入监测失败: {str(e)}")
|
||||
else:
|
||||
st.error("❌ 请输入有效的进场区间(最低价应小于最高价,且都大于0)")
|
||||
|
||||
if cancel:
|
||||
if 'add_to_monitor_id' in st.session_state:
|
||||
del st.session_state.add_to_monitor_id
|
||||
st.rerun()
|
||||
else:
|
||||
st.warning("⚠️ 无法从分析结果中提取关键数据")
|
||||
if st.button("❌ 取消"):
|
||||
if 'add_to_monitor_id' in st.session_state:
|
||||
del st.session_state.add_to_monitor_id
|
||||
st.rerun()
|
||||
|
||||
def display_record_detail(record_id):
|
||||
"""显示单条记录的详细信息"""
|
||||
st.markdown("---")
|
||||
@@ -1108,11 +1291,29 @@ def display_record_detail(record_id):
|
||||
decision_text = final_decision.get('decision_text', str(final_decision))
|
||||
st.write(decision_text)
|
||||
|
||||
# 加入监测功能
|
||||
st.markdown("---")
|
||||
st.subheader("🎯 操作")
|
||||
|
||||
# 检查是否需要显示加入监测的对话框
|
||||
if 'add_to_monitor_id' in st.session_state and st.session_state.add_to_monitor_id == record_id:
|
||||
display_add_to_monitor_dialog(record)
|
||||
else:
|
||||
# 只有在不显示对话框时才显示按钮
|
||||
col1, col2 = st.columns([1, 3])
|
||||
|
||||
with col1:
|
||||
if st.button("➕ 加入监测", type="primary", use_container_width=True):
|
||||
st.session_state.add_to_monitor_id = record_id
|
||||
st.rerun()
|
||||
|
||||
# 返回按钮
|
||||
st.markdown("---")
|
||||
if st.button("⬅️ 返回历史记录列表"):
|
||||
if 'viewing_record_id' in st.session_state:
|
||||
del st.session_state.viewing_record_id
|
||||
if 'add_to_monitor_id' in st.session_state:
|
||||
del st.session_state.add_to_monitor_id
|
||||
st.rerun()
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in New Issue
Block a user