r语言 - Python rpy2 and quantmod examples



Python编程语言在开发金融数据分析应用程序方面帮助了我很多。另外,R也用于数据分析,它有专门的金融数据分析软件包,例如:quantmod。

现在,有rpy2在这两种语言之间进行接口(即Python,我想使用python的强大功能和quantmod包来创建一些金融数据分析应用程序的原型。

到目前为止,我已经花了几个小时在互联网上搜索了一些使用rpy2(python包)并调用quantmod函数的python编程语言的快速入门代码示例。到目前为止,我还没有找到合适的材料。除了rpy2 &quantmod文件。

因此问题如下=>

  1. 有没有人知道一个合适的资源/s让我开始使用python &Quantmod使用rpy2?
  2. 或者,可以有人张贴简单的示例/s python代码调用quantmod函数使用rpy2?

下面是我使用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":

 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'

getSymbols()位似乎有点非常规(R已经具有get()mget()函数)。

这个问题发生在importDefaults()(包Defaults),它本身就是一个奇怪的野兽:它回到2调用帧堆栈,为了获取函数调用的名称(所以这里是"getSymbols")。

quantmod中的getSymbols()函数包含如下代码:

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"。

在rpy2中,调用(来自Python)的R函数在R封闭框架中没有符号(因为封闭框架是Python)。在某种程度上,它就像r的匿名函数。这可能会在高级接口rpy2.robjects中得到改进,但我从来没有找到时间去做它。

一个解决方法是确保函数有一个调用帧和一个符号:
from rpy2.robjects.vectors import ListVector
base = importr("base")
base.do_call("getSymbols", ListVector({'Symbols':"IBM"}))

今天,我还测试了以下方法…即显式调用rpy2.robjects以便在python中调用R脚本。这种方法的示例如下面的python脚本中的In [5]: In [6]:行。有趣的是,这种方法似乎有效:

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>

然而,运行这样的python脚本=> quantmod.getSymbols("IBM")生成RRuntimeError !
因此,主要问题仍然没有解决!

好吧,这里有一些关于我的开发机器的环境细节:

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

最新更新