- library("quantmod")
- #library(stringr) # for str_pad
- tickers <- c(300368, 300399, 603369, 300245, 603806, 600000) #这里请用附件里面的股票代替。
- tickers <- sprintf("%d",tickers)
- stocks<-c()
- foo2 <- function(tickers)
- {
- if(substr(tickers,1,1) != "6")
- {
- return(paste(tickers,"SZ",sep="."))
- }else
- {
- return(paste(tickers,"SS",sep="."))
- }
- }
- stocks <- sapply(tickers, foo2, USE.NAMES=FALSE)
- stocks
- get.stockvalue <- function(stocks)
- {
- cat(stocks,"------- now\n")
- s <- getSymbols(stocks,auto.assign=FALSE)
- s$signal <- (Cl(s) - lag(Cl(s)))/lag(Cl(s))
-
- #find out the fisrt day when the increase is less than 10%
- x <- which(s$signal < 0.095)[1]
- count <- x-2
- highprice <- s[x,2]
-
- yield2 <- as.numeric((as.numeric(seriesHi(s)[,2]) - s[x,2])/s[x,2]*100)
- #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.
- return(list(count=count, highprice=highprice, yield2=yield2))
- }
- update_data <- sapply(stocks,get.stockvalue)
- head(update_data)
这是修改后的代码,亲测可行


雷达卡






京公网安备 11010802022788号







