# NOT RUN {
data(sample_matrix)
sample.xts <- as.xts(sample_matrix)
# plot the Close
plot(sample.xts[,"Close"])
# plot a subset of the data
plot(sample.xts[,"Close"], subset="2007-04-01/2007-06-31")
# function to compute simple returns
simple.ret <- function(x, col.name){
x[,col.name] / lag(x[,col.name]) - 1
}
# plot the close and add a panel with the simple returns
plot(sample.xts[,"Close"])
R <- simple.ret(sample.xts, "Close")
lines(R, type="h", on=NA)
# add the 50 period simple moving average to panel 1 of the plot
library(TTR)
lines(SMA(sample.xts[,"Close"], n = 50), on=1, col="blue")
# add month end points to the chart
points(sample.xts[endpoints(sample.xts[,"Close"], on = "months"), "Close"],
col="red", pch=17, on=1)
# add legend to panel 1
addLegend("topright", on=1,
legend.names = c("Close", "SMA(50)"),
lty=c(1, 1), lwd=c(2, 1),
col=c("black", "blue", "red"))
# }
Run the code above in your browser using DataLab