What to do when IB Historical Data requests sometimes go silent?

Discussion in 'App Development' started by lime, Jul 2, 2024.

  1. lime

    lime

    I use the following codes to download historical data. Most of the times it works no problem, but sometimes it goes silent, no errors reported and just nothing. I would close and re-open the terminal to re-run (same parameters), and then it would work so not a code problem or connection issue (which is raised at code 502).

    I want to set a timeout, get it done in 1 min or kill it, similar to Future.get(timeout, timeoutUNIT) . But I don't want to complicate things and to any threading/Callable. EReader methods and EReaderSignal.waitForSignal don't have a timeout parameter.

    Where/how shall I set a timeout here?

    Code:
    public static void main (String[] args) throws IOException {
            downloader = getDownloader(); //calling constructor
            downloader.start();        
      }
    
    public void start() throws IOException {
            this.openConnection(portNumber); //connect to TWS
            this.request(1);  //for TRADES
            this.request(2); //for BID
            this.request(3);  //for ASK
    
            while ( !this.isBidRequestDone || !this.isAskRequestDone || !this.isTradesRequestDone  ) { 
                this.readerSignal.waitForSignal();
                try {
                    this.reader.processMsgs(); //trigger callback
                } catch (IOException err) {
                    throw new IOException(err);
                }
            } 
            this.saveData(); 
            this.closeConnection(); 
     }
    
    private void request(int reqId) {
            this.setContract();
            this.client.reqHistoricalData(reqId, ...<parameters>);
        }
    
    public void historicalDataEnd(int reqId, String startDateStr, String endDateStr)  {
            switch (reqId) {
                case 1 -> this.isTradesRequestDone = true; 
                case 2 -> this.isBidRequestDone = true;
                case 3 -> this.isAskRequestDone = true;
            }
        }
    
    private void openConnection(int port) { 
            this.readerSignal = new EJavaSignal(); 
            this.client = new EClientSocket(this, this.readerSignal);
            this.client.eConnect("127.0.0.1", port, 0); 
            this.reader = new EReader(this.client, this.readerSignal); 
            this.reader.start(); 
        }