While loop in R code- how to make it faster
In quantstrat package I have located one of the main culprits for slowness of the applyRule function and wonder if there is more efficient to write the while loop. Any feedback would be helpful. For anyone experience wrapping this part into Parallel R. 

As an option apply  would work instead while? Or should I re-write this part into new function such as ruleProc and nextIndex? I am also dveling on Rcpp but that may be a streach. Any help and constructive advice is much appreciated?  



       while (curIndex) {
        timestamp = Dates[curIndex]
        if (isTRUE(hold) & holdtill < timestamp) {
            hold = FALSE
            holdtill = NULL
        }
        types <- sort(factor(names(strategy$rules), levels = c("pre",
            "risk", "order", "rebalance", "exit", "enter", "entry",
            "post")))
        for (type in types) {
            switch(type, pre = {
                if (length(strategy$rules[[type]]) >= 1) {
                  ruleProc(strategy$rules$pre, timestamp = timestamp,
                    path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
                    symbol = symbol, ruletype = type, mktinstr = mktinstr)
                }
            }, risk = {
                if (length(strategy$rules$risk) >= 1) {
                  ruleProc(strategy$rules$risk, timestamp = timestamp,
                    path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
                    symbol = symbol, ruletype = type, mktinstr = mktinstr)
                }
            }, order = {
                if (length(strategy$rules[[type]]) >= 1) {
                  ruleProc(strategy$rules[[type]], timestamp = timestamp,
                    path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
                    symbol = symbol, ruletype = type, mktinstr = mktinstr,)
                } else {
                  if (isTRUE(path.dep)) {
                    timespan <- paste("::", timestamp, sep = "")
                  } else timespan = NULL
                  ruleOrderProc(portfolio = portfolio, symbol = symbol,
                    mktdata = mktdata, timespan = timespan)
                }
            }, rebalance = , exit = , enter = , entry = {
                if (isTRUE(hold)) next()
                if (type == "exit") {
                  if (getPosQty(Portfolio = portfolio, Symbol = symbol,
                    Date = timestamp) == 0) next()
                }
                if (length(strategy$rules[[type]]) >= 1) {
                  ruleProc(strategy$rules[[type]], timestamp = timestamp,
                    path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
                    symbol = symbol, ruletype = type, mktinstr = mktinstr)
                }
                if (isTRUE(path.dep) && length(getOrders(portfolio = portfolio,
                  symbol = symbol, status = "open", timespan = timestamp,
                  which.i = TRUE))) {
                }
            }, post = {
                if (length(strategy$rules$post) >= 1) {
                  ruleProc(strategy$rules$post, timestamp = timestamp,
                    path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
                    symbol = symbol, ruletype = type, mktinstr = mktinstr)
                }
            })
        }
        if (isTRUE(path.dep))
            curIndex <- nextIndex(curIndex)
        else curIndex = FALSE
    }
While loop in R code- how to make it faster
===

In quantstrat package I have located one of the main culprits for slowness of the applyRule function and wonder if there is more efficient to write the while loop. Any feedback would be helpful. For anyone experience wrapping this part into Parallel R. 

As an option apply  would work instead while? Or should I re-write this part into new function such as ruleProc and nextIndex? I am also dveling on Rcpp but that may be a streach. Any help and constructive advice is much appreciated?  



       while (curIndex) {
        timestamp = Dates[curIndex]
        if (isTRUE(hold) & holdtill < timestamp) {
            hold = FALSE
            holdtill = NULL
        }
        types <- sort(factor(names(strategy$rules), levels = c("pre",
            "risk", "order", "rebalance", "exit", "enter", "entry",
            "post")))
        for (type in types) {
            switch(type, pre = {
                if (length(strategy$rules[[type]]) >= 1) {
                  ruleProc(strategy$rules$pre, timestamp = timestamp,
                    path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
                    symbol = symbol, ruletype = type, mktinstr = mktinstr)
                }
            }, risk = {
                if (length(strategy$rules$risk) >= 1) {
                  ruleProc(strategy$rules$risk, timestamp = timestamp,
                    path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
                    symbol = symbol, ruletype = type, mktinstr = mktinstr)
                }
            }, order = {
                if (length(strategy$rules[[type]]) >= 1) {
                  ruleProc(strategy$rules[[type]], timestamp = timestamp,
                    path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
                    symbol = symbol, ruletype = type, mktinstr = mktinstr,)
                } else {
                  if (isTRUE(path.dep)) {
                    timespan <- paste("::", timestamp, sep = "")
                  } else timespan = NULL
                  ruleOrderProc(portfolio = portfolio, symbol = symbol,
                    mktdata = mktdata, timespan = timespan)
                }
            }, rebalance = , exit = , enter = , entry = {
                if (isTRUE(hold)) next()
                if (type == "exit") {
                  if (getPosQty(Portfolio = portfolio, Symbol = symbol,
                    Date = timestamp) == 0) next()
                }
                if (length(strategy$rules[[type]]) >= 1) {
                  ruleProc(strategy$rules[[type]], timestamp = timestamp,
                    path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
                    symbol = symbol, ruletype = type, mktinstr = mktinstr)
                }
                if (isTRUE(path.dep) && length(getOrders(portfolio = portfolio,
                  symbol = symbol, status = "open", timespan = timestamp,
                  which.i = TRUE))) {
                }
            }, post = {
                if (length(strategy$rules$post) >= 1) {
                  ruleProc(strategy$rules$post, timestamp = timestamp,
                    path.dep = path.dep, mktdata = mktdata, portfolio = portfolio,
                    symbol = symbol, ruletype = type, mktinstr = mktinstr)
                }
            })
        }
        if (isTRUE(path.dep))
            curIndex <- nextIndex(curIndex)
        else curIndex = FALSE
    }