问题
I am using Matlab's built-in Interactive Broker library to connect and use TWS. I am trying to request real time data, however, after a while it just gets stuck on the same price. It updates normally for a few minutes and then later it just stops updating and the same prices are given.
Is there something wrong with my code?
try
close(ib);
close(conn);
catch
end
clear all;
ibBuiltInRealtimeData = struct('id',0,'BID_PRICE',0,'BID_SIZE',0,'ASK_PRICE',0,'ASK_SIZE',0);
ib = ibtws('',7496);
f = '233';
ibContract = ib.Handle.createContract;
ibContract.symbol = 'EUR';
ibContract.secType = 'CASH';
ibContract.exchange = 'IDEALPRO';
ibContract.primaryExchange = '';
ibContract.currency = 'USD';
ibContract2 = ib.Handle.createContract;
ibContract2.symbol = 'M6E';
ibContract2.secType = 'FUT';
ibContract2.exchange = 'GLOBEX';
ibContract2.primaryExchange = '';
ibContract2.currency = 'USD';
ibContract2.expiry = '201609';
contracts = {ibContract;ibContract2};
tickerid = realtime(ib,contracts,f);
while true
d2 = ibBuiltInRealtimeData
tickerid
pause(1)
end
回答1:
This may be due to either a networking issue that caused Matlab's connector to get stuck in an invalid state, or IB's server that might have been stuck. You can try to disconnect from IB and then reconnect and re-request realtime data - perhaps this will reset the connection problem and send you good data from that point on.
Alternatively, try using the IB-Matlab connector (http://UndocumentedMatlab.com/IB-Matlab), which is reportedly more robust.
回答2:
If you use a custom event handler you can usually bypass the problematic code in the trader toolbox that causes this function to hang.
try
close(ib);
close(conn);
catch
end
clear all;
global simpleStructure
simpleStructure=struct;
ib = ibtws('',7496);
f = '233';
ibContract = ib.Handle.createContract;
ibContract.symbol = 'EUR';
ibContract.secType = 'CASH';
ibContract.exchange = 'IDEALPRO';
ibContract.primaryExchange = '';
ibContract.currency = 'USD';
ibContract2 = ib.Handle.createContract;
ibContract2.symbol = 'M6E';
ibContract2.secType = 'FUT';
ibContract2.exchange = 'GLOBEX';
ibContract2.primaryExchange = '';
ibContract2.currency = 'USD';
ibContract2.expiry = '201609';
contracts = {ibContract;ibContract2};
tickerid = realtime(ib,contracts,f, @yourEventHandler);
function yourEventHandler(varargin)
global simpleStructure;
id=num2str(varargin{3});
switch varargin{4}
case 0; simpleStructure.(['i' id]).BID_SIZE=varargin{5};
case 1; simpleStructure.(['i' id]).BID_PRICE=varargin{5};
case 2; simpleStructure.(['i' id]).ASK_PRICE=varargin{5};
case 3; simpleStructure.(['i' id]).ASK_SIZE=varargin{5};
end
end
The use of globals is not required, just used for simplicity. I often use a static method as an event handler and wrap everything else in a class. Unless you're multithreading it doesn't really matter. (if you are multithreading just use java or c# and save yourself hours of headaches)
来源:https://stackoverflow.com/questions/37933464/matlab-ib-realtime-data-gets-stuck-after-a-while