Custom build · Finance & Investment

Crypto Trading Bot: a Python Execution Engine and a Next.js Risk Desk

A crypto trading bot built in Python with a Next.js risk desk: exchange REST and WebSocket integration, book-walk fill modelling, exact-decimal accounting, risk gates and verified backups. Validated on paper, ready for live exchange keys.

Unreconciled money in the ledger
0
Crypto trading desk overview: account equity, realised and unrealised P&L, drawdown and six performance chartsThe same crypto trading desk at 390 pixels wide, full length, on a phone

The project

About this build

This is a crypto trading system we designed and built end to end: a Python engine that decides, places and books every trade, and a Next.js desk that shows the account, the risk and the health of the machine in one place. It talks to the exchange over signed REST calls and an authenticated WebSocket feed, keeps its own ledger in exact decimal arithmetic, and refuses to open risk it cannot account for.

Execution sits behind an adapter, so which account the engine trades is a configuration decision rather than a rewrite. It has been running against a paper account while we proved the fills, the accounting and the failure handling; connecting live exchange API keys points the same engine, ledger and risk gates at a real account. The captures on this page are from that paper-validation run.

What we are showing here is the engineering, not trading rules. Every system we build runs its owner's own strategy, so the part worth judging is everything around it: how an order is placed and modelled, how money is accounted for, how risk is capped, and what the machine does when the network or the power fails.

What the system has to do

Three requirements shaped the build:

  • Honest fills. A simulation that assumes you always get the mid price is not a result. Every modelled order has to walk the real order book and pay for the privilege.
  • A ledger that adds up. Every unit of settlement currency has to be traceable from the account balance down to a single fill, fee and funding payment.
  • Survives a bad day. PC restarts, network drops and gaps in the exchange feed must not silently corrupt the book.

And one rule we hold ourselves to on every trading build: prove the plumbing on a paper account before a live key ever touches it.

How we built it

The engine is Python 3.12 with a SQLite ledger in WAL mode. The desk is Next.js 16 and React 19 in strict TypeScript, reading the ledger through a read-only API. The decisions that mattered were mostly about refusing to take shortcuts:

  • Exact money, never floats. Every amount is a decimal, not a binary float. One entry is one database transaction, conversions and funding carry unique keys so a retry cannot double-book, and a reconciliation function checks the money identity on every read.
  • One writer, many readers. The runner holds a kernel-enforced lock file, so a second engine cannot start against the same book. The API opens the database with a read-only URI and cannot write even if a route is wrong.
  • Signed exchange integration. API-key signatures are computed on the server, the private account feed arrives over an authenticated WebSocket, and each connection has its own probe and guard so a stale or rejected session is noticed before an order depends on it.
  • A fill model with a cost. Orders charge 5 basis points as taker and 2 as maker, walk up to 20 displayed levels of the live book, then take a further basis point adverse and round to the tick away from the trader. Partial fills are capped by the size actually displayed, 250ms of latency is recorded, and a book older than 20 seconds — or crossed, or one-sided — is refused rather than traded through.
  • Funding and liquidation, not just price. Positions are isolated margin, funding is booked from the venue's settled rates, liquidation is checked at maintenance margin plus the exit fee, and exits are reduce-only so they can never flip a position.
  • Fail closed. If the runner's status rows or the backup check fail, new entries are refused. A bot that cannot prove it is healthy does not get to open risk.
  • Backups that are proved, not assumed. Each copy is verified by SHA-256, page count and per-table row counts, with 14 kept per kind plus separate copies taken before every migration and restore.
  • Replay after an outage. When PC and network outages damaged five open positions, the engine rebuilt them from closed exchange one-minute bars as it would have booked them at the time, and kept the original rows in an archive. The desk labels those trades on the overview so the history is never quietly rewritten.

Inside the desk

Overview — equity in USDT and INR, realised and unrealised P&L, drawdown from peak and six charts drawn on the server. The mode badge on every page tells you which account the engine is pointed at; these captures are the paper-validation run.

Crypto trading desk overview: account equity, realised and unrealised P&L, drawdown and six performance charts

The trade ledger — gross, fees, modelled slippage, funding and net for every closed trade, with search, filters and sorting. The ledger check runs on every read and prints its answer on the page.

Closed trade ledger with gross, fees, modelled slippage, funding and net P&L, and a ledger check that reconciles

One trade, opened up — both fills, the book-walk price each one was modelled against, the funding settlements while it was open, and the order record behind it. Nothing is summarised away.

Single trade detail showing both fills, the book-walk price behind each, funding settlements and the order record

Market and risk — daily, weekly and drawdown caps that gate new entries, net directional exposure against equity, and an emergency switch that refuses new entries without ever market-selling what is already held.

Risk limits page showing daily, weekly and drawdown caps, net exposure and an emergency switch that halts new entries

System — nine agents with a five-second heartbeat, the database's own row counts, the backup agent's proof of its last copy, and the news filter that blocks entries around high-impact releases.

System health page listing nine supervised agents, database row counts, verified backups and the news event filter

The same desk at 390px, full length. The phone view is a real capture of the responsive layout, not a cropped desktop screenshot.

The same crypto trading desk at 390 pixels wide, full length, on a phone

What keeps it honest

  • Nine supervised agents. News, signals, stops, funding, valuation, backups, review, API and dashboard each report a heartbeat every five seconds. Silence for 60 seconds stops an agent; a stop check that keeps failing for 120 seconds marks the system offline rather than pretending it is fine.
  • Risk gates ahead of every entry. Daily and weekly loss caps, a drawdown ceiling, a fixed fraction of equity risked per trade and a hard cap on concurrent positions. The limits gate new entries and never close a position.
  • An emergency switch with an audit trail. Halting refuses new entries only. Open positions keep their own stops and continue to be managed, funded and valued, so a halt is never a panic market sell.
  • A news and event filter. Headlines and an economic calendar block affected coins and refuse entries in the window around high-impact releases.
  • A dashboard that cannot trade. The API token stays on the server, the browser gets an httpOnly SameSite=Strict session cookie, five wrong passwords lock the login for 15 minutes, and framing is denied outright.
  • 1,005 automated tests. 913 engine tests and 92 dashboard tests, behind linting and strict type checking on both sides of the stack.
  • Charts without a chart library. The equity curve, drawdown and distribution charts are drawn as SVG on the server, so the browser downloads no charting package to render them.

From paper validation to a live account

The paper adapter models fills against the live order book so the accounting, the risk gates and the failure handling can be proved with no money at stake. The exchange gateway signs real orders and reads the real account feed. Moving between them is configuration: the ledger, the risk engine, the alerts and the desk are the same code either way, which is exactly why we validate on paper first.

We do not publish performance figures, and we would be sceptical of anyone who does. A few dozen closed trades says nothing about an edge, and a modelled fill is not proof that a real order would have filled. What we publish is the engineering.

The outcome

The engine has run unattended through real PC and network outages, replayed the trades those outages damaged from exchange minute data, kept the originals in an archive, and still reconciles exactly. Every number on the desk can be traced to a row, and every row to the event that created it.

We build to your rules, not ours. Bring your own strategy — discretionary or systematic — and we build the engine, the exchange integration, the risk gates and the desk around it, on the venue you trade. That is web application development, API development and integrations and dashboards and admin panels applied to trading.

Tell us what you want automated →

The result

0 Unreconciled money in the ledger

Every entry, exit, fee and funding settlement is booked in exact decimal arithmetic and checked against the account on every page load.

Start a project

Let's build yours Let's build yours

Tell us where you are — a new build, a migration or a project that has stopped moving. You get a fixed scope and a plan in writing, not a sales call.

  • Fixed quote in 48 hoursScope, milestones and price in writing before anything starts.
  • No obligationThe plan is yours to keep, even if you build it somewhere else.
  • Straight to the teamYou talk to the people who design and build it, not an account manager.