1
 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.

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
Chris
  • 57
  • 1
  • 7

1 Answers1

0

After searching and researching, the dailyreturns(S) will be the open and close of each trade on a daily basis. It calculates the return based on the numbers obtain from the daily indicators and how we reacted and then it is multipled to the dailyreturns to obtain final numbers.

Chris
  • 57
  • 1
  • 7