在 R 中使用 Alphavantage 的 SP100 循环
Loop of SP100 using Alphavantage in R
我即将写我的经济学学士论文。为此,我需要一个包含 S&P100 中所有公司每日股价的数据集。我已经完成了这项工作,将每家公司写在单独的一行中并使用 alphavantage。
谁能帮我用循环写下面的代码?
library(alphavantager)
av_api_key("KEY")
args(av_get)
AAPL <- av_get(av_fun="TIME_SERIES_DAILY", symbol="AAPL", outputsize = "full")
ABBV <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ABBV", outputsize = "full")
ABT <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ABT", outputsize = "full")
ACN <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ACN", outputsize = "full")
AGN <- av_get(av_fun="TIME_SERIES_DAILY", symbol="AGN", outputsize = "full")
Sys.sleep(20)
AIG <- av_get(av_fun="TIME_SERIES_DAILY", symbol="AIG", outputsize = "full")
ALL <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ALL", outputsize = "full")
您可以使用 lapply
一次性将所有内容放入 1 个巨大的列表中。我在下面举了一个例子,其中有 2 只股票是如何运作的。如果出现计时问题,您可以在 lapply
函数中加入睡眠。如果之后你想要一个巨人 data.frame 中的所有内容,你可以使用 dplyr 中的 bind_rows
将列表中的所有 data.frame 组合成 1 个巨人 data.frame 进一步 data.manipulation.
library(alphavantager)
av_api_key("KEY")
symbols <- c("AAPL", "ABBV")
my_list_of_stocks <- lapply(symbols, function(x) av_get(symbol = x, av_fun = "TIME_SERIES_DAILY", outputsize = "full"))
names(my_list_of_stocks) <- symbols
str(my_list_of_stocks)
List of 2
$ AAPL:Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 5302 obs. of 6 variables:
..$ timestamp: Date[1:5302], format: "1998-01-02" "1998-01-05" "1998-01-06" "1998-01-07" ...
..$ open : num [1:5302] 13.6 16.5 15.9 18.8 17.4 ...
..$ high : num [1:5302] 16.2 16.6 20 19 18.6 ...
..$ low : num [1:5302] 13.5 15.2 14.8 17.3 16.9 ...
..$ close : num [1:5302] 16.2 15.9 18.9 17.5 18.2 ...
..$ volume : num [1:5302] 6411700 5820300 16182800 9300200 6910900 ...
..- attr(*, "spec")=
.. .. cols(
.. .. timestamp = col_date(format = ""),
.. .. open = col_double(),
.. .. high = col_double(),
.. .. low = col_double(),
.. .. close = col_double(),
.. .. volume = col_double()
.. .. )
$ ABBV:Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 1529 obs. of 6 variables:
..$ timestamp: Date[1:1529], format: "2013-01-02" "2013-01-03" "2013-01-04" "2013-01-07" ...
将所有东西合并成大 data.frame。
# merge everything in one giant data.frame
library(dplyr)
my_data <- bind_rows(my_list_of_stocks, .id = "symbol")
# A tibble: 6,831 x 7
symbol timestamp open high low close volume
<chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
1 AAPL 1998-01-02 13.6 16.2 13.5 16.2 6411700
2 AAPL 1998-01-05 16.5 16.6 15.2 15.9 5820300
3 AAPL 1998-01-06 15.9 20 14.8 18.9 16182800
4 AAPL 1998-01-07 18.8 19 17.3 17.5 9300200
5 AAPL 1998-01-08 17.4 18.6 16.9 18.2 6910900
6 AAPL 1998-01-09 18.1 19.4 17.5 18.2 7915600
7 AAPL 1998-01-12 17.4 18.6 17.1 18.2 4610700
8 AAPL 1998-01-13 18.6 19.6 18.5 19.5 5686200
9 AAPL 1998-01-14 19.9 19.9 19.2 19.8 5261300
10 AAPL 1998-01-15 19.2 19.8 18.6 19.2 4993500
# ... with 6,821 more rows
我即将写我的经济学学士论文。为此,我需要一个包含 S&P100 中所有公司每日股价的数据集。我已经完成了这项工作,将每家公司写在单独的一行中并使用 alphavantage。
谁能帮我用循环写下面的代码?
library(alphavantager)
av_api_key("KEY")
args(av_get)
AAPL <- av_get(av_fun="TIME_SERIES_DAILY", symbol="AAPL", outputsize = "full")
ABBV <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ABBV", outputsize = "full")
ABT <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ABT", outputsize = "full")
ACN <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ACN", outputsize = "full")
AGN <- av_get(av_fun="TIME_SERIES_DAILY", symbol="AGN", outputsize = "full")
Sys.sleep(20)
AIG <- av_get(av_fun="TIME_SERIES_DAILY", symbol="AIG", outputsize = "full")
ALL <- av_get(av_fun="TIME_SERIES_DAILY", symbol="ALL", outputsize = "full")
您可以使用 lapply
一次性将所有内容放入 1 个巨大的列表中。我在下面举了一个例子,其中有 2 只股票是如何运作的。如果出现计时问题,您可以在 lapply
函数中加入睡眠。如果之后你想要一个巨人 data.frame 中的所有内容,你可以使用 dplyr 中的 bind_rows
将列表中的所有 data.frame 组合成 1 个巨人 data.frame 进一步 data.manipulation.
library(alphavantager)
av_api_key("KEY")
symbols <- c("AAPL", "ABBV")
my_list_of_stocks <- lapply(symbols, function(x) av_get(symbol = x, av_fun = "TIME_SERIES_DAILY", outputsize = "full"))
names(my_list_of_stocks) <- symbols
str(my_list_of_stocks)
List of 2
$ AAPL:Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 5302 obs. of 6 variables:
..$ timestamp: Date[1:5302], format: "1998-01-02" "1998-01-05" "1998-01-06" "1998-01-07" ...
..$ open : num [1:5302] 13.6 16.5 15.9 18.8 17.4 ...
..$ high : num [1:5302] 16.2 16.6 20 19 18.6 ...
..$ low : num [1:5302] 13.5 15.2 14.8 17.3 16.9 ...
..$ close : num [1:5302] 16.2 15.9 18.9 17.5 18.2 ...
..$ volume : num [1:5302] 6411700 5820300 16182800 9300200 6910900 ...
..- attr(*, "spec")=
.. .. cols(
.. .. timestamp = col_date(format = ""),
.. .. open = col_double(),
.. .. high = col_double(),
.. .. low = col_double(),
.. .. close = col_double(),
.. .. volume = col_double()
.. .. )
$ ABBV:Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 1529 obs. of 6 variables:
..$ timestamp: Date[1:1529], format: "2013-01-02" "2013-01-03" "2013-01-04" "2013-01-07" ...
将所有东西合并成大 data.frame。
# merge everything in one giant data.frame
library(dplyr)
my_data <- bind_rows(my_list_of_stocks, .id = "symbol")
# A tibble: 6,831 x 7
symbol timestamp open high low close volume
<chr> <date> <dbl> <dbl> <dbl> <dbl> <dbl>
1 AAPL 1998-01-02 13.6 16.2 13.5 16.2 6411700
2 AAPL 1998-01-05 16.5 16.6 15.2 15.9 5820300
3 AAPL 1998-01-06 15.9 20 14.8 18.9 16182800
4 AAPL 1998-01-07 18.8 19 17.3 17.5 9300200
5 AAPL 1998-01-08 17.4 18.6 16.9 18.2 6910900
6 AAPL 1998-01-09 18.1 19.4 17.5 18.2 7915600
7 AAPL 1998-01-12 17.4 18.6 17.1 18.2 4610700
8 AAPL 1998-01-13 18.6 19.6 18.5 19.5 5686200
9 AAPL 1998-01-14 19.9 19.9 19.2 19.8 5261300
10 AAPL 1998-01-15 19.2 19.8 18.6 19.2 4993500
# ... with 6,821 more rows