start <- as.POSIXct("2016-09-19")
end <- start + as.difftime(14, units="days")
datetime <- seq(from=start, to=end, by="days")
signal <- c(0,0,1,1,0,0,0,0,1,0,1,1,0,0,0)
price <- c(seq(22.0, 23.4, by=.2), seq(23.4, 22.2, by=-.2))
df <- data.frame(datetime, signal,price)
df$Up_or_down <- 0
I added a datetime column which might be helpful in sequencing.
I have a dataframe of prices and a signal. I need to:
- Find the first instance of the signal (signal == 1)
- Take the price from the row of the first signal and determine
- Did the price go up or down at least $1?
- Place a -1 or +1 in the Up_or_down column at the row of the first signal.
Here's the tricky part: I then need to start at the NEXT signal(in this case it is the next row), and find if the price move $1 up or down from that 2nd starting row.
So, start at the first signal, find if the price moved up or down, then start again at the next signal.