在 For 循环中使用 quantmod 函数 getSymbols 时下载失败 [英] Download failed while using quantmod function getSymbols in For loop

查看:68
本文介绍了在 For 循环中使用 quantmod 函数 getSymbols 时下载失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 quantmod 包获取构成印度 NSE 指数的 1632 只股票的数据.我可以单独下载股票;但是,当我遍历所有股票时,出现超时.如何循环 getSymbols 函数来下载所需数据?

I am trying to get data for the 1632 stocks that comprise the NSE index in India using the quantmod package. I am able to download stocks individually; however, when I loop over all the stocks, I am getting a timeout. How do I loop the getSymbols function to download the desire data?

报如下错误:

错误:两次尝试后20MICRONS.NS"下载失败.错误信息:HTTP 错误 404.5.stop(Symbols.name, "两次尝试后下载失败.错误"," message:\n", attr(dl, "condition")$message, call.= 错误)4.getSymbols.yahoo(Symbols = "'20MICRONS.NS'", env = ,详细 = FALSE,警告 = TRUE,auto.assign = TRUE)3.do.call(paste("getSymbols.", symbol.source, sep = ""), list(Symbols = current.symbols,env = env,verbose = 详细,warnings = 警告,auto.assign = auto.assign,...))2.getSymbols(as.character(x), src = "yahoo")1.f(符号[i])

Error: '20MICRONS.NS' download failed after two attempts. Error message: HTTP error 404. 5. stop(Symbols.name, " download failed after two attempts. Error", " message:\n", attr(dl, "condition")$message, call. = FALSE) 4. getSymbols.yahoo(Symbols = "'20MICRONS.NS'", env = , verbose = FALSE, warnings = TRUE, auto.assign = TRUE) 3. do.call(paste("getSymbols.", symbol.source, sep = ""), list(Symbols = current.symbols, env = env, verbose = verbose, warnings = warnings, auto.assign = auto.assign, ...)) 2. getSymbols(as.character(x), src = "yahoo") 1. f(Symbol[i])

MyData <- read.csv(file="C:/Documents/EQUITY_L.csv", header=TRUE)
Symbol <- MyData$SYMBOL

f <- function(x) { getSymbols(as.character(x), src='yahoo') }
for (i in 1:1632) { f(Symbol[i]) }

推荐答案

好的,我明白了...

首先从以下位置下载符号:https://www.nseindia.com/corporates/content/securities_info.htm这是页面上列出的第一个文件.

First download Symbols from: https://www.nseindia.com/corporates/content/securities_info.htm It's the first file listed on the page.

看来 NSE 文件中的每个符号都需要添加.NS"后缀.这就是为什么您可以单独下载股票,但是当您将文件的 Symbol 列传递给 getSymbols 时它会失败.

It appears each symbol from the NSE file needs to add an ".NS" suffix. That's why you can download the equities individually, but it fails when you pass the Symbol column of your file to getSymbols.

我还会创建一个新环境,将所有库存转储到您的全球环境中并使其易于管理.

I'd also create a new environment to dump every stock into and keep your global environment manageable.

最后,将 NSE_Symbols 传递给 quantmods getSymbols 函数以获取每日数据.我喜欢将 sapplytry 结合使用,这样如果你碰到一个坏符号,HTTP 错误 404 不会停止下载剩余的符号.

Finally, pass NSE_Symbols to quantmods getSymbols function for daily data. I Like to use sapply coupled with try so that if you hit a bad symbol, the HTTP error 404 won't halt the remaining symbols from downloading.

EQUITY_L <- read.csv("~/R/stack-overflow/data/EQUITY_L.csv", stringsAsFactors = FALSE)

NSE_Symbols <- paste0(EQUITY_L$SYMBOL,".NS")

NSE_stocks <- new.env() 

library(quantmod)
sapply(NSE_Symbols, function(x){try(getSymbols(x, env=NSE_stocks), silent=TRUE)})

接下来,测试并找出哪些符号没有下载.除了 17 个之外,我都得到了.

Next, test and find out which symbols didn't download. I was able to get all but 17.

length(NSE_Symbols[!(NSE_Symbols %in% names(NSE_stocks))])
[1] 17

NSE_Symbols[!(NSE_Symbols %in% names(NSE_stocks))]
[1] "3PLAND.NS"     "BHAGYANGR.NS"  "CHEMFAB.NS"    "ELECTROSL.NS"  "GANGESSECU.NS" 
[6] "GMMPFAUDLR.NS" "GUJRAFFIA.NS"  "HBSL.NS"       "KALYANI.NS"    "MAGADSUGAR.NS" 
[11] "MANAKCOAT.NS"  "MCDOWELL-N.NS" "NIRAJISPAT.NS" "PALASHSECU.NS" "SIGIND.NS"
[16]"SPTL.NS"       "SUBCAPCITY.NS"

成功下载的符号将整齐地包含在 NSE_stocks 环境中.

The symbols which downloaded succesfully will be neatly contained in the NSE_stocks environment.

祝你好运,

这篇关于在 For 循环中使用 quantmod 函数 getSymbols 时下载失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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