PINK: fix fee calibration poisoning + fee_settled trade_id linkage

Two related accounting fixes:

1. _calibrate_fee_model startup guard: before calling calibrate_fee,
   compute raw deviation from the published taker/maker rate (ignoring
   any stale calibration_ratio). If >15%, skip and log WARNING rather
   than letting a bad REST fill set calibration_ratio to ~0.8 and cause
   ESTIMATED fees to understate actuals by 20% for the entire session.

2. fee_settled_events trade_id: BingX WS does not echo back our
   clientOrderId in fill events (field "c" is empty). Was falling back
   to BingX's internal orderId (p-e-mq5.../p-x-mq5...) which can't
   be joined to trade_events. Now reads trade_id from kernel slot 0
   (which retains the trade_id until the next ENTER) so
   fee_settled_events.trade_id = BTCUSDT-T-N. Added venue_order_id
   field to persist_fee_settled for bidirectional reconciliation.

128/128 tests green.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Codex
2026-06-08 20:19:10 +02:00
parent c16b5aaaa4
commit 9e210b5a02
2 changed files with 40 additions and 4 deletions

View File

@@ -624,6 +624,7 @@ class PinkClickHousePersistence:
self,
*,
trade_id: str,
venue_order_id: str = "",
fee: float,
fee_asset: str = "USDT",
is_maker: bool = False,
@@ -637,6 +638,9 @@ class PinkClickHousePersistence:
When the WS ORDER_TRADE_UPDATE frame arrives with field "n" (actual
commission), call this method to log the settled truth.
trade_id should be our BTCUSDT-T-N format (from kernel slot).
venue_order_id is BingX's own orderId for bidirectional lookup.
The CH spool stores both the original estimated row AND this settled row.
Downstream queries can reconcile using:
SELECT trade_id, MAX(fee) FILTER(WHERE fee_source='WS_SETTLED') AS settled_fee,
@@ -650,6 +654,7 @@ class PinkClickHousePersistence:
self._sink("fee_settled_events", {
"ts": ts_val.isoformat() if hasattr(ts_val, "isoformat") else str(ts_val),
"trade_id": trade_id,
"venue_order_id": venue_order_id,
"fee": float(fee),
"fee_asset": fee_asset,
"fee_source": "WS_SETTLED",