algorithm - Find running minimum and Max in R -


i have vector of stock prices throughout day:

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

i want find 2 things. algorithm goes through day. want find how far current point historical minimum , maxim throughout day.

there function called 'mdd' in 'stocks' package finds maximum draw down throughout day (i.e. lowest value corresponds point being farthest historical maximum of day). however, don't want lowest value, want vector. have come code below that. however, need way how far point running historical minimum well.

    mddvec<-rep(na,1)    (i in 1: length(bidstock)){      mddvec[i]<-mdd(bidstock[1:i])      } 

finally, typical price calculated (max(day) + min(day) + closing price)/3. there way make work running average using running historical min , max throughout day.

thanks in advance

you need cummmin , cummax cumulative minima , maxima, can calculate how far off-minimum , maximum are, , whatever permutations like:

# in base r, 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 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 

Comments

Popular posts from this blog

javascript - Slick Slider width recalculation -

jsf - PrimeFaces Datatable - What is f:facet actually doing? -

angular2 services - Angular 2 RC 4 Http post not firing -