Convert from R to quantstrat setup for trading strategy backtesting

后端 未结 1 621
傲寒
傲寒 2021-01-03 15:05

I am trying to backtest a trading strategy with \"quantstrat\" package. My strategy is composed by 4 indicators, 3 different EMAs and 1 lagged EMA.

I want to go long

相关标签:
1条回答
  • 2021-01-03 15:52

    The quanstrat-based code will not provide identical results for several reasons. One is that your columns are not correct in your first 3 add.signal calls. All the columns need to have an "EMA." prefix:

    add.signal(qs.strategy,name="sigComparison",
      arguments = list(columns=c("EMA.EMA1","EMA.EMA2"),relationship="gt"),
      label="EMA1.gt.EMA2")
    add.signal(qs.strategy,name="sigComparison",
      arguments = list(columns=c("EMA.EMA1","EMA.EMA3"),relationship="gt"),
      label="EMA1.gt.EMA3")
    add.signal(qs.strategy,name="sigComparison",
      arguments = list(columns=c("EMA.EMA1","EMA.EMA1_lag"),relationship="gt"),
      label="EMA1.gt.EMA1_lag")
    

    Another issue, and likely the biggest cause of differences, is the next signal:

    add.signal(qs.strategy, name = "sigFormula",
      arguments = list(formula="EMA1.gt.EMA2 & EMA1.gt.EMA3 & EMA1.gt.EMA1_lag"),
      label="longEntry")
    

    That creates a signal for every observation where the formula is true, not just the observations where the formula crosses from false to true. You only want the observations where the formula crosses, so you should use:

    add.signal(qs.strategy, name = "sigFormula",
      arguments = list(formula="EMA1.gt.EMA2 & EMA1.gt.EMA3 & EMA1.gt.EMA1_lag",
      cross = TRUE),
      label="longEntry")
    

    Another source of differences is that you always use ~100% of your available equity for your opening long transaction in the blotter version, but you always buy 900 shares in the quantstrat version. You can do something similar in quantstrat by using a custom order sizing function (see osNoOp and osMaxPos for examples of how to write a custom order sizing function).

    0 讨论(0)
提交回复
热议问题