楼主: wqf_cufe
10611 12

[有偿编程] 悬赏帖:quantmod为什么总是在下载批量股票数据时出错?! [推广有奖]

  • 4关注
  • 16粉丝

已卖:4079份资源

教授

89%

还不是VIP/贵宾

-

威望
0
论坛币
4283 个
通用积分
6.5000
学术水平
10 点
热心指数
17 点
信用等级
13 点
经验
565 点
帖子
730
精华
0
在线时间
2236 小时
注册时间
2009-5-26
最后登录
2021-12-29

楼主
wqf_cufe 发表于 2014-11-12 14:28:45 |AI写论文
100论坛币
我想批量地跑下面的一批股票,下载实时数据,我用的是quantmod by R。最近才发现quandl无法下载股票数据了,起码免费的是不行了。所以只剩quantmod一条路了。

我把我要下载的股票代码放在csv里面了。代码我也贴出来了。但是每次跑都会出错,我想写一个程序可以告诉我是哪只股票下载不下来。

另一个问题是我不确定我在最后那个function里面写的,也就是把每只股票的那三个数据取出来做成三列对不对。

请高手们指点!!!最好是能给我修改好能成功下载所有数据的代码。谢谢!!! stock.xlsx (8.72 KB)

  1. library("quantmod")
  2. library(stringr)   # for str_pad

  3. tickers <- c(300368, 300399, 603369, 603806, 600304, 300245) #这里请用附件里面的股票代替。
  4. stocks<-c()

  5. foo2 <- function(tickers) {
  6.   if(substr(tickers,1,1) < 6) {
  7.     return(paste(str_pad(tickers,width=6,side="left",pad="0"),"SZ",sep="."))
  8.   } else {
  9.     return(paste(tickers,"SS",sep="."))
  10.   }
  11. }

  12. stocks <- sapply(tickers,foo2)

  13. get.stockvalue <- function(stocks) {
  14.   s <- getSymbols(stocks,auto.assign=FALSE)
  15.   s$signal <- (Cl(s) - lag(Cl(s)))/lag(Cl(s))

  16.   #find out the fisrt day when the increase is less than 10%
  17.   x <- which(s$signal < 0.095)[1]
  18.   count <- x-2
  19.   highprice <- s[x,2]

  20.   yield2 <- as.numeric((as.numeric(seriesHi(s)[,2]) - s[x,2])/s[x,2]*100)
  21.   return(count, highprice, yield2)#here I want to return three column values into a matrix with 57 rows and 3 columns. Please tell me if the code is not correct here.

  22. }

  23. update_data <- sapply(stocks,get.stockvalue)
复制代码


最佳答案

xuruilong100 查看完整内容

这是修改后的代码,亲测可行
关键词:quantmod quant 股票数据 Ant Mod 股票代码 程序

沙发
xuruilong100 发表于 2014-11-12 14:28:46
  1. library("quantmod")
  2. #library(stringr)   # for str_pad

  3. tickers <- c(300368, 300399, 603369, 300245, 603806, 600000) #这里请用附件里面的股票代替。
  4. tickers <- sprintf("%d",tickers)
  5. stocks<-c()

  6. foo2 <- function(tickers)
  7. {
  8.   if(substr(tickers,1,1) != "6")
  9.   {
  10.     return(paste(tickers,"SZ",sep="."))
  11.   }else
  12.   {
  13.     return(paste(tickers,"SS",sep="."))
  14.   }
  15. }

  16. stocks <- sapply(tickers, foo2, USE.NAMES=FALSE)
  17. stocks
  18. get.stockvalue <- function(stocks)
  19. {
  20.   cat(stocks,"------- now\n")
  21.   s <- getSymbols(stocks,auto.assign=FALSE)
  22.   s$signal <- (Cl(s) - lag(Cl(s)))/lag(Cl(s))
  23.   
  24.   #find out the fisrt day when the increase is less than 10%
  25.   x <- which(s$signal < 0.095)[1]
  26.   count <- x-2
  27.   highprice <- s[x,2]
  28.   
  29.   yield2 <- as.numeric((as.numeric(seriesHi(s)[,2]) - s[x,2])/s[x,2]*100)
  30.   #return(count, highprice, yield2)#here I want to return three column values into a matrix with 57 rows and 3 columns. Please tell me if the code is not correct here.
  31.   return(list(count=count, highprice=highprice, yield2=yield2))
  32. }

  33. update_data <- sapply(stocks,get.stockvalue)
  34. head(update_data)
复制代码

这是修改后的代码,亲测可行
已有 1 人评分经验 论坛币 收起 理由
李会超 + 36 + 24 热心帮助其他会员

总评分: 经验 + 36  论坛币 + 24   查看全部评分

藤椅
hricchaya 发表于 2014-11-12 23:42:35
把程序里s <- getSymbols(stocks,auto.assign=FALSE)改成:
  s <- tryCatch({getSymbols(stocks,auto.assign=FALSE)}, error=function(e){cat(stocks, ": failed to get\n"); print(e); return})
  if(!is.xts(s)){
    return(list(count=NULL, highprice=NULL, yield2=NULL))
  }
  else{...}运行结果如图:
getsymb.jpg

板凳
wqf_cufe 发表于 2014-11-13 00:14:20
xuruilong100 发表于 2014-11-12 16:34
这是修改后的代码,亲测可行
但是我这里还是报错,Error in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m,  :
  cannot open URL 'http://chart.yahoo.com/table.csv?s=603009.SS&a=0&b=01&c=2007&d=10&e=12&f=2014&g=d&q=q&y=0&z=603009.SS&x=.csv' In addition: There were 24 warnings (use warnings() to see them)
> head(update_data)
Error in head(update_data) : object 'update_data' not found

报纸
wqf_cufe 发表于 2014-11-13 00:19:32
hricchaya 发表于 2014-11-12 23:42
把程序里s
你能把这个更新到你的code里面吗?谢谢

地板
hubifeng? 学生认证  发表于 2014-11-13 00:29:55
wqf_cufe 发表于 2014-11-13 00:14
但是我这里还是报错,Error in download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m,  :  ...
重启一下Rstudio试试

7
wqf_cufe 发表于 2014-11-13 00:46:38
hubifeng? 发表于 2014-11-13 00:29
重启一下Rstudio试试
请问如何将行列转置成三列,第一列是代码,第二列是cout,第三列是highprice,第四列是yield2?

8
wqf_cufe 发表于 2014-11-13 00:55:54
hubifeng? 发表于 2014-11-13 00:29
重启一下Rstudio试试
已经可以下载数据了,但是有很多股票下载不了,这是为什么呢?

9
xuruilong100 发表于 2014-11-13 08:48:13
wqf_cufe 发表于 2014-11-13 00:55
已经可以下载数据了,但是有很多股票下载不了,这是为什么呢?
捕获.JPG

603009是今年7月上市的,yahoo没落之后中国方面的业务怠惰了,建议改用其他数据源,记得要加好评哦亲
已有 1 人评分经验 热心指数 收起 理由
hubifeng? + 60 + 1 热心帮助其他会员

总评分: 经验 + 60  热心指数 + 1   查看全部评分

10
hricchaya 发表于 2014-11-13 13:49:51
wqf_cufe 发表于 2014-11-13 00:19
你能把这个更新到你的code里面吗?谢谢
  1. get.stockvalue <- function(stock) {
  2.   cat(stock, ":start......\n")
  3.   s <- tryCatch({getSymbols(stock,auto.assign=FALSE)},
  4.                 error=function(e){cat(stock, ":failed to get!\nerror message:\n"); print(e)})
  5.   if(!is.xts(s)){
  6.     return(list(count=NA, highprice=NA, yield2=NA))
  7.   }
  8.   else{
  9.     cat(stock,":OK!\n")
  10.     s$signal <- (Cl(s) - lag(Cl(s)))/lag(Cl(s))
  11.   
  12.     #find out the fisrt day when the increase is less than 10%
  13.     x <- which(s$signal < 0.095)[1]
  14.     count <- x-2
  15.     highprice <- s[x,2]
  16.   
  17.     yield2 <- as.numeric((as.numeric(seriesHi(s)[,2]) - s[x,2])/s[x,2]*100)
  18.     return(list(count=count, highprice=highprice, yield2=yield2))#here I want to return three column values into a matrix with 57 rows and 3 columns. Please tell me if the code is not correct here.
  19.   }
  20. }
复制代码

您需要登录后才可以回帖 登录 | 我要注册

本版微信群
加好友,备注cda
拉您进交流群
GMT+8, 2026-1-7 04:49