0

In order to explain what i'm trying to do , i have 73 assets and i'm trying to backtest possible pair trading strategies. So i did test previously all possible pair combinations for cointegration and i stored them in a variable named combos, which is a 2x869 matrix containing in each column the id of both assets that are cointegrated, so if i take the first column of this variable i get (1,25) that means that the asset number 1 and number 25 in my data are cointegrated. The second variable is datats, it an xts objects, it contain the prices of my 73 assets in different dates. Now i'm trying to run a loop on them in order to backtest the pair trading strategy using the PairTrading package in R.

Here is the code using stock.price within the package.

install.packages("PairTrading", repos="http://R-Forge.R-project.org")
install.packages("plyr")
library(PairTrading)
library(plyr)
datats<-stock.price
combos <- combn(ncol(datats),2)
adply(combos, 2, function(x) {  
  price.pair <- datats[,c(x[1],x[2])]
  params <-EstimateParametersHistorically(price.pair, period = 180) 
  signal <- Simple(params$spread, 0.05)
  return.pairtrading <- Return(price.pair, lag(signal), lag(params$hedge.ratio)) 
  returnS<-(100 * cumprod(1 + return.pairtrading)) 
  out <- data.frame("return"=returnS)
  return(out)
}) 

This code works fine, the problem is when i use my own database instead of the one within the package.

So what i'm trying to do is test the strategy aka the function defined previously, with the combination stored in combos, which means for the first execution i want to test the strategy between the asset number 1 and asset number 25, then between 1 and 61 and so on, so i use datats[,c(x[1],x[2])] to do that, and i store the cumulative return of the strategy returnS<-(100 * cumprod(1 + return.pairtrading)). I tested the above function manually and it works perfectly, the problem is when i try to loop it, so my guess is the loop is working fine, but something after breaks the code.

This is the error i get:

Error in apply(merge(signal[, 1], weight.pair[, 1], return.pair[, 1],  : 
  dim(X) must have a positive length

Here is an update, the code works fine even with my data, however it seems to break in certain points, for instance, i managed to run all combinations from 1 to 400 in a single execution, however 408 seems to break the code, and when i use the combination 408 manually, the function works fine, yet in breaks when i try to loop? this error is very confusing and i've been unable to advance today because of it, kindly help if you can.

  • 2
    "And here is the error:" -- with what function call applied to what data? Please give a [mcve]. See [this](https://stackoverflow.com/a/5963610/4996248) for how to do it in R. There is evidently a mismatch between your assumptions about your data and your actual data, but since we don't know your data and can do no more than guess about your assumptions, it is hard to see how anyone could answer your question. – John Coleman Apr 03 '18 at 10:34
  • I have two variables: combos contain the "id" of the assets, for instance if i take the first column it contain 1 and 25, that means that my asset number 1 and asset number 25 are cointegrated. datats is a xts object, it contain the prices of my 73 assets, so when i use datats[,1] i get the first asset in my data, the function i'm using needs two assets and i want to test on the ones that are cointegrated, so use datats[,c(, x[1],x[2])] in order to do that. – Owen Cleberson Apr 03 '18 at 10:40
  • Click on the link in his response. If you follow the instructions people can help you more easily. – Dom Apr 03 '18 at 10:41
  • "I have two variables ... " -- good for you, but *we don't*. If you want help, take the effort to make your problem reproducible by others. – John Coleman Apr 03 '18 at 10:42
  • I edited the text, i added pictures of the two variable and i explained them, thank you. – Owen Cleberson Apr 03 '18 at 10:52
  • 2
    Something is "reproducible" if it has the property that someone can copy/paste your code into R and directly run it, getting exactly the same error that you get. An image of (parts of) a couple of your variables doesn't make it thus reproducible. There exists a substantial barrier for anyone who would want to help you. Why not remove that barrier? – John Coleman Apr 03 '18 at 10:55
  • Here is a debugging hint: make the function inside of the `adply()` call its own named function and debug that function before you use it in `adply()`. Your code is trying to do too much at once. It is hard to tell if the error lies in that function, or in the `adply()` call itself passing in arguments which don't make sense. As an added benefit, the `adply()` call would be much more readable if it looks something like `adply(combos,2,f)` (but perhaps with a more informative name than `f`). – John Coleman Apr 03 '18 at 11:01
  • I modified the code in order to make it reproducible, by using a smaller datasets contained in the library itself, now it works, so i really don't understand the error. – Owen Cleberson Apr 03 '18 at 11:07
  • Thanks -- a much better question now. I need to go to work in a few minutes so can't download the package and test, but I upvoted your question. Hopefully you will get an answer soon. – John Coleman Apr 03 '18 at 11:11
  • 1
    I'm sorry it the first time i use stackoverflow, thank you for your patience and help! – Owen Cleberson Apr 03 '18 at 11:13

0 Answers0