In a previous post I documented my local setup for accessing the Interactive Brokers API via their Gateway application. I’m now in a position where I need to deploy my code onto a VM. My local setup will no longer suffice.
To get the Gateway running on a remote (headless) VM I wrapped it up in a Docker image. The image uses IBC to handle the Gateway login process and relies on 2FA being disabled.
Pull Image
The first step is to pull the image.
docker pull datawookie/ib-gateway:1016.1h
The images are tagged with the corresponding version of the Gateway application.
Environment Variables
Create a .env
file to store credentials. The contents would look something like this.
TWS_USERID=test
TWS_PASSWORD=B4xvWc&BnxNvpF8
TRADING_MODE=live
VNC_SERVER_PASSWORD=secret
The variables TWS_USERID
and TWS_PASSWORD
are the username and password that you use to login to your Interactive Brokers account.
Create Container
Now run the image. For the moment I’m forced to use host networking to get around the fact that the Gateway application will by default only accept connections from localhost
. It’s possible to configure the Gateway otherwise, but I have not worked out how to do this from within the Docker image. Host networking is not ideal, but I’m being pragmatic for the moment. 📌 Note: It’s possible to use something like socat
to work around this problem, however I found that having socat
in the pipeline made the data stream unreliable and the API response was often corrupted.
docker run \
-d \
--rm \
--name ib-gateway \
--env-file .env \
--restart always \
--network host \
datawookie/ib-gateway:1016.1h
Port 4001 is used to access the API. Port 5900 can be used to access the container via VNC. This will allow you to see the running Gateway application and is useful for debugging. Below is an example of VNC connection to the container.
Restart Container
I find that from time to time I get somewhat random errors back from the API. Restarting the Gateway container often helps clear these up.
docker restart ib-gateway
Connecting
I’m using R. Below is some example code for connecting to the API.
First install the {IBrokers}
package.
devtools::install_github("joshuaulrich/IBrokers@v0.10-1")
Now load the package, connect to the Gateway and start interacting with the API.
library(IBrokers)
gateway <- ibgConnect(host = "localhost", port = 4001)
reqMktDataType(gateway, mktDataType = 3)
GOOG <- reqHistoricalData(
gateway,
twsEquity(symbol = "GOOG"),
barSize = "1 day",
duration = "1 M"
)
GOOG.Open GOOG.High GOOG.Low GOOG.Close GOOG.Volume GOOG.WAP GOOG.hasGaps GOOG.Count
2022-05-24 2129.74 2129.74 2044.16 2118.52 11795 2090.682 0 8522
2022-05-25 2102.84 2130.90 2084.22 2116.79 7492 2107.238 0 5496
2022-05-26 2117.15 2179.11 2109.76 2165.92 4710 2157.135 0 3460
2022-05-27 2195.77 2257.36 2191.00 2255.98 4725 2235.618 0 3687
2022-05-31 2261.20 2328.67 2251.45 2280.78 6959 2288.742 0 5189
2022-06-01 2298.63 2347.98 2271.01 2282.74 4858 2299.082 0 3410
2022-06-02 2280.33 2357.96 2266.16 2354.92 4277 2323.191 0 3054
2022-06-03 2319.81 2327.29 2273.36 2291.28 3987 2295.317 0 3066
2022-06-06 2331.39 2387.97 2330.56 2340.21 4032 2354.320 0 2841
2022-06-07 2312.95 2354.98 2302.51 2344.59 4756 2334.259 0 3544
2022-06-08 2337.64 2372.92 2333.93 2344.76 3850 2351.766 0 2720
2022-06-09 2329.98 2367.00 2297.34 2298.36 4066 2340.245 0 2802
2022-06-10 2256.49 2269.94 2217.22 2228.55 4505 2237.320 0 3353
2022-06-13 2150.26 2184.37 2131.76 2137.53 6281 2154.499 0 4412
2022-06-14 2141.09 2169.15 2127.04 2143.88 4339 2145.177 0 2983
2022-06-15 2177.99 2241.26 2162.37 2207.81 5772 2194.423 0 3873
2022-06-16 2162.99 2185.81 2115.85 2132.72 6070 2144.903 0 4505
2022-06-17 2132.91 2184.99 2112.57 2157.31 8562 2147.329 0 4480
2022-06-21 2193.16 2253.46 2185.87 2240.30 6674 2234.450 0 4744
2022-06-22 2223.26 2275.39 2214.48 2240.68 4193 2253.970 0 2750
Happy (automated) trading!