softwarespartan / IB4m

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

Line missing from ExecutionDetailsExample? #52

Open smunk03 opened 5 years ago

smunk03 commented 5 years ago

Thanks for a great API wrapper!

I would like to place an MKT order with stoploss and takeprofit. like 20 ticks each way or so.

I can do it for a LMT order, but then I have to give the exact prices for buy-limit, tp and sl, and cant use the the relative way as above. (I accomplish that by placing 3 orders with the two sl (STP) and tp (LMT) orders connected to the parent).

If I could place the initial order as a MKT, and then get the fill-price, then I can calculate the other two order prices, but I cant find that value anywhere.

My guess would be the ExecutionDetails, but when I look at the ExecutionDetailsExample, there seems to miss the information about how buf gets populated, and I have not been able to find that information somewhere else.

Could you please help me? Am I right with the missing part in the example, and is there maybe a better way to accomplish what I want?

(I use the 973.jar version)

thanks a lot, S.

softwarespartan commented 5 years ago

Thanks for reaching out.

I guess I am not totally certain what you mean

The buf gets configured in this call

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

and be sure to ask TWS to auto associate api orders

session.eClientSocket.reqAutoOpenOrders(true);

Then whenever TWS fires ExecutionDetails events they will get put in the associated buffer.

Does that make sense?

smunk03 commented 5 years ago

Great, thanks a lot, I needed that line! (and I'm pretty sure it is missing from the example).

I trade forex, and want simply to f.ex buy EURUSD, with stoploss 20 pips and takeprofit 20 pips. Like

buy("EURUSD", 20, 20);

But the order function I use requires actual prices, so I have to call it like this:

buy("EURUSD", 1.244, 1.242, 1.246)

but that would require me to know the exact market price, which I dont. And if my limit price is wrong, the trade will not get matched, which introduces a new level of uncertainty...

So if you know of a better way to do that, I would be happy to hear, but event the answer you already gave is helpful and should be enought to accomplish it the way I'm going about it!

So thanks, and wish you an wonderful weekend!

Soren.

softwarespartan commented 5 years ago

Be sure to check out advance order algos in the IB API

https://interactivebrokers.github.io/tws-api/advanced_orders.html

Also, sounds like you can just stream market data L1 and/or L2 to figure out exact mkt price

https://interactivebrokers.github.io/tws-api/market_data.html#market_subscriptions