malkhut: conditional slippage (Fable)

This commit is contained in:
Codex
2026-07-15 17:05:16 +02:00
parent 619966605e
commit c03d914e7a
3 changed files with 41 additions and 4 deletions

View File

@@ -568,8 +568,20 @@ class MinimalCryptoLOBCWM:
# ── Fill Quality computation ────────────────────────────────────────
mid = state.book.mid if state.book.bids and state.book.asks else 0.0
slippage_bps = 0.0
expected_slippage_bps = 0.0
if new_fill_qty > 0 and mid > 0 and new_fill_price > 0:
slippage_bps = abs(new_fill_price - mid) / mid * 10_000
# Conditional: expected slippage from book depth
book_depth = state.book.asks if isinstance(our_action, FulfilmentAction) and our_action.side == Side.BUY else state.book.bids if isinstance(our_action, FulfilmentAction) else ()
cumulative_usd = 0.0
cumulative_levels = 0
for level in (book_depth or ()):
cumulative_usd += level.price * level.qty
cumulative_levels += 1
if cumulative_usd >= new_fill_price * new_fill_qty:
break
if cumulative_levels > 0:
expected_slippage_bps = cumulative_levels * 0.1
is_maker_fill = (our_action.order_type and our_action.order_type.value == "LIMIT") or our_action.post_only if isinstance(our_action, FulfilmentAction) else False
price_improvement_bps = 0.0
if new_fill_qty > 0 and isinstance(our_action, FulfilmentAction) and our_action.post_only and our_action.side:
@@ -594,7 +606,8 @@ class MinimalCryptoLOBCWM:
fill_value = 0.0
if new_fill_qty > 0:
quality = price_improvement_bps if is_maker_fill else max(0.0, spread_bps - slippage_bps)
fill_value = quality - abs(post_fill_adverse) * 0.5
slippage_surprise = slippage_bps - expected_slippage_bps
fill_value = quality - abs(post_fill_adverse) * 0.5 - max(0.0, slippage_surprise) * 0.3
fq = FillQuality(
filled=new_fill_qty > 0,
@@ -602,6 +615,7 @@ class MinimalCryptoLOBCWM:
fill_price=new_fill_price,
requested_qty=our_action.qty_fraction * state.account.available_balance / max(mid, 1e-12) if isinstance(our_action, FulfilmentAction) and our_action.qty_fraction > 0 and mid > 0 else 0.0,
slippage_bps=slippage_bps,
expected_slippage_bps=expected_slippage_bps,
price_improvement_bps=price_improvement_bps,
levels_consumed=0,
is_maker_fill=is_maker_fill,