Vectorized local min/max finding

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

Vectorized local min/max finding

Paolo Giusti
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

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vectorized local min/max finding

Patrick Burns-2
1) The 'rle' function is likely to help you.
Try:

rle(diff > 0)

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

Pat


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
>
> _______________________________________________
> [hidden email] 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
[hidden email]
http://www.burns-stat.com
http://www.portfolioprobe.com/blog
twitter: @portfolioprobe

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vectorized local min/max finding

Matthew Clegg
In reply to this post by Paolo Giusti
Perhaps the following would work for you:

diffs <- sin(1:20)  # Some random data
crosses <- which(sign(diffs[1:(length(diffs)-1)] * diffs[2:length(diffs)])
<= 0)+1

1) length(crosses)
2) diff(crosses)
3) sapply(2:length(crosses), function(k)
min(diffs[crosses[k-1]:(crosses[k]-1)]))

Matthew Clegg

On Fri, Mar 23, 2012 at 6:07 AM, Paolo Giusti <[hidden email]>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
>
> _______________________________________________
> [hidden email] 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.
>



--
Matthew Clegg
[hidden email]

        [[alternative HTML version deleted]]

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Vectorized local min/max finding

Joshua Ulrich
In reply to this post by Paolo Giusti
Here is how I would solve the problem using xts:

library(quantmod)
getSymbols("SPY")
SPY$ma <- SMA(Cl(SPY),50)
SPY$diff <- (Cl(SPY)-SPY$ma)
SPY$diff[1:50] <- 0
SPY$group <- cumsum(c(0,diff(SPY$diff>0,na.pad=FALSE)) != 0)
SPY$grpLen <- ave(SPY$diff, SPY$group, FUN=length)
SPY$grpMin <- ave(SPY$diff, SPY$group, FUN=min)
SPY$grpMax <- ave(SPY$diff, SPY$group, FUN=max)

Best,
--
Joshua Ulrich  |  FOSS Trading: www.fosstrading.com

R/Finance 2012: Applied Finance with R
www.RinFinance.com



On Fri, Mar 23, 2012 at 5:07 AM, Paolo Giusti <[hidden email]> 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
>
> _______________________________________________
> [hidden email] 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.

_______________________________________________
[hidden email] 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.