I'm trying to place an OCO order on a Financial advisor account using ibrokers and R.
How do I place an OCO order? How could I include a stop and take profit with each leg of the OCO that's also cancelled?
Thanks for any guidance!
Sample code:
Crude <- twsFuture('CL', 'NYMEX', '201505')
fiveMin <- strftime(Sys.Date(), "%Y%m%d")
fiveMin <- paste(fiveMin, "09:05:00", sep=" ")
Price <- reqHistoricalData(tws, Contract=Crude, barSize = "5 mins",
duration = "30 S", useRTH = 0,endDateTime=(fiveMin))
HighPriceStr <- toString(Price$CLK5.High)
MktHigh <- (as.numeric(HighPriceStr))
LowPriceStr <- toString(Price$CLK5.Low)
MktLow <- (as.numeric(LowPriceStr))
#calculate range width
range <- (MktHigh - MktLow)
#enter orders if 5 min range <= .50 cents
if (range <= .50){
#place oco lmt entry @ mkt high + .02, lmt sell @ mkt low - .02
#sample limit order for FA account group named Futures.
#IBrokers:::.placeOrder(twsOC, Crude, twsOrder(reqIds(tws), "SELL", "8", "LMT", lmtPrice = (Stop), faGroup ="Futures", faMethod ="EqualQuantity"))
}
Partial answer:
Interactive Brokers uses OCA orders, one cancels all.
Here's a sample:
IBrokers:::.placeOrder(twsOC, MiniCrude, twsOrder(reqIds(tws), "BUY", "3", "LMT", lmtPrice = (BreakTarget), ocaGroup = (breakerdirection), faGroup ="MiniFutures", faMethod ="EqualQuantity"))
IBrokers:::.placeOrder(twsOC, MiniCrude, twsOrder(reqIds(tws), "BUY", "3", "STP", auxPrice = (BreakStop), ocaGroup = (breakerdirection), faGroup ="MiniFutures", faMethod ="EqualQuantity"))
The second part of the question, how to place the above orders only after the limit entry has been filled.
reqExecutions()
I don't have the sample code written yet.