0

I am trying to test an intraday trading strategy using R, and I found the IBrokers API package that sends orders to Interactive Brokers TWS platrform, which would be perfect for me. Unfortunately my programming skills aren´t good enought to get the job done!

In any case, my strategy would be looking at the market depth placing orders above and below the market at prices that depend on the size of the bids and ask.

From what I have learned until now, I beleive the reqMktDepth function creates an infinite loop that updates the bids and asks through the twsCALLBACK.

What I tried to do is to create a function, called callback.intraday that does my trading strategy and subsequently calls the twsCALLBACK, from what I understood I should use the reqMktDepth and use my function inside it.

Below is the code of my function:

callback.intraday <- function(numoflots=50, file=f, lenghtofdepth=10, target=0.30, mintick=0.01){
  csvfile <- na.omit(read.delim(file=file, sep=" ", stringsAsFactors = FALSE, col.names=c("date", "time", "NA", "NA", "NA", "position", "operation", "bid.ask", "price", "size", "NA")))
  csvfile <- csvfile[1:(lenghtofdepth*2),]
  csvfile <- csvfile[order(csvfile$position),]

  #0 is ask, 1 is bid
  askdepth <- biddepth<- 0  
  for (i in 1:nrow(csvfile)){

    if(!csvfile$position[i]==0 && csvfile$bid.ask[i]==1 && askdepth<numoflots){
      askdepth <- askdepth+as.numeric(csvfile$size[i])
      buyprice <- as.numeric(csvfile$price[i])-mintick
    }

    if(!csvfile$position[i]==0 && csvfile$bid.ask[i]==0 && biddepth<numoflots){
      biddepth <- biddepth+as.numeric(csvfile$size[i])
      sellprice <- as.numeric(csvfile$price[i])+mintick
    }


  }


  #entering buyside
  #ids <- as.numeric(reqIds(tws))
  #entorder <- twsOrder(ids,"BUY", "1", "STPLMT", lmtPrice = buyprice, auxPrice = buyprice+2*mintick)
  #ids <- as.numeric(reqIds(tws))
  #tarorder <- twsOrder(ids,"SELL", "1", "LMT", lmtPrice = buyprice+target)
  #placeOrder(twsconn=tws, Contract=security, Order=entorder)
  #placeOrder(twsconn=tws, Contract=security, Order=tarorder)

  #entering sellside
  #ids <- as.numeric(reqIds(tws))
  #entorder <- twsOrder(ids,"SELL", "1", "STPLMT", lmtPrice = sellprice, auxPrice = sellprice+2*mintick)
  #ids <- as.numeric(reqIds(tws))
  #tarorder <- twsOrder(ids,"BUY", "1", "LMT", lmtPrice = sellprice-target)
  #placeOrder(twsconn=tws, Contract=security, Order=entorder)
  #placeOrder(twsconn=tws, Contract=security, Order=tarorder)

  twsCALLBACK(twsCon = tws)
}

I still haven´t tested the sending the orders into the market since I am still stuck with the loop.

Below is the code that I am running and getting an "unused arguments" error.

    require(IBrokers)
tws <- twsConnect(port=7496)
isConnected(tws)
symb <- "CL"
exchange <- "NYMEX"
expiry <- "201804"
f <- "C:/Users/augus/Dropbox/Trading/R/Trading/Intraday/depth.csv"
contract <- twsFuture(symbol=symb, exch=exchange, expiry=expiry)

reqMktDepth(tws, contract, file=f, CALLBACK = callback.intraday)

Thanks for the help

Augusto

4

0 回答 0