Python programming language has helped me a lot in developing financial data analysis applications. Alternatively, there is the R for data analysis too, which has dedicated financial data analysis packages, for example: quantmod .
Now, that there is rpy2 to interface between both these languages(ie Python & R). I would like to prototype some financial data analysis applications using the power of python with the quantmod package.
By now, I have spent several hours searching the internet for some quick starter code examples in python programming language that uses rpy2(python package) and calls quantmod functions. So far, I have not been successful in finding any suitable material... apart from the rpy2 & quantmod documentations.
Therefore the question is as follows =>
Here is an attempt of mine in implementing a prototype using rpy2 & quantmod:
from rpy2.robjects.packages import importr
sta = {"skeleton.TA": "skeleton_dot_TA", "skeleton_TA": "skeleton_uscore_TA"}
quantmod = importr('quantmod', robject_translations = sta)
IBM = quantmod.getSymbols("IBM")
Problem with the above code(quantmodplot.py) is that it produces "RuntimeError" as follows:
As of 0.4-0, ‘getSymbols’ uses env=parent.frame() and
auto.assign=TRUE by default.
This behavior will be phased out in 0.5-0 when the call will
default to use auto.assign=FALSE. getOption("getSymbols.env") and
getOptions("getSymbols.auto.assign") are now checked for alternate defaults
This message is shown once per session and may be disabled by setting
options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
Error in as.character(sc[[1]]) :
cannot coerce type 'closure' to vector of type 'character'
Traceback (most recent call last):
File "quantmodplot.py", line 6, in <module>
IBM = quantmod.getSymbols("IBM")
File "/usr/local/lib/python2.7/dist-packages/rpy2-2.3.6-py2.7-linux-i686.egg/rpy2/robjects/functions.py", line 86, in __call__
return super(SignatureTranslatedFunction, self).__call__(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/rpy2-2.3.6-py2.7-linux-i686.egg/rpy2/robjects/functions.py", line 35, in __call__
res = super(Function, self).__call__(*new_args, **new_kwargs)
rpy2.rinterface.RRuntimeError: Error in as.character(sc[[1]]) :
cannot coerce type 'closure' to vector of type 'character'
Your help will be greatly appreciated...
The getSymbols()
bit seems a little unconventional (R already has the functions get()
and mget()
).
The problem occurs in importDefaults()
(package Defaults
), itself a strange beast: it goes back 2 calling frames up the stack, in order to fetch the name of the function calling (so here "getSymbols").
The function getSymbols()
in quantmod
contains the code:
function (Symbols = NULL, env = parent.frame(), reload.Symbols = FALSE,
verbose = FALSE, warnings = TRUE, src = "yahoo", symbol.lookup = TRUE,
auto.assign = getOption("getSymbols.auto.assign", TRUE),
...)
{
# (...code cut out...)
importDefaults("getSymbols")
The call importDefaults("getSymbols)
goes up the calling stack in order to get the symbol of the calling function, that is "getSymbols".
In rpy2, an R function called (from Python) does not have a symbol in an R enclosing frame (since the enclosing frame is Python). In a way it is like an anonymous function for R. This could probably be improved in the high-level interface rpy2.robjects
, but I never found the time to work on it.
A workaround is to make sure that the function has a calling frame and a symbol:
from rpy2.robjects.vectors import ListVector
base = importr("base")
base.do_call("getSymbols", ListVector({'Symbols':"IBM"}))
Today, I have also tested the following approach... ie explicitly calling rpy2.robjects
in-order to call R script within python. Example of such an approach are lines In [5]:
In [6]:
in the below python script. Interestingly, this approach seems to work:
In [1]: from rpy2.robjects import r
In [2]: from rpy2.robjects.packages import importr
In [3]: sta = {"skeleton.TA": "skeleton_dot_TA", "skeleton_TA": "skeleton_uscore_TA"}
In [4]: quantmod = importr('quantmod', robject_translations = sta)
In [5]: IBM = r("getSymbols('IBM', src='google', from='2013-01-01')")
As of 0.4-0, ‘getSymbols’ uses env=parent.frame() and
auto.assign=TRUE by default.
This behavior will be phased out in 0.5-0 when the call will
default to use auto.assign=FALSE. getOption("getSymbols.env") and
getOptions("getSymbols.auto.assign") are now checked for alternate defaults
This message is shown once per session and may be disabled by setting
options("getSymbols.warning4.0"=FALSE). See ?getSymbol for more details
In [6]: r("candleChart(IBM, TA=NULL)")
Out[6]: <RS4 - Python:0xae3a14c / R:0xaae455c>
However, running such a python script => quantmod.getSymbols("IBM")
produces RRuntimeError
!
Hence, the primary problem still remains unresolved!
Well, here are some environmental details regarding my development machine:
OS: Ubuntu Linux 12.04
Python version: '2.7.3'
Python package: rpy2-2.3.6
R version: 3.0.0 (2013-04-03)
R package: quantmod_0.4-0
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.