gpt4 book ai didi

r - 发出股价创历史新高的信号

转载 作者:行者123 更新时间:2023-12-02 21:57:45 25 4
gpt4 key购买 nike

我创建了一个虚拟股票价格 xts 对象,如下所示:

temp <- data.frame(date=as.Date("2010-01-01")+(0:99))
set.seed(1)
temp[,"price"] <- sample(1:100,nrow(temp),replace=TRUE)
# temp <- as.xts(temp[,"price"],order.by=temp[,"date"])

temp[,"record_hi"] <- FALSE

for (loop in (2:nrow(temp))) {
if (all(temp[loop,"price"]>=temp[1:loop,"price"])) {
temp[loop,"record_hi"] <- TRUE
}
}

我想创建一个信号,以便一旦价格达到历史新高,我在使用for-loop之前做了类似的事情,然后意识到我太愚蠢了,不知道xts 提供相同的输出要快得多。

如何使用 xts 创建此类信号?

最佳答案

仅第一行有所不同:

temp$record_hi2 <- temp$price == cummax(temp$price)

# date price record_hi record_hi2
# 1 2010-01-01 27 FALSE TRUE
# 2 2010-01-02 38 TRUE TRUE
# 3 2010-01-03 58 TRUE TRUE
# 4 2010-01-04 91 TRUE TRUE
# 5 2010-01-05 21 FALSE FALSE
# 6 2010-01-06 90 FALSE FALSE
# 7 2010-01-07 95 TRUE TRUE
# 8 2010-01-08 67 FALSE FALSE
# 9 2010-01-09 63 FALSE FALSE
# 10 2010-01-10 7 FALSE FALSE
# 11 2010-01-11 21 FALSE FALSE
# 12 2010-01-12 18 FALSE FALSE
# 13 2010-01-13 69 FALSE FALSE
# 14 2010-01-14 39 FALSE FALSE
# 15 2010-01-15 77 FALSE FALSE
# 16 2010-01-16 50 FALSE FALSE
# 17 2010-01-17 72 FALSE FALSE
# 18 2010-01-18 100 TRUE TRUE
# 19 2010-01-19 39 FALSE FALSE
# <snip>

关于r - 发出股价创历史新高的信号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17463963/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com