I have the following code which pulls out prices for multiple stocks in parallel from a REST api. not from IB. Then it places my orders in ib.
results = asyncio.run(get_data(new_portfolio.keys()))
app = ib_class("127.0.0.1", 4002, 999)
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()
app.cancel_all_orders()
open_positions = app.read_positions()
# some calculations to check what positions to add and remove in a for loop
new_portfolio = open_positions.update(results)
for ....
for o in bracket:
app.placeOrder(...
app.nextOrderId()
However, it takes a while to query the connect and query the openpositions.
I want to do it before I do my asyncio.run to query the prices from my api like this.
app = ib_class("127.0.0.1", 4002, 999)
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()
app.cancel_all_orders()
open_positions = app.read_positions()
results = asyncio.run(get_data(new_portfolio.keys()))
# some calculations to check what positions to add and remove in a for loop
new_portfolio = open_positions.update(results)
for ....
for o in bracket:
app.placeOrder(...
app.nextOrderId()
But when I do that, some of the orders don't go through.
I think it's because there's a clash with the asyncio and IB's threading.
Is there a way to make sure that this does not happen?