An end-to-end Binance USDS futures trading stack built around FastAPI, Redis Streams, Postgres, and a worker that places atomic entry/stop/take-profit orders from calculated signals.
- Install deps (Python 3.11+):
pipenv install --dev - Configure env: copy
.env.exampleto.envand setDATABASE_URL/DB_URL,REDIS_URL,SECRET_KEY,CREDENTIALS_MASTER_KEY, and Binance keys (BINANCE_API_KEY/BINANCE_API_SECRETor user-specific creds). - Migrate DB:
alembic -c alembic.ini upgrade head - Run services locally (separate shells):
- API:
uvicorn app.main:app --reload --port 8000 - Ingestor:
python -m services.ingestor.main - Calc2:
python -m services.calc2.main - Worker:
python -m services.worker.main
- API:
- Docker Compose (all services):
docker compose up --build
- FastAPI App (
app/main.py): Auth (/auth), encrypted credential management (/credentials), and health checks. JWT signing usesSECRET_KEY; API secrets are encrypted withCREDENTIALS_MASTER_KEY. - Ingestor (
services/ingestor): Subscribes to Binance 1m klines, optionally backfills, publishes 1m/2m candles to Redis Streams with caps/retention. - Calc2 (
services/calc2): Consumes candle streams, computes indicators/regime, and emits ARM/DISARM trading signals. - Worker (
services/worker): Reads signals, validates balances, sets leverage, and places entry/stop/take-profit orders on Binance (or dry-run adapter). Persists order state to Postgres and monitors fills/cancels.
DATABASE_URL/DB_URL: Postgres DSN (postgresql+asyncpg://...).REDIS_URL: Redis connection for streams/caching.SECRET_KEY: JWT signing key (required).CREDENTIALS_MASTER_KEY: Master key for encrypting/decrypting stored API credentials (required).BINANCE_API_KEY/BINANCE_API_SECRET: Default keys for scripts; per-user keys stored via/credentials.BINANCE_TESTNET:trueto target Binance testnet in scripts.- Worker-specific:
WORKER_SYMBOLS,TIMEFRAME,DRY_RUN_MODE,POSTGRES_DSN(typically same asDATABASE_URL). - Ingestor:
PAIRS_1M,BACKFILL_ON_START,STREAM_MAXLEN_*,STREAM_RETENTION_MS_*.
- API:
uvicorn app.main:app --reload --port 8000 - Ingestor:
python -m services.ingestor.main - Calc2:
python -m services.calc2.main - Worker:
python -m services.worker.main(setCREDENTIALS_MASTER_KEYand DB/Redis DSNs) - Docker Compose (all services):
docker compose up --build
- Unit tests:
pytest -q(or target worker:pytest tests/unit/worker). - Static checks:
mypy appandruff check app. - Binance smoke order (testnet-ready):
BINANCE_TESTNET=true python app/scripts/smoke_order.py --symbol BTCUSDT --price 10000 --qty 0.001 --side BUY --type LIMIT --timeInForce GTC
app/api: FastAPI routers (auth, credentials, health).app/db/models: ORM models (users, bots, api_credentials, order_states) with Pydantic schemas inapp/db/schemas.services/ingestor: Binance WS ingestion → Redis Streams.services/calc2: Indicator/regime/signal pipeline consuming Redis streams.services/worker: Signal processing, order execution, monitoring, Binance adapters, Postgres/Redis gateways.
- Startup flow: API serves auth/credentials; ingestor -> Redis streams -> calc2 signals -> worker executes orders. (Add architecture diagram here if you have one; see
architecture.mdfor text outline.)
- Ensure
SECRET_KEYandCREDENTIALS_MASTER_KEYare set before starting the API or worker; missing keys will prevent credential storage/decryption and invalidate JWTs. - Use
DRY_RUN_MODE=truewhen testing the worker without hitting live Binance endpoints.