[R-sig-finance] Price Smoothing

Steven D. Moffitt steve.moffitt at mail.stuart.iit.edu
Tue May 10 14:57:59 CEST 2005


Dear ManojW,

There is a function EWMA in the RMetrics package.

You can also write a script such as the following:

expma <- function(alpha,price.vec) {
  if(missing(alpha)) stop("alpha argument missing")
  if( alpha < 0 || alpha > 1 ) stop("alpha must be between 0 and 1 inclusive")
  if(missing(price.vec)) stop("price vector argument missing")
  n <- length(price.vec)
  x <- rep(0,n)
  x[1] <- price.vec[1]
  if( n > 1 )
  {
    for( i in 2:n )
      x[i] <- alpha*price.vec[i-1] + (1-alpha)*x[i-1]
  }
  xed
}

Since loops are slow in R, it will take some time to calculate all moving averages.  But if you save them, they are easily updated via the baxic formula.  

If you really need speed, however, you should write a function in C/C++ and use in inside or outside R.

Best,

Steve Moffit




---------- Original Message ----------------------------------
From: "ManojW" <manojsw at gmail.com>
Date:  Tue, 10 May 2005 20:57:23 +0900

>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
>
>_______________________________________________
>R-sig-finance at stat.math.ethz.ch mailing list
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>



More information about the R-sig-finance mailing list