{"spec_id":"depth-order-book","library":"plotly","language":"python","code":"\"\"\" anyplot.ai\ndepth-order-book: Order Book Depth Chart\nLibrary: plotly 6.8.0 | Python 3.13.13\nQuality: 90/100 | Created: 2026-06-15\n\"\"\"\n\nimport os\n\nimport numpy as np\nimport plotly.graph_objects as go\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\nGRID = \"rgba(26,26,23,0.15)\" if THEME == \"light\" else \"rgba(240,239,232,0.15)\"\n\n# Imprint palette — semantic: green=bid (buy/gain), red=ask (sell/loss)\nBID_COLOR = \"#009E73\"  # Imprint position 1 — buy-side\nASK_COLOR = \"#AE3030\"  # Imprint matte-red semantic anchor — sell-side\n\n# Data: BTC/USD synthetic order book snapshot\nnp.random.seed(42)\nMID_PRICE = 60_000.0\nSPREAD = 12.0\nN_LEVELS = 50\nTICK = 8.0\n\nbest_bid = MID_PRICE - SPREAD / 2  # 59994.0\nbest_ask = MID_PRICE + SPREAD / 2  # 60006.0\n\nbid_prices = np.array([best_bid - i * TICK for i in range(N_LEVELS)])\nask_prices = np.array([best_ask + i * TICK for i in range(N_LEVELS)])\n\nbid_qtys = np.random.exponential(0.4, N_LEVELS) + 0.04 * np.arange(N_LEVELS)\nask_qtys = np.random.exponential(0.4, N_LEVELS) + 0.04 * np.arange(N_LEVELS)\n\n# Synthetic order walls: support ~$200 below mid, resistance ~$150 above\nbid_qtys[25:27] += 7.5\nask_qtys[19:21] += 9.0\n\nbid_cumulative = np.cumsum(bid_qtys)\nask_cumulative = np.cumsum(ask_qtys)\n\n# Step-chart arrays — bids: ascending price (worst→best), descending cumulative\nbid_x = bid_prices[::-1]\nbid_y = bid_cumulative[::-1]\n# asks: ascending price (best→worst), ascending cumulative\nask_x = ask_prices\nask_y = ask_cumulative\n\ny_max = max(bid_cumulative[-1], ask_cumulative[-1]) * 1.10\n\n# Wall coordinates for annotations\nsupport_x = bid_prices[25]\nsupport_y = bid_cumulative[25]\nresist_x = ask_prices[19]\nresist_y = ask_cumulative[19]\n\ntitle = \"BTC/USD Order Book · depth-order-book · python · plotly · anyplot.ai\"\n\n# Plot\nfig = go.Figure()\n\n# Bid area — green step fill\nfig.add_trace(\n    go.Scatter(\n        x=bid_x,\n        y=bid_y,\n        name=\"Bid (Buy)\",\n        mode=\"lines\",\n        line=dict(color=BID_COLOR, width=2.5, shape=\"hv\"),\n        fill=\"tozeroy\",\n        fillcolor=\"rgba(0,158,115,0.18)\",\n    )\n)\n\n# Ask area — red step fill\nfig.add_trace(\n    go.Scatter(\n        x=ask_x,\n        y=ask_y,\n        name=\"Ask (Sell)\",\n        mode=\"lines\",\n        line=dict(color=ASK_COLOR, width=2.5, shape=\"hv\"),\n        fill=\"tozeroy\",\n        fillcolor=\"rgba(174,48,48,0.18)\",\n    )\n)\n\n# Mid-price dashed vertical line\nfig.add_shape(\n    type=\"line\", x0=MID_PRICE, y0=0, x1=MID_PRICE, y1=y_max, line=dict(color=INK_SOFT, width=1.5, dash=\"dash\")\n)\n\n# Mid-price annotation box\nfig.add_annotation(\n    x=MID_PRICE,\n    y=y_max * 0.92,\n    text=f\"Mid: {MID_PRICE:,.0f}  |  Spread: {SPREAD:.0f}\",\n    showarrow=False,\n    font=dict(size=10, color=INK),\n    align=\"center\",\n    bgcolor=ELEVATED_BG,\n    bordercolor=INK_SOFT,\n    borderwidth=1,\n    borderpad=5,\n)\n\n# Support wall label\nfig.add_annotation(\n    x=support_x,\n    y=support_y,\n    text=\"Support Wall\",\n    showarrow=True,\n    arrowhead=2,\n    arrowcolor=BID_COLOR,\n    arrowwidth=1.5,\n    font=dict(size=9, color=BID_COLOR),\n    ax=-55,\n    ay=-35,\n)\n\n# Resistance wall label\nfig.add_annotation(\n    x=resist_x,\n    y=resist_y,\n    text=\"Resistance Wall\",\n    showarrow=True,\n    arrowhead=2,\n    arrowcolor=ASK_COLOR,\n    arrowwidth=1.5,\n    font=dict(size=9, color=ASK_COLOR),\n    ax=55,\n    ay=-35,\n)\n\n# Style\nfig.update_layout(\n    autosize=False,\n    paper_bgcolor=PAGE_BG,\n    plot_bgcolor=PAGE_BG,\n    title=dict(text=title, font=dict(size=16, color=INK), x=0.5, xanchor=\"center\"),\n    xaxis=dict(\n        title=dict(text=\"Price (USD)\", font=dict(size=12, color=INK)),\n        tickfont=dict(size=10, color=INK_SOFT),\n        tickprefix=\"$\",\n        tickformat=\",.0f\",\n        gridcolor=GRID,\n        linecolor=INK_SOFT,\n        zerolinecolor=GRID,\n        showgrid=True,\n        showline=True,\n    ),\n    yaxis=dict(\n        title=dict(text=\"Cumulative Volume (BTC)\", font=dict(size=12, color=INK)),\n        tickfont=dict(size=10, color=INK_SOFT),\n        gridcolor=GRID,\n        linecolor=INK_SOFT,\n        zerolinecolor=GRID,\n        showgrid=True,\n        showline=True,\n        range=[0, y_max],\n    ),\n    legend=dict(\n        bgcolor=ELEVATED_BG,\n        bordercolor=INK_SOFT,\n        borderwidth=1,\n        font=dict(size=10, color=INK_SOFT),\n        x=0.01,\n        y=0.99,\n        xanchor=\"left\",\n        yanchor=\"top\",\n    ),\n    margin=dict(l=80, r=40, t=80, b=60),\n    hovermode=\"x unified\",\n)\n\n# Save\nfig.write_image(f\"plot-{THEME}.png\", width=800, height=450, scale=4)\nfig.write_html(f\"plot-{THEME}.html\", include_plotlyjs=\"cdn\")\n"}