MOHRWURM(1)Martin Mohr · Software & Web

ARTICLE

cTrader bots on a VPS with Docker

One container per bot, and the pitfalls of running them around the clock

TEXT

cTrader bots on a Windows VPS usually live in the desktop app, kept open in a remote session. That works, but every bot is a window. After a reboot everything has to be clicked back into place by hand, and a stopped bot only gets noticed when someone looks.

Spotware’s cTrader CLI changes that. It runs a cBot without the desktop app, and Spotware ships it as a Docker image. A bot becomes a container on a small Linux server. This is what the setup looks like, and which pitfalls show up when bots run around the clock on demo and prop-challenge accounts.

First, a word of caution: this is a technical guide and not financial advice. Whether a bot makes money depends on its strategy, and there is nothing about that here.

What you need

  • A Linux server with Docker. A running bot takes about 350 MB of memory; measured, it was 340 to 375 MB per bot after six hours. The CPU is bored most of the time and only works briefly when a bar closes. 2 vCPU and 4 GB are enough for a handful of bots.
  • A synchronised clock. timedatectl should show System clock synchronized: yes. Daily loss limits and trading days depend on the time.
  • Your cTrader ID and the compiled bot as an .algo file, plus its parameters as a .cbotset if you exported them from the desktop app.

One bot, one container

Every bot gets its own container. If one crashes, the others keep running, and each has its own log and its own limits.

First a folder with the bot, its parameters and the password. The password goes in through read, so it stays out of the shell history:

mkdir -p /opt/cbots/us30 && cd /opt/cbots/us30
cp ~/MyBot.algo bot.algo
cp ~/MyBot.cbotset params.cbotset
install -m 600 /dev/null ctid.pwd
read -rs -p "cTrader password: " PW && printf '%s' "$PW" > ctid.pwd && unset PW

Before the bot starts, a call with the same image shows whether login and account are right:

docker run --rm -v /opt/cbots/us30:/mnt/bot:ro ghcr.io/spotware/ctrader-console:5.9.11 \
  accounts --ctid=you@example.com --pwd-file=/mnt/bot/ctid.pwd

With symbols --account=1234567 instead of accounts, the CLI lists the account’s symbols. Worth doing, because the names differ between brokers: the same Dow Jones index is US30.cash at FTMO and US30 at The Trading Pit.

Then the bot itself:

docker run -d --name us30 --hostname us30 \
  --restart on-failure \
  --memory 1g --memory-swap 1g \
  --log-opt max-size=10m --log-opt max-file=5 \
  --cap-drop ALL --security-opt no-new-privileges \
  -v /opt/cbots/us30:/mnt/bot:ro \
  ghcr.io/spotware/ctrader-console:5.9.11 \
  run /mnt/bot/bot.algo /mnt/bot/params.cbotset \
  --ctid=you@example.com --pwd-file=/mnt/bot/ctid.pwd \
  --account=1234567 --symbol=US30.cash --period=h1 \
  --exit-on-stop

What the options do:

  • A fixed image version, never latest. Try a new CLI version with one bot on a demo account first. Otherwise every bot picks up a new version at the next pull without being asked.
  • The password as a file (--pwd-file), not as an argument. An argument shows up in docker inspect and in the process list. If the container cannot read the file, it has to belong to the image’s user: docker image inspect shows it under Config.User.
  • --exit-on-stop ends the container when the bot stops, for example because its own daily loss rule kicked in. Without it the container keeps running empty, and from the outside everything looks healthy.
  • --memory 1g with the same --memory-swap: a bot with a memory leak cannot take the whole server down. That is almost three times what a bot usually needs. If it gets too tight, the kernel ends only that bot.
  • Log rotation, because otherwise the logs fill the disk over weeks.
  • --cap-drop ALL and no-new-privileges: a bot you did not write gets no extra rights inside the container.
  • --full-access is left out on purpose. The CLI only asks for it when a bot sets AccessRights.FullAccess in its code, and then turns off its sandbox. metadata /mnt/bot/bot.algo with the same image shows beforehand which rights a bot wants.

Pitfalls in operation

After a server reboot the bots do not come back. Docker ends the containers with SIGTERM, the CLI shuts the bot down cleanly, exit code 0. For on-failure that is no failure, so Docker restarts nothing. unless-stopped would bring them back, but also a bot that ended itself on purpose because of its own loss rule. So something has to remember which bots are meant to run. With plain Docker that is a small script or one systemd unit per bot.

Every new container is a new session. Your cTrader ID lists every container as another “cTrader Console”, and Docker gives every container a random hostname. If you recreate bots often, dozens of entries pile up there. A fixed --hostname per bot keeps that small. The CLI cannot log out; old entries have to be ended in the cTrader ID.

Every container needs a new password. After the password of the cTrader ID changes, a running bot carries on at first. At its next restart it fails on the old file with “Invalid ctid or password” and, with on-failure, tries again every few seconds. So the new file has to go into every container that uses this login.

Exit code 137 is not always a crash. Docker reports 137 when it kills a container that did not stop within the timeout. The kernel ending it for the memory limit gives the same code. Only docker inspect --format '{{.State.OOMKilled}}' us30 tells you which one it was.

A running container does not mean the bot is trading. docker ps only shows that the process is alive. Whether the bot is still connected to the broker, or whether a symbol was not found, is only in the log.

Keeping an eye on it

Up to here Docker is enough. With several bots on several accounts, though, the view across all of them is missing: which bot runs with which parameters on which account, what it logged last and how the account is doing. That is what wickwatch is for, a self-hosted dashboard, open source (AGPL-3.0). For transparency: wickwatch is my project.

wickwatch overview with accounts, alerts and bot instances
The wickwatch overview: accounts, prop challenges and bots on one page

wickwatch takes care of the pitfalls above. It creates the containers with a fixed hostname, log rotation and no extra rights, with a memory limit if you want one, and copies bot, parameters and password into them instead of mounting host folders. Bots that are meant to run, it starts again after a reboot. A changed password goes into every container of that login. It notifies you through Telegram, Slack, Discord or ntfy when a bot stops, crashes or loses its broker connection. For prop challenges it shows daily loss, maximum loss and trading days.

wickwatch reaches Docker only through a docker-socket-proxy that allows containers and images and nothing else. The dashboard can close positions, so it belongs behind HTTPS and a login with a second factor, better still behind a VPN. The guide with the compose file, updates and troubleshooting is in the repo under deploy/INSTALL.md, screenshots on wickwatch.dev.

Prop firms and licence

Before you run bots on a server, read your prop firm’s rules. Some forbid several accounts from the same IP address, others have rules on VPS and automation.

The image comes from Spotware, and by pulling it you accept its licence terms. Spotware’s CLI documentation explicitly names scripts and docker run as intended use.

wickwatch is not affiliated with Spotware and includes no cTrader software. cTrader is a trademark of its respective owner.

What is still open

wickwatch reads balance and equity about once a minute, less often for accounts with no bot and no open positions. The prop-challenge limits and the optional loss guard are a safety net, not a guarantee. Only cTrader is supported today. The core is broker-neutral and the adapter interfaces are documented, but nobody has written a second adapter yet.

If you run cBots on a server in another way, I would like to hear how.