Reputation: 33
I have data.frame, that shows current bid and ask prices of a stock and my signal at that time.
time bid_price ask_price signal
10:10:01.000500 50.02 50.05 50.03
10:10:01.000855 50.02 50.03 50.05
10:10:01.000856 50.02 50.03 50.06
at 10:10:01.000856, while i have signal at 50.06, I can't use it. I can only use signal 50 microseconds ago
So I need this outcome data.frame
at 10:10:01.000856, 50 microseconds ago, the time is 10:01:01.000806, so the usable signal that time is 50.03
time bid_price ask_price signal signal_50microseconds_ago
10:10:01.000500 50.02 50.05 50.03 NA
10:10:01.000855 50.02 50.04 50.05 50.03
10:10:01.000856 50.02 50.04 50.06 50.03
Is there a R / python solution that generates the outcome data.frame?
For example, say we first load the data.frame into xts
object then we might have
xts_obj$signal_50microseconds_ago <- get_time_lag_wish_this_function_exists(xts_obj$signal,lag=0.000050)
Note: I don't think I can simply usexts.lag
1 because I would end up moving 50.05 down, not 50.03
time bid_price ask_price signal signal_from_lag1
10:10:01.000500 50.02 50.05 50.03 NA
10:10:01.000855 50.02 50.04 50.05 50.03
10:10:01.000856 50.02 50.04 50.06 50.05
Upvotes: 3
Views: 334
Reputation: 3485
This is the approach I would take to align the values with the most recent previous observation. It only uses the xts
merge function and the na.locf()
to fill merged by time values forward:
d <- read.table(stringsAsFactors=F, header=T, text="
time bid_price ask_price signal
10:10:01.000500 50.02 50.05 50.03
10:10:01.000855 50.02 50.03 50.05
10:10:01.000856 50.02 50.03 50.06
")
t <- as.POSIXct(paste0("2015-05-28 ", d$time))
#format(t, "%Y-%m-%d %H:%M:%OS9")
library(xts)
d_xts <- xts(d[,-1], order.by=t)
## Lag the signal by 50 microseconds:
signal_lag <- xts(d[,"signal"], order.by=t+0.000050)
merge_xts <- merge(d_xts, signal_lag)
## Carry last lagged value forward:
merge_xts$signal_lag <- na.locf(merge_xts$signal_lag)
## Finally subset back to only original rows:
merge_xts <- merge_xts[ !is.na(merge_xts$signal) ]
The resulting merge_xts
object:
> merge_xts
bid_price ask_price
2015-05-28 10:10:01 50.02 50.05
2015-05-28 10:10:01 50.02 50.03
2015-05-28 10:10:01 50.02 50.03
signal signal_lag
2015-05-28 10:10:01 50.03 NA
2015-05-28 10:10:01 50.05 50.03
2015-05-28 10:10:01 50.06 50.03
Upvotes: 1