# [R-SIG-Finance] Test for new event and save data in new data.frame

Mark Knecht markknecht at gmail.com
Thu Oct 4 01:12:27 CEST 2012

```Hi again,
Again, I haven't touched R in a couple of years and am just getting
going on a little idea. Thanks in advance.

If you all think this is better done somewhere else like
StackOverflow let me know and I'll post there instead.

The following very simplified code is meant to represent reading
through price data (PriceData) to extract what is happening with
prices after a trade has started and as it progresses. The column MP
(MarketPosition) is lagged. When MP switches from 0 to 1 a new long
trade is entered. I'd like to collect the next 5 bars of data for that
column. In this example there are 4 trades occurring at bars 3, 8, 11
& 15. The results for this data are a data.frame with 5 rows and 4
columns. (The real one will be much larger...)

QUESTIONS:

1) How do I do a logical test something like ((c3 = 1) AND (c4 = 0))
for each row in PriceData to determine when a trade started?

2) If the test above is true, how do I copy c2[row:(row+4)] into a

I'm guessing this might be a job for one of the apply functions but
I'm not sure which one or how to do it. The couple of R books I've got
aren't making it clear yet,

In the code below EVERYTHING below the comment block is only to
show what I want to create.  It will all go away when the questions
above turn into R code.

I hope this is reasonably clear. Let me know if it isn't.

Thanks,
Mark

MyLag <- function(x, k) c(rep(NA, k), x[1:(length(x)-k)] )

c1 = 1:20
c2 = c(5,6,7,8,1,2,3,4,7,2,3,4,5,7,8,9,1,2,1,1)
c3 = c(0,0,1,1,1,0,0,1,0,0,1,1,0,0,1,1,1,0,0,0)
c4 = MyLag(c3, 1)

PriceData = data.frame(cbind(c1,c2,c3,c4))
colnames(PriceData) = c("BarNum","Price", "MP","LagMP")

PriceData

# This represents the data.frame I'd like to create -> (TradeData)
#
# When ((c3 = 1) AND (c4 = 0)) then copy c2[row:row+4] and
# put it in a new column in the data frame
# Eventually there is one column in the data.frame for

t1 = c(7,8,1,2,3)
t2 = c(4,7,2,3,4)
t3 = c(3,4,5,7,8)
t4 = c(8,9,1,2,1)