r - Buy & sell after local minima and maxima -
i've got data series of form date. open high low , close (prices). want create local maxima , minima close column of data. further want buy after 2 days of local minima @ close , sell after 2 days of local maxima @ close. further want calculate profit , loss same. code same under.
require(quantmod) tckr1<-"^nsei" start<-sys.date()-200 end<- format(sys.date(),"%y-%m-%d") # yyyy-mm-dd getsymbols(tckr1, from=start, to=end) data<- nsei$nsei.close data$n <- 1:nrow(data) data$z <- zigzag(data$nsei.close , change = 2 , percent = t) data$level<- data[c(findpeaks(data$z) , findvalleys(data$z)) - 1 , ] data$nsei.close.1<- null data$n.1<- null data$trade<- lag(data$level,2)
now need data column tell me when buy , sell +1 , -1 , calculate profit , loss same. in above mentioned data buy when n= 29 @ 5719.70 , when n=36 @ 5851.20 etc.
regards ashish
require(quantmod) tckr1<-"^nsei" start<-sys.date()-200 end<- format(sys.date(),"%y-%m-%d") # yyyy-mm-dd getsymbols(tckr1, from=start, to=end) data<- nsei$nsei.close data$n <- 1:nrow(data) data$z <- zigzag(data$nsei.close , change = 2 , percent = t) data$level<- data[c(findpeaks(data$z) , findvalleys(data$z)) - 1 , ] ex <- data[c(findpeaks(data$z) , findvalleys(data$z)) - 1 , ] data$trade<- data$level data$trade[is.na(data$level)]<- 0 data$trade[data$trade!=0,]<- c(1,-1)
this way can trade column +/- 1.
Comments
Post a Comment