xtsible 对象,在 quantmod 中循环 [英] xtsible object, looping in quantmod

查看:34
本文介绍了xtsible 对象,在 quantmod 中循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想遍历股票代码列表并用 chartSeries 打印它们.这比总是改变论点要容易得多.不幸的是,当我想循环或子集时,我总是遇到错误:

I would like to loop through a list of stock symbols and print them with chartSeries. It would be easier than always changing the argument. Unfortunatly I always get an error, when I want to loop or subset:

Error in try.xts(x, error = "chartSeries requires an xtsible object"):
  chartSeries requires an xtsible object

这里是产生错误的代码:

Here the code that produces the error:

library(quantmod)
stocks <- c("FIS", "AXP", "AVB")
symbols <- (getSymbols(stocks, src='yahoo'))
for (i in symbols){
    chartSeries(i, theme="white",
        TA="addVo();addBBands();addCCI();addSMA(20, col='blue');
        addSMA(5, col='red');addSMA(50, col='black')", subset='last 30 days')     
}

或仅:

  chartSeries(symbols[1], theme="white",
      TA="addVo();addBBands();addCCI();addSMA(20, col='blue');
      addSMA(5, col='red');addSMA(50, col='black')", subset='last 30 days')

推荐答案

symbols 是一个字符向量.它不是 xts 对象的列表.对字符向量调用 chartSeries 会导致错误.

symbols is a character vector. It's not a list of xts objects. Calling chartSeries on a character vector causes the error.

R> chartSeries("OOPS")
Error in try.xts(x, error = "chartSeries requires an xtsible object") : 
  chartSeries requires an xtsible object

一种解决方案是将所有下载的数据放在一个环境中,然后对环境中的每个对象调用chartSeries.

One solution is to put all the downloaded data into one environment, then call chartSeries on every object in the environment.

library(quantmod)
stocks <- c("FIS", "AXP", "AVB")
stockEnv <- new.env()
symbols <- getSymbols(stocks, src='yahoo', env=stockEnv)
for (stock in ls(stockEnv)){
    chartSeries(stockEnv[[stock]], theme="white", name=stock,
        TA="addVo();addBBands();addCCI();addSMA(20, col='blue');
        addSMA(5, col='red');addSMA(50, col='black')", subset='last 30 days')     
}

这篇关于xtsible 对象,在 quantmod 中循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆