{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Lead-Lag Effect Detection in HFT\n",
        "\n",
        "Explore whether one asset, venue, or signal appears to move before another at short horizons.\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",
        "Lead-lag effects are about timing. If asset A consistently moves slightly before asset B, a trader might use A as a short-horizon signal.\n",
        "\n",
        "This gets very subtle in real markets because of noise, latency, asynchronous data, and overfitting."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Starter Code\n",
        "\n",
        "1. Create or load two high-frequency price series.\n",
        "2. Align timestamps.\n",
        "3. Compute short-horizon returns.\n",
        "4. Check cross-correlations at different lags.\n",
        "5. Test whether the relationship survives out-of-sample."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "\n",
        "rng = np.random.default_rng(123)\n",
        "\n",
        "n = 5000\n",
        "base_shocks = rng.normal(0, 1, n)\n",
        "asset_a_returns = 0.001 * base_shocks + 0.0005 * rng.normal(size=n)\n",
        "\n",
        "# Toy lead-lag: asset B reacts to asset A with a delay.\n",
        "lag = 3\n",
        "asset_b_returns = np.roll(asset_a_returns, lag) * 0.4 + 0.0008 * rng.normal(size=n)\n",
        "\n",
        "prices = pd.DataFrame({\n",
        "    \"A\": 100 + np.cumsum(asset_a_returns),\n",
        "    \"B\": 50 + np.cumsum(asset_b_returns)\n",
        "})\n",
        "\n",
        "returns = prices.pct_change().dropna()\n",
        "returns.head()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "def lagged_correlation(x, y, max_lag=20):\n",
        "    # Correlation between x_t and y_{t+lag}.\n",
        "    # Positive lag here asks: does x lead y?\n",
        "    # TODO:\n",
        "    # - Add confidence intervals.\n",
        "    # - Use non-overlapping returns.\n",
        "    # - Try event-time instead of clock-time.\n",
        "    rows = []\n",
        "    for lag in range(-max_lag, max_lag + 1):\n",
        "        corr = x.corr(y.shift(-lag))\n",
        "        rows.append({\"lag\": lag, \"correlation\": corr})\n",
        "    return pd.DataFrame(rows)\n",
        "\n",
        "lag_scan = lagged_correlation(returns[\"A\"], returns[\"B\"], max_lag=15)\n",
        "lag_scan.sort_values(\"correlation\", ascending=False).head()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Open-ended extensions\n",
        "\n",
        "- Use trade data or order book data instead of synthetic prices\n",
        "- Try lead-lag across ETFs and constituents\n",
        "- Try crypto spot vs perpetual futures\n",
        "- Add a simple walk-forward validation\n",
        "- Explain how latency would affect whether the signal is actually tradable"
      ]
    },
    {
      "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
}
