You can subset the data
as usual:
data<- xts(rep(NA, 32),seq(as.Date("2013/1/1"), as.Date("2013/2/1"), by = "days"))
data[BuyDates] <- 1
data[SellDates] <- -1
Then you can carry forward the non-NA values using na.locf
.
na.locf(data)
Question
I have two signal series and a data series as below.
BuyDates<-seq(as.Date("2013/1/1"), as.Date("2013/3/1"), by = "5 days")
SellDates<-seq(as.Date("2013/1/1"), as.Date("2013/3/1"), by = "7 days")
data<- xts(c(rnorm(32,100,3)),seq(as.Date("2013/1/1"), as.Date("2013/2/1"), by = "days"))
What i want is,the dates on which data
gets buy signal from BuyDates
,the value of data
should be replaced by 1
and for SellDates
it should be -1
.And,on the remaining days in the sequence,1 or -1 should be carried forward till it gets the opposite signal,and for the days till the 1st signal,value should be replaced with NA
.
kindly help
Solution
You can subset the data
as usual:
data<- xts(rep(NA, 32),seq(as.Date("2013/1/1"), as.Date("2013/2/1"), by = "days"))
data[BuyDates] <- 1
data[SellDates] <- -1
Then you can carry forward the non-NA values using na.locf
.
na.locf(data)