My definition of batch-trading: Given $N$ BUY orders, $M$ SELL orders and $O$ ($O < N$) as the max number of open positions to be held. Batch-trading should monitor the orders and when $O$ BUY orders are filled it would cancel the $N-O$ BUY Orders.

To perform this, below is the pseudo-code:

for every PositionMessage that comes in: if number_of_filled_openpositions >= Max: // cancel is performed only ONCE, I set a "flag" to make sure // cancel is called only once during this entire process cancel_unfilled_BUY_orders_one_by_one() // OpenPositions have a TimeUpdated field, // Sell order will be generated starting from the last filled orders // Sell order is generated only after the position is fully filled Sell_excess_filled_orders()

The problem I am facing now is, depending on the day, I might get correct number of cancellations and correct number of excess_positions_sold. But on other days, I sometimes run into max number of messages exceeds limit.. or remote host forcibly disconnected your connection or incorrect number of cancellation or excess_filled_sold.

I have tried introducing a time-delay within cancel_unfilled_BUY_orders_one_by_one() so that each cancel order is sent after a time-delay.

I am finding it hard to debug because I need the market-to-be-open to test exhaustively, which is 00:00 (midnight) for me and on weekends the markets are not open (this is a hobby project).

What else should I consider? what am I missing? Has anyone else tried doing something similar?

Happy to elaborate more if required.


1 Answer 1


Interactive Brokers posted a webinar on Dec 13 2016 about using IBridgePy to connect to IB's API. https://www.youtube.com/watch?v=hogXB07OJ_I. It is pretty easy to solve the problem you mentioned. You may check out the website of IBridgePy at www.IBridgePy.com To learn how to use it, you may refer to its documentation.

  • $\begingroup$ Please elaborate on how to solve the problem. Providing an external link is not helping anyone $\endgroup$ Feb 3, 2017 at 12:18
  • 2
    $\begingroup$ @user2979010 it’s more about the fact that your question is pretty much directly answered in both the video hui provided and in Interactive Broker’s docs. $\endgroup$
    – Theodore
    Jul 19, 2018 at 19:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.