简体   繁体   English

计算时间序列的滚动最小值/最大值

[英]Calcing Rolling Min/Max Values on a time series

For the following data (tiblle is named eq_quotes)对于以下数据(tiblle 被命名为 eq_quotes)

Date        High    Low    Close    Rolling Max Rolling Min
12/16/2020  371.16  368.87  370.17  371.16      363.26
12/15/2020  369.59  365.92  369.59  371.05      363.26
12/14/2020  369.8   364.47  364.66  371.05      363.26
12/11/2020  366.74  363.26  366.3   371.05      359.17
12/10/2020  367.86  364.43  366.73  371.05      359.17
12/9/2020   371.05  365.95  366.85  371.05      359.17
12/8/2020   370.78  367.67  370.17  370.78      359.17
12/7/2020   369.62  367.72  369.09  369.85      354.87
12/4/2020   369.85  367.22  369.85  369.85      354.87
12/3/2020   368.19  365.5   366.69  368.19      354.15
12/2/2020   366.96  364.2   366.79  367.68      354.15
12/1/2020   367.68  364.93  366.02  367.68      354.15
11/30/2020  363.12  359.17  362.06  364.18      354.15
11/27/2020  364.18  362.58  363.67  364.18      354.15
11/25/2020  363.16  361.48  362.66  363.81      351.26
11/24/2020  363.81  359.29  363.22  363.81      351.26
11/23/2020  358.82  354.87  357.46  362.78      350.51
11/20/2020  357.72  355.25  355.33  364.38      350.51
11/19/2020  358.18  354.15  357.78  364.38      347.65
11/18/2020  361.5   356.24  356.28  364.38      347.65

I am trying to calculate a single rolling min/max for the three columns of data after the date column.我正在尝试为日期列之后的三列数据计算单个滚动最小值/最大值。
The width of the window would be 10 and start with the values in that row. window 的宽度将为 10,并从该行中的值开始。

The last two columns are what the data should be for that row in the data最后两列是数据中该行的数据应该是什么

I have tried multiple methods to calculate the data using RollMax trying the following:我尝试了多种方法来使用 RollMax 尝试以下方法来计算数据:

  1. Rollmax point at the 3 columns I want to use as the input Something like (deleted the code): Rollmax 指向我想用作输入的 3 列 类似(删除代码):
    rollmax(eq_quotes[,2:4],
                 k=10,
                 fill=NA,
                 align = "left")

==> Challenge is that is created a 3x # of rows matrix and did not return a single value for that row 2) Max of 3 executions of Rollmax on each of the columns ==> 挑战是创建了一个 3x # 行矩阵并且没有为该行返回单个值 2) 在每一列上最多执行 3 次 Rollmax

                max(rollmax(eq_quotes$High,
                            k=10,
                            fill=NA,
                            align = "left"),
                       rollmax(eq_quotes$Low,
                               k=10,
                               fill=NA,
                               align = "left"),
                       rollmax(eq_quotes$Close,
                               k=10,
                               fill=NA,
                               align = "left")
                       
                    )
  1. Tried to slice the data into 10 row sub-tables but still run into the challenge of how to calculate the max/min of 3 columns of data试图将数据切片为 10 行子表,但仍然遇到如何计算 3 列数据的最大值/最小值的挑战

You can try the following:您可以尝试以下方法:

library(zoo)
library(dplyr)

df %>%
  mutate(across(High:Close, ~rollapply(.x, 10, 
                min, align = 'left', partial = TRUE), .names = '{col}_min'),  
         rolling_min = pmin(High_min, Low_min, Close_min), 
         across(High:Close, ~rollapply(.x, 10, 
                max, align = 'left', partial = TRUE), .names = '{col}_max'),  
         rolling_max = pmax(High_max, Low_max, Close_max)) %>%
  select(Date:Close, rolling_min, rolling_max)

This returns:这将返回:

#         Date   High    Low  Close rolling_min rolling_max
#1  12/16/2020 371.16 368.87 370.17      363.26      371.16
#2  12/15/2020 369.59 365.92 369.59      363.26      371.05
#3  12/14/2020 369.80 364.47 364.66      363.26      371.05
#4  12/11/2020 366.74 363.26 366.30      359.17      371.05
#5  12/10/2020 367.86 364.43 366.73      359.17      371.05
#6   12/9/2020 371.05 365.95 366.85      359.17      371.05
#7   12/8/2020 370.78 367.67 370.17      359.17      370.78
#8   12/7/2020 369.62 367.72 369.09      354.87      369.85
#9   12/4/2020 369.85 367.22 369.85      354.87      369.85
#10  12/3/2020 368.19 365.50 366.69      354.15      368.19
#11  12/2/2020 366.96 364.20 366.79      354.15      367.68
#12  12/1/2020 367.68 364.93 366.02      354.15      367.68
#13 11/30/2020 363.12 359.17 362.06      354.15      364.18
#14 11/27/2020 364.18 362.58 363.67      354.15      364.18
#15 11/25/2020 363.16 361.48 362.66      354.15      363.81
#16 11/24/2020 363.81 359.29 363.22      354.15      363.81
#17 11/23/2020 358.82 354.87 357.46      354.15      361.50
#18 11/20/2020 357.72 355.25 355.33      354.15      361.50
#19 11/19/2020 358.18 354.15 357.78      354.15      361.50
#20 11/18/2020 361.50 356.24 356.28      356.24      361.50

For the 3 columns we calculate rolling min and max and then using pmin and pmax we gather one min and max for each row.对于 3 列,我们计算滚动最小值和最大值,然后使用pminpmax我们为每一行收集一个最小值和最大值。

Since the Low and Close cannot be higher than the High we only need to take the rolling maximum of the High column and similarly the rolling minimum of the Low.由于最低价和收盘价不能高于最高价,我们只需要取最高价列的滚动最大值,类似地取最低价的滚动最小值。 Also note that for any numeric vector x that min(x) = -max(-x).另请注意,对于任何数字向量 x,min(x) = -max(-x)。

library(dplyr)
library(zoo)

eq_quotes %>%
  mutate(Roll_Max = rollmax(High, 10, align = "left", fill = NA),
         Roll_Min = -rollmax(-Low, 10, align = "left", fill = NA))

Note笔记

Lines <- "
Date        High    Low    Close    Rolling_Max Rolling_Min
12/16/2020  371.16  368.87  370.17  371.16      363.26
12/15/2020  369.59  365.92  369.59  371.05      363.26
12/14/2020  369.8   364.47  364.66  371.05      363.26
12/11/2020  366.74  363.26  366.3   371.05      359.17
12/10/2020  367.86  364.43  366.73  371.05      359.17
12/9/2020   371.05  365.95  366.85  371.05      359.17
12/8/2020   370.78  367.67  370.17  370.78      359.17
12/7/2020   369.62  367.72  369.09  369.85      354.87
12/4/2020   369.85  367.22  369.85  369.85      354.87
12/3/2020   368.19  365.5   366.69  368.19      354.15
12/2/2020   366.96  364.2   366.79  367.68      354.15
12/1/2020   367.68  364.93  366.02  367.68      354.15
11/30/2020  363.12  359.17  362.06  364.18      354.15
11/27/2020  364.18  362.58  363.67  364.18      354.15
11/25/2020  363.16  361.48  362.66  363.81      351.26
11/24/2020  363.81  359.29  363.22  363.81      351.26
11/23/2020  358.82  354.87  357.46  362.78      350.51
11/20/2020  357.72  355.25  355.33  364.38      350.51
11/19/2020  358.18  354.15  357.78  364.38      347.65
11/18/2020  361.5   356.24  356.28  364.38      347.65"
eq_quotes <- read.table(text = Lines, header = TRUE)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM