{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c723d607",
   "metadata": {},
   "source": [
    "# 03 — Libro de Ordenes en Tiempo Real (Binance)\n",
    "\n",
    "Dos enfoques para mantener un order book local:\n",
    "\n",
    "### Enfoque A: Partial Book Depth (simple)\n",
    "Stream `<symbol>@depth<levels>@100ms` con levels = 5, 10, 20.\n",
    "Envia snapshot completo del top N en cada update. Sin logica de sync.\n",
    "\n",
    "### Enfoque B: Diff Depth + REST Snapshot (completo)\n",
    "1. Abrir stream `<symbol>@depth@100ms` (diffs incrementales)\n",
    "2. Buffear eventos iniciales\n",
    "3. Pedir snapshot REST: `GET /api/v3/depth?symbol=X&limit=1000`\n",
    "4. Descartar eventos con `u <= lastUpdateId` del snapshot\n",
    "5. Primer evento procesado debe tener `U <= lastUpdateId+1` AND `u >= lastUpdateId+1`\n",
    "6. Aplicar: qty > 0 = update nivel, qty = 0 = eliminar nivel\n",
    "7. Validar continuidad: cada evento `U` == anterior `u + 1`, si no, re-sync\n",
    "\n",
    "### Campos del depth update\n",
    "```json\n",
    "{\n",
    "  \"U\": 157,  \"u\": 160,\n",
    "  \"b\": [[\"price\", \"qty\"], ...],  \n",
    "  \"a\": [[\"price\", \"qty\"], ...]   \n",
    "}\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "23b19294",
   "metadata": {},
   "outputs": [],
   "source": [
    "import asyncio\n",
    "import json\n",
    "import requests\n",
    "import websockets\n",
    "from decimal import Decimal\n",
    "from collections import deque\n",
    "import pandas as pd\n",
    "\n",
    "BASE = \"https://api.binance.com\"\n",
    "WS_BASE = \"wss://stream.binance.com:9443/ws\""
   ]
  },
  {
   "cell_type": "markdown",
   "id": "cff459c9",
   "metadata": {},
   "source": [
    "## Enfoque A: Partial Book Depth (simple, sin sync)\n",
    "\n",
    "Stream `<symbol>@depth<levels>@100ms` — recibe snapshot completo del top N cada 100ms.\n",
    "\n",
    "Ideal para monitoreo rapido sin necesidad de mantener estado."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bf70712b",
   "metadata": {},
   "outputs": [],
   "source": [
    "async def stream_top_book(symbol: str, levels: int = 10, snapshots: int = 50) -> list[dict]:\n",
    "    \"\"\"Captura N snapshots del top del order book.\"\"\"\n",
    "    url = f\"{WS_BASE}/{symbol.lower()}@depth{levels}@100ms\"\n",
    "    results = []\n",
    "\n",
    "    async with websockets.connect(url) as ws:\n",
    "        while len(results) < snapshots:\n",
    "            data = json.loads(await ws.recv())\n",
    "            best_bid = (float(data[\"bids\"][0][0]), float(data[\"bids\"][0][1]))\n",
    "            best_ask = (float(data[\"asks\"][0][0]), float(data[\"asks\"][0][1]))\n",
    "            spread = best_ask[0] - best_bid[0]\n",
    "            mid = (best_bid[0] + best_ask[0]) / 2\n",
    "            results.append({\n",
    "                \"time\": pd.Timestamp.now(tz=\"UTC\"),\n",
    "                \"best_bid\": best_bid[0], \"bid_qty\": best_bid[1],\n",
    "                \"best_ask\": best_ask[0], \"ask_qty\": best_ask[1],\n",
    "                \"spread\": spread, \"spread_bps\": (spread / mid) * 10000,\n",
    "                \"bids\": [(float(p), float(q)) for p, q in data[\"bids\"]],\n",
    "                \"asks\": [(float(p), float(q)) for p, q in data[\"asks\"]],\n",
    "            })\n",
    "\n",
    "    return results"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "99c8d974",
   "source": "### Ejemplo: Capturar 50 snapshots del top-10 del book de BTCUSDT",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "id": "855fc378",
   "source": "snapshots = await stream_top_book(\"BTCUSDT\", levels=10, snapshots=50)\ndf_book = pd.DataFrame([{k: v for k, v in s.items() if k not in (\"bids\", \"asks\")} for s in snapshots])\nprint(f\"Capturados {len(df_book)} snapshots del order book\")\nprint(f\"Spread medio: {df_book['spread'].mean():.2f} USD ({df_book['spread_bps'].mean():.2f} bps)\")\nprint(f\"Spread min: {df_book['spread'].min():.2f}, max: {df_book['spread'].max():.2f}\")\ndf_book.head(10)",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "271ea442",
   "source": "## Enfoque B: Order Book completo con diff depth + REST sync\n\nMantiene un order book local completo sincronizado via WebSocket diffs.\nMas complejo pero da acceso a todos los niveles de precio.",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "id": "79701eff",
   "source": "class LocalOrderBook:\n    \"\"\"Order book local sincronizado con Binance via WebSocket diffs.\"\"\"\n\n    def __init__(self, symbol: str, depth_limit: int = 1000):\n        self.symbol = symbol.upper()\n        self.depth_limit = depth_limit\n        self.bids: dict[Decimal, Decimal] = {}  # price -> qty\n        self.asks: dict[Decimal, Decimal] = {}\n        self.last_update_id: int = 0\n        self._synced = False\n\n    def _get_snapshot(self) -> dict:\n        resp = requests.get(f\"{BASE}/api/v3/depth\",\n                          params={\"symbol\": self.symbol, \"limit\": self.depth_limit})\n        resp.raise_for_status()\n        return resp.json()\n\n    def _apply_update(self, sides: list[tuple[str, dict]]):\n        for price_str, qty_str in sides:\n            price, qty = Decimal(price_str), Decimal(qty_str)\n            return price, qty\n\n    def apply_event(self, event: dict):\n        for price_str, qty_str in event.get(\"b\", []):\n            p, q = Decimal(price_str), Decimal(qty_str)\n            if q == 0:\n                self.bids.pop(p, None)\n            else:\n                self.bids[p] = q\n        for price_str, qty_str in event.get(\"a\", []):\n            p, q = Decimal(price_str), Decimal(qty_str)\n            if q == 0:\n                self.asks.pop(p, None)\n            else:\n                self.asks[p] = q\n        self.last_update_id = event[\"u\"]\n\n    def best_bid(self) -> tuple[float, float] | None:\n        if not self.bids:\n            return None\n        p = max(self.bids)\n        return (float(p), float(self.bids[p]))\n\n    def best_ask(self) -> tuple[float, float] | None:\n        if not self.asks:\n            return None\n        p = min(self.asks)\n        return (float(p), float(self.asks[p]))\n\n    def spread(self) -> float | None:\n        b, a = self.best_bid(), self.best_ask()\n        return a[0] - b[0] if b and a else None\n\n    def top_n(self, n: int = 5) -> dict:\n        return {\n            \"bids\": [(float(p), float(q)) for p, q in sorted(self.bids.items(), reverse=True)[:n]],\n            \"asks\": [(float(p), float(q)) for p, q in sorted(self.asks.items())[:n]],\n        }\n\n    async def run(self, duration_seconds: int = 30):\n        \"\"\"Sincroniza y mantiene el book durante N segundos.\"\"\"\n        import time as _time\n        url = f\"{WS_BASE}/{self.symbol.lower()}@depth@100ms\"\n        buffer = []\n        start = _time.time()\n\n        async with websockets.connect(url) as ws:\n            while _time.time() - start < duration_seconds:\n                msg = json.loads(await asyncio.wait_for(ws.recv(), timeout=5))\n\n                if not self._synced:\n                    buffer.append(msg)\n                    if len(buffer) >= 3:\n                        snap = self._get_snapshot()\n                        # Inicializar desde snapshot\n                        self.bids = {Decimal(p): Decimal(q) for p, q in snap[\"bids\"]}\n                        self.asks = {Decimal(p): Decimal(q) for p, q in snap[\"asks\"]}\n                        self.last_update_id = snap[\"lastUpdateId\"]\n\n                        # Procesar buffer\n                        for evt in buffer:\n                            if evt[\"u\"] <= self.last_update_id:\n                                continue\n                            if not self._synced and evt[\"U\"] <= self.last_update_id + 1:\n                                self._synced = True\n                            if self._synced:\n                                self.apply_event(evt)\n                        buffer.clear()\n                        if self._synced:\n                            print(f\"Book sincronizado! Levels: {len(self.bids)} bids, {len(self.asks)} asks\")\n                else:\n                    if msg[\"U\"] != self.last_update_id + 1:\n                        print(\"Gap detectado, re-sync...\")\n                        self._synced = False\n                        buffer = [msg]\n                        continue\n                    self.apply_event(msg)",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "7370d164",
   "source": "### Ejemplo: Correr el order book 30 segundos y ver el estado",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "id": "ae407462",
   "source": "book = LocalOrderBook(\"BTCUSDT\")\nawait book.run(duration_seconds=30)\n\nprint(f\"\\nBest bid: {book.best_bid()}\")\nprint(f\"Best ask: {book.best_ask()}\")\nprint(f\"Spread: {book.spread():.2f} USD\")\nprint(f\"\\nTop 5:\")\ntop = book.top_n(5)\nprint(\"BIDS:\", top[\"bids\"])\nprint(\"ASKS:\", top[\"asks\"])",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  },
  {
   "cell_type": "markdown",
   "id": "c11ab0fb",
   "source": "## Snapshot REST directo (alternativa simple)\n\nPara consultas puntuales sin mantener estado, un GET directo al depth endpoint.",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "id": "9a31a0ba",
   "source": "def get_depth_snapshot(symbol: str, limit: int = 20) -> dict:\n    \"\"\"Snapshot directo del order book via REST.\"\"\"\n    resp = requests.get(f\"{BASE}/api/v3/depth\", params={\"symbol\": symbol, \"limit\": limit})\n    resp.raise_for_status()\n    data = resp.json()\n    return {\n        \"bids\": [(float(p), float(q)) for p, q in data[\"bids\"]],\n        \"asks\": [(float(p), float(q)) for p, q in data[\"asks\"]],\n        \"last_update_id\": data[\"lastUpdateId\"],\n    }\n\nsnap = get_depth_snapshot(\"BTCUSDT\", limit=10)\nprint(f\"Top 10 bids y asks (update_id={snap['last_update_id']}):\")\nprint(\"\\nBIDS (compra):\")\nfor p, q in snap[\"bids\"]:\n    print(f\"  {p:>12.2f}  |  {q:.6f}\")\nprint(\"\\nASKS (venta):\")\nfor p, q in snap[\"asks\"]:\n    print(f\"  {p:>12.2f}  |  {q:.6f}\")",
   "metadata": {},
   "execution_count": null,
   "outputs": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}