R/localMinMax.R

Defines functions localMinMax

Documented in localMinMax

#' Function to find the local minimum and maximum of a time series
#' @param x the time sereis
#' @param  stepT the moving window to find the local min and max
#' @return a two elements list, the first is the local min time index and the second is local max time index
#' @export
localMinMax<- function(x,stepT){
  timeX<- time(x)
  t_minP<- c()
  t_maxP<- c()
  for(i in (1+stepT):(length(timeX)- stepT)){
    timeSlice<- timeX[(i- stepT):(i +stepT)]
    x_temp<- x[timeSlice,]
    if(prod(as.vector(x_temp)>=as.vector(x_temp[timeSlice[stepT+1],]))){
      t_minP<- append(timeX[i],t_minP)
    }
    if(prod(as.vector(x_temp)<=as.vector(x_temp[timeSlice[stepT+1],]))){
      t_maxP<- append(timeX[i],t_maxP)
    }
  }
  return(list(timeX,t_minP,t_maxP))
}
zlfccnu/econophysics documentation built on Feb. 23, 2022, 10:22 p.m.