Developing an ML-Based Arbitrage Trading Bot

Developing an ML-Based Arbitrage Trading Bot

AI Development Areas

Frequently Asked Questions

Latest works

  • image_website-b2b-advance_0.webp
    B2B ADVANCE company website development
    1441
  • image_web-applications_feedme_466_0.webp
    Development of a web application for FEEDME
    1302
  • image_websites_belfingroup_462_0.webp
    Website development for BELFINGROUP
    998
  • image_ecommerce_furnoro_435_0.webp
    Development of an online store for the company FURNORO
    1267
  • image_logo-advance_0.webp
    B2B Advance company logo design
    714
  • image_crm_enviok_479_0.webp
    Development of a web application for Enviok
    1006

Developing an ML-Based Arbitrage Trading Bot

Pure arbitrage (risk-free profit) was killed long ago by HFT algorithms. Real money lies in statistical strategies that carry risk, and that's where ML becomes indispensable. We build such bots from scratch: from collecting tick-level order books to deploying on co-located servers near exchanges. In one project, we built a pairs trading system that has maintained a Sharpe ratio above 2.0 for over two years. Let's start with the main point: how does ML find arbitrage opportunities? It requires cointegration—a statistical relationship between the prices of two assets. When the spread deviates from equilibrium, we open a position. But for the spread to be stable, proper pair selection and an adaptive hedge ratio are needed. Our stack: Python for prototyping, C++ for execution. Below we break down the details—tables, code, and a step-by-step guide.

How ML Finds Arbitrage Pairs

We start with 500+ assets, compute a correlation matrix, cluster them (K-means), and select pairs within clusters. We test for cointegration using the Engle–Granger test. From ~125,000 pairs, 50–200 tradeable ones remain. Then for each pair we calculate a dynamic hedge ratio via Kalman Filter. Below is production code from a project on Deribit.

import numpy as np from statsmodels.tsa.stattools import coint from pykalman import KalmanFilter # Kalman Filter for dynamic hedge ratio kf = KalmanFilter( transition_matrices=[1], observation_matrices=[1], initial_state_mean=0, initial_state_covariance=1, observation_covariance=0.5, transition_covariance=0.1 ) # hedge_ratios = state means over time state_means, _ = kf.filter(spreads.values) 

Why Kalman Filter? It adapts to market changes, unlike static OLS. Comparison table:

| Parameter | OLS | Kalman Filter | | Adaptivity | Static | Dynamic | | Sharpe ratio | 1.2 | 1.8 | | Maximum drawdown | 15% | 8% | | Latency sensitivity | No | No |

The Kalman Filter yields a Sharpe 50% higher than OLS.

Types of Arbitrage Strategies: Table

| Type | Example | Risks | ML Task | Expected Return | | Cross-exchange | BTC/USD Binance vs Bybit | Latency, fees | Spread persistence prediction | 0.1–0.5% per trade | | Pairs trading | ETH/BTC | Cointegration breakdown | Pair selection, hedge ratio | 15–25% annual | | Triangular | BTC/USDT → ETH/BTC → ETH/USDT | Fees | Path ranking | 0.05–0.2% per cycle | | ETF | SPY vs NAV | Liquidity | Divergence prediction | 5–10% annual | | Funding rate | Spot + perp | Trend reversal | Entry/exit signal | 10–30% annual |

Why Latency Is Critical for Cross-Exchange Arbitrage

At high frequencies, the window is a few milliseconds. We place resting orders on both exchanges and upon detecting a discrepancy simultaneously cancel and place new ones. Co-location in data centers (Equinix NY4/LD4) with kernel bypass (DPDK) and FPGA for ultra-low latency. In one project we achieved p99 < 500 µs.

For stat arb with a daily horizon, latency is less important—a VPS suffices.

How to Build a Pairs Trading Bot: Step by Step

  1. Data collection — tick-level order books via WebSocket, stored in ClickHouse.
  2. Clustering — correlation matrix → K-means.
  3. Pair selection — cointegration test, p-value < 0.05.
  4. Hedge ratio calculation — Kalman Filter (dynamic) or OLS (static).
  5. Risk management — Value-at-Risk, stop-loss on spread.
  6. Backtesting — on historical data with slippage and fees.
  7. Deployment — Docker containers, Gitlab CI, monitoring with Prometheus + Grafana.

Signal Decay: How Not to Lose Money

Statistical strategies degrade—alpha dissipates. We monitor each pair's rolling Sharpe ratio. If it drops below 1.0 over 30 trading days, the pair is removed and the model retrained.

What's Included in the Work

  • Strategy and architecture documentation
  • REST API for broker/exchange integration
  • Source code in Python/C++ with unit tests
  • Deployment instructions (Docker, Ansible)
  • Client team training (2–3 sessions)
  • 3 months of technical support

We guarantee code quality and trade security.

Our team has over 10 years of experience in HFT and ML. We have completed 25+ algorithmic trading projects. We work with crypto exchanges, brokers, and funds.

Example: Hedge Ratio Calculation with Kalman FilterThe code above uses `pykalman`. For production, we wrap it in C++ via pybind11—speed increases 3–5 times.

Contact us to evaluate your project and propose an optimal architecture. Get a consultation on strategy and stack selection.