Research-only simulation backend for a crypto exchange proof-of-concept, focused on:
- API governance (auth, rate limiting, logging)
- Risk-based leverage control (tiered leverage + margin + exposure + liquidation simulation)
- KPI observability (Prometheus metrics + KPI API)
- FastAPI (Python)
- PostgreSQL
- Redis
- Prometheus
- Docker Compose
crypto-exchange-poc-backend/
├── app/
│ ├── main.py
│ ├── core/
│ │ ├── config.py
│ │ ├── security.py
│ ├── api/
│ │ ├── deps.py
│ │ ├── routes/
│ │ │ ├── order.py
│ │ │ ├── account.py
│ │ │ ├── market.py
│ │ │ ├── kpi.py
│ ├── services/
│ │ ├── order_service.py
│ │ ├── account_service.py
│ │ ├── market_service.py
│ ├── risk_engine/
│ │ ├── leverage.py
│ │ ├── margin.py
│ │ ├── exposure.py
│ │ ├── engine.py
│ ├── models/
│ │ ├── user.py
│ │ ├── account.py
│ │ ├── order.py
│ │ ├── trade.py
│ ├── schemas/
│ │ ├── order.py
│ │ ├── account.py
│ │ ├── market.py
│ │ ├── kpi.py
│ ├── middleware/
│ │ ├── auth.py
│ │ ├── rate_limit.py
│ │ ├── logging.py
│ ├── kpi/
│ │ ├── collector.py
│ │ ├── aggregator.py
│ ├── metrics/
│ │ ├── prometheus.py
├── docker/
│ ├── docker-compose.yml
│ ├── prometheus.yml
├── requirements.txt
├── README.md
From the docker directory:
docker-compose upBackend: http://localhost:8000
Prometheus: http://localhost:9090
This backend is wired to Binance Spot Testnet for market prices and order placement.
- Create Spot Testnet API credentials from Binance Testnet.
- In the
dockerdirectory, create a.envfile:
BINANCE_API_KEY=your_testnet_api_key
BINANCE_API_SECRET=your_testnet_api_secret- Start services:
docker compose upIf credentials are not set, market endpoints still try Testnet public price calls, but order placement/cancel to Testnet will return an error.
- Issue JWT via
POST /auth/tokenwith API key payload:beginner-keyintermediate-keyadvanced-key
- Use JWT on protected endpoints:
Authorization: Bearer <access_token>
- Redis-backed limit:
100 requests/minuteper API key - Returns
429when exceeded
Each request logs:
- endpoint
- latency
- status code
POST /auth/tokenissue JWT access tokenPOST /orderscreate and execute order (risk checks + Binance Testnet order placement)POST /orders/{order_id}/cancel?user_id=...cancel order (if not yet filled)GET /account/{user_id}account + margin + positionsGET /market/price/{symbol}current Binance Testnet price (fallback to local simulation)POST /market/tickrefresh market prices from Binance Testnet (fallback to local simulation)
beginner→ max5xintermediate→ max10xadvanced→ max20x
RequiredMargin = OrderSize * Price / Leverage
Order rejected if:
- insufficient balance for required margin
- global exposure threshold exceeded
The engine tracks:
- total exposure per user
- global exposure
Liquidation simulation:
- if margin ratio
< liquidation threshold, positions are marked liquidated
Exposed at GET /metrics:
api_request_latency_secondsapi_error_totalapi_requests_totalliquidation_events_totalauth_failures_totalrate_limit_hits_total
GET /kpi/systemGET /kpi/tradingGET /kpi/security
Example response shape:
{
"latency_ms": 85,
"error_rate": 0.02,
"uptime": 99.95
}- This is a simulation system for research purposes only.
- It is not a production exchange backend.