Skip to content
Prev 69704 / 398526 Next

without the loop

On 5/13/05, Omar Lakkis <uofiowa at gmail.com> wrote:
Given the complex looping it would be better if you provided
documentation with your post and a reproducible example, not
just a code snippet.  See the posting guide.

At any rate, it seems that what you want to do is to append 1
whenever the settle price exceeds the high of the last w
time points and a -1 whenever the settle price is below the low of
the last w time points.

Represent the prices as a zoo series with 3 columns: 
high, low, settle and use the following (untested) loop-free
code:

high <- 1; low <- 2; settle <- 3
W <- w+1
r <- rapply(prices, W, function(x) 
	sign(x[W,settle] > max(x[-W,high])) - (x[W,settle] < min(x[-W,low])),
	by.column = FALSE, align = "right")
)
r[r!=0]