历史库存数据

时间:2019-06-12 05:13:45

标签: r

我想获取所有标普500股票在特定日期的收盘价。

我已经拖了SO来寻找答案,它们属于以下几种:

  1. 下载具有开始和结束日期的标准普尔特定股票-返回的收盘价高于收盘价,这需要一行连接所有股票并放慢速度,
  2. 下载具有开始和结束日期的标准普尔所有股票-始终显示“下载错误”

例如:

library(BatchGetSymbols)

first.date <- Sys.Date() - 160
last.date <- Sys.Date() - 1

all_stocks <- GetSP500Stocks()
tickers <- all_stocks$tickers

show <- BatchGetSymbols(tickers = tickers,
                     first.date = first.date,
                     last.date = last.date)

这总是返回:

"Adobe Systems Inc | yahoo (7|505) | Not Cached

- Error in download..

以此类推。

我只想要三列-代码,first.date和last.date

感谢任何帮助!

2 个答案:

答案 0 :(得分:1)

AuthorizeEndpoint用作tickers,而不是all_stocks$company

all_stocks$tickers

对我来说,尽管将具有股票行情信息的列的名称命名为library(BatchGetSymbols) tickers <- all_stocks$company show <- BatchGetSymbols(tickers = tickers, first.date = first.date,last.date = last.date) ,而将具有公司名称的列的名称命名为company,对我来说却是不合常规的。

答案 1 :(得分:0)

您可以在此处找到标准普尔500指数的成分股。

https://en.wikipedia.org/wiki/List_of_S%26P_500_companies

library(quantmod)
e <- new.env()
getSymbols("MMM;ABT;ABBV;ABMD;ACN;
ATVI;ADBE;AMD;AAP;AES;AMG;AFL;A;APD;
AKAM;ALK;ALB;ARE;ALXN;ALGN;ALLE;AGN;ADS;
LNT;ALL;GOOGL", env = e)
pframe <- do.call(merge, as.list(e))
head(pframe)

也尝试一下。

library(quantmod)

Nasdaq100_Symbols <- c('GE','PG','MSFT','AAPL','PFE','AMD','DELL')

# put all stocks in one list object    
stocks <- lapply(Nasdaq100_Symbols, getSymbols, auto.assign = FALSE)

# following is not needed but if you want to use the list for other purposes 
# it is a good practice to name all the different list objects.

# names(stocks) <- Nasdaq100_Symbols


# merge all stocks into 1 xts object
nasdaq100 <- Reduce(merge, stocks)

# fill NA's with 0
nasdaq100 <- na.fill(nasdaq100, 0)

outcomeSymbol <- "GE.Volume" # <-- used GE as that data is available in the downloaded data set

# merge outcome to data
nasdaq100 <- merge(nasdaq100, lm1 = lag(nasdaq[, outcomeSymbol], -1))

# turn into data.frame
nasdaq100_df <- data.frame(date = index(nasdaq100), coredata(nasdaq100))

最后,尝试使用此代码。

library(rvest)
url <- "https://en.wikipedia.org/wiki/List_of_S%26P_500_companies"
SP500 <- url %>%
  html() %>%
  html_nodes(xpath='//*[@id="mw-content-text"]/div/table[1]') %>%
  html_table()
SP500 <- SP500[[1]]
SP500

作为替代方法,请参见下面的链接,以获得有关如何执行此操作的更多想法。

https://www.r-bloggers.com/downloading-sp-500-stock-data-from-googlequandl-with-r-command-line-script/

https://www.business-science.io/investments/2016/10/23/SP500_Analysis.html

https://www.business-science.io/investments/2016/11/30/Russell2000_Analysis.html