按特定功能筛选股票列表

Screening list of stocks by a specific function

我有这个样本股票列表:

stock.list = c("AMZN","GOOGL","AAPL","FB","TSLA")

我有以下功能:

fnct<-function(x){
require(quantmod)
ticker <- getSymbols(x,src="google", auto.assign=FALSE)
#number of rows
n<-nrow(ticker)

# last day's close
lastvalue <- as.numeric((ticker[n,4]))

# previous day's close
prevvalue <- as.numeric((ticker[n-1,4]))

# price change
prchange <- lastvalue - prevvalue

res<-(

paste0(prchange)

)
return(res)
}

函数的输出是一个数字,表示最后一天收盘价与前一天收盘价之间的差值(本质上,输出中的正数表示股价上涨,负数表示股价上涨它下降了)

例如,使用此函数为AMZN今天returns一个正数:

 > fnct('AMZN')
[1] "17.3200000000001"

我想将该函数应用于股票列表,以便它只显示那些有积极变化的股票名称 (>0),如果可能的话,在它们旁边显示变化的数值 (本质上,消除负变化的股票)

所以理想的输出应该是这样的:

AMZN    17.3200000000001
GOOGL   14.95
...and so on

这是我目前想出来的,但行不通

for(x in stock.list) {
paste0(x,fnct(x))
}

我知道这并不能消除负面变化的股票,但这是我能走多远。即使这样似乎也行不通。

理想情况下,它应该足够稳健以跳过 NA(例如,列表中股票名称拼写错误,或列表中特定股票的数据缺失)

有什么建议吗?提前致谢。

两件事:

在for循环中,您需要使用print函数来查看粘贴语句的结果。

其次,没有理由用"paste"转换价格变化,您应该将其保留为数值。试试这个:

require(quantmod)

stock.list = c("AMZN","GOOGL","AAPL","FB","TSLA")

fnct<-function(x){
    ticker <- getSymbols(x,src="google", auto.assign=FALSE)
    n<-nrow(ticker)
    lastvalue <- as.numeric((ticker[n,4]))
    prevvalue <- as.numeric((ticker[n-1,4]))
    prchange <- lastvalue - prevvalue
    return(prchange)
}

prchanges <- sapply(stock.list, fnct)
results <- data.frame(symbol=stock.list, prchange=prchanges, stringsAsFactors=F)

results_positive <- subset(results, prchange > 0)
print(results_positive)

> print(results_positive)
      symbol prchange
AMZN    AMZN    17.32
GOOGL  GOOGL    14.95
AAPL    AAPL     0.78
FB        FB     2.03

最小的修复方法是将循环更改为...

for (x in stock.list) {
    if(as.numeric(fnct(x)) >= 0) print(paste(x, fnct(x)))
}

您的函数需要容忍拼写错误,最简单的行为是让它跳过任何未找到的符号,而不是试图猜测您要做什么。

fnct <- function(x) {
    require(quantmod)

    ticker <- suppressWarnings(tryCatch({ getSymbols(x, src="google", auto.assign=FALSE) }, error = function(e) NULL))

    if (!is.null(ticker)) {

        #number of rows
        n <- nrow(ticker)

        # last day's close
        lastvalue <- as.numeric((ticker[n,4]))

        # previous day's close
        prevvalue <- as.numeric((ticker[n-1,4]))

        # price change
        prchange <- lastvalue - prevvalue

        res <- paste0(prchange)
    } else {
        res <- "Not found"
    }

    return(res)
}

然后你的循环变成:

for (x in stock.list) {
    result <- fnct(x)
    if (result == "Not found" || (as.numeric(result) >= 0 & as.numeric(result) < 100) ) print(paste(x, result))
}

澄清一下,这是从您拥有的东西到您想要的东西的最直接方式。这不一定是最好的方法。

以下是如何使用列表和 lapply:

stock.list = list("AMZN","GOOGL","AAPL","FB","TSLA") #put tickers in a list
res <- lapply(stock.list,fnct) #use func on list
res <- lapply(res,as.numeric)  #change to numeric
res <- Map(cbind,stock.list,res) #add ticker
res <- do.call(rbind,res)       #unlist to table
res <- as.data.frame(res)
colnames(res) <- c("Stock","Change")
res[res$Change!=0,]  #show only stocks with non-zero changes

  Stock            Change
1  AMZN  17.3200000000001
2 GOOGL             14.95
3  AAPL 0.780000000000001
4    FB              2.03
5  TSLA             -2.56

获取最后价格变化的一种非常紧凑(无自定义函数,无循环)和直接的方法是使用 qmao 包,它已经具有为您提供价格框架的功能(PF) 一组符号。

library(quantmod)
library(qmao)

stock.list = c("AMZN","GOOGL","AAPL","FB","TSLA")
getSymbols(stock.list,src="google")
prices <- PF(stock.list,silent = TRUE)
priceChange <- diff(prices)
t(last(priceChange))
      2017-04-18
AMZN        1.79
GOOGL      -1.14
AAPL       -0.63
FB         -0.46
TSLA       -1.19