Skip to main content

Binance Quant Engine ⚙️

CI PyPI Python License: MIT LinkedIn

A strategy-agnostic Binance USDT-M futures backtest & execution engine. Backtest and live trading run through the same strategy code, so a look-ahead bug can't exist in one and not the other — and an MCP server lets any MCP-compatible agent run backtests without writing a line of Python.

Extracted from the infra layer of a real, currently-running Binance USDT-M futures bot — the alpha (strategy, parameters, live P&L) stays private; what's public is the part every retail algo trader rebuilds badly at least once: a backtester that can't cheat, and an execution layer that survives Binance's actual API quirks. The bundled demo strategy is textbook logic (Bollinger squeeze); it exists to exercise the engine end to end, not to make you money.

This repository supersedes quantbox-engine, which is no longer maintained. If you have that repo cloned, switch to this one.

Why not just write your own backtest loop?

Because the failure modes here are the ones that quietly wreck a real account, not the ones a unit test catches:

Problem What usually happens What this engine does
Look-ahead bias A signal computed over a full array accidentally sees future bars The backtester physically hands the strategy only close[: t + 1] each step — there is no future in the array to peek at (enforced by test)
Backtest/live divergence Backtest logic gets "ported" to the live bot and drifts One TradingStrategy object, same method calls, same order, in both paths
Binance tick-size rejection A price a float-division away from the tick grid gets silently rejected or mispriced round_to_tick snaps every stop/limit/activation price to the symbol's grid before it leaves the process
Dead-man's switch Bot crashes → open position has no stop-loss SL/TP/trailing stops are placed on Binance's Algo Order API, so the exchange — not your process — enforces the exit

Quick start

pip install binance-quant-engine
# or: uv add binance-quant-engine

bqe-backtest --demo      # backtest the bundled demo strategy on synthetic data

Demo backtest run

⚠️ The numbers above are from a demo strategy on synthetic data. They prove the engine runs; they say nothing about profitability.

Backtest your own data

from binance_quant_engine.backtest.vectorized import run_backtest
from binance_quant_engine.data.klines import load_csv
from binance_quant_engine.strategy.demo_squeeze import SqueezeStrategy

high, low, close = load_csv("BTCUSDT_1h.csv")   # columns: high,low,close
result = run_backtest(SqueezeStrategy(), high, low, close, fee=0.0004, slippage=0.0002)
print(result.summary())
# {'n_trades': ..., 'total_return': ..., 'win_rate': ..., 'profit_factor': ..., 'max_drawdown': ...}

Bring your own strategy by implementing TradingStrategy — a typing.Protocol, no base class required. Wiring it into live execution: docs/USAGE.md.

MCP server — let an agent run your backtests

pip install "binance-quant-engine[mcp]"
binance-quant-engine-mcp   # stdio MCP server

Exposes run_demo_backtest, backtest_csv, and describe_strategy_protocol to any MCP client (Claude Code, Claude Desktop, etc.) — point an agent at a CSV of OHLC data and it can backtest a strategy idea in the same turn, no local Python environment required on the agent's side.

This is deliberately backtest-only. Nothing under execution/ (order placement, cancellation, bracket management) is reachable through the MCP server — there is no tool call that can touch a live Binance order. If you want an agent that also trades, that's a decision you wire yourself, explicitly, outside this server.

Design

Design choice How
No-look-ahead by construction The backtester hands the strategy close[:t+1] (bars completed as of now) every step — there's no future in the array to see.
Backtest = live, same code The same TradingStrategy object is driven by the backtester and the live bot, in the same call order. No reimplementation gap.
Pluggable strategy The engine only ever talks to a strategy through TradingStrategy (PEP 544). No inheritance required.
Costs are net, not gross Taker fee + slippage charged on both entry and exit legs.
Server-side exits SL/TP/trailing stops live on Binance's Algo Order API — the exchange enforces them even if your process dies. (brackets.py)
Tick-safe prices Every price sent to the exchange is snapped to the symbol's tick grid first (utils.py) — a category of Binance rejection this engine doesn't have.

Structure

Decide (strategy) / measure (backtest) / execute (live) are kept apart and meet only at TradingStrategy.

flowchart LR
    subgraph Data["binance_quant_engine/data/"]
        CSV[("OHLCV CSV\nload_csv()")]
        SYN[["synth_ohlcv()\nsynthetic data"]]
        CACHE["cache.py\nmemory + gzip cache"]
    end

    subgraph Decide["binance_quant_engine/strategy/ — decide"]
        PROTO{{"TradingStrategy\n(PEP 544 protocol)"}}
        SQZ["demo_squeeze.py\nSqueezeStrategy"]
        PROTO -.->|"implements"| SQZ
    end

    CSV --> BT
    SYN --> BT
    CACHE -.->|"caches"| CSV

    subgraph Measure["binance_quant_engine/backtest/ — measure"]
        BT["vectorized.run_backtest()\nhands close[:t+1] only\n(no look-ahead)"]
        RES["BacktestResult\ntrades · equity curve · MDD"]
        BT --> RES
    end

    BT <-->|"update_market_data / on_bar\nopen·update·close_position"| PROTO

    subgraph Execute["binance_quant_engine/execution/ — execute (.[live])"]
        HOST["host.py\nScalperProtocol (live bot host)"]
        BRACKET["brackets.py\nBracketMixin — SL/TP/trailing"]
        UTIL["utils.py\nround_to_tick · dead-status set"]
        ALGO["algo_api.py\nAlgoApiClient"]
        HOST --> BRACKET --> ALGO
        BRACKET -.-> UTIL
    end

    subgraph MCP["binance_quant_engine/mcp/ — optional (.[mcp])"]
        SRV["server.py\nrun_demo_backtest · backtest_csv"]
    end
    BT -.->|"callable via"| SRV

    PROTO ==>|"same interface<br/>(backtest = live)"| HOST
    ALGO --> BINANCE[("Binance USDT-M\nFutures Algo Order API")]
  • How to plug in a strategy and wire up live execution → docs/USAGE.md
  • Design rationale for no-look-ahead, backtest/live parity, and the cost model → docs/ARCHITECTURE.md

License

MIT


Bugs and questions → Issues.

Younghwan Chae · GitHub @younghwan91 · LinkedIn — other open-source quant projects (Korean equities, US equities, crypto) are on the profile page.

Release files for binance-quant-engine 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for binance-quant-engine 0.1.2
File Size Uploaded
binance_quant_engine-0.1.2.tar.gz 109.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for binance-quant-engine 0.1.2
File Interpreter ABI Platform
binance_quant_engine-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 143.9 kB

Release files / binance_quant_engine-0.1.2.tar.gz

Download URL binance_quant_engine-0.1.2.tar.gz
Size 109.2 kB
Tags Source
SHA-256 checksum
How to use checksums
726a5392ad646c46c66fa01235481594000edad9338e621cfcf54578903db025
BLAKE2b-256 checksum
How to use checksums
ac0e72cbbd13b3646cbeb90d01451c8a8b527acea3ce6072f30ac4781abcbca5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 13, 2026.

Transparency log

Release files / binance_quant_engine-0.1.2-py3-none-any.whl

Download URL binance_quant_engine-0.1.2-py3-none-any.whl
Size 34.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ee4faaf18d7df9e52d779c94d673daa679d90d73bb117b09ae0ebec3380f9809
BLAKE2b-256 checksum
How to use checksums
24af65b592db59f8d229445aad5990202b430e0bb34a336dc72f91f77dd62024
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Sep 13, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page