diff --git a/app.py b/app.py index a4e8437..87ef7e0 100644 --- a/app.py +++ b/app.py @@ -1280,34 +1280,83 @@ def trades(): def update_trade(tid): d = request.form conn = get_db() + row = conn.execute("SELECT * FROM trade_logs WHERE id=?", (tid,)).fetchone() + if not row: + conn.close() + flash("记录不存在") + return redirect(url_for("records")) + row = dict(row) + entry = float(d.get("entry_price") or 0) + close_px = float(d.get("close_price") or 0) + lots = float(d.get("lots") or 0) + sl_raw = d.get("stop_loss") + tp_raw = d.get("take_profit") + stop_loss = float(sl_raw) if sl_raw not in (None, "") else None + take_profit = float(tp_raw) if tp_raw not in (None, "") else None + open_time = (d.get("open_time") or row.get("open_time") or "").strip() + close_time = (d.get("close_time") or row.get("close_time") or "").strip() + direction = (d.get("direction") or row.get("direction") or "long").strip() + + from trade_log_lib import recalc_trade_log_pnl, refresh_trade_log_equity_chain, _read_initial_capital + from trading_context import get_trading_mode + + pnl = float(row.get("pnl") or 0) + fee = float(row.get("fee") or 0) + pnl_net = float(row.get("pnl_net") or 0) + old_entry = float(row.get("entry_price") or 0) + old_close = float(row.get("close_price") or 0) + old_lots = float(row.get("lots") or 0) + prices_changed = ( + abs(entry - old_entry) > 0.0001 + or abs(close_px - old_close) > 0.0001 + or abs(lots - old_lots) > 0.0001 + ) + if prices_changed and close_px > 0 and entry > 0 and lots > 0: + calc = recalc_trade_log_pnl( + symbol=row.get("symbol") or "", + direction=direction, + entry_price=entry, + close_price=close_px, + lots=lots, + stop_loss=stop_loss, + take_profit=take_profit, + open_time=open_time, + close_time=close_time, + trading_mode=get_trading_mode(get_setting), + ) + pnl = calc["pnl"] + fee = calc["fee"] + pnl_net = calc["pnl_net"] + conn.execute( """UPDATE trade_logs SET symbol_name=?, monitor_type=?, direction=?, entry_price=?, stop_loss=?, take_profit=?, close_price=?, lots=?, margin=?, holding_minutes=?, open_time=?, close_time=?, - pnl=?, result=?, verified=1 + pnl=?, fee=?, pnl_net=?, result=?, verified=1 WHERE id=?""", ( d.get("symbol_name", "").strip(), d.get("monitor_type", "").strip(), - d.get("direction", "").strip(), - float(d.get("entry_price") or 0), - float(d.get("stop_loss") or 0), - float(d.get("take_profit") or 0), - float(d.get("close_price") or 0), - float(d.get("lots") or 0), + direction, + entry, + stop_loss, + take_profit, + close_px, + lots, float(d.get("margin") or 0), int(d.get("holding_minutes") or 0), - d.get("open_time", "").strip(), - d.get("close_time", "").strip(), - float(d.get("pnl") or 0), + open_time, + close_time, + pnl, + fee, + pnl_net, d.get("result", "").strip(), tid, ), ) try: - cap = float(get_setting("live_capital", "0") or 0) - refresh_trade_log_equity_chain(conn, cap if cap > 0 else None) + refresh_trade_log_equity_chain(conn, _read_initial_capital(conn)) except Exception as exc: app.logger.debug("equity chain refresh after trade edit: %s", exc) conn.commit() diff --git a/static/js/records.js b/static/js/records.js index 23e4cd5..b890061 100644 --- a/static/js/records.js +++ b/static/js/records.js @@ -22,7 +22,7 @@ { label: '合约', value: esc(data.symbol_code), wide: false }, { label: '类型', value: esc(data.monitor_type) + ' · ' + esc(data.source), wide: false }, { label: '方向', value: esc(data.direction), wide: false }, - { label: '成交价', value: esc(data.entry_price), wide: false }, + { label: '开仓价', value: esc(data.entry_price), wide: false }, { label: '平仓价', value: esc(data.close_price), wide: false }, { label: '手数', value: esc(data.lots), wide: false }, { label: '止损', value: esc(data.stop_loss), wide: false }, diff --git a/templates/records.html b/templates/records.html index 384fa76..5042226 100644 --- a/templates/records.html +++ b/templates/records.html @@ -93,6 +93,9 @@ {% else %}

CTP 未连接时仅显示本地数据库记录;连接后打开本页会自动同步柜台成交。

{% endif %} +

+ 跨日持仓的盈亏以平仓价与柜台结算为准;表格中「开仓价」为程序记录,「平仓价」为成交回报,二者不一致时请以平仓价核对净盈亏。 +