0%

【How 2】 Set Up Trading API Template In Python - Q&A

In the last post in this series, we’re going to look at some questions that I discovered while working on connecting to Interactive Broker API. Some of them are due to the obscurity of the configuration and hard to find the right place to configure them, and some of them would need the extra tool to resolve. I put all of them down into one post and share it with you.


Become a Medium member to continue learning without limits. I’ll receive a small portion of your membership fee if you use the following link, at no extra cost to you.

If you enjoy reading this and my other articles, feel free to join Medium membership program to read more about Quantitative Trading Strategy.


Previous researches

Q&A

1. When I’m using apscheduler and ib_insync at the same time, there are errors and I can’t get my trading script to work

Apscheduler is the standard package in my quantitative trading setup. It’s a python library that helps you schedule your python code/function to be run at a specific DateTime or regular intervals with consideration of timezone. I gotta recommend this library to those traders/developers who have similar requirements in their trading scripts.

However, both apscheduler and ib_insync packages use the design of multi-threading in their package. If you include both of them, you’ll run into a problem and find a RuntimeError occurred when you try to run your script. Fortunately, ib_insync package includes this functionality to enable nested threading.

1
RuntimeError: There is no current event loop in thread 'ThreadPoolExecutor-0_0'.

RuntimeError saying there is no current event loop

And all you have to do is to:

  1. Call ib_insync.util.patchAsyncio() after you import ib_insync library.
  2. Use AsyncIOScheduler to create your scheduler.
  3. Add async before the scheduled function definition.
    1
    2
    3
    # ibkr_api.py
    import ib_insync
    ib_insync.util.patchAsyncio()
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# main.py
from apscheduler.schedulers.asyncio import AsyncIOScheduler
import asyncio

async def test():
print(datetime.datetime.now().strftime(’%Y-%m-%d-%H_%M_%S’))

if __name__ == '__main__':
scheduler = AsyncIOScheduler()

scheduler.add_job(
test,
'cron',
day_of_week='mon-fri',
hour=9,
minute=10,
timezone=ZoneInfo('US/Easter'),
)

Apscheduler + ib_insync code snippet

By making the above changes, the apscheduler and ib_insync can coexist in the same script.

Note: There are things that could still go wrong as using the nested threads are relatively complicated.
Reference: Using ib-insync with APScheduler

2. Why I can’t get a valid stock price back by using reqMktData?

There are several steps and restrictions for requesting stock prices using reqMktData() function:

  1. Before requesting a market quote, you need to subscribe to the market data on the IBKR platform. You can find the management page in the TWS or IB gateway tab “Account” -> “Manage Account” -> “Subscribe Market Data/Research”.
  2. The ib.reqMarketDataType(N) is to specify what kind of data type you would like to request. For example, if you request market data type = 1 (live market data) outside the trading hour, you won’t be able to receive any valid pricing data from the server. Therefore choose the market data type carefully, and test and explore their limitation.
  3. As said in the previous post, this entire querying of pricing data is an asynchronous process, meaning you could run into the situation that you’re accessing the pricing data while your script is still trying to fetch the data from the server. Therefore, remember to use ib.sleep() wisely to ensure you only access the pricing data when the pricing data is returned.

Reference: 【How 2】 Set Up Trading API Template In Python - Placing orders with Interactive Brokers

3. There are popup windows that prevent me from placing orders using API. What happened?

Inside the TWS and the IB gateway, there are pre-configured conditions that prevent API consumers to place an unintended order. If you accidentally place an order that falls outside of the size or value range, or if the current market data is lagged and hasn’t been updated for a long time, then the TWS/IB gateway will pop up a warning window to tell you that there is a potential hazard to place such an order.

To prevent this from happening and stop your trading script, you can check the box in API -> Precautions -> “Bypass Order Precaution for API Order” to prevent the warning dialog boxes from popping up when you place orders through API. Yet, you have to bare the risk of unexpected loss when your script goes wrong.

Check this checkbox to prevent the warning appears when placing orders via API

Reference: API Precautions

4. How could I reset my paper account?

Whenever you feel like starting a new test from a clean slate, you can always reset your paper account. However, the setting is quite hard to find. You have to log in to your paper account on the IBKR website, and then follow the below steps to reset your paper account.

How to reset your paper account 1

How to reset your paper account 2

How to reset your paper account 3

5. There are times that my TWS or IB gateway won’t successfully auto-reconnect. What should I do?

TWS and IB gateway are two very important intermediaries to relay the API instructions from your local trading script to the remote IBKR API server. However, there is a hidden mechanism inside TWS and IB gateway applications. These two applications need to restart every day and will automatically reconnect, and require human intervention to log in again every seven days. Therefore, there are two scenarios that we need to address:

  1. Q: How do we keep the connection with IBKR after the software applications have auto-reconnected?
    • A: Avoid using the long connection as possible. Disconnect your IB() instance as long as the required actions are done, and reconnect to the server when new actions are needed.
  2. Q: What if there’s an error occurred while software applications are rebooting?
    • A: As the software applications are run locally on your desktop or laptop, meaning this type of software crash is not monitored by any script or process. One possible solution is to wrap the headless software application inside docker. You can download the docker image of “ib-gateway-docker” from here and run this docker container on your local machine so that the process can be protected and monitored by the daemon inside the docker container.

Reference:IB gateway docker

6. What to do with 2-factor authentication when trading using a real account?

Since Interactive Brokers adopts two-factor authentication for logging in and buying/selling stocks, it essentially means that working with Interactive Brokers API won’t be fully automated (See here and here). Every time you place a random order or log in to your TWS/IB gateway application, you will receive a message on your smartphone to confirm your corresponding action once more “manually“. Here are two posts that could give you a rough idea of how to work with this two-factor authentication system:

There could be a possibility in the future to have a workaround to bypass this system. Currently, having your smartphone with you during trading hours would seem to be the most promising method.


This is the last post in this Set-Up Trading API Template In Python series. I hope you enjoy reading these and let me know if there is any other topic you would like to read.

Disclaimer: Nothing herein is financial advice or even a recommendation to trade real money. Many platforms exist for simulated trading (paper trading) which can be used for building and developing the strategies discussed. Please use common sense and consult a professional before trading or investing your hard-earned money.

Enjoy reading? Some donations would motivate me to produce more quality content