Fix D1 open elapsed (use current bar); upsert floating P/L on dashboard.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-10-21 08:44:15 +08:00
parent 27d14472ea
commit 26a3c760db
4 changed files with 154 additions and 38 deletions
+104 -28
View File
@@ -339,6 +339,7 @@ def register_routes(app: Flask) -> None:
"side": t.get("side"),
"volume": t.get("volume"),
"profit": t.get("profit"),
"close_time": t.get("close_time"),
"account_name": t.get("account_name"),
"machine_name": t.get("machine_name"),
"ticket": t.get("ticket"),
@@ -658,35 +659,110 @@ def register_routes(app: Flask) -> None:
if not act.get("account_id"):
return jsonify({"ok": False, "error": "激活码未绑定账户"}), 400
snapshot = data.get("snapshot") or {}
conn.execute(
"""
INSERT INTO trades(
ticket, symbol, side, volume, open_price, close_price,
open_time, close_time, profit, sl, tp,
account_id, machine_id, entry_style, review_note, snapshot_json, created_at
) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
""",
(
str(data.get("ticket") or ""),
(data.get("symbol") or "XAUUSD").upper(),
(data.get("side") or "").lower(),
float(data.get("volume") or 0),
float(data.get("open_price") or 0),
float(data.get("close_price") or 0) if data.get("close_price") is not None else None,
data.get("open_time"),
data.get("close_time"),
float(data.get("profit") or 0) if data.get("profit") is not None else None,
data.get("sl"),
data.get("tp"),
act["account_id"],
act["machine_id"],
(data.get("entry_style") or "").strip(),
(data.get("review_note") or "").strip(),
json.dumps(snapshot, ensure_ascii=False),
database.now(),
),
ticket = str(data.get("ticket") or "").strip()
symbol = (data.get("symbol") or "XAUUSD").upper()
side = (data.get("side") or "").lower()
volume = float(data.get("volume") or 0)
open_price = float(data.get("open_price") or 0)
close_price = (
float(data.get("close_price") or 0)
if data.get("close_price") is not None
else None
)
tid = conn.execute("SELECT last_insert_rowid()").fetchone()[0]
open_time = data.get("open_time")
close_time = data.get("close_time")
if data.get("profit") is None:
profit = None
else:
profit = float(data.get("profit") or 0)
sl = data.get("sl")
tp = data.get("tp")
entry_style = (data.get("entry_style") or "").strip()
review_note = (data.get("review_note") or "").strip()
snap_json = json.dumps(snapshot, ensure_ascii=False)
existing = None
if ticket:
existing = conn.execute(
"""
SELECT id, entry_style, review_note, open_time, close_time, close_price
FROM trades
WHERE account_id=? AND ticket=?
ORDER BY id DESC LIMIT 1
""",
(act["account_id"], ticket),
).fetchone()
if existing:
# 浮盈更新不覆盖已有平仓;空备注不覆盖已标注
keep_close = existing["close_time"] not in (None, "") and close_time in (None, "")
new_close_time = existing["close_time"] if keep_close else close_time
new_close_price = existing["close_price"] if keep_close else close_price
new_entry = entry_style or (existing["entry_style"] or "")
new_note = review_note or (existing["review_note"] or "")
new_open_time = open_time if open_time is not None else existing["open_time"]
if keep_close:
# 已平仓单不再被浮盈推送改写盈亏
profit = conn.execute(
"SELECT profit FROM trades WHERE id=?", (existing["id"],)
).fetchone()["profit"]
conn.execute(
"""
UPDATE trades SET
symbol=?, side=?, volume=?, open_price=?, close_price=?,
open_time=?, close_time=?, profit=?, sl=?, tp=?,
machine_id=?, entry_style=?, review_note=?, snapshot_json=?
WHERE id=?
""",
(
symbol,
side,
volume,
open_price,
new_close_price,
new_open_time,
new_close_time,
profit,
sl,
tp,
act["machine_id"],
new_entry,
new_note,
snap_json,
existing["id"],
),
)
tid = int(existing["id"])
else:
conn.execute(
"""
INSERT INTO trades(
ticket, symbol, side, volume, open_price, close_price,
open_time, close_time, profit, sl, tp,
account_id, machine_id, entry_style, review_note, snapshot_json, created_at
) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)
""",
(
ticket,
symbol,
side,
volume,
open_price,
close_price,
open_time,
close_time,
profit,
sl,
tp,
act["account_id"],
act["machine_id"],
entry_style,
review_note,
snap_json,
database.now(),
),
)
tid = conn.execute("SELECT last_insert_rowid()").fetchone()[0]
return jsonify({"ok": True, "id": tid})
@app.post("/api/v1/bars")
+7 -3
View File
@@ -60,8 +60,8 @@
<td>{{ t.symbol }}</td>
<td><span class="tag {% if t.side=='buy' %}green{% else %}red{% endif %}">{{ t.side }}</span></td>
<td>{{ t.volume if t.volume is not none else '—' }}</td>
<td style="color:{% if t.profit and t.profit>0 %}var(--green){% elif t.profit and t.profit<0 %}var(--red){% else %}inherit{% endif %}">
{{ t.profit if t.profit is not none else '—' }}
<td style="color:{% if t.profit is not none and t.profit>0 %}var(--green){% elif t.profit is not none and t.profit<0 %}var(--red){% else %}inherit{% endif %}">
{% if t.profit is none %}—{% else %}{{ '%.2f'|format(t.profit) }}{% if not t.close_time %}<span class="muted"> 浮</span>{% endif %}{% endif %}
</td>
<td>{{ t.account_name or '—' }}</td>
<td class="muted">{{ t.ticket or '—' }}</td>
@@ -149,7 +149,11 @@
'<td>' + esc(t.symbol) + '</td>' +
'<td><span class="tag ' + sideCls + '">' + esc(t.side) + '</span></td>' +
'<td>' + esc(t.volume != null ? t.volume : '—') + '</td>' +
'<td style="color:' + profitColor(t.profit) + '">' + esc(t.profit != null ? t.profit : '—') + '</td>' +
'<td style="color:' + profitColor(t.profit) + '">' +
(t.profit == null || t.profit === ''
? '—'
: (Number(t.profit).toFixed(2) + (t.close_time ? '' : ' <span class="muted">浮</span>'))) +
'</td>' +
'<td>' + esc(t.account_name || '—') + '</td>' +
'<td class="muted">' + esc(t.ticket || '—') + '</td>' +
'<td><a href="' + detailBase + t.id + '">复盘</a></td>' +