Here is a relatively simple script (with comments as to the logic interspersed):
# Some of these libraries are probably not needed here, but leaving them in place harms nothing: library(tseries) library(xts) library(quantmod) library(fGarch) library(fTrading) library(ggplot2) # Set the working directory, where the data file is located, and read the raw data setwd('C:/cygwin/home/Ted/New.Task/NKS-quotes/NKS-quotes') x = read.table("quotes_M11.dat", header = FALSE, sep="\t", skip=0) str(x) # Set up the date column dt<-sprintf("%s %04d",x$V2,x$V4) dt<-as.POSIXlt(dt,format="%Y-%m-%d %H%M") # Prepare a frame that gets converted to an xts object y <- data.frame(dt,x$V5) colnames(y) <- c("tickdate","price") # Make the xts object, and then the OHLC object (as an aside, the tick data includes volume, but I have yet to figure out how to make an OHLC object hat includes volume) z <- xts(y[,2],y[,1]) alpha <- to.minutes3(z, OHLC=TRUE) colnames(alpha) <- c("Open","High","Low","Close") alpha$rel_t <- seq(1-nrow(alpha),0) # Just to check the code for the regression, apply the regression to the whole series (unless the series is realy short or has a strong slow pattern the regression result is not useful except to show that the code works) polyfit <- lm(Close ~ poly(rel_t,4),alpha) polyfit2 <- lm(Close ~ rel_t + I(rel_t^2) + I(rel_t^3) + I(rel_t^4), data=alpha) # This is the objective, where all the magic happens rollRegFun <- function(d,i) { # set up the relative time variable, so that the current record has rt = 0 d$rt <- seq(1-nrow(d),0) # apply the regression to fit a 4th degree polynomial in rt polyfit <- lm(Close ~ poly(rt,4),d) # get the coefficients p <- coef(polyfit) # get the roots of the first derivative of the fitted polynomial pr <- polyroot(c(p[2],2*p[3],3*p[4],4*p[5])) # define a function that evaluates the second derivative as a function of x dd <- function(x) { rv = 2*p[3]+6*p[4]*x+12*p[5]*x*x;rv;} # evaluate the second derivative at the ith root, and print the result r <- dd(pr[i]) r } rollRegFun(alpha,1) rollRegFun(alpha,2) rollRegFun(alpha,3) The code I show above does not give an error, but if the function is re-written as: rFun <- function(d) { d$rt <- seq(1-nrow(d),0) polyfit <- lm(Close ~ poly(rt,4),d) p <- coef(polyfit) pr <- polyroot(c(p[2],2*p[3],3*p[4],4*p[5])) dd <- function(x) { rv = 2*p[3]+6*p[4]*x+12*p[5]*x*x;rv;} r <- dd(pr[1]) r } And I try to get rollapply to execute it on a moving window, I get errors. E.g. > rollapply(as.zoo(alpha),60,rFun) Error in from:to : argument of length 0 Yet, the following works: rollapply(alpha$Close,60,mean) what do I have to do to either my function or my use of rollapply in order to get it to work? Thanks Ted [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.