如何简化 quantmod 命令以加载股票数据

How to simplify quantmod commands to load stock data

如果我想加载股票数据,我是这样做的(以 Google 为例):

## most recent close price
getSymbols("GOOG")
last(GOOG)[,4]

## total equity
getFinancials("GOOG")
viewFinancials(GOOG.f, type='BS', period='A',subset = NULL)['Total Equity',1]

## Net Income
viewFinancials(GOOG.f, type='IS', period='Q',subset = NULL)['Net Income',1]

...不胜枚举。

但是如果只输入一次 GOOG 然后在其余代码中将其替换为通用名称会更实用。如何在 quantmod 中完成?

选项 auto.assign=FALSE 应该可以解决问题。

以下是您的代码的修改版本。将其扩展到更多的代码并处理它们,例如,在一个循环中应该很简单。

library(quantmod)
CollectionOfTickers <- c("GOOG")
IndexOfCurrentTicker <- 1
# the part that follows could be extracted as a function
CurrentTicker <- getSymbols(CollectionOfTickers[IndexOfCurrentTicker], auto.assign=FALSE)
Cl(last(CurrentTicker)) ## most recent close price
## total equity
CurrentTickerFinancials <- getFinancials(CollectionOfTickers[IndexOfCurrentTicker], auto.assign=FALSE)
viewFinancials(CurrentTickerFinancials, type='BS', period='A',subset = NULL)['Total Equity',1]

## Net Income
viewFinancials(CurrentTickerFinancials, type='IS', period='Q',subset = NULL)['Net Income',1]

请注意,"GOOG" 不再是硬编码的。它仅在向量 CollectionOfTickers 中定义一次,并且通过使用变量 IndexOfCurrentTicker 检索该向量的条目,该变量可以表示更大的代码集合中的循环变量。


编辑

此代码的一个变体可以像这样对多个自动收报机执行循环:

library(quantmod)
CollectionOfTickers <- c("GOOG","AAPL","TSLA","MSFT")
for (TickerName in CollectionOfTickers) {
  CurrentTicker <- getSymbols(TickerName, auto.assign=FALSE)
  cat("========\nData for ticker ", TickerName,"\n")
  ## most recent close price:
  print(Cl(last(CurrentTicker))) 
  CurrentTickerFinancials <- getFinancials(TickerName, auto.assign=FALSE)
  ## total equity:
  print(viewFinancials(CurrentTickerFinancials, type='BS', period='A',subset = NULL)['Total Equity',1])
  ## Net Income:
  print(viewFinancials(CurrentTickerFinancials, type='IS', period='Q',subset = NULL)['Net Income',1])
  cat("========\n")
}

代码质量可以通过一些进一步的重构来提高,但无论如何这应该有效。

希望对您有所帮助。

我想这就是你想要的。如果您需要其他东西...post 返回...

require(XML)
require(plyr)

getKeyStats_xpath <- function(symbol) {
  yahoo.URL <- "http://finance.yahoo.com/q/ks?s="
  html_text <- htmlParse(paste(yahoo.URL, symbol, sep = ""), encoding="UTF-8")

  #search for <td> nodes anywhere that have class 'yfnc_tablehead1'
  nodes <- getNodeSet(html_text, "/*//td[@class='yfnc_tablehead1']")

  if(length(nodes) > 0 ) {
    measures <- sapply(nodes, xmlValue)

    #Clean up the column name
    measures <- gsub(" *[0-9]*:", "", gsub(" \(.*?\)[0-9]*:","", measures))   

    #Remove dups
    dups <- which(duplicated(measures))
    #print(dups) 
    for(i in 1:length(dups)) 
      measures[dups[i]] = paste(measures[dups[i]], i, sep=" ")

    #use siblings function to get value
    values <- sapply(nodes, function(x)  xmlValue(getSibling(x)))

    df <- data.frame(t(values))
    colnames(df) <- measures
    return(df)
  } else {
    # break
    cat("Could not find",symbol,"\n")
    return(data.frame(NA))
  }
}


tickers <- c("AXP","BA","CAT","CSCO","CVX","DD","DIS","GE","GS","HD","IBM","INTC","JNJ","JPM","KO","MCD","MMM","MRK","MSFT","NKE","PFE","PG","T","TRV","UNH","UTX","V","VZ","WMT","XOM")
stats <- ldply(tickers, getKeyStats_xpath)
stats <- stats[!rowSums(is.na(stats)) == length(stats),]
rownames(stats) <- tickers
write.csv(t(stats), "FinancialStats_updated.csv",row.names=TRUE)