Getting the Interactive Brokers API working in 8 minutes

For everyone who's thinking "yeah sure, I can program... but what's actually the first step to program a bot that can talk to an exchange?"

Getting the Interactive Brokers API working in 8 minutes

This one's best in video form I think:

But if you prefer a half-baked blog:


These are the first steps you'd take to start exploring the Interactive Brokers API and everything it offers. We'll be looking at the sample Java application they provide, TestJavaClient, which actually has a UI that makes exploration relatively simple. Not easy - it's confusingly laid out and uses design principles from the 90s (that is, no design principles at all) - but simple!

1. Log in to Trader Workstation (TWS)

The Interactive Brokers API requires you to be running Trader Workstation on your machine (there's a more advanced way without doing that, but we'll ignore that for now). So, open Trader Workstation and log in! On the log-in screen, you can choose between Live Trading and Paper Trading (also known as Simulated Trading). Paper trading is awesome because it's a fake account with fake money, but it uses real market data - so there's no consequences to testing going awry.

2. Download the Interactive Brokers API

Download the Interactive Brokers API from https://interactivebrokers.github.io/. I'm using the latest stable version for MacOS, but whatever floats your boat. This is likely no different for windows or linux.

3. Set up the project in IntelliJ

... or your IDE of choice. Or if you're a command line kinda person, don't open anything at all. Who needs visual programs?

But in IntelliJ, we want to Create a New Project From Existing Sources. The source we're using is in the TWS API zip we just downloaded (Mine's twsapi_macunix.976.01). In the IBJTs folder (what the heck does that mean?), you'll find a "samples" folder and a "source" folder. Source is where the actual API code we want to call lives. But Samples is where the sample code interactive brokers provides lives, and this is the project we want to open up. So select samples -> Java, and import that using "import project from external model", select Eclipse, and click through til it opens the projects. You'll see there's a few subfolders, like apidemo, samples, dnhedge, rfq, testbed, but we'll be using Main.java in TestJavaClient. That is, IBJTs/samples/Java/TestJavaClient/Main.java.

The file tree for IBJTs/samples/Java

IBJts/samples/Java/

├── ApiDemo.jar

├── TestJavaClient

│ ├── AccountDlg.java

│ ├── AccountSummary.java

│ ├── AcctUpdatesDlg.java

│ ├── AlgoParamsDlg.java

│ ├── ComboLegDlg.java

│ ├── ConnectDlg.java

│ ├── DeltaNeutralContractDlg.java

│ ├── ExecFilterDlg.java

│ ├── ExtOrdDlg.java

│ ├── FAAllocationInfoDlg.java

│ ├── FinancialAdvisorDlg.java

│ ├── GroupsDlg.java

│ ├── HistoricalDataDlg.java

│ ├── HistoricalNewsDlg.java

│ ├── IBGridBagPanel.java

│ ├── IBTextPanel.java

│ ├── JDialogBox.java

│ ├── LogConfigDlg.java

│ ├── Main.java

│ ├── MarketRuleDlg.java

│ ├── MktDepthDlg.java

│ ├── NewsArticleDlg.java

│ ├── NewsBulletinDlg.java

│ ├── OrderDlg.java

│ ├── PnLDlg.java

│ ├── PnLSingleDlg.java

│ ├── PositionsDlg.java

│ ├── SampleFrame.java

│ ├── ScannerDlg.java

│ ├── SecDefOptParamsReqDlg.java

│ ├── SmartComboRoutingParamsDlg.java

│ └── SmartComponentsParamsReqDlg.java

├── apidemo

│ ├── AccountInfoPanel.java

│ ├── AccountPositionsMultiPanel.java

│ ├── AccountSummaryPanel.java

│ ├── AdjustedPanel.java

│ ├── AdvisorPanel.java

│ ├── ApiDemo.java

│ ├── Chart.java

│ ├── ComboPanel.java

│ ├── CompletedOrdersPanel.java

│ ├── ConditionDlg.java

│ ├── ConditionsModel.java

│ ├── ConditionsPanel.java

│ ├── ContractConditionPanel.java

│ ├── ContractDlg.java

│ ├── ContractInfoPanel.java

│ ├── ContractLookupButton.java

│ ├── ContractPanel.java

│ ├── ContractSearchDlg.java

│ ├── ExercisePanel.java

│ ├── FamilyCodesPanel.java

│ ├── Histogram.java

│ ├── HistoricalTickBidAskModel.java

│ ├── HistoricalTickLastModel.java

│ ├── HistoricalTickModel.java

│ ├── HistoricalTickResultsPanel.java

│ ├── MarginConditionPanel.java

│ ├── MarketDataPanel.java

│ ├── MarketValueSummaryPanel.java

│ ├── MktDepthExchangesPanel.java

│ ├── NewsPanel.java

│ ├── OnOKPanel.java

│ ├── OperatorConditionPanel.java

│ ├── OptParamsModel.java

│ ├── OptionChainsPanel.java

│ ├── OptionsPanel.java

│ ├── OrdersPanel.java

│ ├── PegBenchPanel.java

│ ├── PercentConditionPanel.java

│ ├── PnLModel.java

│ ├── PnLSingleModel.java

│ ├── PositionsPanel.java

│ ├── PriceConditionPanel.java

│ ├── StratPanel.java

│ ├── Test.java

│ ├── TickByTickModel.java

│ ├── TickByTickResultsPanel.java

│ ├── TicketDlg.java

│ ├── TimeConditionPanel.java

│ ├── TopModel.java

│ ├── TradeConditionPanel.java

│ ├── TradesPanel.java

│ ├── TradingPanel.java

│ ├── VolumeConditionPanel.java

│ └── util

│ ├── HtmlButton.java

│ ├── IConnectionConfiguration.java

│ ├── NewLookAndFeel.java

│ ├── NewTabbedPanel.java

│ ├── TCombo.java

│ ├── UpperField.java

│ ├── Util.java

│ └── VerticalPanel.java

├── javatest.iml

└── samples

├── dnhedge

│ ├── DNHedgeOrder.java

│ └── SampleDNHedge.java

├── rfq

│ ├── RfqOrder.java

│ ├── SampleRfq.java

│ └── SimpleWrapper.java

└── testbed

├── EWrapperImpl.java

├── Testbed.java

├── advisor

│ └── FAMethodSamples.java

├── contracts

│ └── ContractSamples.java

├── orders

│ ├── AvailableAlgoParams.java

│ └── OrderSamples.java

└── scanner

└── ScannerSubscriptionSamples.java

Then you'll want to add a library, so go through the Project Structure settings, add a Java library, and select the IBJTs/source/Java folder. Click apply, and we should be good to go.

4. Run the code

Run the main method in TestJavaClient/Main.java. This opens up a swing UI, broken into two sections - on the left there's log sections for market data, historical data, TWS (trader workstation) server responses, and info and error logs from the sample code itself. On the right are all the buttons to play with the API itself. If you're curious about what API functionality exists, look no further than this mess of buttons. Or read the docs, it's way better.

5. Connect the sample application to Trader Workstation

Hit the "Connect" button in the swing UI, and leave all the defaults as is. You'll only need to change the port field from 7496 to 7497 if you're using Paper Trading. Press ok, and you should see in the logs that you're successfully connected.

6. Use the API!

Try clicking around the sample application buttons.

Use "Req Mkt Data" to get a market data snapshot for a stock (the bid, ask, etc, for that particular point in time).

Use "historical data" to get historical candles for a period of history, for a candle size of your choosing.

Use "req tick-by-tick" to get tick by tick data, that is, ongoing live updates for a stock's transactions on the exchange.

And use "place order" to place an order. Use with caution if you're not in paper trading mode.

To see what the output looks like, it's in the video.


Happy coding and trading. If you're building something, I'd love to hear what you're aiming to build and what you're trying out!