lbilli / rib

An R implementation of Interactive Brokers API
GNU General Public License v3.0
34 stars 7 forks source link

Not recognising Trigger Price Order parameter #8

Closed simonskok closed 4 years ago

simonskok commented 4 years ago

Hi, thank you for writing this great package! Its the only one on Github written in R that truly provides a comprehensive connection to the IB TWS. While looking at some of the more complicated orders, I came across the following two problems. The goal is to attach a Trailing Stop Loss order and change the trailing percent parameter when a certain threshold is breached.

However, in the example below, the Trigger Price and the corresponding trailing percent is not recognised, i.e. the order is send to IB TWS, but the trigger price column is empty.

wrap <- IBWrapCustom$new()
ic   <- IBClient$new(wrap)

ic$connect(port=7496, clientId=10)

ic$checkMsg()

ID       <- 4258

Action   <- "BUY"

Contract <- IBContract("AAPL")

 lapply(1:2, function(i) {

    if (i == 1){

    Order          <- IBOrder(totalQuantity = 10, lmtPrice = 310, action = "BUY")

    Order$orderId  <- ID + i

    Order$transmit <- FALSE

  } else if (i == 2){

    Order                        <- rib::Order

    Order$orderId                <- ID + i 

    Order$action                 <- "SELL"
    Order$totalQuantity          <- 10
    Order$orderType              <- "TRAIL"
    Order$trailingPercent        <- 0.5

    Order$parentId               <- ID + 1
    Order$triggerPrice           <- 315
    Order$adjustableTrailingUnit <- 0
    Order$adjustedTrailingAmount <- 1

    Order$transmit               <- TRUE

  }

  Order$outsideRth                    <- TRUE
  Order$overridePercentageConstraints <- TRUE

  ic$placeOrder(id = ID + i, contract = Contract, order = Order)

})

Any clues on what could cause this behaviour? The second problem is that in another example the trigger price is recognised, but it only accepts adjustableTrailingUnit = 0, which corresponds to trailing amount in nominal change, not in percentage points (adjustableTrailingUnit = 1). Contrary to the code above, this parameter always fails, i.e. attached orders with adjustableTrailingUnit = 1 are always dark in IB TWS (it displays "ticks" in the Adj_Trailing_Amount column). Going through your source sode it is not obvious why Order$adjustableTrailingUnit <- 0 is accepted whereas Order$adjustableTrailingUnit <- 1 is not. It is a small technical difficulty and the only one I haven't been able to solve by myself in an otherwise fantastic package so it would be really great if you could provide some thoughts or fixes.

lbilli commented 4 years ago

Thanks for the interest in the package. I'm no expert in these types of orders, however I'd first distinguish between fields that are used to set plain TRAIL orders, which in the TWS Order ticket window appear under "Order Description", e.g.:

orderType
auxPrice
trailingPercent
trailStopPrice
...

and those used to set Adjust TRAIL orders, which in the TWS Order ticket window appear under the "Adjust Stop / Trailing Stop" section, e.g.:

adjustedOrderType
triggerPrice
adjustedStopPrice
adjustedStopLimitPrice
adjustedTrailingAmount
adjustableTrailingUnit
...

I haven't tried it, but in your case you might need to set also order.adjustedOrderType <- "TRAIL" or similar, before the other adjusted* fields become active.

In situations like this, what I find useful is to create first the desired order directly in TWS and then from a connected R client issue:

ic$reqAllOpenOrders()

and see which fields have been affected.

simonskok commented 4 years ago

Thanks for your feedback. For some reason it is more stable to add adjustedOrderType parameter even if the order type stays the same. With regards to the adjustableTrailingUnit problem calling ic$reqAllOpenOrders() resolved the issue as the official TWS API contains an error... adjustableTrailingUnit is 100 for percentage changes and not 1 as stated in the official docs.