Connecting the dots: Interactive Brokers and SmartBots.

“You can’t connect the dots looking forward; you can only connect them looking backwards. So you have to trust that the dots will somehow connect in your future. You have to trust in something your gut, destiny, life, karma, whatever. This approach has never let me down, and it has made all the difference in my life.” Steve Jobs
The world of trading frequently displays a paradox: despite the rapid advancement of technology, a gap still prevents the smooth operation of trading procedures. The lack of unified systems and platforms forces traders to struggle with lengthy lag times between the conception of an idea and the execution of a trade. This article explores the union of Interactive Brokers and SmartBots in an effort to address this urgent challenge. Understanding how this integration functions will allow traders and investing enthusiasts to take full advantage of these developments, promoting quick decision-making and execution.
You’ll have a real-time trading platform that is completely functional by the end of this tutorial. Let’s go:
1) Install and run SmartBots
SmartBots is a state-of-the-art, multi-asset trading platform developed in Python. It empowers users to seamlessly transition from backtesting trading strategies to real-time implementation. Offering an integrated suite of components like real-time data feeds, connections to any broker, portfolio strategy management, real-time alerts, and an event-driven architecture, SmartBots makes the complex world of trading simple and intuitive. Whether you’re interested in financial markets, cryptocurrencies, or sports betting, SmartBots is incredibly adaptable to any trading arena.
Please refer to the installation guide located here for simple installation and getting started.
2) Install and run IB’s Trader Workstation
To start using the Interactive Brokers API, you first need to download and install Trader Workstation (TWS) and set it up. Below is a detailed guide on how to do these steps:
- Download Trader Workstation (TWS): Go to the Interactive Brokers downloads page. Click the “Download” button under “Trader Workstation” to download the latest version of TWS.
- Install Trader Workstation (TWS): Open the installer after the download is finished, then follow the on-screen directions to install TWS on your computer.
- Configure Trader Workstation (TWS) for the API: TWS must be configured to accept API connections after installation. Open TWS and carry out these actions to achieve this
- Open an testing account here.
- Go to “Edit” > “Global Configuration” at the top of the TWS window.
- In the “Global Configuration” window, click on “API” > “Settings” in the left-hand menu.

- Optional: If you want further security against sending orders to the IB, select “Read-Only API” in the optional field. You should be aware that doing this will prevent you from sending orders and make your API connection read-only.
- Optional: To allow API access solely from your machine, select “Allow connections from localhost only.” By blocking external machines from connecting to your API, this adds further protection.
- Write down the port number in the “Socket port” column. For real accounts or paper accounts, it is typically 7496 or 7497.
- To save the changes, click “Apply” and then “OK.”
3) Connecting the Dots: IB and SmartBots
The process can be divided into three steps: understanding the importance of integration, setting up parameters, and specifying the tickers for trading.
Integration:
Due to SmartBots’ strong and adaptable connectivity features, Interactive Brokers (IB) and SmartBots have a lot of possibilities for integration. One of these benefits is its built-in capability to smoothly link with IB, a top online trading platform. By opening up a wide range of trading opportunities, this integration enhances your trading approach.
Setting up Parameters in SmartBots:
Here is a visual guide to understanding these parameters:
To initiate this integration, you will need to configure certain parameters in the SmartBots’ configuration file. This file can be found at cd docker/compose.env
. The parameters include:
CLIENT_IB_PROVIDER
: Your unique client ID from IB to connect to the provider.HOST_IB
: The IP address associated with your IB account.PORT_IB
: The specific port for SmartBots to connect to your IB account.CLIENT_IB_BROKER
: A unique ID required to connect to the broker.ACCOUNT_IB
: The name of the specific IB account you will be using.BROKER_FINANCIAL
: This should be set to ib.

Specifying the Tickers:
SmartBots directory contains a file called ticker_info_ib.csv
. This file, which contains information about the tickers you intend to trade with, is critical to the interaction between IB and SmartBots. Given the operational characteristics of IB, it is critical that these tickers in the file are properly formatted. The file contains samples to help you along the way.
4) The trading circle: From data to trade.
After connecting the dots, now is the time to check its potential.
Check out a full example with full code here.
Downloading data

Set the parameters (interval, unit, symbols, start_date and end_date) according to your needs. Once the data retrieval process is complete, this data will be stored in a MongoDB collection. The name of this collection is dynamically generated and follows the format:
name_library = f'{provider}_historical_{interval}'
# ib_historical_1m

Before backtesting your approach, you can visualize the data you’ve downloaded to make sure everything has been handled properly. By doing this, you can steer clear of potential problems that might jeopardize the accuracy of your backtest.
Checking Ideas!
It is time to run the backtesting. We will use a straightforward moving average crossover approach for illustration.


Finally, sending trades
This serves as an illustration of how to send any order; in this case, it constructs the appropriate ‘order’ event and sends it to the broker_ib service via the RabbitMQ service.

5) Putting all together
Your journey doesn’t stop at simulating a trading strategy. Backtesting is only the tip of the iceberg.
If you follow the instructions in the first section of this article (Install and run SmartBots), you should have the following services running: portainer, mongodb, rabbitmq, event keeper, and jupyterlab.
But if your plan is to have bots working for you around-the-clock, now is the time to operate more services, connect them, and orchestrate them all.
Services to execute:
data_provider_ib: This service connects to Interactive Brokers and subscribes to a set of symbols defined by the FINANCIAL_SYMBOLS configuration option. It generates minute bars that are delivered to the bot_financial_trading service and stored in MongoDB. To run this service, you must:
cd SmartBots/docker
docker compose -f docker-compose_financial.yml --env-file ./compose.env up -d data_provider_ib

After successful execution, go to Portainer. Every minute, a new bar is generated in the “logs” tab of the service we just started. This visual confirmation assures that your operation is running smoothly and continually, allowing you to track the progress of the procedure.
bot_financial_trading: This service executes the strategy and receives the minute bars created by the previous service. In our case, we use a simple moving averages crossover strategy, but you can implement your own strategy according to your needs. To specify the strategy you want to implement, navigate to:
cd src/application/config_portfolios/config_financial
In this file, you should indicate the strategy you wish to implement. If it doesn’t exist, you should create it.

To run this service:
cd SmartBots/docker
docker compose -f docker-compose_financial.yml --env-file ./compose.env up -d bot_financial_trading
If the strategy’s filters are met, it will create an ‘order’ event, which will be sent to the broker_ib service.
broker_ib: This service awaits an investment order, acting as the primary conduit to Interactive Brokers (IB). It dispatches the trading order, verifies correct execution, and maintains an up-to-date record of account balances and positions. In doing so, it ensures a smooth, well-rounded trading experience with IB.
To run this service:
cd SmartBots/docker
docker compose -f docker-compose_financial.yml --env-file ./compose.env up -d broker_ib
After completing this instruction, you will be able to utilize Python in conjunction with SmartBots to send any form of order to IB. This potent combination brings us a whole new universe of automatic trading opportunities.
Explore, master, and, most importantly, apply your gained knowledge. Use Python and SmartBots to unlock the potential of automated trading and achieve your trading objectives. The financial world is your oyster; now is the time to find your pearl!
Always keep in mind that the road of learning and implementing never ends. If you have any questions about how to use SmartBots or if you’ve established a trading strategy that you’d want to automate, please contact us via email (andres@smartlever.tech). We’d be delighted to assist you in navigating the fascinating world of automated trading.