feat: queue-aware paper fills, kill switch, systemd services, WQI+FundingArb in paper trader
- live/paper_trader.py: replaced random 5% fill probability in simulate_avellaneda() with QueueAwareFillModel — fills only when aggressor volume exceeds depth ahead, regime-adaptive quote placement (tight in LOW_VOL, wide in HIGH_VOL). Integrated WQI Predictor and Funding Rate Arb as new strategies with signal generation. Dashboard metrics now include WQI summaries, funding arb status, and fill model throughput stats (fill rate, fills vs skips). 14 strategies total. - scripts/kill_switch.py: emergency kill switch — flattens all positions, cancels all open orders, verifies account is flat. Supports --dry-run, --mainnet, retry logic, L1 action signing. Reads private key from HL_PRIVATE_KEY env or ~/.hl/key. - infrastructure/systemd/: three service unit files for production deployment: ftdt-collector (data collection), ftdt-paper (trading node v2), ftdt-dashboard (FastAPI backend). Includes memory/cpu limits, auto-restart, log rotation. 321 tests passing.
This commit is contained in:
+127
-55
@@ -20,6 +20,9 @@ from strategies.deep_lob import DeepLOB
|
||||
from strategies.cartea_jaimungal import CarteaJaimungal
|
||||
from strategies.queue_imbalance import QueueImbalance
|
||||
from strategies.gueant import GueantMM
|
||||
from strategies.wqi_predictor import WQIPredictor
|
||||
from strategies.funding_arb_strategy import FundingArb
|
||||
from sim.fills import QueueAwareFillModel
|
||||
|
||||
logging.basicConfig(level=logging.INFO, format="%(asctime)s [paper] %(message)s", datefmt="%H:%M:%S")
|
||||
log = logging.getLogger("ftdt-paper")
|
||||
@@ -129,6 +132,20 @@ STRATEGIES = {
|
||||
"signals": [], "type": "gueant", "size": 0.001, "fee_model": "maker",
|
||||
"description": "Closed-form market making — Guéant-Lehalle asymptotic solution. Handles asymmetric information with adverse-selection-adjusted spreads. Computationally efficient closed form.",
|
||||
},
|
||||
"WQI Predictor": {
|
||||
"allocation": 10000.0, "instrument": "BTC", "pnl": 0.0,
|
||||
"trades_today": 0, "wins": 0, "win_rate": 0.0, "status": "idle",
|
||||
"position": 0.0, "entry_price": 0.0, "fee_paid": 0.0,
|
||||
"signals": [], "type": "wqi", "size": 0.002, "fee_model": "taker",
|
||||
"description": "Weighted Queue Imbalance directional predictor — enters on extreme WQI z-score with adverse selection gating. Exits on timeout, reversal, or stop-loss.",
|
||||
},
|
||||
"Funding Rate Arb": {
|
||||
"allocation": 10000.0, "instrument": "BTC", "pnl": 0.0,
|
||||
"trades_today": 0, "wins": 0, "win_rate": 0.0, "status": "idle",
|
||||
"position": 0.0, "entry_price": 0.0, "fee_paid": 0.0,
|
||||
"signals": [], "type": "funding_arb", "size": 0.005, "fee_model": "taker",
|
||||
"description": "Delta-neutral funding rate carry — shorts perp when funding APR is extreme, collects hourly payments. Exits when rate fades, flips, or max hold reached.",
|
||||
},
|
||||
}
|
||||
|
||||
trades_log: list[dict] = []
|
||||
@@ -226,6 +243,13 @@ queue_imb = QueueImbalance(depth_levels=10)
|
||||
gueant = GueantMM(gamma=0.1, sigma=0.015, k=1.5, T=3600, max_pos=0.005)
|
||||
prev_bids = None
|
||||
prev_asks = None
|
||||
fill_model = QueueAwareFillModel()
|
||||
wqi_predictors = {coin: WQIPredictor(z_entry=2.0, max_hold_seconds=30,
|
||||
stop_loss_bps=5.0, take_profit_bps=10.0,
|
||||
size=0.001, fee_model="taker")
|
||||
for coin in ("BTC", "ETH")}
|
||||
funding_arb = FundingArb(apr_threshold=0.30, apr_exit=0.10, size=0.001,
|
||||
max_hold_hours=48.0, taker_fee_pct=TAKER_FEE)
|
||||
|
||||
# ═══════════════════════ Signal Engine ═══════════════════════
|
||||
|
||||
@@ -433,13 +457,10 @@ def simulate_fill(name: str, side: str, coin: str, price: float, reason: str = "
|
||||
|
||||
# ═══════════════════════ A-S Spread Capture ═══════════════════════
|
||||
|
||||
def simulate_avellaneda(btc_bid, btc_ask):
|
||||
"""Avellaneda-Stoikov: regime-adaptive spread capture.
|
||||
|
||||
Regime-dependent behavior:
|
||||
LOW_VOL → fill_prob=25%, tight margins (capture small spreads frequently)
|
||||
NORMAL → fill_prob=15%, baseline
|
||||
HIGH_VOL → fill_prob=8%, skip if spread too wide (adverse selection risk)
|
||||
def simulate_avellaneda(btc_bid, btc_ask, bid_depth=2.0, ask_depth=2.0):
|
||||
"""Avellaneda-Stoikov: queue-aware spread capture with regime gating.
|
||||
|
||||
Uses QueueAwareFillModel instead of random probabilities.
|
||||
"""
|
||||
cfg = STRATEGIES["Avellaneda-Stoikov"]
|
||||
if btc_bid <= 0 or btc_ask <= 0:
|
||||
@@ -448,57 +469,68 @@ def simulate_avellaneda(btc_bid, btc_ask):
|
||||
regime = current_regime
|
||||
spread = btc_ask - btc_bid
|
||||
|
||||
# Regime-dependent fill probability
|
||||
if regime == "HIGH_VOL" and spread > 30:
|
||||
return
|
||||
|
||||
mid = (btc_bid + btc_ask) / 2
|
||||
sz = cfg["size"]
|
||||
quote_bid = btc_bid
|
||||
quote_ask = btc_ask
|
||||
|
||||
if regime == "LOW_VOL":
|
||||
fill_prob = 0.25
|
||||
quote_bid = btc_bid + spread * 0.05
|
||||
quote_ask = btc_ask - spread * 0.05
|
||||
elif regime == "HIGH_VOL":
|
||||
fill_prob = 0.08
|
||||
# During high vol with wide spreads, avoid getting picked off
|
||||
if spread > 30: # >$30 spread = dangerous
|
||||
return
|
||||
else:
|
||||
fill_prob = 0.15
|
||||
quote_bid = btc_bid - spread * 0.1
|
||||
quote_ask = btc_ask + spread * 0.1
|
||||
|
||||
if random.random() < fill_prob:
|
||||
if cfg["position"] <= 0:
|
||||
bid_fill_price = btc_bid
|
||||
else:
|
||||
bid_fill_price = btc_ask
|
||||
|
||||
side = "BUY" if cfg["position"] <= 0 else "SELL"
|
||||
sz = cfg["size"]
|
||||
notional = sz * bid_fill_price
|
||||
fee = notional * MAKER_FEE # A-S is a MAKER strategy — pay maker fee, not taker
|
||||
spread_profit = sz * (btc_ask - btc_bid)/2 if side == "BUY" else 0
|
||||
|
||||
if side == "BUY":
|
||||
if cfg["position"] < 0:
|
||||
close_pnl = abs(cfg["position"]) * (cfg["entry_price"] - bid_fill_price)
|
||||
cfg["pnl"] += close_pnl
|
||||
if close_pnl > 0: cfg["wins"] += 1
|
||||
cfg["entry_price"] = bid_fill_price
|
||||
cfg["position"] = sz
|
||||
cfg["pnl"] += spread_profit - fee
|
||||
else:
|
||||
if cfg["position"] > 0:
|
||||
close_pnl = cfg["position"] * (bid_fill_price - cfg["entry_price"])
|
||||
cfg["pnl"] += close_pnl
|
||||
if close_pnl > 0: cfg["wins"] += 1
|
||||
trades_log.append({
|
||||
"time": datetime.now().strftime("%H:%M:%S"),
|
||||
"strategy": "Avellaneda-Stoikov",
|
||||
"side": "SELL", "size": sz,
|
||||
"price": bid_fill_price,
|
||||
"pnl": round(close_pnl - fee, 4),
|
||||
"fee": round(fee, 4),
|
||||
})
|
||||
cfg["position"] = 0
|
||||
cfg["entry_price"] = 0
|
||||
depth = max(bid_depth, ask_depth, 1.0)
|
||||
depth_ahead = depth * 0.5
|
||||
|
||||
bid_fill = fill_model.check_fill(
|
||||
aggressor_side="sell", agg_size=depth * 0.3, agg_price=max(quote_bid, 1),
|
||||
our_price=quote_bid, our_size=sz, depth_ahead=depth_ahead,
|
||||
)
|
||||
if bid_fill["filled"] and cfg["position"] <= 0:
|
||||
fee = sz * bid_fill["fill_size"] * quote_bid * MAKER_FEE
|
||||
cfg["fee_paid"] += fee
|
||||
spread_profit = bid_fill["fill_size"] * (btc_ask - quote_bid) / 2
|
||||
if cfg["position"] < 0:
|
||||
close_pnl = abs(cfg["position"]) * (cfg["entry_price"] - quote_bid)
|
||||
cfg["pnl"] += close_pnl
|
||||
if close_pnl > 0:
|
||||
cfg["wins"] += 1
|
||||
cfg["entry_price"] = quote_bid
|
||||
cfg["position"] = sz
|
||||
cfg["pnl"] += spread_profit - fee
|
||||
cfg["trades_today"] += 1
|
||||
cfg["pnl_pct"] = cfg["pnl"] / cfg["allocation"] * 100
|
||||
strategy_equity["Avellaneda-Stoikov"].append({"t": time.time(), "v": cfg["allocation"] + cfg["pnl"]})
|
||||
strategy_equity["Avellaneda-Stoikov"].append(
|
||||
{"t": time.time(), "v": cfg["allocation"] + cfg["pnl"]}
|
||||
)
|
||||
return
|
||||
|
||||
ask_fill = fill_model.check_fill(
|
||||
aggressor_side="buy", agg_size=depth * 0.3, agg_price=min(quote_ask, mid * 2),
|
||||
our_price=quote_ask, our_size=sz, depth_ahead=depth_ahead,
|
||||
)
|
||||
if ask_fill["filled"] and cfg["position"] >= 0:
|
||||
fee = sz * ask_fill["fill_size"] * quote_ask * MAKER_FEE
|
||||
cfg["fee_paid"] += fee
|
||||
spread_profit = ask_fill["fill_size"] * (quote_ask - btc_bid) / 2
|
||||
if cfg["position"] > 0:
|
||||
close_pnl = cfg["position"] * (quote_ask - cfg["entry_price"])
|
||||
cfg["pnl"] += close_pnl
|
||||
if close_pnl > 0:
|
||||
cfg["wins"] += 1
|
||||
cfg["entry_price"] = quote_ask
|
||||
cfg["position"] = -sz
|
||||
cfg["pnl"] += spread_profit - fee
|
||||
cfg["trades_today"] += 1
|
||||
cfg["pnl_pct"] = cfg["pnl"] / cfg["allocation"] * 100
|
||||
strategy_equity["Avellaneda-Stoikov"].append(
|
||||
{"t": time.time(), "v": cfg["allocation"] + cfg["pnl"]}
|
||||
)
|
||||
|
||||
|
||||
# ═══════════════════════ Metrics ═══════════════════════
|
||||
@@ -522,6 +554,13 @@ def write_metrics():
|
||||
"strategy_equity": {k: list(v)[-300:] for k, v in strategy_equity.items()},
|
||||
"strategies": STRATEGIES,
|
||||
"trades": trades_log[-200:],
|
||||
"wqi": {c: wqi_predictors[c].summary() for c in wqi_predictors},
|
||||
"funding_arb": funding_arb.summary(),
|
||||
"fill_model": {
|
||||
"fill_rate": round(fill_model.fill_rate(), 4),
|
||||
"fills": fill_model.fill_count,
|
||||
"skips": fill_model.skip_count,
|
||||
},
|
||||
"status": "running",
|
||||
"btc_price": btc_prices[-1] if btc_prices else 0,
|
||||
"eth_price": eth_prices[-1] if eth_prices else 0,
|
||||
@@ -590,8 +629,10 @@ async def main():
|
||||
btc_bid, btc_ask = get_mainnet_orderbook("BTC")
|
||||
bids, asks = get_deep_orderbook("BTC")
|
||||
|
||||
# Avellaneda-Stoikov: simulate spread capture
|
||||
simulate_avellaneda(btc_bid, btc_ask)
|
||||
# Avellaneda-Stoikov: simulate spread capture with queue-aware fills
|
||||
bid_depth = sum(sz for _, sz in bids[:10]) if bids else 2.0
|
||||
ask_depth = sum(sz for _, sz in asks[:10]) if asks else 2.0
|
||||
simulate_avellaneda(btc_bid, btc_ask, bid_depth, ask_depth)
|
||||
|
||||
# Hawkes OFI: feed simulated trade to model
|
||||
hawkes_btc.update("B" if tick % 2 == 0 else "S", 0.001, btc)
|
||||
@@ -660,7 +701,6 @@ async def main():
|
||||
g_quotes = gueant.optimal_quotes(
|
||||
btc, gueant_inv, tick % 3600,
|
||||
adverse_prob=queue_imb.wqi_history[-1] if queue_imb.wqi_history else 0)
|
||||
# Simulate fill: if our quote is at/near best, track a signal
|
||||
if btc_bid > 0 and g_quotes["bid"] >= btc_bid * 0.999:
|
||||
STRATEGIES["Guéant Market Making"]["signals"].append({
|
||||
"time": time.time(), "signal": "BUY",
|
||||
@@ -672,7 +712,39 @@ async def main():
|
||||
"strength": 0.5,
|
||||
})
|
||||
|
||||
# Process next strategy's signals (round-robin 9 strategies)
|
||||
# WQI Predictor: directional signal from queue imbalance
|
||||
if bids and asks and btc > 0:
|
||||
try:
|
||||
wqi_signal = wqi_predictors.get("BTC")
|
||||
if wqi_signal:
|
||||
sig = wqi_signal.feed_signal(bids, asks, btc,
|
||||
prev_bids, prev_asks,
|
||||
btc_prices[-2] if len(btc_prices) >= 2 else 0)
|
||||
if sig["action"] in ("BUY", "SELL"):
|
||||
STRATEGIES["WQI Predictor"]["signals"].append({
|
||||
"time": time.time(),
|
||||
"signal": sig["action"],
|
||||
"strength": abs(sig["z_score"]) / 3.0,
|
||||
"reason": f"z={sig['z_score']:.2f}_wqi={sig['wqi']:.3f}",
|
||||
})
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
# Funding Rate Arb: check funding signal
|
||||
if funding_rates and isinstance(funding_rates[-1], dict):
|
||||
fr = funding_rates[-1].get("BTC", 0)
|
||||
if fr != 0:
|
||||
annual_apr = abs(fr) * 1095
|
||||
arb_signal = funding_arb.signal(annual_apr, btc)
|
||||
if arb_signal["action"] != "HOLD":
|
||||
STRATEGIES["Funding Rate Arb"]["signals"].append({
|
||||
"time": time.time(),
|
||||
"signal": arb_signal["action"],
|
||||
"strength": min(1.0, annual_apr),
|
||||
"reason": arb_signal.get("reason", ""),
|
||||
})
|
||||
|
||||
# Process next strategy's signals (round-robin all strategies)
|
||||
total_strats = len(strategy_names)
|
||||
name = strategy_names[idx % total_strats]
|
||||
idx += 1
|
||||
|
||||
Reference in New Issue
Block a user