修复交易记录bug
This commit is contained in:
@@ -3508,6 +3508,7 @@ def is_no_position_error(err_msg):
|
||||
"nothing to close",
|
||||
"pos size is 0",
|
||||
"position amount is 0",
|
||||
"empty position",
|
||||
]
|
||||
return any(k in msg for k in keywords)
|
||||
|
||||
@@ -3943,7 +3944,9 @@ def reconcile_external_closes(conn, days=None):
|
||||
cutoff_ms = int((app_now() - timedelta(days=d)).timestamp() * 1000)
|
||||
except Exception:
|
||||
cutoff_ms = None
|
||||
rows = conn.execute("SELECT * FROM order_monitors WHERE status='active'").fetchall()
|
||||
rows = conn.execute(
|
||||
"SELECT * FROM order_monitors WHERE status IN ('active', 'error')"
|
||||
).fetchall()
|
||||
for r in rows:
|
||||
if cutoff_ms is not None:
|
||||
opened_at_v = get_opened_at_value(r)
|
||||
@@ -3951,6 +3954,17 @@ def reconcile_external_closes(conn, days=None):
|
||||
# 手动同步按最近 N 天过滤,避免把更早历史单误同步进来
|
||||
if opened_ms is None or opened_ms < cutoff_ms:
|
||||
continue
|
||||
oid = int(r["id"])
|
||||
if r["status"] == "error":
|
||||
opened_at_chk = get_opened_at_value(r)
|
||||
existing = conn.execute(
|
||||
"SELECT id FROM trade_records WHERE symbol=? AND opened_at=? AND monitor_type=? LIMIT 1",
|
||||
(r["symbol"], opened_at_chk, order_row_monitor_type(r)),
|
||||
).fetchone()
|
||||
if existing:
|
||||
conn.execute("UPDATE order_monitors SET status='stopped' WHERE id=?", (oid,))
|
||||
synced_count += 1
|
||||
continue
|
||||
exchange_symbol = r["exchange_symbol"] or normalize_exchange_symbol(r["symbol"])
|
||||
live_contracts = get_live_position_contracts(exchange_symbol, r["direction"])
|
||||
if live_contracts is None:
|
||||
@@ -5386,7 +5400,47 @@ def check_order_monitors():
|
||||
conn.execute("UPDATE order_monitors SET status='stopped' WHERE id=?", (pid,))
|
||||
conn.commit()
|
||||
continue
|
||||
conn.execute("UPDATE order_monitors SET status='error' WHERE id=?", (pid,))
|
||||
ex_sym_fail = r["exchange_symbol"] or normalize_exchange_symbol(sym)
|
||||
cancel_binance_futures_open_orders(ex_sym_fail)
|
||||
live_contracts = get_live_position_contracts(ex_sym_fail, direction)
|
||||
if live_contracts is not None and live_contracts <= 0:
|
||||
record_res, record_pnl, record_closed, sync_miss = resolve_synced_flat_close(
|
||||
r, opened_at, opened_at_ms=opened_at_ms
|
||||
)
|
||||
record_miss = f"{sync_miss};本地触发{res}时平仓API失败:{e}"
|
||||
monitor_status = "stopped"
|
||||
else:
|
||||
record_res, record_pnl, record_closed = res, pnl_amount, closed_at
|
||||
record_miss = f"触发{res}后交易所平仓失败(请核对交易所仓位):{e}"
|
||||
monitor_status = "error"
|
||||
record_hold = calc_hold_seconds(
|
||||
opened_at, parse_dt_for_trading_day(record_closed) or now
|
||||
)
|
||||
insert_trade_record(
|
||||
conn,
|
||||
symbol=sym,
|
||||
monitor_type=order_row_monitor_type(r),
|
||||
key_signal_type=order_row_key_signal_type(r),
|
||||
direction=direction,
|
||||
trigger_price=trigger_price,
|
||||
stop_loss=stop_loss,
|
||||
initial_stop_loss=r["initial_stop_loss"] or stop_loss,
|
||||
take_profit=take_profit,
|
||||
margin_capital=margin_capital,
|
||||
leverage=leverage,
|
||||
pnl_amount=record_pnl,
|
||||
hold_seconds=record_hold,
|
||||
trade_style=r["trade_style"],
|
||||
risk_amount=r["risk_amount"],
|
||||
planned_rr=calc_rr_ratio(direction, trigger_price, r["initial_stop_loss"] or stop_loss, take_profit),
|
||||
actual_rr=calc_actual_rr(record_pnl, r["risk_amount"]),
|
||||
result=record_res,
|
||||
miss_reason=record_miss,
|
||||
opened_at=opened_at,
|
||||
closed_at=record_closed,
|
||||
)
|
||||
session_capital = update_session_capital(conn, session_date, record_pnl)
|
||||
conn.execute("UPDATE order_monitors SET status=? WHERE id=?", (monitor_status, pid))
|
||||
conn.commit()
|
||||
send_wechat_msg(
|
||||
build_wechat_monitor_error_message(
|
||||
@@ -5396,6 +5450,23 @@ def check_order_monitors():
|
||||
error_text=str(e),
|
||||
)
|
||||
)
|
||||
if monitor_status == "stopped":
|
||||
send_wechat_msg(
|
||||
build_wechat_close_message(
|
||||
symbol=sym,
|
||||
direction=direction,
|
||||
result=f"{record_res}(已补记入交易记录)",
|
||||
pnl_amount=record_pnl,
|
||||
hold_seconds=record_hold,
|
||||
trigger_price=trigger_price,
|
||||
current_price=p,
|
||||
stop_loss=stop_loss,
|
||||
take_profit=take_profit,
|
||||
close_order_id="-",
|
||||
extra_note=record_miss,
|
||||
session_capital_fallback=session_capital,
|
||||
)
|
||||
)
|
||||
continue
|
||||
cancel_binance_futures_open_orders(r["exchange_symbol"] or normalize_exchange_symbol(sym))
|
||||
exit_ref = exit_p if exit_p and float(exit_p) > 0 else p
|
||||
|
||||
Reference in New Issue
Block a user