I've tried to replicate your example code regarding the download of the open orders, but I have some issues with that.
I have a main file that instantiates the connection, init the buffer and req all open orders and then in a function I try to obtain all the informations I need and create a table with them. The issue I have that it may pass several (>10) seconds sometimes between the bracket order insertion into the tws and the placement in the table. Consider that I am not vey well versed in the buffer/event logic framework implemented. If it is possible, is there a method/command like the Python API (or even the java ones) that request the open orders and output it without buffer or event activation (like a single shot Matlab function).
Attached is an example of the code I use.
`%% MAIN FILE PART
% init session with TWS
session = TWS.Session.getInstance();
% connect with TWS application
session.eClientSocket.eConnect('127.0.0.1',7496,0);
% create local buffer for open order and order status events
[buf,lh] = TWS.initBufferForEvent( ...
{ ...
TWS.Events.OPENORDER ,...
TWS.Events.ORDERSTATUS ...
} ...
,1000);
session.eClientSocket.reqAllOpenOrders();
%% EXAMPLE OF THE FUNCTION
function myfunction()
% get the events from the buffer and convert to cell array
events = collection2cell(buf); % va messo in global buf, nel main gia c e
% get the i'th event
e = events{i};
switch class(e)
% process OpenOrder events
case 'com.tws.Handler$OpenOrderEvent'
% Extract information
symbol = string(e.data.contract.symbol);
type = string(e.data.order.orderType);
action = string(e.data.order.action);
price = double(e.data.order.lmtPrice);
auxPrice = double(e.data.order.auxPrice);
orderID = double(e.data.order.orderId);
permID = double(e.data.order.permId);
quantity = double(e.data.order.totalQuantity);
% Create a row for the table
newRow = table(symbol, type, action, price, auxPrice, orderID, permID, "", quantity, "", 'VariableNames', {'symbol', 'order_type', 'action', 'lmt_price', 'aux_price', 'orderID', 'permID', 'status', 'quantity', 'filled'});
% Append the new row to the table
orderTable = [orderTable; newRow];
% process OrderStatus events
case 'com.tws.Handler$OrderStatusEvent'
% Extract information
orderID = double(e.data.orderId);
permID = double(e.data.permId);
status = string(e.data.status);
quantity = double(e.data.remaining);
filled = double(e.data.filled);
newRow = table("", "", "", "", "", orderID, permID, status, quantity, filled, 'VariableNames', {'symbol', 'order_type', 'action', 'lmt_price', 'aux_price', 'orderID', 'permID', 'status', 'quantity', 'filled'});
% Append the new row to the table
orderTable = [orderTable; newRow];
% Find rows with matching permID
idx = find(orderTable.permID == permID);
end
end
% Sort the table by the 'permID' column
orderTable = sortrows(orderTable, 'permID', 'ascend');
% Iterate over the rows of the table
for i = 1:height(orderTable)
% Check if symbol is empty
if isempty(orderTable.symbol{i})
% Find the non-empty symbol with the same permID
nonEmptyIdx = find(~cellfun(@isempty, orderTable.symbol) & orderTable.permID == orderTable.permID(i), 1);
% Replace the empty symbol with the non-empty symbol
orderTable.symbol{i} = orderTable.symbol{nonEmptyIdx};
end
Hi! First of all, thanks for your work!
I've tried to replicate your example code regarding the download of the open orders, but I have some issues with that.
I have a main file that instantiates the connection, init the buffer and req all open orders and then in a function I try to obtain all the informations I need and create a table with them. The issue I have that it may pass several (>10) seconds sometimes between the bracket order insertion into the tws and the placement in the table. Consider that I am not vey well versed in the buffer/event logic framework implemented. If it is possible, is there a method/command like the Python API (or even the java ones) that request the open orders and output it without buffer or event activation (like a single shot Matlab function).
Attached is an example of the code I use.
`%% MAIN FILE PART
% init session with TWS session = TWS.Session.getInstance();
% connect with TWS application session.eClientSocket.eConnect('127.0.0.1',7496,0);
contract = com.ib.client.Contract();
% configure contract contract.symbol(ticker) ; contract.secType('STK') ; contract.exchange('SMART'); contract.currency('USD') ;
% create local buffer for open order and order status events [buf,lh] = TWS.initBufferForEvent( ... { ... TWS.Events.OPENORDER ,... TWS.Events.ORDERSTATUS ... } ... ,1000);
session.eClientSocket.reqAllOpenOrders();
%% EXAMPLE OF THE FUNCTION function myfunction() % get the events from the buffer and convert to cell array events = collection2cell(buf); % va messo in global buf, nel main gia c e
% Initialize an empty table with variable types orderTable = table('Size', [0, 10], 'VariableTypes', {'string', 'string', 'string', 'double', 'double', 'double', 'double', 'string', 'double', 'double'}, 'VariableNames', {'symbol', 'order_type', 'action', 'lmt_price', 'aux_price', 'orderID', 'permID','status', 'quantity', 'filled'});
for i = 1:numel(events)
end
% Sort the table by the 'permID' column orderTable = sortrows(orderTable, 'permID', 'ascend');
% Iterate over the rows of the table for i = 1:height(orderTable) % Check if symbol is empty if isempty(orderTable.symbol{i}) % Find the non-empty symbol with the same permID nonEmptyIdx = find(~cellfun(@isempty, orderTable.symbol) & orderTable.permID == orderTable.permID(i), 1); % Replace the empty symbol with the non-empty symbol orderTable.symbol{i} = orderTable.symbol{nonEmptyIdx}; end
end
`