library(quantmod)
library(PerformanceAnalytics)
s <- get(getSymbols('SPY'))["2012::"]
s$sma20 <- SMA(Cl(s) , 20)
s$position <- ifelse(Cl(s) > s$sma20 , 1 , -1)
myReturn <- lag(s$position) * dailyReturn(s)
charts.PerformanceSummary(cbind(dailyReturn(s),myReturn))
I found the code above on another stackoverflow forum which was relatively old. I was wondering with this simple strategy above that trades when SMA close prices are above 20.
What does it mean when it enters a trade? Does it enter a trade every time the close time is above 20 SMA? Or does it enter the position once at SMA above 20 then exits when it is lower than 20?
I don't see the portion of the code where it leaves the trade.
Simple question, but I wasn't sure how R calculates the return on these strategies.
Thank you in advance.