softwarespartan / IB4m

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

Wrong requested positions #134

Open achille1112 opened 2 years ago

achille1112 commented 2 years ago

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

clear all; close all; clc;

percorso = 'W:\Software\IB4m-master\IB4m-master';

addpath(percorso); addpath(percorso,fullfile(percorso,'docs'))
javaaddpath(fullfile(percorso,'Jar','TWS973.jar'))

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

% create local buffer for historical 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();

cell_position = collection2cell(hde.data);

M_position = {};

for i = 1:size(cell_position,1)

    M_position{i,1} = char(cell_position{i}.contract.symbol);

    M_position{i,2} = char(cell_position{i}.contract.exchange);

    M_position{i,3} = cell_position{i}.position;

end

Sometimes the M_position matrix is ​​wrong even if I wait a few minutes after the execution of all the orders. Why does this happen?

Best Regards. Domenico.

softwarespartan commented 2 years ago

Thanks for opening issue. Can you elaborate what you mean by “wrong”? Do you mean that the position data is not correct or that there are missing positions or other ?

achille1112 commented 2 years ago

Thank you Abel for your answer! Today I requested the position and on a stock the position data was incorrect (the real position was -480 while the position calculated through the code was -408).

Best Regards. Domenico.

achille1112 commented 2 years ago

Dear Abel, also today the position data was incorrect: after about 15 minutes the real position was different from the calculated position.

Best regards. Domenico.

Despair2000 commented 2 years ago

If the API returns a wrong value this has most likely nothing to do with IB4m. A more suitable place to ask this question might be https://groups.io/g/twsapi.

achille1112 commented 2 years ago

Thanks Despair!

Best regards. Domenico

giovannetti87 commented 2 years ago

Hi Domenico and Despair,

I totally see the problem now, sorry for the earlier flow of dumb question. I got the problem: I was calling the metabuf rather than the data buffer. Now I see what you mean and I confirm I could get all the data I needed! I will shortly try to add more instruments to see how it looks like with multiple subscriptions

achille1112 commented 2 years ago

Hi Despair, I understand what is the problem: when I calculate the positions I obtain a matrix "M_position" where some stock has multiple relative rows. For example if I consider "A2A" stock in the following matrix which is obtained with the previous code:

'A2A'   'BVME'  -11052
'A2A'   'BVME'  -10208
'IF'    'BVME'  -300
'A2A'   'BVME'  -10208
'BSS1'  'BVME'  180
'A2A'   'BVME'  -11052
'BRE1'  'BVME'  -352
'IF'    'BVME'  -360
'BSS1'  'BVME'  180
'ISP'   'BVME'  2112
'A2A'   'BVME'  -11052
'IF'    'BVME'  -360
'IF'    'BVME'  -360
'ASC'   'BVME'  3498
'REC'   'BVME'  -440
'ATL'   'BVME'  640
'BSS1'  'BVME'  150
'LDO'   'BVME'  1344
'A2A'   'BVME'  -2424
'BSS1'  'BVME'  180
'ERG'   'BVME'  736
'A2A'   'BVME'  -11052
'UCG'   'BVME'  7064
'BGN'   'BVME'  22
'A2A'   'BVME'  -10208
'REC'   'BVME'  -568
'REC'   'BVME'  -440
'MB'    'BVME'  -3200
'A2A'   'BVME'  -10208
'REC'   'BVME'  -440
'BSS1'  'BVME'  180
'IF'    'BVME'  -360
'IF'    'BVME'  -360
'A2A'   'BVME'  -11052
'A2A'   'BVME'  -6882
'LDO'   'BVME'  1344
'BSS1'  'BVME'  180
'BMED'  'BVME'  -3047
'A2A'   'BVME'  -10208
'AZM'   'BVME'  572
'G' 'BVME'  -480
'HER'   'BVME'  3840
'REC'   'BVME'  -440
'IF'    'BVME'  -360
'BZU'   'BVME'  341
'LDO'   'BVME'  1344
'A2A'   'BVME'  -6424
'BSS1'  'BVME'  180
'REC'   'BVME'  -440
'LDO'   'BVME'  1344
'REC'   'BVME'  -440
'IP'    'BVME'  132
'PRY'   'BVME'  160

the real position is -11052 but the wrong position -2424 also appears. Is there any way to select the right position?

Best Regards Domenico.

Despair2000 commented 2 years ago

I don't know what is happening. I never see duplicates when I request positions.

But what are you trying to do? If you just want to get your whole portfolio you can maybe try to request portfolio updates. I use this more often than rePositions.

achille1112 commented 2 years ago

Hi Despair, I would like to have my whole portfolio, then I will try to request portfolio updates. I thank you for your patience!

Best Regards Domenico.

Despair2000 commented 2 years ago

Try the following function. It should give you your portfolio:

`function [portfolio] = getPortfolio global session account

% create local buffer for protfolio update events [buf, lh] = TWS.initBufferForEvent(TWS.Events.PORTFOLIOUPDATE,100);

% request account updates reqAccountUpdates(session.eClientSocket,true,account);

i = uint8(0); while isEmpty(buf) & i < 6 pause(0.5) i = i + 1; end

if i < 6 pos = collection2cell(buf); if isempty(pos) disp('There are currently no open positions.') portfolio = []; else portfolio = cellfun(@parsePortfolio,pos); end else disp('There are currently no open positions.') portfolio = []; end % cancel account updates reqAccountUpdates(session.eClientSocket,false,account); end

function port = parsePortfolio(pos) if strcmp(secType(pos.data.contract),'OPT') c = 100; else c = 1; end port.symbol = char(symbol(pos.data.contract)); port.size = pos.data.position; port.right = char(right(pos.data.contract)); port.positionValue = pos.data.marketValue; port.positionPrice = pos.data.averageCost / c; port.currentPrice = pos.data.marketPrice; port.positionGain = pos.data.unrealizedPNL; port.realizedPnL = pos.data.realizedPNL; port.conId = conid(pos.data.contract); end`

achille1112 commented 2 years ago

Thank you very much Despair!!!

Best Regards. Domenico.