rterp / SumZeroTrading

A Java API for Developing Automated Trading Applications for the Equity, Futures, and Currency Markets
http://rterp.github.io/SumZeroTrading/
Other
145 stars 47 forks source link

IB Disconnection #28

Open vinceyap88 opened 7 years ago

vinceyap88 commented 7 years ago

I have noticed that there are some threads running after calling the InteractiveBrokersClientInterface.unsubscribeLevel1() method as well as InteractiveBrokersClientInterface.disconnect(). Are there any methods which can be used to close the running threads after the connect() method and subscribeLevel1() method?

rterp commented 7 years ago

There is a thread running to process the internal queue of IB data from the level 1&2 subscribers, and order events. Is there an issue with these threads continuing to run even if there are no current level 1 subscribers?

vinceyap88 commented 7 years ago

The program need to have proper exit. In order to exit properly, we need to stop the running threads. Is there anyway to stop it?

rterp commented 7 years ago

I see. Probably the easiest thing to do will be to convert the threads to daemon threads, so if they are still running when the main thread stops, the application will shut down without needing to stop the daemon threads.

charlye commented 7 years ago

Hi Rob,

I think what Vince mentioned was partly caused by blocked threads in IBQuoteProcessor.

    public void run() {
        while( shouldRun ) {
            try {
                processData( quoteBlockingQueue.take() );
            } catch( Exception ex ) {
                //logger.error(ex, ex);
                ex.printStackTrace();
            }
        }
    }

When stopProcessor() method was called, shouldRun is changed to false with intention to exit the while loop, but the thread could be permablocked if no new data is put into the queue. I suggest to also interrupt this thread in stopProcessor()

rterp commented 7 years ago

Ok, I see what you are saying now. You're right, if you aren't subscribed to any quotes, or if its after market hours and no quotes are coming in. The other option would be to use the poll(long timeout, TimeUnit unit) on the blockingQueue rather than take(), and give it a 500ms or so timeout value, so if there was nothing in the queue, the operation would timeout, it would see shouldRun is false, and exit the loop.