feat: 策略追踪评估系统(版本化+理论盈亏+浮盈/浮亏+自动SL/TP检测+30s刷新)

This commit is contained in:
hmo
2026-07-27 19:03:05 +08:00
parent 12e3abc57d
commit c572bbc3ef
4 changed files with 305 additions and 82 deletions
+44
View File
@@ -488,6 +488,50 @@ def api_watchlist():
return jsonify({"error": str(e)}), 500
@app.route("/api/tracking")
def get_tracking():
"""策略追踪评估:所有推荐的历史记录和结果"""
import sqlite3
conn = sqlite3.connect("/home/hmo/MoFin/data/mofin.db")
conn.row_factory = sqlite3.Row
rows = conn.execute("""
SELECT st.*, lp.price as current_price
FROM strategy_tracking st
LEFT JOIN live_prices lp ON st.code = lp.code
ORDER BY
CASE st.status WHEN 'active' THEN 0 WHEN 'hit_tp' THEN 1 WHEN 'hit_sl' THEN 2 ELSE 3 END,
st.tracked_at DESC
""").fetchall()
tracks = []
for r in rows:
d = dict(r)
# 理论盈亏:按买入区中值买入,当前价 vs 中值
if d.get('entry_mid') and d.get('current_price') and d.get('status') == 'active':
mid = float(d['entry_mid'])
price = float(d['current_price'])
d['theoretical_pnl'] = round((price - mid) / mid * 100, 2)
# 浮盈:有实操且 active 状态
if d.get('actual_entry') and d.get('actual_shares') and d.get('status') == 'active' and d.get('current_price'):
entry = float(d['actual_entry'])
price = float(d['current_price'])
shares = int(d['actual_shares'])
d['floating_pnl'] = round((price - entry) / entry * 100, 2)
d['floating_amount'] = round((price - entry) * shares, 2)
tracks.append(d)
conn.close()
return jsonify({
"tracks": tracks,
"stats": {
"total": len(tracks),
"active": sum(1 for r in tracks if r["status"] == "active"),
"hit_tp": sum(1 for r in tracks if r["status"] == "hit_tp"),
"hit_sl": sum(1 for r in tracks if r["status"] == "hit_sl"),
"expired": sum(1 for r in tracks if r["status"] == "expired"),
"manual": sum(1 for r in tracks if r["status"] == "manual_close"),
}
})
@app.route("/api/overview")
def api_overview():
"""概览数据"""