0

I am building an app to place orders through the Python API an am having an issue with the consistency of order transmission where only one order can be received and executed per app instance.

As an example if I run the below code it will execute and transmit a sample order indefinitely, no matter how many times I run the script.

from ib_insync import *

# connect to Interactive Brokers 
ib = IB()
ib.connect('127.0.0.1', 7497, clientId=3) #4002 / 7497

stock = Stock("AAPL", 'SMART', 'USD')

order = MarketOrder('BUY', 10)
trade = ib.placeOrder(stock, order)
print('Done')

However if I define the order part as a function and run it, it will only transmit an order once no matter how many times I call the function. If I restart the script then I can send another order with the same function but again only once.

Is there a way around this as I want to be able to send other orders without having to restart the app.

Dodo
  • 55
  • 10

1 Answers1

0

My earlier answer (below) is erroneous and should be disregarded. Per Ewald de Wit, ib_insync author: "There's no need to set the orderId, it's issued automatically when the order is placed."

The OP's issue has been raised (and resolved) as part of the discussion on other posts. See, for example, my remarks at IB_insync - Sanic error after one successful order preventing any further orders


IB requires a unique, ascending value, orderId to accompany each order. One simple method of accomplishing this is to use a timestamp, as follows:

stock = Stock("AAPL", 'SMART', 'USD')

order = MarketOrder('BUY', 10)

curr_dt = datetime.now()
order.orderId = int(round(curr_dt.timestamp()))

trade = ib.placeOrder(stock, order)

Note that this will not work if at least one second hasn't elapsed between orders; otherwise the second order will get the same timestamp and orderId. Should this occur, you'll have to find an alternate way to generate the orderId.

Snorkel
  • 11
  • 3