diff --git a/app/db.py b/app/db.py index dd241df..b8398f0 100644 --- a/app/db.py +++ b/app/db.py @@ -268,6 +268,7 @@ def resolve_web_user_for_activation(conn: sqlite3.Connection, act: dict) -> Opti def _trades_where( account_id: Optional[int] = None, machine_id: Optional[int] = None, + open_only: bool = False, ) -> tuple[str, list[Any]]: where: list[str] = [] params: list[Any] = [] @@ -277,6 +278,8 @@ def _trades_where( if machine_id is not None: where.append("t.machine_id=?") params.append(machine_id) + if open_only: + where.append("(t.close_time IS NULL OR t.close_time='')") clause = (" WHERE " + " AND ".join(where)) if where else "" return clause, params @@ -285,8 +288,9 @@ def count_trades( conn: sqlite3.Connection, account_id: Optional[int] = None, machine_id: Optional[int] = None, + open_only: bool = False, ) -> int: - clause, params = _trades_where(account_id, machine_id) + clause, params = _trades_where(account_id, machine_id, open_only=open_only) sql = f"SELECT COUNT(*) AS c FROM trades t{clause}" row = conn.execute(sql, tuple(params)).fetchone() return int(row["c"] if row else 0) @@ -298,15 +302,21 @@ def list_trades( account_id: Optional[int] = None, machine_id: Optional[int] = None, offset: int = 0, + open_only: bool = False, ) -> list[dict]: - clause, params = _trades_where(account_id, machine_id) + clause, params = _trades_where(account_id, machine_id, open_only=open_only) + order = ( + "ORDER BY COALESCE(t.open_time, t.created_at) DESC" + if open_only + else "ORDER BY COALESCE(t.close_time, t.open_time, t.created_at) DESC" + ) sql = f""" SELECT t.*, a.name AS account_name, m.name AS machine_name FROM trades t LEFT JOIN accounts a ON a.id = t.account_id LEFT JOIN machines m ON m.id = t.machine_id {clause} - ORDER BY COALESCE(t.close_time, t.open_time, t.created_at) DESC + {order} LIMIT ? OFFSET ? """ params.extend([limit, max(0, int(offset))]) diff --git a/app/routes.py b/app/routes.py index 7f42ab2..0b84a8c 100644 --- a/app/routes.py +++ b/app/routes.py @@ -280,25 +280,26 @@ def register_routes(app: Flask) -> None: t["hold_duration"] = t["hold_duration"] + "(持仓中)" def _dashboard_payload(u: dict) -> dict: + # 总览只看当前持仓(未平仓),历史成交在「交易复盘」 with database.get_db() as conn: if u["role"] == "admin": accounts = database.list_accounts(conn) - trades = database.list_trades(conn, 40) + trades = database.list_trades(conn, 100, open_only=True) else: accounts = [ a for a in database.list_accounts(conn) if a["id"] == u.get("account_id") ] - trades = database.list_trades(conn, 40, account_id=u.get("account_id")) + trades = database.list_trades( + conn, 100, account_id=u.get("account_id"), open_only=True + ) machines = database.list_machines_for_user(conn, u) online_machines = [m for m in machines if m.get("online")] for m in machines: m["last_seen_fmt"] = _fmt_ts(m.get("last_seen_at")) for t in trades: - t["time_fmt"] = _fmt_ts( - t.get("close_time") or t.get("open_time") or t.get("created_at") - ) + t["time_fmt"] = _fmt_ts(t.get("open_time") or t.get("created_at")) return { "accounts": accounts, "machines": machines, diff --git a/app/templates/dashboard.html b/app/templates/dashboard.html index 3116c1c..41d7e8f 100644 --- a/app/templates/dashboard.html +++ b/app/templates/dashboard.html @@ -11,7 +11,7 @@
下载通用辅助包并用激活码绑定;成交自动上报到本页实时记录与交易复盘。
+下载通用辅助包并用激活码绑定;本页显示当前持仓,历史成交见交易复盘。
| 时间 | 机器 | 品种 | 方向 | 手数 | -盈亏 | 账户 | 单号 | + | 开仓时间 | 机器 | 品种 | 方向 | 手数 | +浮盈 | 账户 | 单号 | {{ t.side }} | {{ t.volume if t.volume is not none else '—' }} | - {% if t.profit is none %}—{% else %}{{ '%.2f'|format(t.profit) }}{% if not t.close_time %} 浮{% endif %}{% endif %} + {% if t.profit is none %}—{% else %}{{ '%.2f'|format(t.profit) }}{% endif %} | {{ t.account_name or '—' }} | {{ t.ticket or '—' }} | 复盘 | {% else %} -
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| 暂无成交。客户端上报后将实时显示。 | |||||||||||||||||
| 当前无持仓。开仓后由客户端上报显示。 | |||||||||||||||||