How to deploy a trading bot that survives 24/7
A trading bot that works on your laptop is not a deployed bot. Markets run around the clock; your laptop sleeps, your Wi-Fi drops, and a crash at 3am can leave a position open with no stop. Deploying means putting the bot somewhere reliable, keeping it alive through crashes, protecting its secrets, and making sure that if it does die, it dies safely. This guide covers the VPS, Docker, process supervision and the dead-man's switch.
Step 1 — pick a VPS near your exchange
Run the bot on a small cloud VPS, not your laptop. Latency matters less for a daily-bar bot than for a scalper, but proximity to your exchange's servers still trims slippage. A 1–2 vCPU instance is plenty for most strategies.
Many crypto exchanges host in AWS Tokyo or similar regions. Put the bot in the same region to cut round-trip time. For a slow strategy any reliable VPS is fine — uptime beats microseconds.
Step 2 — containerize with Docker
Docker makes the bot reproducible: the same image runs identically on your machine and the VPS, with pinned dependencies.
dockerfile · DockerfileFROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "bot.py"]
Step 3 — keep it alive through crashes
A bot will crash eventually — a network blip, an API hiccup, an unhandled edge case. The goal is automatic, safe restart. With Docker, a restart policy does it; on bare metal, use systemd.
ini · trading-bot.service# /etc/systemd/system/trading-bot.service
[Service]
ExecStart=/usr/bin/docker start -a trading-bot
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
After a restart the bot must re-read its real position from the exchange, not assume it's flat. A bot that forgets an open position on restart can double up or leave a trade unmanaged. Always fetch open orders and positions on boot.
Step 4 — secrets and logging
Never bake API keys into the image. Inject them as environment variables at runtime and log every decision and order so you can audit what the bot did when something looks wrong.
bash · run.shdocker run -d --name trading-bot --restart=always \
-e EXCHANGE_KEY="$EXCHANGE_KEY" \
-e EXCHANGE_SECRET="$EXCHANGE_SECRET" \
-v /var/log/bot:/app/logs trading-bot
Step 5 — the dead-man's switch
The most important deployment feature is graceful failure. If the bot loses connectivity, hits a max drawdown, or stops sending heartbeats, it should flatten positions and halt rather than trade blind. Wire a heartbeat to a monitor (or a Telegram alert) that pages you when the bot goes silent.
Deployment checklist
- Strategy proven on the backtester and in paper trading first.
- Dockerized with pinned dependencies.
- Auto-restart that reconciles position on boot.
- Secrets via env vars; see key security.
- Full order logging + heartbeat alert.
- Dead-man's switch: flatten and halt on failure or max drawdown.
Frequently asked questions
Where should I host a trading bot?
Run it on a small cloud VPS rather than your laptop so it stays online 24/7. A 1–2 vCPU instance handles most strategies. Placing it in the same cloud region as your exchange's servers trims latency, though for slow daily-bar bots uptime matters far more than microseconds.
Do I need Docker to deploy a trading bot?
No, but it helps. Docker pins dependencies and makes the bot run identically on your machine and the server, and its restart policy keeps the bot alive through crashes. The alternative on bare metal is a systemd service with Restart=always.
How do I keep a trading bot running 24/7?
Use a process supervisor — Docker's restart policy or systemd — to relaunch the bot automatically after a crash. Critically, the bot must reconcile its real position from the exchange on startup so a restart doesn't double a trade or abandon an open one.
What is a dead-man's switch for a trading bot?
It's a safety mechanism that flattens positions and halts trading if the bot loses connectivity, stops sending heartbeats, or breaches a max-drawdown limit. It ensures that when the bot fails, it fails safely instead of trading blind or leaving positions unmanaged.