2

我有以下代码:它从 csv 文件中保存历史股票数据,这正是我需要的,但是所有数据都是环境格式,我无法使用它。是否可以调整代码以便将信息保存为“数据”以便于处理。也许有一种方法可以将环境转换为更友好的格式,这可能是一个解决方案。总而言之,我有我需要的信息,但我现在不知道如何使用它 :)

#install.packages("quantmod")
library("quantmod")
#Script to download prices from yahoo
#and Save the prices to a RData file
#The tickers will be loaded from a csv file

#Script Parameters
tickerlist <- "sp500.csv"  #CSV containing tickers on rows
savefilename <- "stockdata.RData" #The file to save the data in
startDate = as.Date("2005-01-13") #Specify what date to get the prices from
maxretryattempts <- 5 #If there is an error downloading a price how many times to retry

#Load the list of ticker symbols from a csv, each row contains a ticker
stocksLst <- read.csv("sp500.csv", header = F, stringsAsFactors = F)
stockData <- new.env() #Make a new environment for quantmod to store data in
nrstocks = length(stocksLst[,1]) #The number of stocks to download

#Download all the stock data
for (i in 1:nrstocks){
for(t in 1:maxretryattempts){

   tryCatch(
       {
           #This is the statement to Try
           #Check to see if the variables exists
           #NEAT TRICK ON HOW TO TURN A STRING INTO A VARIABLE
           #SEE  http://www.r-bloggers.com/converting-a-string-to-a-variable-name-on-the-fly-and-vice-versa-in-r/
            if(!is.null(eval(parse(text=paste("stockData$",stocksLst[i,1],sep=""))))){
                #The variable exists so dont need to download data for this stock
                #So lets break out of the retry loop and process the next stock
                #cat("No need to retry")
                break
            }

          #The stock wasnt previously downloaded so lets attempt to download it
          cat("(",i,"/",nrstocks,") ","Downloading ", stocksLst[i,1] , "\t\t Attempt: ", t , "/", maxretryattempts,"\n")
          getSymbols(stocksLst[i,1], env = stockData, src = "yahoo", from = startDate)
       }
    #Specify the catch function, and the finally function
   , error = function(e) print(e))
 }
}

#Lets save the stock data to a data file
tryCatch(
{
save(stockData, file=savefilename)
cat("Sucessfully saved the stock data to %s",savefilename)
}
, error = function(e) print(e))
4

3 回答 3

1

为您的数据创建新环境的过程 ( stockData <- new.env()) 是否是此功能的典型过程?

您的for循环实际上并没有分配任何对象,它只是打印getSymbols. 您可以将这些存储在列表中,即

stockData[i] <- getSymbols(<stuff>)

另外:考虑以整齐的格式( )tidyquant存储相同结果的较新包: https ://github.com/mdancho84/tidyquanttibble

于 2017-01-24T23:04:49.433 回答
1

设置e包含 IBM 和 MSFT 的测试环境。然后给定这些股票的向量加上 GOOG 和 TSLA 用于setdiff排除已经在e下载剩余部分的股票:

# test data
e <- new.env()
stks <- c("IBM", "MSFT", "GOOG", "TSLA")
getSymbols(stks[1:2], env = e)

# run
rest <- setdiff(stks, ls(e))
getSymbols(rest, env = e)
于 2017-01-24T23:05:13.680 回答
0

如果我理解这个问题,您最终会尝试获取指数中每只股票的股价。这是包的一个很好的用例,tidyquant因为包已经内置了错误处理。错误作为 NAs 返回,并从结果中删除,同时向用户提供警告消息。

tidyquant包中尝试这个,v0.3.0(当前 CRAN 版本):

    library(tidyquant)
    sp_500 <- tq_get("SP500", get = "stock.index") %>%
        tq_get(get = "stock.prices")

请注意,上面的代码块正在更改。我们不赞成使用不同索引函数的get = "stock.index"选项, . 以下是下一个 CRAN 版本中的内容,以及在开发版本v0.3.0.9030中发布的内容:tq_get()tq_index()

    library(tidyquant)
    sp_500 <- tq_index("SP500") %>%
        tq_get(get = "stock.prices", complete_cases = TRUE)

默认值为complete_cases = TRUE. complete_cases = FALSE如果您想返回带有 NA 的值,请进行更改。请注意,如果由于符号不正确而无法检索股票价格,则会返回 NA。如果存在 NA,您将获得一个“嵌套”数据框和一条警告消息,说明哪个符号有错误。

于 2017-01-27T15:58:57.657 回答