I have some simple R code that reads a list of stocks prices. I would like to plot the ZigZag indicator, highlight all the inflection points, and print out the value of the last three inflection points. This is supposed to do that, but it doesn't work properly. Any ideas why?
library(TTR)
mydata <-read.csv("EURUSD.csv", sep=",",header=TRUE)
attach(mydata)
plot(BAR, PRICE)
zz <- ZigZag(PRICE, change = 5, percent = TRUE)
lines(zz, col = "blue")
#get the inflection points
infl <- c( FALSE, diff(diff(zz)>0)!=0 )
points(mydata$BAR[infl ], mydata$PRICE[infl ], col="red")
#print the last 3 inflection points
print( tail(mydata$PRICE[infl],1) )
print( tail(mydata$PRICE[infl],2) )
print( tail(mydata$PRICE[infl],3) )