0

I am trying to reproduce R results in Python. The following R code works:

library("TTR")
library("zoo")
library("xts")
library("quantmod")
getSymbols("^GSPC",from = "2014-01-01", to = "2015-01-01")
dataf = GSPC[,c("GSPC.High", "GSPC.Low", "GSPC.Close")]
result = CCI(dataf, n=20, c=0.015)

But not the following Python code:

from datetime import datetime
from rpy2.robjects.packages import importr
TTR = importr('TTR')
import pandas_datareader as pdr
from rpy2.robjects import pandas2ri
pandas2ri.activate()

GSPC = pdr.get_data_yahoo(symbols='^GSPC', start=datetime(2014, 1, 1), end=datetime(2015, 1, 1))
dataf = GSPC[['High', 'Low', 'Close']]
result = TTR.CCI(dataf, n=20, c=0.015)

The error I get occurs on the last line when using TTR.CCI. Traceback and error returned is:

Traceback (most recent call last):
File "svm_strat_test_oliver.py", line 30, in <module> result = TTR.CCI(dataf, n=20, c=0.015)
File "/usr/local/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 178, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "/usr/local/lib/python2.7/site-packages/rpy2/robjects/functions.py", line 106, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in `[.data.frame`(center, beg:NROW(x)) : undefined columns selected
Oliver Angelil
  • 1,099
  • 15
  • 31
  • 1
    The error without the traceback is often much less helpful than the full traceback. Also, try breaking down nested function calls in one-liners if unsure about what it happening. It makes it easier inspect whether each function is behaving as expected. For example, try: `dataf = GSPC[['High', 'Low', 'Close']]` and then call `TTR.CCI()` on `dataf`. – lgautier Dec 04 '16 at 16:59
  • @lgautier I have added the traceback and broken down the one-liners. – Oliver Angelil Dec 04 '16 at 19:25
  • 1
    As the error message says: rpy2 does not know what to do with pandas data frames. Check the answers for this question: http://stackoverflow.com/questions/30922213/minimal-example-of-rpy2-regression-using-pandas-data-frame – lgautier Dec 04 '16 at 21:12
  • Thanks. I activated `pandas2ri` in my preamble and tried `TTR.CCI('High~Low~Close', data=dataf, n=20, c=0.015)`, but I'm getting `rpy2.rinterface.RRuntimeError: Error in runSum(x, n) : Invalid 'n'`. In the link you provided, arguments are not passed through for the linear fit, so this might be a bit different. Would you recommend importing `TTR` with `importr` as shown in my question? – Oliver Angelil Dec 04 '16 at 21:57
  • Your call to `CCI()` in R appears to differ from the call to `CCI()` in Python... – lgautier Dec 05 '16 at 02:54
  • Yes as `'High~Low~Close', data=dataf'` is the syntax used by unutbu in the link you supplied. – Oliver Angelil Dec 05 '16 at 02:59
  • User unutbu used that syntax because that is the proper way to specify the arguments to `stats::lm`. `TTR::CCI` is not `stats::lm`, so you need to specify the arugments that `TTR::CCI` expects (as you did in your R code). – Joshua Ulrich Dec 05 '16 at 12:18
  • I have updated the question and traceback based on the comments supplied here (replaced `'High~Low~Close'` with `dataf` in the last line of Python). Thanks all, but I'm starting to think we hit a wall. – Oliver Angelil Dec 06 '16 at 10:26

1 Answers1

0

Your data.frame in the R code is actually an "xts" "zoo" object you just need to convert it to one in the python code:

rzoo = importr('zoo')
datazoo = zoo.as_zoo_xts(dataf)
result = TTR.CCI(datazoo, n=20, c=0.015)
Meow
  • 1,207
  • 15
  • 23