[R-SIG-Finance] Vectorized local min/max finding

Patrick Burns patrick at burns-stat.com
Fri Mar 23 11:23:58 CET 2012

1) The 'rle' function is likely to help you.

rle(diff > 0)

2) You are likely to hear about this question
not being finance specific so other venues
would be more appropriate.


On 23/03/2012 10:07, Paolo Giusti wrote:
> I am trying to find a vectorized solution (if there is one) to a simple problem.
> I have a vector of values representing the close prices of a security
> and another vector of their moving average.
> I construct a simple "distance" object of their difference:
> diff<- close - ma
> Now I would like to calculate some simple statistics like:
> 1. The number of times the diff crosses the '0' line
> 2. The duration between each cross (number of samples)
> 3. The min and max values of diff between each cross.
> This is easy to do using "for loops" but really slow. Given this is a
> really basic problem I'm hoping that there a faster solution.
> Thank you
> Paolo
> _______________________________________________
> R-SIG-Finance at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only. If you want to post, subscribe first.
> -- Also note that this is not the r-help list where general R questions should go.

Patrick Burns
patrick at burns-stat.com
twitter: @portfolioprobe

More information about the R-SIG-Finance mailing list