Open achille1112 opened 3 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 ?
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.
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.
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.
Thanks Despair!
Best regards. Domenico
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
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.
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.
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.
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`
Thank you very much Despair!!!
Best Regards. Domenico.
Dear Abel, I have a problem when I request my positions via API using the following code:
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.