{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Order Book Imbalance Model\n",
        "\n",
        "Build a simple short-horizon prediction model using bid/ask depth imbalance.\n",
        "\n",
        "**How to use this notebook:** We give you the structure, the key ideas, starter code, and places where you need to make the important research decisions yourself."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## What you should have by the end\n",
        "\n",
        "- A working prototype\n",
        "- A few graphs/tables that explain what your model is doing\n",
        "- A short write-up of your assumptions\n",
        "- At least one extension that makes the project feel like yours"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Concept\n",
        "\n",
        "Order book imbalance asks whether there is more quantity waiting on the bid side or ask side.\n",
        "\n",
        "A common beginner hypothesis is: if bid depth is much larger than ask depth, the next price move may be more likely upward. The project is figuring out when this is true, when it is fake, and how quickly the signal decays."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Starter Code\n",
        "\n",
        "1. Create or load level-one/level-two order book snapshots.\n",
        "2. Compute imbalance features.\n",
        "3. Define a future mid-price movement label.\n",
        "4. Train a simple model.\n",
        "5. Check performance by horizon."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "from sklearn.linear_model import LogisticRegression\n",
        "from sklearn.model_selection import train_test_split\n",
        "from sklearn.metrics import classification_report\n",
        "\n",
        "rng = np.random.default_rng(88)\n",
        "\n",
        "n = 3000\n",
        "bid_size = rng.exponential(100, n)\n",
        "ask_size = rng.exponential(100, n)\n",
        "\n",
        "imbalance = (bid_size - ask_size) / (bid_size + ask_size)\n",
        "\n",
        "# Toy labels with weak relationship.\n",
        "probability_up = 1 / (1 + np.exp(-1.5 * imbalance))\n",
        "y = rng.binomial(1, probability_up)\n",
        "\n",
        "book = pd.DataFrame({\n",
        "    \"bid_size\": bid_size,\n",
        "    \"ask_size\": ask_size,\n",
        "    \"imbalance\": imbalance,\n",
        "    \"future_mid_up\": y\n",
        "})\n",
        "\n",
        "book.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "X = book[[\"imbalance\"]]\n",
        "y = book[\"future_mid_up\"]\n",
        "\n",
        "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, shuffle=False)\n",
        "\n",
        "model = LogisticRegression()\n",
        "model.fit(X_train, y_train)\n",
        "\n",
        "preds = model.predict(X_test)\n",
        "print(classification_report(y_test, preds))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Open-ended extensions\n",
        "\n",
        "- Add deeper book levels\n",
        "- Add spread and recent trade direction\n",
        "- Test 1-tick, 5-tick, and 20-tick horizons\n",
        "- Include transaction costs and queue priority\n",
        "- Explain why a statistically significant signal may still be untradable"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Final write-up prompts\n",
        "\n",
        "Answer these before putting the project on your resume:\n",
        "\n",
        "1. What problem were you trying to solve?\n",
        "2. What assumptions did your model make?\n",
        "3. Where did the model work well?\n",
        "4. Where did it fail?\n",
        "5. What would you improve with more time?\n",
        "6. What is one interesting thing you learned that was not obvious before starting?"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3.x"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
