在 R 中查找运行最小值和最大值 [英] Find running minimum and Max in R

查看:45
本文介绍了在 R 中查找运行最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一整天的股票价格向量:

I have a vector of stock prices throughout the day:

> head(bidStock)
         [,1]
[1,] 1179.754
[2,] 1178.000
[3,] 1178.438
[4,] 1178.367
[5,] 1178.830
[6,] 1178.830

我想找到两件事.就像我一样,算法一天天过去.我想让它找出当前点与全天历史最小值和最大值之间的距离.

I want to find two things. As I the algorithm goes through the day. I want it to find how far the current point is from the historical minimum and maxim throughout the day.

'stocks' 包中有一个名为 'mdd' 的函数,它可以找到全天的最大跌幅(即对应于离当天历史最大值最远的点的最低值).但是,我不想要最低值,我想要一个向量.我想出了下面的代码来做到这一点.但是,我还需要一种方法来处理一个点与运行历史最小值之间的距离.

There is a function called 'mdd' in the 'stocks' package which finds the maximum draw down throughout the day (i.e. the lowest value which corresponds to a point being the farthest from the historical maximum of the day). However, I don't want just the lowest value, I want a vector. I have come up with the code below to do that. However, I need a way to do with how far a point is from the running historical minimum as well.

    mddVec<-rep(NA,1)
   for (i in 1: length(bidStock)){
     mddVec[i]<-mdd(bidStock[1:i])
     }

最后,典型价格由(最大(天)+最小(天)+收盘价)/3计算.有没有一种方法可以使用全天运行历史最小值和最大值,使其像运行平均值一样工作.

Finally, typical price is calculated by (max(day) + min(day) + closing price)/3. Is there a way to make that work like a running average using running historical min and max throughout the day.

提前感谢您的帮助

推荐答案

你只需要 cummmincummax 用于累积最小值和最大值,从中你可以计算出多远非最小值和最大值,以及您喜欢的任何排列:

You just need cummmin and cummax for cumulative minima and maxima, from which you can calculate how far off-minimum and maximum you are, and whatever permutations you like:

# in base R, as data.frame
df <- data.frame(price = bidStock, 
                 min = cummin(bidStock), 
                 max = cummax(bidStock))
df$off_min <- df$price - df$min
df$off_max <- df$price - df$max
df$typical_price <- (df$price + df$min + df$max) / 3    # using price for closing price

df
##      price      min      max off_min off_max typical_price
## 1 1179.754 1179.754 1179.754   0.000   0.000      1179.754
## 2 1178.000 1178.000 1179.754   0.000  -1.754      1178.585
## 3 1178.438 1178.000 1179.754   0.438  -1.316      1178.731
## 4 1178.367 1178.000 1179.754   0.367  -1.387      1178.707
## 5 1178.830 1178.000 1179.754   0.830  -0.924      1178.861
## 6 1178.830 1178.000 1179.754   0.830  -0.924      1178.861

# or in dplyr
library(dplyr)

data.frame(price = bidStock) %>% 
    mutate(min = cummin(bidStock), 
           max = cummax(bidStock), 
           off_min = price - min, 
           off_max = price - max,
           typical_price = (price + min + max) / 3)
##      price      min      max off_min off_max typical_price
## 1 1179.754 1179.754 1179.754   0.000   0.000      1179.754
## 2 1178.000 1178.000 1179.754   0.000  -1.754      1178.585
## 3 1178.438 1178.000 1179.754   0.438  -1.316      1178.731
## 4 1178.367 1178.000 1179.754   0.367  -1.387      1178.707
## 5 1178.830 1178.000 1179.754   0.830  -0.924      1178.861
## 6 1178.830 1178.000 1179.754   0.830  -0.924      1178.861

这篇关于在 R 中查找运行最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆