Problem using 'execution()' function IB Trading Toolbox.
Show older comments
I have a problem using the 'executions' function for IB. When I call:
f = ib.Handle.createExecutionFilter;
f.side = 'BUY';
f.secType = ibContract.sectype;
f.exchange = ibContract.exchange;
f.symbol = ibContract.symbol;
f.time = char(datetime(now-1/24,'ConvertFrom', 'datenum','Format', 'yyyyMMdd HH:mm:ss')); %WORKS in the request, but only the last execution is displayed.
d = ib.executions(f);
I get only 1 execution returned, although I have made 6 BUY executions in the last hour. I am not that familiair with events in MATLAB so please do not blame me for newby mistakes here below! ;-)
Looking at the callback function MATLAB built, I argue executions() should give me all the requested executions in the last hour ... Not just the last (see the callback code below). The callback function looks at the size of 'ibBuiltInExecutionData' and then adds one... When I request 6 executions 'numEls' is 0 or 1, but never greater then 1. The size of 'ibBuiltInExecutionData' should increase on each event, right...?
function ibBuiltInExecutionEventHandler(varargin)
%IBBUILTINEXECUTIONEVENTHANDLER Interactive Brokers' Trader Workstation built in executions data event handler.
persistent ibBuiltInExecutionData
numEls = size(ibBuiltInExecutionData,1);
% Trap event type
switch varargin{end-1}
case {'execDetailsEx'}
if numEls == 1
ibBuiltInExecutionData.contract = get(varargin{6}.contract);
ibBuiltInExecutionData.execution = get(varargin{6}.execution);
else
ibBuiltInExecutionData(numEls+1).contract = get(varargin{6}.contract);
ibBuiltInExecutionData(numEls+1).execution = get(varargin{6}.execution);
end
assignin('base','ibBuiltInExecutionData',ibBuiltInExecutionData);
case {'execDetailsEnd'}
if numEls == 1
ibBuiltInExecutionData.enddetails = varargin{4};
else
ibBuiltInExecutionData(numEls+1).enddetails = varargin{4};
end
% Return data to base workspace
assignin('base','ibBuiltInExecutionData',ibBuiltInExecutionData);
clear ibBuiltInExecutionData
evtListeners = varargin{1}.eventlisteners;
i = strcmp(evtListeners(:,1),'execDetailsEx');
varargin{1}.unregisterevent([{evtListeners{i,1}}' {evtListeners{i,2}}']);
i = strcmp(evtListeners(:,1),'execDetailsEnd');
varargin{1}.unregisterevent([{evtListeners{i,1}}' {evtListeners{i,2}}']);
varargin{end}.DataRequest = false;
end
The function executions() with this callback gives me:
d =
struct with fields:
contract: [1×1 struct]
execution: [1×1 struct]
enddetails: [1×1 struct]
I argue this should be a 1x6 struct...
When I comment out line 71-74 & 77 and 82-84 & 86 (bypassing the if/else-statements for numEls) I do get more then one execution returned, but it is only the first and the last execution. See the code below:
function ibBuiltInExecutionEventHandler(varargin)
%IBBUILTINEXECUTIONEVENTHANDLER Interactive Brokers' Trader Workstation built in executions data event handler.
persistent ibBuiltInExecutionData
numEls = size(ibBuiltInExecutionData,1);
% Trap event type
switch varargin{end-1}
case {'execDetailsEx'}
% if numEls == 1
% ibBuiltInExecutionData.contract = get(varargin{6}.contract);
% ibBuiltInExecutionData.execution = get(varargin{6}.execution);
% else
ibBuiltInExecutionData(numEls+1).contract = get(varargin{6}.contract);
ibBuiltInExecutionData(numEls+1).execution = get(varargin{6}.execution);
% end
assignin('base','ibBuiltInExecutionData',ibBuiltInExecutionData);
case {'execDetailsEnd'}
% if numEls == 1
% ibBuiltInExecutionData.enddetails = varargin{4};
% else
ibBuiltInExecutionData(numEls+1).enddetails = varargin{4};
% end
% Return data to base workspace
assignin('base','ibBuiltInExecutionData',ibBuiltInExecutionData);
clear ibBuiltInExecutionData
evtListeners = varargin{1}.eventlisteners;
i = strcmp(evtListeners(:,1),'execDetailsEx');
varargin{1}.unregisterevent([{evtListeners{i,1}}' {evtListeners{i,2}}']);
i = strcmp(evtListeners(:,1),'execDetailsEnd');
varargin{1}.unregisterevent([{evtListeners{i,1}}' {evtListeners{i,2}}']);
varargin{end}.DataRequest = false;
end
Using this callback I get:
d =
1×2 struct array with fields:
contract
execution
enddetails
This includes only the first and last execution...
Why does the size of 'ibBuiltInExecutionData' not increase? How can I get all the requested executions in my workspace returned in variable 'd'?
Thank you in advance!
My system setup is:
- The most recent version of the IB API (9.79.01).
- My Matlab version 9.7.0.1296695 (R2019b) Update 4
- The Trading Toolbox is also the most recent (Version 3.6 (R2019b) 18-Jul-2019)
Gijs van Beek
4 Comments
Gijs van Beek
on 3 Mar 2020
Annie Leonhart
on 24 Mar 2020
Good to see someone learning how the event handler works and solving the solution.
Once you know how the event handlers work ;) everything becomes easy.
Also, it’ll be easier to troubleshoot if you’re using the Gateway with the api log on.
Gijs van Beek
on 24 Mar 2020
Annie Leonhart
on 30 Mar 2020
Yes, there is a huge difference.
- Faster
- Able to troubleshoot API calls.
With the Gateway, even without an eventhandler, you can run the request, and see the output in the Gateway. You can then build the eventhandler from the output. The output is messy, but it's good enough to know what events you needs to catch with the handler, and the fields.
Answers (0)
Categories
Find more on Transaction Cost Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!