Python rpy2 和 quantmod 示例 [英] Python rpy2 and quantmod examples
问题描述
Python 编程语言对我开发金融数据分析应用程序有很大帮助.或者,还有用于数据分析的 R,它有专门的财务数据分析包,例如:quantmod.
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.
现在,有 rpy2 来连接两者这些语言(即 Python 和 R).我想使用 python 的强大功能和 quantmod 包来制作一些金融数据分析应用程序的原型.
Now, that there is rpy2 to interface between both these languages(i.e. Python & R). I would like to prototype some financial data analysis applications using the power of python with the quantmod package.
到目前为止,我已经花了几个小时在互联网上搜索一些使用 rpy2(python 包)并调用 quantmod 函数的 Python 编程语言的快速入门代码示例.到目前为止,我还没有成功找到任何合适的材料......除了 rpy2 &quantmod 文档.
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.
因此问题如下 =>
- 有没有人知道合适的资源让我开始使用 python &使用 rpy2 进行 quantmod?
- 或者,有人可以发布使用 rpy2 调用 quantmod 函数的 Pythonic 代码的简单示例吗?
这是我使用 rpy2 & 实现原型的尝试.量化模型:
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")
上述代码(quantmodplot.py)的问题在于它产生如下RuntimeError":
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...
推荐答案
getSymbols()
位似乎有点不合常规(R 已经有函数 get()
和mget()
).
The getSymbols()
bit seems a little unconventional (R already has the functions get()
and mget()
).
问题发生在importDefaults()
(包Defaults
)中,它本身就是一个奇怪的野兽:它返回堆栈中的2个调用帧,以获取名称函数调用(所以这里是getSymbols").
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").
quantmod
中的函数 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")
调用 importDefaults("getSymbols)
进入调用堆栈以获取调用函数的符号,即getSymbols".
The call importDefaults("getSymbols)
goes up the calling stack in order to get the symbol of the calling function, that is "getSymbols".
在 rpy2 中,(来自 Python)调用的 R 函数在 R 封闭框架中没有符号(因为封闭框架是 Python).在某种程度上,它就像 R 的匿名函数.这可能可以在高级接口 rpy2.robjects
中改进,但我一直没有时间研究它.
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"}))
这篇关于Python rpy2 和 quantmod 示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!