0

I use a code (link is below) to open an order in Interactive Brokers ( I use a paper account) but when I tried to close the opened order after 5 seconds I was not able to do so.What am I doing wrong?

library(IBrokers)
myconid = 3
twsobj  = twsConnect(myconid)
myaud = twsCurrency("AUD",currency="USD",exch="IDEALPRO",primary="",strike="0.0",right="",local="",multiplier="",include_expired="0",conId=myconid)
Sys.sleep(2)
myorderid = as.integer(reqIds(twsobj))
print(myorderid)
Sys.sleep(2)
# my workaround:
options("scipen"=4)
placeOrder(twsobj, myaud, twsOrder(myorderid,"SELL", 1, "MKT"))
Sys.sleep(5)
placeOrder(twsobj, myaud, twsOrder(myorderid,"BUY", 1, "MKT"))

Link that I used:[IBrokers - How I send 100000 to IBrokers:::.placeOrder?

UPDATE( Following brian's answer ): I use a code (link is below) to open an order in Interactive Brokers ( I use a paper account) but when I tried to close the opened order after 5 seconds I was not able to do so.What am I doing wrong?

 library(IBrokers)
    myconid = 3
    twsobj  = twsConnect(myconid)
    myaud = twsCurrency("AUD",currency="USD",exch="IDEALPRO",primary="",strike="0.0",right="",local="",multiplier="",include_expired="0",conId=myconid)
    Sys.sleep(2)
    print(myorderid)
    Sys.sleep(2)
    # my workaround:
    options("scipen"=4)
    placeOrder(twsobj, myaud, twsOrder(122,"SELL", 1, "MKT"))
    Sys.sleep(5)
    placeOrder(twsobj, myaud, twsOrder(123,"BUY", 1, "MKT"))
Community
  • 1
  • 1
mql4beginner
  • 2,193
  • 5
  • 34
  • 73
  • just FYI IDEALPRO has a minimum of 25000 USD so maybe that's messing things up. Also the new paper trade account defaults to port 7497, but if you've connected that's probably ok. I don't know what else is wrong, but my understanding is the IBrokers package isn't really finished and tested much. – brian Jan 05 '16 at 16:51
  • Thanks @brian you are right about the "IDEALPRO has a minimum of 25000 USD" but the real issue was the orderID as you wrote.Now the code is working fine.. – mql4beginner Jan 05 '16 at 17:38

2 Answers2

1

You need to use a different order id. They are not re-useable. You should increment by at least 1 for ever, even the next day, month etc.. When you connect, TWS returns the next valid ID. I don't know where it is in R or what reqIds returns (in java it gets a range of ids), but you need to use a unique number. Some people just use seconds from the epoch or you can look for a nextValidId() method.

Also, look for error messages, if you get "duplicate order id", you should realize your mistake.

brian
  • 10,619
  • 4
  • 21
  • 79
  • Hello @brian. Thanks for the tips,followed them and now it works. I'll do an update in question.Also, for the other readers there is an explanation in the following page: https://www.interactivebrokers.com/en/software/api/apiguide/api/api_order_ids.htm – mql4beginner Jan 05 '16 at 13:26
1

I see two things, first if you want to modify, close the open order that you just introduced you should use the same orderId to select that exact order.

Also, you introduced a MKT order, it is not limit, are you sure that the order is not executing before you can cancel it?

ibapilessons
  • 69
  • 1
  • 3