Quantcast

IBrokers and fill Price

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

IBrokers and fill Price

harrisonmark1
I have been trying to figure out how to use IBrokers and the API to
place orders, etc.  I have read through the support archives, google,
the docs, presentations, etc and I still can not figure out where I am
going wrong.

Specifically I am trying to place a market order, get my fill price,
and then put in a stop order higher/lower than the fill price.  I am
able to get ids, place the orders, and even seeing what my fill price
is via reqOpenOrders but I am not sure how to get the fill price from
IB in a way that lets me use it - i.e. not just 'cat' to the screen.

Any help would be appreciated.

# Session Info:

R version 2.14.0 (2011-10-31)
Platform: x86_64-pc-mingw32/x64 (64-bit)

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United
States.1252
[3] LC_MONETARY=English_United States.1252 LC_NUMERIC=C
[5] LC_TIME=English_United States.1252

attached base packages:
[1] graphics  grDevices utils     datasets  stats     methods   base

other attached packages:
 [1] blotter_0.8.4              twsInstrument_1.4-0
IBrokers_0.9-3             qmao_1.2.1
 [5] FinancialInstrument_0.13.6 quantmod_0.3-17            TTR_0.21-1
               Defaults_1.1-1
 [9] xts_0.8-7                  zoo_1.7-7

loaded via a namespace (and not attached):
[1] grid_2.14.0    lattice_0.20-6 tools_2.14.0

# Code Examples
tws <- twsConnect(clientId=2)

symbol <- c("GC")
contract <- buildIBcontract(twsFUT(symbol, "NYMEX", '201206'))

id <- reqIds(tws)
a <- twsOrder(id, action = 'BUY', totalQuantity = '1', orderType = 'MKT')
placeOrder(tws, contract, a)
reqOpenOrders(tws)

TWS Message: 2 -1 2104 Market data farm connection is OK:cusfuture
TWS Message: 2 -1 2104 Market data farm connection is OK:usfuture
TWS Message: 2 -1 2104 Market data farm connection is OK:eurofarm
TWS Message: 2 -1 2104 Market data farm connection is OK:cashfarm
TWS Message: 2 -1 2104 Market data farm connection is OK:usopt
TWS Message: 2 -1 2104 Market data farm connection is OK:usfarm
TWS Message: 2 -1 2106 HMDS data farm connection is OK:ushmds
TWS Execution: orderId=1 time=2012-05-06 18:02:10 side=BOT shares=1
symbol=GC conId=45008148 price=1640.50
TWS OrderStatus: orderId=1 status=Filled filled=1 remaining=0
averageFillPrice=1640.50
TWS OrderStatus: orderId=1 status=Filled filled=1 remaining=0
averageFillPrice=1640.50
TWS OrderStatus: orderId=1 status=Filled filled=1 remaining=0
averageFillPrice=1640.50

twsDisconnect(tws)

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: IBrokers and fill Price

soren wilkening
For getting the actual fill price you should use

reqExecutions()

instead of

reqOpenOrders()

I am even surprised to hear that for a MKT order, there is price information containted in the result of the reqOpenOrders() !

I am not anywhere near my R code right now but as a first step, you should look at the sourcecode of the

eWrapper()

function/object. One of its purposes is to read events that come back from the API and put the data containted in them (i.e. the execution price) into a format that you can use. But the unmodified eWrapper will, by default only 'cat' the data to the console. I cannot verify this now but i think even reqOpenOrders() may use an 'eWrapper' object to produce the output that you have attached below. Just look at the source of reqOpenOrders by typing 'reqOpenOrders' at the R prompt, without the brackets.

hth

Soren

http://censix.com
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: IBrokers and fill Price

harrisonmark1
Soren -

Thanks for the response.  I am not sure I have a good handle on the
'eWrapper' or what I should be doing with it so any more help there
would be appreciated.

Having said that I have been looking at code and trying reqExecutions.
 Is the reqExecutions supposed to return anything?  The IBrokers
reference seems to imply reqExecutions is not going to return
anything.  Is this a case of needing to run the below followed by a
twsCALLBACK?

# reqExecutions test

tmpExFilter <- twsExecutionFilter(clientId = '2', , symbol = symbol,
exchange = "NYMEX")
reqExecutions(tws, reqId = id, ExecutionFilter = tmpExFilter)

On Mon, May 7, 2012 at 6:46 AM, soren wilkening <[hidden email]> wrote:

> For getting the actual fill price you should use
>
> reqExecutions()
>
> instead of
>
> reqOpenOrders()
>
> I am even surprised to hear that for a MKT order, there is price information
> containted in the result of the reqOpenOrders() !
>
> I am not anywhere near my R code right now but as a first step, you should
> look at the sourcecode of the
>
> eWrapper()
>
> function/object. One of its purposes is to read events that come back from
> the API and put the data containted in them (i.e. the execution price) into
> a format that you can use. But the unmodified eWrapper will, by default only
> 'cat' the data to the console. I cannot verify this now but i think even
> reqOpenOrders() may use an 'eWrapper' object to produce the output that you
> have attached below. Just look at the source of reqOpenOrders by typing
> 'reqOpenOrders' at the R prompt, without the brackets.
>
> hth
>
> Soren
>
> http://censix.com
>
> -----
> http://censix.com
> --
> View this message in context: http://r.789695.n4.nabble.com/IBrokers-and-fill-Price-tp4613710p4614595.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.

_______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: IBrokers and fill Price

soren wilkening
This post was updated on .
Note that the IB API works on the basis of requests/replies. The replies happen in the form of 'events' (strings of data) that the IB API sends back to you through the socket connection after you have issued a request. it is your responsibility to capture and process those events! Also you cannot assume that a sequence of requests A,B,C,... will lead to a sequence of replies A*,B*,C*,.... in that same order since the communication is -ansynchronous-. This means that the replies could come back in any order, i.e. B*,C*,A*,...  I also happens that a single request, like 'placeOrder' will trigger a whole zoo of different reply events, such as OPEN_ORDER events, ORDER_STATUS events, and hopefully an EXECUTION_DATA event.


some sample code attached

example.r
Loading...