如何绕过使用不同日期的重复函数?
How do I get around recurring functions that use different dates?
我需要我的函数在股票的某个时期工作,在示例中,AKER["2013-11-19/2018-11-19"]
,从 2013 年 10 月到 2018 年 10 月。然后再次工作,但这次离我设置的日期,像这样 AKER["2014-11-19/2018-11-19"]
。然后又一次。再一次。
That's what I got:
resistence_line_by_volume <- function(x) {
open_prices <- x[,1]
close_prices <- x[,4]
volume_amount <- x[,5]
average_open_and_close <- (open_prices + close_prices)/2
weighet_price_volume <- (average_open_and_close*volume_amount)/sum(volume_amount)
result <- sum(weighet_price_volume)
result
}
getSymbols("AKER")
[1] "AKER"
resistence_line_by_volume(AKER["2013-11-19/2018-11-19")
[1] 3.353938
resistence_line_by_volume(AKER["2014-11-19/2018-11-19")
[1] 3.319899
resistence_line_by_volume(AKER["2015-11-19/2018-11-19")
[1] 3.290728
resistence_line_by_volume(AKER["2016-11-19/2018-11-19")
[1] 3.256264
resistence_line_by_volume(AKER["2017-11-19/2018-11-19")
[1] 3.191081
And that's what I need (some version of that):
resistence_line_by_volume(AKER["2013-11-19/2018-11-19")
[1] 3.353938
[2] 3.319899
[3] 3.290728
[4] 3.256264
[5] 3.191081
如何将所有这些功能每次都缩短一年?
如果日期数量有限,我们可以手动创建一个日期向量
library(quantmod)
dates <- c("2013-11-19/2018-11-19","2014-11-19/2018-11-19","2015-11-19/2018-11-19",
"2016-11-19/2018-11-19", "2017-11-19/2018-11-19")
然后使用任何循环技术遍历 dates
(sapply
、lapply
、map
、for
循环等)
sapply(dates, function(x) resistence_line_by_volume(AKER[x]), USE.NAMES = FALSE)
#[1] 3.327881 3.294591 3.266057 3.232329 3.168454
或者我们也可以使用 seq
以编程方式生成日期
dates <- paste(seq(as.Date("2013-11-19"), length.out = 5, by = "year"),
"2018-11-19", sep = "/")
sapply(dates, function(x) resistence_line_by_volume(AKER[x]), USE.NAMES = FALSE)
#[1] 3.327881 3.294591 3.266057 3.232329 3.168454
我需要我的函数在股票的某个时期工作,在示例中,AKER["2013-11-19/2018-11-19"]
,从 2013 年 10 月到 2018 年 10 月。然后再次工作,但这次离我设置的日期,像这样 AKER["2014-11-19/2018-11-19"]
。然后又一次。再一次。
That's what I got:
resistence_line_by_volume <- function(x) {
open_prices <- x[,1]
close_prices <- x[,4]
volume_amount <- x[,5]
average_open_and_close <- (open_prices + close_prices)/2
weighet_price_volume <- (average_open_and_close*volume_amount)/sum(volume_amount)
result <- sum(weighet_price_volume)
result
}
getSymbols("AKER")
[1] "AKER"
resistence_line_by_volume(AKER["2013-11-19/2018-11-19")
[1] 3.353938
resistence_line_by_volume(AKER["2014-11-19/2018-11-19")
[1] 3.319899
resistence_line_by_volume(AKER["2015-11-19/2018-11-19")
[1] 3.290728
resistence_line_by_volume(AKER["2016-11-19/2018-11-19")
[1] 3.256264
resistence_line_by_volume(AKER["2017-11-19/2018-11-19")
[1] 3.191081
And that's what I need (some version of that):
resistence_line_by_volume(AKER["2013-11-19/2018-11-19")
[1] 3.353938
[2] 3.319899
[3] 3.290728
[4] 3.256264
[5] 3.191081
如何将所有这些功能每次都缩短一年?
如果日期数量有限,我们可以手动创建一个日期向量
library(quantmod)
dates <- c("2013-11-19/2018-11-19","2014-11-19/2018-11-19","2015-11-19/2018-11-19",
"2016-11-19/2018-11-19", "2017-11-19/2018-11-19")
然后使用任何循环技术遍历 dates
(sapply
、lapply
、map
、for
循环等)
sapply(dates, function(x) resistence_line_by_volume(AKER[x]), USE.NAMES = FALSE)
#[1] 3.327881 3.294591 3.266057 3.232329 3.168454
或者我们也可以使用 seq
dates <- paste(seq(as.Date("2013-11-19"), length.out = 5, by = "year"),
"2018-11-19", sep = "/")
sapply(dates, function(x) resistence_line_by_volume(AKER[x]), USE.NAMES = FALSE)
#[1] 3.327881 3.294591 3.266057 3.232329 3.168454