Fix TP/SL exit classification when exchange fill slips past the tight band.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-01 09:38:28 +08:00
parent 4b4dca9e3c
commit a00699aec3
5 changed files with 80 additions and 70 deletions
+1 -23
View File
@@ -256,6 +256,7 @@ from lib.common.history_window_lib import (
utc_window_to_utc_sql_strings,
)
from lib.trade.trade_result_lib import (
classify_exit_by_levels,
count_winning_trades,
filter_trade_records_excluding_miss,
normalize_result_with_pnl,
@@ -3381,29 +3382,6 @@ def ms_to_app_local_str(ms):
return app_now_str()
def classify_exit_by_levels(direction, trigger_price, stop_loss, take_profit, exit_price):
"""根据成交价相对止盈/止损位归类;无法可靠归类时返回 None."""
try:
tp = float(take_profit)
sl = float(stop_loss)
ex = float(exit_price)
trig = float(trigger_price)
except (TypeError, ValueError):
return None
band = max(abs(trig) * 0.0008, abs(tp - sl) * 0.003, 1e-12)
if direction == "long":
if ex >= tp - band:
return "止盈"
if ex <= sl + band:
return "止损"
else:
if ex <= tp + band:
return "止盈"
if ex >= sl - band:
return "止损"
return None
def fetch_latest_closing_fill(exchange_symbol, direction, opened_at_str, opened_at_ms=None):
"""取开仓以来最近一笔减仓成交(与方向一致);失败返回 None."""
if not (OKX_API_KEY and OKX_API_SECRET and OKX_API_PASSPHRASE):