softwarespartan / IB4m

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

Positions request #130

Closed achille1112 closed 3 years ago

achille1112 commented 3 years ago

Dear Abel, I have a problem when I request my positions via API using the following code:

% initialize session with TWS
session = TWS.Session.getInstance();

% create local buffer for Positions data events 
[bufP,lhP] = TWS.initBufferForEvent({
    TWS.Events.POSITIONS,...
    });

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

session.eClientSocket.reqPositions(); pause(0.5)

hde = bufP.get();

I obtain 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)

Even if I wait several seconds I always get the same error.

Could you help me to understand why this happens?

Best Regards. Domenico.

softwarespartan commented 3 years ago

It just means the buffer was empty. Here if call “get” on empty buffer then an exception is thrown. Can just do “if buf.size()>0; e=buf.get(); end

achille1112 commented 3 years ago

Ok, thank you very much for your answer. The buffer is empty when there are not open positions at the start of the session?

Best regards. Domenico

Despair2000 commented 3 years ago

If there are no open positions the request doesn't return anything so the buffer remains empty. But even if there are positions it can sometimes take 1-2s for them to show up in the buffer so it can be a good idea to implement a loop that waits for something to drop in the buffer. This is true also for other requests. Especially if you have a fast computer you often have to wait for the answer of the API.

achille1112 commented 3 years ago

Thanks Despair2000! But even if I wait some minutes I obtain the same error.

Best regards. Domenico

Despair2000 commented 3 years ago

And you have open positions?

Minutes it should definitely not take.

achille1112 commented 3 years ago

When there are not open positions at the start of the session I obtain the error indipendently from the seconds that I wait. If I open position and after I close position (there are no open positions) then I not obtain the error.

Best regards Domenico

Despair2000 commented 3 years ago

Ok, and what happens if there is an open position?

achille1112 commented 3 years ago

If there is an open position at the start of the session I obtain no error.

Best regards Domenico

Despair2000 commented 3 years ago

Sounds like all works fine. A closed position is often still returned but with positionSize 0.

achille1112 commented 3 years ago

Ok thanks Despair2000! Then if I open a position and after I close the position I obtain no error because the position is with positionSize=0. If there is no open position at the start of the session I obtain the error because there is no position in the buffer.

Is that right or did I get it wrong?

Best regards Domenico

Despair2000 commented 3 years ago

When you create the buffer it is obviously empty. When you request the open positions while there is a position the request returns this position to the buffer. When you close the position the same position is still returned on request with size 0.

You should see that if you look at the returned data.

achille1112 commented 3 years ago

Thank you very much Despair!

Best Regards Domenico