Search code examples
rquantmodtrading

Test multiple MA for buy and sell


Is there a way to test different Moving averages to find the best that gives highest profit?

I would lik to test one MA for buy and one MA for sell. Currently I ony have this, which uses the same MA for buy and sell.

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))

Solution

  • Run 'macd.R' and 'macdParameters.R' in the demo folder of quantstrat, and then go from there for your needs. They show how to find optimal values of moving averages in a macd context. I think that would be the most effective way for you to solve your problem.

    # If you are unsure of how to get quantstrat:
    library(devtools)
    install_github("braverock/quantstrat")