Python Trading 2 – How to connect to Interactive Brokers TWS with PyCharm and the API


In the last part we have downloaded and installed PyCharm, an integrated development environment for Python.

And we have downloaded and installed the API from Interactive Brokers.

Now we need to download and configure TWS, also known as Trader Work Station. This is basically the client software that is used.

We need to configure it in a way that will enable us to use Python and the API to control it.


The software can be found on the Interactive Brokers website. At the time of this recording it is available when you click on “technology”.

In our case we will go with the latest version for Windows.

The download is only a few megabytes, afterwards we can start the installation process.

After a few seconds the login dialog appears.

Here I need to provide my user credentials. I have already created demo credentials, so I click on “paper trading” and  click on “paper log in.”

But it doesn’t work. I tried it on another PC, with the same problems.

Afterwards I found an option to change to “stable”.

Now TWS started to download some files and the login screen now looks different.


Still, no success. It says “server error”.

Two hours later, I tried again. And this time the connection works!

Just in case you run into the same problems, here are my settings that worked for the login window.

It says “logging in crowd2019” this time.

So far, so good. I need to investigate the cause of these connection problems in the future. 

Two hours ago I have tried two accounts on two different computers with two different network connections. 

I have also allowed two ports in my firewall, but nothing had worked.

Okay, now what?

The Trade Working Station needs to be prepared for our API access.

On the right upper corner of the main window there is a wrench symbol, when we click it we can select the settings.

Inside of the settings window we need to navigate to the API section.


According to the documentation the port for paper trading is 7497. 

The read only API mark needs to be removed.

We also need to enable ActiveX and Socket Clients.

And we need to check the “allow connections from localhost only” option, at least for now.

After I have clicked on OK, an API announcement window comes up.

Okay, so far, so good. Now we need to make our first connection.

  1. Make sure that TWS is opened and that you are logged in.
  2. Make sure that you have already installed the TWS API
  3. Open PyCharm

Now click on “Open” and navigate to c:\TWS API

After you have clicked on OK, you should see the folder content in the navigation window of PyCharm.

Here you need to navigate to TWS API – source and right click on the folder pythonclient.

Scroll down and select “Mark Directory as” – Sources Root.

Afterwards navigate to TWS API – samples  – Python – Testbed and right click on

Select “Run Program” in the menu.

Now you should see two things happen.

A lot of text is shown in the console window of PyCharm

It starts with the current state of the connection to several data farms.

The second thing that is happening is that TWS now shows up and you might now see a lot of test orders appear in your account.

Congratulations, you have just made your first connection with Python and the Interactive Brokers API!