0

I want to build a simple trading strategies using quantstrat. I am struggling because the results I had are different from the results of the strategy written without using the package. In particular this is the idea:

  • I want to go long when: EMA1 > EMA2 & EMA1 > EMA3 & EMA1_lag < EMA1

  • I want to exit and go flat when: EMA1 < EMA3

here is what i wrote and works:

# trading algo 
for( i in 1:nrow(SPY) ) {
  # update values for this date
  CurrentDate <- time(SPY)[i]
  equity = getEndEq(b.strategy, CurrentDate)
  ClosePrice <- as.numeric(Cl(SPY[i,]))
  Posn <- getPosQty(b.strategy, Symbol='SPY', Date=CurrentDate)
  UnitSize = as.numeric(trunc(equity/ClosePrice))
  EMA1 <- as.numeric(SPY[i,'EMA_1'])
  EMA2 <- as.numeric(SPY[i,'EMA_2'])
  EMA3 <- as.numeric(SPY[i,'EMA_3'])
  EMA1_lag <- as.numeric(SPY[i,'EMA_1_lag'])
  # change market position if necessary
  if( !is.na(EMA1) & !is.na(EMA2) & !is.na(EMA3) & !is.na(EMA1_lag) ) {
    if( Posn == 0 ) { # No position, test to go Long
      if( EMA1 > EMA2 & EMA1 > EMA3 & EMA1_lag<EMA1) {
        # enter long position
        addTxn(b.strategy, Symbol='SPY', TxnDate=CurrentDate,
               TxnPrice=ClosePrice, TxnQty = UnitSize, TxnFees=0)
      }
    } else { # Have a position, so check exit
      if( EMA1 < EMA3) {
        # exit position
        addTxn(b.strategy, Symbol='SPY', TxnDate=CurrentDate,
               TxnPrice=ClosePrice, TxnQty = -Posn, TxnFees=0)
      } else {
        if( i==nrow(SPY) ) # exit on last day
          addTxn(b.strategy, Symbol='SPY', TxnDate=CurrentDate,
                 TxnPrice=ClosePrice, TxnQty = -Posn, TxnFees=0)
      }
    }
  }
  updatePortf(b.strategy,Dates=CurrentDate)
  updateAcct(b.strategy,Dates=CurrentDate)
  updateEndEq(b.strategy,CurrentDate)
} # End dates loop

In quantstrat I wrote like this, but it works giving different results:

add.indicator(strategy = qs.strategy, name = "EMA",
              arguments = list(x = quote(na.locf(Cl(mktdata))), n=10), label="EMA1")
add.indicator(strategy = qs.strategy, name = "EMA",
              arguments = list(x = quote(na.locf(Cl(mktdata))), n=25), label="EMA2")
add.indicator(strategy = qs.strategy, name = "EMA",
              arguments = list(x = quote(na.locf(Cl(mktdata))), n=30), label="EMA3")
add.indicator(strategy = qs.strategy, name = "EMA",
              arguments = list(x = quote(lag(na.locf(Cl(mktdata)))), n=10), label="EMA1_lag")
# entry signals
add.signal(qs.strategy,name="sigComparison",
           arguments = list(columns=c("EMA1","EMA2"),relationship="gt"),
           label="EMA1.gt.EMA2")
add.signal(qs.strategy,name="sigComparison",
           arguments = list(columns=c("EMA1","EMA3"),relationship="gt"),
           label="EMA1.gt.EMA3")
add.signal(qs.strategy,name="sigComparison",
           arguments = list(columns=c("EMA1","EMA1_lag"),relationship="gt"),
           label="EMA1.gt.EMA1_lag")
add.signal(qs.strategy, name = "sigFormula",
           arguments = list(formula="EMA1.gt.EMA2 & EMA1.gt.EMA3 & EMA1.gt.EMA1_lag"),
           label="longEntry")
# exit signals
add.signal(qs.strategy,name="sigComparison",
           arguments = list(columns=c("EMA1","EMA3"),relationship="lt"),
           label="EMA1.lt.EMA3")
# RULES
# go long when 3 condition
add.rule(qs.strategy, name='ruleSignal',
         arguments = list(sigcol="longEntry", sigval=TRUE, orderqty=900,
                      ordertype='market', orderside='long'), 
         type='enter')
# exit when 1 condition
add.rule(qs.strategy, name='ruleSignal',
         arguments = list(sigcol="EMA1.lt.EMA3", sigval=TRUE, orderqty='all',
                          ordertype='market', orderside='long'),
         type='exit')

Could anyone help me to understand why it does not give similar results?

Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
gcats
  • 41
  • 6
  • Your code is not [reproducible](http://stackoverflow.com/q/5963269/271616), which makes it more difficult to help you. Also, I'm inclined to trust quantstrat's results over manual calculations done in a loop, because it's easy to make mistakes when re-writing backtesting logic. – Joshua Ulrich Sep 09 '15 at 09:50
  • 1
    Thank you for having answer Joshua. Here: http://stackoverflow.com/questions/32169176/convert-from-r-to-quantstrat-setup-for-trading-strategy-backtesting the link of my previous question, with reproducible code, but nobody answered. – gcats Sep 09 '15 at 09:56
  • Please don't post a duplicate question (especially of poorer quality) if someone doesn't answer your question. – Joshua Ulrich Sep 09 '15 at 10:02

0 Answers0