函数getSymbols()可以采用变量而不是自动收报机吗?

时间:2021-07-15 16:28:55

I want to loop through a CSV of some companies tickers and download their respective ticker data.

我想循环一些公司代码的CSV并下载各自的股票代码数据。

So far I have defined the variable 'ticker' as follows:

到目前为止,我已将变量'ticker'定义如下:

ticker <- companyList[1, 'Symbol']

when I print 'ticker' to the screen, it shows the 1st stocks ticker perfectly fine, but when I try to pass the ticker variable into quantmod's getSymbols function, I get an error.

当我在屏幕上打印“自动收报机”时,它显示第1个股票行情完好无损,但是当我尝试将自动收报机变量传递给quantmod的getSymbols函数时,我收到错误。

getSymbols(ticker)
Error in do.call(paste("getSymbols.", symbol.source, sep = ""), list(Symbols =   current.symbols;  : 
 could not find function "getSymbols.940"

Is there an error in my syntax? Is there another way to go about doing this? Any book reccomendations?

我的语法有错误吗?还有另一种方法可以做到这一点吗?任何书籍推荐?

1 个解决方案

#1


1  

The easiest way is to specify an environment, for example assuming you have an vector with tickers, than you can do the following:

最简单的方法是指定一个环境,例如假设你有一个带有代码的向量,而不是你可以执行以下操作:

# tickers example
tik <- c("MMM", "ABT", "ABBV", "ANF", "ACT")

# new environment
stockData <- new.env() 

# download data
getSymbols(tik, env = stockData)

Than you can index to access the stocks: eg:

您可以索引访问股票:例如:

stockData$MMM

           MMM.Open MMM.High MMM.Low MMM.Close MMM.Volume MMM.Adjusted
# 2007-01-03    77.53    78.85   77.38     78.26    3781500        65.42
# 2007-01-04    78.40    78.41   77.45     77.95    2968400        65.16
# 2007-01-05    77.89    77.90   77.01     77.42    2765200        64.72
# 2007-01-08    77.42    78.04   76.97     77.59    2434500        64.86
# 2007-01-09    78.00    78.23   77.44     77.68    1896800        64.93
# 2007-01-10    77.31    77.96   77.04     77.85    1787500        65.07

same for all other stocks.

所有其他股票也一样。

See also here: Quantmod

另见:Quantmod

hth

心连心

EDIT:

编辑:

for exporting you can use something like this:

出口你可以使用这样的东西:

# this will export 
lapply(stockData, function(x){
  write.csv(x, file=paste0(gsub(".Open", "", names(x)[1]), ".csv"))
    })

# the data can be found in the folder (execute the function!!!)
getwd()

#1


1  

The easiest way is to specify an environment, for example assuming you have an vector with tickers, than you can do the following:

最简单的方法是指定一个环境,例如假设你有一个带有代码的向量,而不是你可以执行以下操作:

# tickers example
tik <- c("MMM", "ABT", "ABBV", "ANF", "ACT")

# new environment
stockData <- new.env() 

# download data
getSymbols(tik, env = stockData)

Than you can index to access the stocks: eg:

您可以索引访问股票:例如:

stockData$MMM

           MMM.Open MMM.High MMM.Low MMM.Close MMM.Volume MMM.Adjusted
# 2007-01-03    77.53    78.85   77.38     78.26    3781500        65.42
# 2007-01-04    78.40    78.41   77.45     77.95    2968400        65.16
# 2007-01-05    77.89    77.90   77.01     77.42    2765200        64.72
# 2007-01-08    77.42    78.04   76.97     77.59    2434500        64.86
# 2007-01-09    78.00    78.23   77.44     77.68    1896800        64.93
# 2007-01-10    77.31    77.96   77.04     77.85    1787500        65.07

same for all other stocks.

所有其他股票也一样。

See also here: Quantmod

另见:Quantmod

hth

心连心

EDIT:

编辑:

for exporting you can use something like this:

出口你可以使用这样的东西:

# this will export 
lapply(stockData, function(x){
  write.csv(x, file=paste0(gsub(".Open", "", names(x)[1]), ".csv"))
    })

# the data can be found in the folder (execute the function!!!)
getwd()