简体   繁体   English

Python rpy2和Quantmod示例

[英]Python rpy2 and quantmod examples

Python programming language has helped me a lot in developing financial data analysis applications. Python编程语言在开发财务数据分析应用程序方面给了我很大帮助。 Alternatively, there is the R for data analysis too, which has dedicated financial data analysis packages, for example: quantmod . 另外,也有用于数据分析的R,它具有专用的财务数据分析软件包,例如: quantmod

Now, that there is rpy2 to interface between both these languages(ie Python & R). 现在,这两种语言之间都存在rpy2接口(即Python和R)。 I would like to prototype some financial data analysis applications using the power of python with the quantmod package. 我想使用带有quantmod包的python的功能来原型化一些财务数据分析应用程序。

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. 到目前为止,我已经花了几个小时在互联网上搜索一些使用rpy2(python软件包)并调用quantmod函数的python编程语言的快速入门代码示例。 So far, I have not been successful in finding any suitable material... apart from the rpy2 & quantmod documentations. 到目前为止,除了rpy2和quantmod文档外,我还没有成功找到任何合适的材料。

Therefore the question is as follows => 因此问题如下=>

  1. Does any one know of a suitable resource/s to get me started with python & quantmod using rpy2? 有谁知道合适的资源来让我开始使用rpy2进行python&quantmod吗?
  2. Alternatively, can some one post simple example/s of pythonic code that calls quantmod functions using rpy2? 或者,是否可以发布一些使用rpy2调用quantmod函数的简单的Python代码示例?

Here is an attempt of mine in implementing a prototype using rpy2 & quantmod: 这是我尝试使用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: 上面的代码(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'

Your help will be greatly appreciated... 对你的帮助表示感谢...

The getSymbols() bit seems a little unconventional (R already has the functions get() and mget() ). getSymbols()位似乎有点不合常规(R已经具有函数get()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"). 问题发生在importDefaults() (包Defaults )(它本身是一个奇怪的野兽)中:它返回堆栈上的两个调用帧,以获取调用函数的名称(因此在此处为“ getSymbols”)。

The function getSymbols() in quantmod contains the code: 该功能getSymbols()quantmod包含的代码:

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". 调用importDefaults("getSymbols)进入调用堆栈,以获取调用函数的符号,即“ 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). 在rpy2中,称为(来自Python)的R函数在R封闭框架中没有符号(因为封闭框架是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. 从某种意义上说,它就像R的匿名函数。这可能可以在高级接口rpy2.robjects得到改善,但我从未发现有时间进行研究。

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. 今天,我还测试了以下方法...即显式调用rpy2.robjects以便在python中调用R脚本。 Example of such an approach are lines In [5]: In [6]: in the below python script. 这种方法的示例是In [5]:中的行In [5]: In [6]:中的以下python脚本中的行。 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 ! 但是,运行这样的python脚本=> quantmod.getSymbols("IBM")会产生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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM