Files
arbpulse_github/openspec/changes/archive/2026-07-19-fix-kraken-phantom-book-levels/tasks.md
T
Mauricio Barragan 4ffce9a59e fix(exchanges): truncate Kraken book to subscribed depth + crossed-book guard (WAY-77)
Kraken WS v2 book channel does not send deletes for levels evicted from
its top-N window; without client-side truncation those levels lingered
forever as phantom quotes, eventually crossing the local book (bid >= ask)
and feeding the engine a fake permanent arbitrage (~$62.9M bogus P&L).

- BookSide.truncate() removes levels beyond the best depth prices from
  the internal map (not just the emitted array)
- KrakenConnector uses depth 10 consistently (subscription + LocalBook)
  and truncates both sides after every update
- ExchangeConnector.emit() drops internally crossed books, logs and
  forces a resync (book reset + reconnect for a fresh snapshot)
- Unit tests for truncation and the crossed-book guard
- OpenSpec: order-book-integrity spec; change archived (2026-07-19)

Refs: Linear WAY-77
Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-19 11:43:12 -06:00

1.3 KiB

Tasks — fix-kraken-phantom-book-levels

1. LocalBook: truncado real

  • Añadir BookSide.truncate() que elimine del Map los niveles fuera de los mejores depth precios del lado
  • Unit tests de truncate(): elimina el peor nivel al exceder depth, no toca nada si size <= depth, y el bid fantasma desaparece tras updates sucesivos

2. Conector Kraken

  • Hacer ExchangeConnector.depth sobreescribible por subclase e inicializar LocalBook con el depth del conector
  • Fijar depth 10 en KrakenConnector (igual a la suscripción) y truncar ambos lados tras aplicar cada update

3. Guard de libro cruzado

  • En ExchangeConnector.emit(): si bids[0].price >= asks[0].price, no emitir, log warn, reset del libro y reconexión para re-sync
  • Unit test del guard: libro cruzado no se emite a listeners y dispara re-sync

4. Documentación

  • Actualizar skill exchange-ws (truncado obligatorio en Kraken v2, guard de cruce en la base, depth por conector)
  • Revisar README/AGENTS por menciones al manejo del libro que queden desactualizadas

5. Verificación

  • npm run typecheck + npm test en verde
  • Arrancar en local con feeds reales y verificar via /api/state que Kraken emite libro no cruzado y quotes coherentes con el mercado (matar el proceso al terminar)