softwarespartan / IB4m

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

Request orderId #127

Closed achille1112 closed 3 years ago

achille1112 commented 3 years ago

Hello, After an order has been executed, is there any way to save only its orderId?

Best Regards Domenico.

Despair2000 commented 3 years ago

After an order is executed you receive the execution details. These include the orderId of the executed order. If you want to save this ID you will have to do this in your code.

achille1112 commented 3 years ago

Thank you Despair2000 for your answer.

For example:

% initialize session with TWS
session = TWS.Session.getInstance();

% create local buffer for contract details events
[buf,lh] = TWS.initBufferForEvent({
    TWS.Events.NEXTORDERID,...
    TWS.Events.OPENORDER,...
    TWS.Events.ORDERSTATUS ...
    TWS.Events.EXECUTIONDETAILS ,...
    });

% connect to TWS
session.eClientSocket.eConnect('127.0.0.1',7496,0); pause(0.5)

% request the next order id
session.eClientSocket.reqIds(true); pause(1);

% blab about it

orderId = buf.get().data.nextOrderId;

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

contract.symbol('AZM')
contract.exchange('SMART');
contract.primaryExch('BVME');
contract.currency('EUR');
contract.secType('STK');

order = com.ib.client.Order();
order.action('BUY')
order.totalQuantity(100)
order.orderType('LMT')
order.lmtPrice(20.40);

session.eClientSocket.placeOrder(orderId,contract,order);

After the order is executed how do I have its orderid and execution price?

Best Regards Domenico.

Despair2000 commented 3 years ago

After your order is executed you will receive a executionDetails event in your buffer. These details include both the execution price and the orderId.

Then you will see that there are a lot of events. Especially since you have just one buffer for all events. OrderStatus events are particularly messy. There are always many duplicates and so on. You will have to take care of this.

Also the executionDetails come often with many revisions and corrections (especially when there are partial fills) so you must make sure that you get the right one. You probably also want to subscribe to the commissionReport. This comes along with the executionDetails and gives you the paid commission and for closed trades the realized pnl.

achille1112 commented 3 years ago

But after

execEvents = collection2cell(buf);
exectionOrder = execEvents{1}.data;

I get as result a Java String (where there are orderid and price) but I'm not able to extract the orderid e the price. Is there a method to extract this data easily?

Best Regards. Domenico

Despair2000 commented 3 years ago

The execution-object has many methods. Issue methods(execEvents{1}.data) in your example and you will get a list.

For example orderId(execEvents{1}.data) will give you the orderId.

achille1112 commented 3 years ago

Thank tuo very much Despair2000!!!

Best Regards Domenico.

achille1112 commented 3 years ago

Dear Despair, the methods for the object execEvents{1}.data are:

Methods for class com.tws.ExecutionDetails:

ExecutionDetails  getClass          notify            toString          
equals            hashCode          notifyAll         wait       

but there is not orderId method.

The value of execEvents{1}.data is:

orderId = 9
clientId = 10
conid = 67957194
symbol = AZM
secType = STK
expiry = null
strike = 0.0
right = None
multiplier = null
exchange = BVME
primaryExch = null
currency = EUR
localSymbol = AZM
tradingClass = AZM
execId = 0000e859.60bda1c3.01.01
time = 20210607  13:41:39
acctNumber = DU3462642
executionExchange = BVME
side = BOT
shares = 100.0
price = 20.74
permId = 1835137620
liquidation = 0
cumQty = 100.0
avgPrice = 20.74
orderRef = null
evRule = null
evMultiplier = 0.0

but I'm not able to extract orderId value.

Best Regards. Domenico.

Despair2000 commented 3 years ago

You are looking at the wrong object. See the attached screen-shot:

Capture

In the executionDetails object are two objects. The contract and the execution. Try again methods(execEvents{1}.data.execution).

achille1112 commented 3 years ago

Thank you very much Despair2000, now it works.

Best regards. Domenico