softwarespartan / IB4m

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

Here's some sample code to connect and enter a futures order with a Good after time #121

Open mvalley21 opened 3 years ago

mvalley21 commented 3 years ago

session = TWS.Session.getInstance(); session.eClientSocket.eConnect('localhost',7496,0);

% contract details %[buf,lh] = TWS.initBufferForEvent(TWS.Events.CONTRACTDETAILS); %session.eClientSocket.reqContractDetails(0,contract); pause(2); % Contract details have a lot going on so make sure to look through the references listed below. %buf.get().data; details = collection2cell(buf.get().data); buf.clear();

%get the next orderId [buf,lh] = TWS.initBufferForEvent(TWS.Events.NEXTORDERID); session.eClientSocket.reqIds(true); pause(1); nid=buf.get().data.nextOrderId; fprintf('the next order id is: %d\n',nid);

[buf,lh] = TWS.initBufferForEvent(TWS.Events.EXECUTIONDETAILS);

% init contract contract = com.ib.client.Contract();

% configure contract contract.symbol('NQ'); contract.localSymbol('NQM1'); contract.exchange('SMART'); contract.primaryExch('GLOBEX'); contract.currency('USD') ; contract.secType('FUT');

parent = com.ib.client.Order(); parent.account('DU12345'); parent.action('BUY'); parent.tif('GTC'); parent.goodAfterTime('20210326 06:40:00 PST')

parent.orderType('MKT'); parent.totalQuantity(1);

parent.transmit(false);

session.eClientSocket.placeOrder(nid,contract,parent); pause(1);