What's wrong with this reqHistoricalData() call?

Discussion in 'Interactive Brokers' started by lime, Jun 20, 2024.

  1. lime

    lime

    This is preliminary and trying to send a hist data request and see what comes back.
    It is not wrapped in a thread and no loop.

    What's wrong with it? Is there something wrong with my request? The `historicalData()` callback is never entered.

    Code:
    import com.ib.client.*;
    import java.util.concurrent.TimeUnit;
    
    public class HistoricalDataDownloader implements EWrapper {
        
        private EJavaSignal javaSignal = new EJavaSignal();
        private EClientSocket mySocket = new EClientSocket(this, javaSignal); 
    
        private Contract contract = new Contract(); //Contract obj
        private String ticker = "MSFT"; //stock ticker
        private String reqEndDateTime = "20240617 16:00:00";  
        private String reqLookbackWindow = "1 D"; 
        private PriceDataType reqDataType = PriceDataType.TRADES; 
        private String reqBarSize = "2 hrs"; //data granularity 
        
         public static void main (String[] args) {
    
            HistoricalDataDownloader myDownloader = new HistoricalDataDownloader();
            myDownloader.openConnection();
            System.out.println( "Connected to TWS: " + myDownloader.mySocket.isConnected() );
    
            myDownloader.request();
    
            try { //pause a bit after request
                System.out.println("Am going to sleep");
                TimeUnit.SECONDS.sleep(8);
                System.out.println("bye");
            } catch (InterruptedException err) {
                System.out.println("interrupted");
            }
    
        }
    
        private enum PriceDataType {
            BID, 
            ASK,
            TRADES
        }
    
        /*
        @see: https://ibkrcampus.com/ibkr-api-page/twsapi-doc/#requesting-historical-bars
        */
        private void request() {
            this.setContract();
            this.mySocket.reqHistoricalData(1, this.contract, this.reqEndDateTime, this.reqLookbackWindow, this.reqBarSize, this.reqDataType.name(), 1, 1, false, null);
        }
    
        /*
        @see: https://ibkrcampus.com/ibkr-api-page/twsapi-ref/#ewrapper-pub-func
        */
        @Override
        public void historicalData(int reqId, Bar candlestick) {
            System.out.println("I am inside the receiver callback");
        }
    
        public void historicalDataEnd(int reqId, String startDateStr, String endDateStr) {
        }
        
        private void openConnection() { 
            this.mySocket.eConnect("127.0.0.1", 7496, 0);
        }
    
        private void closeConnection() { 
            this.mySocket.eDisconnect();
        }
    
        private void setContract() {
            this.contract.symbol(this.ticker);
            this.contract.secType("STK");
            this.contract.currency("USD");
            this.contract.exchange("SMART"); 
        }
    
     
  2. lime

    lime

    It seems I completely missed this EReader thing
     
  3. You need to define the historicalData() routine as the callback. I don't see this being done anywhere in the code.