I am trying to run four nested loops like below. I am attempting to find the best combination of MA1, MA2, SL, and LS. To maintain simplicity I showed you an example of what I am running the the comment in the middle. As you can see below MA1 is 1-364, MA2 is 2-365, SL and LS are .001 - .05. I set it up so MA2 always starts one more than MA1 as checking 1 and 2 is the same as checking 2 and 1 (In this case).
main <- function(...) {
MA1 <- 1
repeat{
MA1 <- MA1 + 1
if(MA1>364){
break
}
MA2 <- MA1
repeat{
MA2 <- MA2 + 1
SL <- 0
if(MA2>365){
break
}
repeat{
SL <- SL + .001
if(SL>.05){
break
}
LS <- 0
repeat{
LS <- LS + .001
Day <- MA2 + 1
if(LS>.05){
break
}
n <- n + 1
#Finding the Profit/Loss of the combination
#PnL <- FindPnL(MA1, MA2, SL, LS)
}
}
}
}
return(n)
}
The n <- N+1 is just for running sake
Is there a different way I can create this looping sequence? Right now, using system.time() user system elapsed 156.972 0.693 158.555 With my goal making that number as small as possible.