0

I have a problem with my quantstrat trading strategy. The Result of the Endequity doesnt consider my init_equity of 100000. If i change the init_equity to 100 i get the same Endequity. My code looks like this. Can someone imagine were my problem is? Thank you

Sys.setenv(TZ = 'UTC')
currency('USD')
init_date <- '2006-12-31'
start_date <- '2007-01-01'
end_date <- '2017-12-31'
init_equity <- '100000'
adjustment <- TRUE


getSymbols(Symbols = 'QQQ', src = 'yahoo', index.class = 'POSIXct', 
from = start_date, to = end_date)


QQQ = na.omit(QQQ)

symbols <- 'QQQ'

stock(symbols, currency = 'USD', multiplier = 1)


strategy.st <- 'basic_strat'
portfolio.st <- 'basic_portfolio'
account.st <- 'basic_account'

rm.strat(strategy.st)
rm.strat(portfolio.st)
rm.strat(account.st)

initPortf(name = portfolio.st, symbols = 'QQQ', initDate = init_date)
initAcct(name = account.st, portfolios = portfolio.st, 
initDate = init_date, initEq = init_equity)
initOrders(portfolio = portfolio.st, symbols = 'QQQ', initDate = init_date)
strategy(strategy.st, store = TRUE)


tradesize <- 100
for (sym in symbols) {
addPosLimit(portfolio.st, sym, start(get(sym)), tradesize)
}

add.indicator(strategy = strategy.st, name = 'DonchianChannel', 
          arguments = list(HL = quote(HLC(mktdata)[, 1:2]), 
                           n = 260),
          label = 'DCH' )

ratioPriceSeries <- function(x, multiplier = 0.85,
                         col = 'high.DCH') {
x$ratioPrice <- multiplier * x[, col]
x[,'ratioPrice']
}

add.indicator(strategy = strategy.st, name = 'ratioPriceSeries', 
          arguments = list(x = quote(mktdata), multiplier = 0.85, col = 'high.DCH'), label = 'Lratio')

mktdata_ind <- applyIndicators(strategy.st, mktdata = QQQ)
mktdata_ind[is.na(mktdata)] = 0
knitr::kable(tail(mktdata_ind))

add.signal(strategy.st, name = 'sigCrossover',
       arguments = list(label = 'co', data = quote(mktdata), columns = c('Close', 'Lratio'),
                        relation = 'gt'), label = 'enterL')

add.signal(strategy.st, name = 'sigCrossover',
       arguments = list(label = 'co', data = quote(mktdata), columns = c('Close', 'Lratio'),
                        relation = 'lt'), label = 'exitL')

mktdata_sig <- applySignals(strategy.st, mktdata = mktdata_ind)
mktdata_sig[is.na(mktdata_sig)] = 0
knitr::kable(tail(mktdata_sig))

sum(mktdata_sig[,'enterL'] == 1,na.rm = TRUE)
sum(mktdata_sig[,'exitL'] == 1, na.rm = TRUE)


add.rule(strategy.st,name='ruleSignal',
     arguments = list(sigcol="enterL",
                      sigval=TRUE,
                      orderqty=100,
                      ordertype='market',
                      orderside='long',
                      osFUN=osMaxPos,
                      threshold=NULL,
                      prefer = 'Open'),
     type='enter',
     label='enterLong'
)

add.rule(strategy.st,name='ruleSignal',
     arguments = list(sigcol="exitL",
                      sigval=TRUE,
                      orderqty='all',
                      ordertype='market',
                      orderside='long',
                      orderset='DC.set',
                      prefer = 'Open'),
     type='exit',
     label='exitLong'
)

applyStrategy(strategy.st, portfolio=portfolio.st)
updatePortf(Portfolio = portfolio.st)
updateAcct(account.st)

here i get this warning message

[1] "basic_account"
Warning message:
In rbind(deparse.level, ...) :
mismatched types: converting objects to numeric

.

updateEndEq(account.st)

orderbook <- getOrderBook(portfolio.st)

trade_stats <- tradeStats(portfolio.st)
trade_stats1 <- as.data.frame(t(tradeStats(portfolio.st)))
knitr::kable(trade_stats1)



chart.Posn(Portfolio = portfolio.st, Symbol = 'QQQ')

The result look like this:

knitr::kable(trade_stats1[c('Portfolio', 'Symbol', 'Num.Txns', 'Num.Trades', 'End.Equity'),])


|           |x               |
|:----------|:---------------|
|Portfolio  |basic_portfolio |
|Symbol     |QQQ             |
|Num.Txns   |17              |
|Num.Trades |8               |
|End.Equity |10570           |

The problem ist the End.Equity which is wrong in my opinion.

Community
  • 1
  • 1
Ramon
  • 71
  • 8

1 Answers1

0

I ran your code and changed to from:

init_equity <- '100000'

to

init_equity <- '100'

Here are the results I got in order:

End.Eq = 2017-12-29 110570

End.Eq = 2017-12-29 10670

Try running the following script to get End.Eq:

a <- getAccount(account.st)
End.Eq <- tail(a$summary$End.Eq, n = 1)

The code: tradeStats(portfolio.st)is simply providing you the cumulative PL not end.Eq

Hope it helped!

boniface316
  • 489
  • 3
  • 17