[R-sig-finance] Price Smoothing

BBands bbands at gmail.com
Tue May 10 16:53:45 CEST 2005


On 5/10/05, ManojW <manojsw at gmail.com> wrote:
> Dear All,
>     I want to achieve the follwing:
> 
>     For close to 2000 securities (having 10 years of daily closing prices),
> I want to perform a 65-days exponential smoothing of prices.
> 
>     What would be the fastest way of performing the above task? Is there any
> package that can readily achieve this task?
> 
>     I have tried to use HoltWinters (alpha = 0.20, beta = 0, gamma= 0) in a
> recursive loop fashion but the whole processing is very slow (as expected)
> hence this request.
> 
>     Thanks in advance.
> 
> Regards
> 
> Manoj

Here is a routine that Dirk contributed to the Crusher Project to
compute Bollinger Bands that illustrates the basic technique for
smoothing quite well.

computeBollingerBands <- function(dat, ndays=20, nsd=2) {
    # compute Bollinger Bands
    weights <- rep(1/ndays, ndays) # create a normalised weight vector
    # and apply it as a one-sided moving average calculations, see help(filter)
    bbmiddle <- as.vector(filter(dat$Close, weights,
  		method="convolution", side=1))
    # use var(x) = E(x^2) - E(x)^2 to compute rolling variances
    v <- filter(dat$Close^2, weights, method="convolution", side=1) - bbmiddle^2
    # from which we calculate rolling standard deviations the usual way
    bbsd <- as.vector(sqrt(v))
    bbupper <- bbmiddle + nsd*bbsd # upper Bollinger band
    bblower <- bbmiddle - nsd*bbsd # lower Bollinger band
    # now extend the data frame with a few new columns
    cbind(dat, bbmiddle, bbsd, bbupper, bblower)
}

Just change the weight vector to a exponential vector and away you go.

Hope that helps,

    jab
-- 
John Bollinger, CFA, CMT
www.BollingerBands.com

If you advance far enough, you arrive at the beginning.



More information about the R-sig-finance mailing list