softwarespartan / IB4m

Interactive Brokers API for Matlab
GNU General Public License v2.0
62 stars 21 forks source link

Buffer is already empty #126

Closed achille1112 closed 2 years ago

achille1112 commented 3 years ago

Dear Abel, I tried to download historical data through the following code:


session = TWS.Session.getInstance();

% create local buffer for historical data events 
buf = TWS.initBufferForEvent({
    TWS.Events.HISTORICALDATA,...
    });

% connect to TWS
session.eClientSocket.eConnect('127.0.0.1',7496,0);

pause(1)

contract = com.ib.client.Contract();
contract.symbol('AZM')
contract.exchange('SMART');
contract.primaryExch('BVME');
contract.currency('EUR');
contract.secType('STK');

tickerId = 1000001;
endDateTime = [datestr(today,'yyyymmdd') ' 00:00:00'];
durationString = '3 D';
barSizeSetting = '1 min';
whatToShow = 'TRADES';
useRTH = 1;
formatDate = 1;
keepUpToDate = false;

% ix = 0; while(isEmpty(buf) && ix < 100); ix = ix+1; pause(0.1); end

% request historical data for our contract for a year w/ 1-day bars
session.eClientSocket.reqHistoricalData(1000001,contract,endDateTime,durationString,barSizeSetting,whatToShow,useRTH,formatDate,keepUpToDate,[]); pause(0.5);

% Retreive the historical data event from the buffer
hde = buf.get();

and I get the following error:

Java exception occurred:
org.apache.commons.collections.BufferUnderflowException: The buffer is already empty

    at org.apache.commons.collections.buffer.BoundedFifoBuffer.get(BoundedFifoBuffer.java:261)

If I try the same code but with

[buf,lh] = TWS.initBufferForEvent({
    TWS.Events.HISTORICALDATA,...
    });

I no longer get the error above. Could you help me to understand why this happens?

Best Regards. Domenico.