Moving Algos from Windows to Linux
I always try to write code that is portable like using
boost instead of calling native APIs or in the case of algo trading using C# which runs on most operating systems with Mono. Lately, I’ve been having so many issues with Windows or hardware that I’ve decided to just move it all to Linux so I don’t have to worry about it not starting when the market is open.
My main issues with Windows:
- Random Windows updates or doing updates then it decides to reboot in the middle of the night.
- Hardware or driver issues causing system instablity that take forever to diagnose.
I use Linux and docker for almost everything now so it only makes sense to move it to containers. I created containers for TWS and IB Gateway a few months ago but never got around to actually setting it up with the algos.
This is what my old setup was like: (for futures, otherwise just start at 9:25 AM on weekdays)
- Start TWS at 12:50 AM on weekdays using Windows scheduler.
- Start TWS at 5:55 PM on Sunday.
- Start the algo application 5 minutes after TWS opens.
I replicated the same process under Linux using cron and containers. I just setup the cronjobs so do not rely on them working as they have not been tested, yet.
I do not use a service here because I want to control it with cron. If you decide to do this make sure the docker IP has been whitelisted inside TWS (
172.17.0.1 in my case).
You’d be surprised how responsive it is with VNC. Oh, and do not run this over the internet without some additional security like an SSH tunnel or a VPN.
50 0 * * 1-5 docker stop tws; docker run -d --rm --name tws -e ARGS="username=USER password=PASS" -p 5920:5900 -p 7496:7496 -p 7497:7497 tws 55 17 * * 0 docker stop tws; docker run -d --rm --name tws -e ARGS="username=USER password=PASS" -p 5920:5900 -p 7496:7496 -p 7497:7497 tws 0 17 * * 5 docker stop tws
I use a simple systemd service that does not auto restart on exit.
55 0 * * 1-5 systemctl restart AlgoTrade.service 0 18 * * 0 systemctl restart AlgoTrade.service 0 17 * * 5 systemctl stop AlgoTrade.service
I decided to move TWS from cron to systemd services so it’s easier to restart things if needed. This has been tested more thoroughly.
[Unit] Description=TWS Requires=docker.service After=docker.service [Service] ExecStart=/usr/bin/docker run --rm --name tws -e ARGS="username=USER password=PASS" -p 5920:5900 -p 7496:7496 -p 7497:7497 tws ExecStop=/usr/bin/docker stop tws [Install] WantedBy=multi-user.target
50 0 * * 1-5 systemctl restart TWS.service 55 17 * * 0 systemctl restart TWS.service 0 17 * * 5 systemctl stop TWS.service 52 0 * * 1-5 systemctl restart AlgoTrade.service 0 18 * * 0 systemctl restart AlgoTrade.service 0 17 * * 5 systemctl stop AlgoTrade.service 0 0 * * 1-5 systemctl stop AlgoTrade.service
I use TWS instead of IB Gateway because I want to be able to more easily monitor trades, and trades rarely occur during the IB restart times so it’s been fine so far.
I have been using this method of starting TWS and my algo app for a few weeks and it has been working perfectly. I never have to worry about it not starting at the appropriate time due to Windows.