R/LagFunctions.R

Defines functions LagOHLC LagOC

Documented in LagOC LagOHLC

# Function is.OC and is.HL are not included in quantmod
is.OC <- function (x)
{
  if (has.Op(x) & has.Cl(x)) {
     TRUE
  }
  else FALSE
}

is.HL <- function (x)
{
  if (has.Hi(x) & has.Lo(x)) {
     TRUE
  }
  else FALSE
}

LagOHLC <- function(TS, k=1) {
  if (!is.OHLC(TS)) {
    stop("Price series must contain Open, High, Low and Close.")
  }
  result <- cbind(stats::lag(Op(TS),k), stats::lag(Hi(TS),k), stats::lag(Lo(TS),k), stats::lag(Cl(TS),k))
  colnames(result) <- c(
    paste(colnames(Op(TS)),k,sep='.Lag.'),
    paste(colnames(Hi(TS)),k,sep='.Lag.'),
    paste(colnames(Lo(TS)),k,sep='.Lag.'),
    paste(colnames(Cl(TS)),k,sep='.Lag.'))
  return(result)
}

LagOC <- function(TS, k=1) {
  if (!is.OC(TS)) {
    stop("Price series must contain Open and Close.")
  }
  result <- cbind(stats::lag(Op(TS),k), stats::lag(Cl(TS),k))
  colnames(result) <- c(
    paste(colnames(Op(TS)),k,sep='.Lag.'),
    paste(colnames(Cl(TS)),k,sep='.Lag.'))
  return(result)
}

Try the candlesticks package in your browser

Any scripts or data that you put into this service are public.

candlesticks documentation built on Feb. 2, 2020, 3:01 a.m.