在 R 中使用循环和 Quantmod 收集股票信息
Collecting stock info using a loop and Quantmod in R
我想使用一个循环收集几只股票的信息,并将所有需要的信息保存到一个数据框中。我需要使用循环,因为我使用的方法(见下文)效率不高。它仅检索 select 个股票的信息并跳过一些。以下是我尝试过的:
library(quantmod)
library(TTR)
stocks <-c("MRO", "TSLA", "HAL", "XOM", "DIN", "DRI", "DENN","WEN", "SPCE", "DE", "DRI", "KSS", "AAL","DFS", "LYV","SPXL")
dataEnv <- new.env()
getSymbols(stocks, from = "2014-02-01",to= "2016-01-01", env=dataEnv)
plist <- eapply(dataEnv,Ad)
pframe <- do.call(merge, plist)
pframe1 <- as.data.frame(apply(pframe[,1:ncol(pframe)],2,function(x) diff(x)*100/head(x,-1)))
您可以使用 tidyquant 或 BatchGetSymbols 包。在处理来自雅虎的数据时,我个人的偏好是后者。
使用 tidyquant:
library(tidyquant)
stocks <-c("MRO", "TSLA", "HAL", "XOM", "DIN", "DRI", "DENN","WEN", "SPCE", "DE", "DRI", "KSS", "AAL","DFS", "LYV","SPXL")
tq_stocks <- tq_get(stocks, from = "2014-02-01",to= "2016-01-01")
tq_stocks
# A tibble: 7,245 x 8
symbol date open high low close volume adjusted
<chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 MRO 2014-02-03 32.8 32.8 32.0 32.1 8983000 28.1
2 MRO 2014-02-04 32.2 32.4 31.9 32.3 10932900 28.4
3 MRO 2014-02-05 32.3 32.4 31.6 32.1 6534500 28.1
4 MRO 2014-02-06 31.7 33.0 31.6 31.8 9408400 27.9
5 MRO 2014-02-07 31.9 32.8 31.7 32.6 8184400 28.6
6 MRO 2014-02-10 32.5 32.5 32.0 32.3 5862600 28.3
7 MRO 2014-02-11 32.3 32.9 32.3 32.7 6140400 28.7
8 MRO 2014-02-12 33.0 33.3 32.8 33.3 5202500 29.2
9 MRO 2014-02-13 33.0 33.4 32.7 33.3 6755900 29.2
10 MRO 2014-02-14 33.0 33.4 32.9 33.2 6096300 29.3
tidyquant 会给出一些警告。这些可以无视,已经开工单解决这些问题
使用 BatchGetSymbols:
library(BatchGetSymbols)
batch_stocks <- BatchGetSymbols(stocks, first.date = "2014-02-01", last.date = "2016-01-01")
str(batch_stocks)
List of 2
$ df.control: tibble [15 x 6] (S3: tbl_df/tbl/data.frame)
..$ ticker : chr [1:15] "MRO" "TSLA" "HAL" "XOM" ...
..$ src : chr [1:15] "yahoo" "yahoo" "yahoo" "yahoo" ...
..$ download.status : chr [1:15] "OK" "OK" "OK" "OK" ...
..$ total.obs : int [1:15] 483 483 483 483 483 483 483 483 483 483 ...
..$ perc.benchmark.dates: num [1:15] 1 1 1 1 1 1 1 1 1 1 ...
..$ threshold.decision : chr [1:15] "KEEP" "KEEP" "KEEP" "KEEP" ...
$ df.tickers:'data.frame': 6762 obs. of 10 variables:
..$ price.open : num [1:6762] 32.8 32.2 32.3 31.7 31.9 ...
..$ price.high : num [1:6762] 32.8 32.4 32.4 33 32.8 ...
..$ price.low : num [1:6762] 32 31.9 31.6 31.6 31.7 ...
..$ price.close : num [1:6762] 32.1 32.3 32.1 31.8 32.6 ...
..$ volume : num [1:6762] 8983000 10932900 6534500 9408400 8184400 ...
..$ price.adjusted : num [1:6762] 28.1 28.4 28.1 27.9 28.6 ...
..$ ref.date : Date[1:6762], format: "2014-02-03" "2014-02-04" "2014-02-05" "2014-02-06" ...
..$ ticker : chr [1:6762] "MRO" "MRO" "MRO" "MRO" ...
..$ ret.adjusted.prices: num [1:6762] NA 0.00873 -0.00742 -0.00903 0.02483 ...
..$ ret.closing.prices : num [1:6762] NA 0.00873 -0.00742 -0.00903 0.02483 ...
batch_stocks 将是 2 data.frames 的列表。第一个是控件 data.frame,它显示是否所有代码都已正确下载。第二个 data.frame 包含所有代码数据。 BatchGetSymbols 的一个优点是,如果您将它与 future 包结合使用,它可以 运行 并行。此外,如果您已经在本地拥有数据,则不会再次下载数据。所以运行连续3次这样,它只会下载一次数据,其余的从暂存数据中获取。
我想使用一个循环收集几只股票的信息,并将所有需要的信息保存到一个数据框中。我需要使用循环,因为我使用的方法(见下文)效率不高。它仅检索 select 个股票的信息并跳过一些。以下是我尝试过的:
library(quantmod)
library(TTR)
stocks <-c("MRO", "TSLA", "HAL", "XOM", "DIN", "DRI", "DENN","WEN", "SPCE", "DE", "DRI", "KSS", "AAL","DFS", "LYV","SPXL")
dataEnv <- new.env()
getSymbols(stocks, from = "2014-02-01",to= "2016-01-01", env=dataEnv)
plist <- eapply(dataEnv,Ad)
pframe <- do.call(merge, plist)
pframe1 <- as.data.frame(apply(pframe[,1:ncol(pframe)],2,function(x) diff(x)*100/head(x,-1)))
您可以使用 tidyquant 或 BatchGetSymbols 包。在处理来自雅虎的数据时,我个人的偏好是后者。
使用 tidyquant:
library(tidyquant)
stocks <-c("MRO", "TSLA", "HAL", "XOM", "DIN", "DRI", "DENN","WEN", "SPCE", "DE", "DRI", "KSS", "AAL","DFS", "LYV","SPXL")
tq_stocks <- tq_get(stocks, from = "2014-02-01",to= "2016-01-01")
tq_stocks
# A tibble: 7,245 x 8
symbol date open high low close volume adjusted
<chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 MRO 2014-02-03 32.8 32.8 32.0 32.1 8983000 28.1
2 MRO 2014-02-04 32.2 32.4 31.9 32.3 10932900 28.4
3 MRO 2014-02-05 32.3 32.4 31.6 32.1 6534500 28.1
4 MRO 2014-02-06 31.7 33.0 31.6 31.8 9408400 27.9
5 MRO 2014-02-07 31.9 32.8 31.7 32.6 8184400 28.6
6 MRO 2014-02-10 32.5 32.5 32.0 32.3 5862600 28.3
7 MRO 2014-02-11 32.3 32.9 32.3 32.7 6140400 28.7
8 MRO 2014-02-12 33.0 33.3 32.8 33.3 5202500 29.2
9 MRO 2014-02-13 33.0 33.4 32.7 33.3 6755900 29.2
10 MRO 2014-02-14 33.0 33.4 32.9 33.2 6096300 29.3
tidyquant 会给出一些警告。这些可以无视,已经开工单解决这些问题
使用 BatchGetSymbols:
library(BatchGetSymbols)
batch_stocks <- BatchGetSymbols(stocks, first.date = "2014-02-01", last.date = "2016-01-01")
str(batch_stocks)
List of 2
$ df.control: tibble [15 x 6] (S3: tbl_df/tbl/data.frame)
..$ ticker : chr [1:15] "MRO" "TSLA" "HAL" "XOM" ...
..$ src : chr [1:15] "yahoo" "yahoo" "yahoo" "yahoo" ...
..$ download.status : chr [1:15] "OK" "OK" "OK" "OK" ...
..$ total.obs : int [1:15] 483 483 483 483 483 483 483 483 483 483 ...
..$ perc.benchmark.dates: num [1:15] 1 1 1 1 1 1 1 1 1 1 ...
..$ threshold.decision : chr [1:15] "KEEP" "KEEP" "KEEP" "KEEP" ...
$ df.tickers:'data.frame': 6762 obs. of 10 variables:
..$ price.open : num [1:6762] 32.8 32.2 32.3 31.7 31.9 ...
..$ price.high : num [1:6762] 32.8 32.4 32.4 33 32.8 ...
..$ price.low : num [1:6762] 32 31.9 31.6 31.6 31.7 ...
..$ price.close : num [1:6762] 32.1 32.3 32.1 31.8 32.6 ...
..$ volume : num [1:6762] 8983000 10932900 6534500 9408400 8184400 ...
..$ price.adjusted : num [1:6762] 28.1 28.4 28.1 27.9 28.6 ...
..$ ref.date : Date[1:6762], format: "2014-02-03" "2014-02-04" "2014-02-05" "2014-02-06" ...
..$ ticker : chr [1:6762] "MRO" "MRO" "MRO" "MRO" ...
..$ ret.adjusted.prices: num [1:6762] NA 0.00873 -0.00742 -0.00903 0.02483 ...
..$ ret.closing.prices : num [1:6762] NA 0.00873 -0.00742 -0.00903 0.02483 ...
batch_stocks 将是 2 data.frames 的列表。第一个是控件 data.frame,它显示是否所有代码都已正确下载。第二个 data.frame 包含所有代码数据。 BatchGetSymbols 的一个优点是,如果您将它与 future 包结合使用,它可以 运行 并行。此外,如果您已经在本地拥有数据,则不会再次下载数据。所以运行连续3次这样,它只会下载一次数据,其余的从暂存数据中获取。