Quantstrat Trade Statistics产生-100%的累积回报?

时间:2018-04-20 15:28:29

标签: r trading quantstrat technical-indicator

我在Quantstrat上使用交易统计学,并获得了看好这个世界的正面结束股票(起始资产= 1m)。然而,令人费解的是,当我制作表格时累积回报为-1.0。

您如何才能获得正的期末权益和负累积回报?我对结束股权的理解是错误的吗?

library(quantmod)
library(FinancialInstrument)
library(PerformanceAnalytics)
library(foreach)
library(blotter)
library(quantstrat)

options("getSymbols.yahoo.warning"=FALSE)
options("getSymbols.warning4.0"=FALSE)

initDate="1990-01-01"
from ="2009-01-01"
to ="2013-01-01"
symbols = c("SPY")
currency("USD")
getSymbols(symbols, from=from, to=to, adjust=TRUE)

stock(symbols, currency="USD", multiplier=1)
initEq=1000000

strategy.st <- portfolio.st <- account.st <- "mystrat"

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


initPortf(name=portfolio.st,
          symbols=symbols,
          initDate=initDate,
          currency='USD')
initAcct(name=account.st,
         portfolios=portfolio.st,
         initDate=initDate,
         currency='USD',
         initEq=initEq)
initOrders(portfolio=portfolio.st,
           initDate=initDate)

strategy(strategy.st, store=TRUE)

### Add Indicators

nRSI <- 21
buyThresh <- 50
sellThresh <- 50

#Indicator for EMA long medium short

nEMAL<- 200
nEMAM<- 30
nEMAS<- 13
nEMAF<- 5

add.indicator(strategy.st, name="RSI",
              arguments=list(price=quote(Cl(mktdata)), n=nRSI),
              label="rsi")

add.indicator(strategy.st, name="EMA",
              arguments=list(x=quote(Cl(mktdata)), n=nEMAL),
              label="EMAL")

add.indicator(strategy.st, name="EMA",
              arguments=list(x=quote(Cl(mktdata)), n=nEMAM),
              label="EMAM")

add.indicator(strategy.st, name="EMA",
              arguments=list(x=quote(Cl(mktdata)), n=nEMAS),
              label="EMAS")

add.indicator(strategy.st, name="EMA",
              arguments=list(x=quote(Cl(mktdata)), n=nEMAF),
              label="EMAF")

upsig <- function(data) {
  sig <- data[, "rsi"] >50 & data[, "EMA.EMAM"] > data[, "EMA.EMAL"]   
  colnames(sig) <- "upSig"
  sig
}

downsig <- function(data) {
  sig <- data[, "rsi"] <50 & data[, "EMA.EMAM"] < data[, "EMA.EMAL"]   
  colnames(sig) <- "downSig"
  sig
}



### Add Signal- Enter

add.signal(strategy.st, name="upsig",
           arguments=list(data = quote(mktdata)),
           label = "entersig")

add.signal(strategy.st, name="downsig",
           arguments=list(data = quote(mktdata)),
           label = "exitsig")

### Add rule - Enter

add.rule(strategy.st,
         name='ruleSignal',
         arguments = list(sigcol="upSig.entersig",
                          sigval=TRUE,
                          orderqty=1000,
                          ordertype='market',
                          orderside='long',
                          threshold=NULL),
         type='enter',
         path.dep=TRUE)


### Add rule- Exit

add.rule(strategy.st,
         name='ruleSignal',
         arguments = list(sigcol="downSig.exitsig",
                          sigval=TRUE,
                          orderqty= -1000,
                          ordertype='market',
                          orderside='long',
                          pricemethod='market',
                          replace=FALSE),
         type='exit',
         path.dep=TRUE)

start_t<-Sys.time()
out<-try(applyStrategy(strategy=strategy.st,
                       portfolios=portfolio.st))


updatePortf(portfolio.st)
updateAcct(portfolio.st)
updateEndEq(account.st)

for(symbol in symbols) {
  chart.Posn(
    Portfolio=portfolio.st,
    Symbol=symbol,
    log=TRUE)
}

tstats <- tradeStats(portfolio.st)
t(tstats)

rets <- PortfReturns(Account = account.st)
rownames(rets) <- NULL
charts.PerformanceSummary(rets, colorset = bluefocus)

tab.perf <- table.Arbitrary(rets,
                            metrics=c(
                              "Return.cumulative",
                              "Return.annualized",
                              "SharpeRatio.annualized",
                              "CalmarRatio"),
                            metricsNames=c(
                              "Cumulative Return",
                              "Annualized Return",
                              "Annualized Sharpe Ratio",
                              "Calmar Ratio"))
tab.perf


#Test here
#test <-try(applyIndicators(strategy.st,mktdata=OHLC(AAPL)))
#head(test, n=40)

1 个答案:

答案 0 :(得分:2)

首先,你只进入很多多头头寸。然后是很多连续的退出交易!问题是你在很长一段时间内在每个信号条上获得长信号。并且您并没有限制您的策略进入越来越多的多头头寸,每个头寸1000个。您可以使用osMaxPosaddPosLimit来处理限制仓位时的交易量向特定方向开放。

在这里,您可以停止在每个栏上发生如此多的长条目。当rsi在上方和下方超过50时,您希望条目为真。使用sigCrossover。其次,使用addPosLimit来限制允许的总职位数量。看看mktdata下次调试你的策略,检查你没有一行接一个多个输入信号。

您获得-1暨回报,因为您提供了无意义的输入。如此多的购买(你在每个连续的一天购买1k单位,而入口信号是真的,然后连续多天出售)。很明显,这不是预期的。检查getTxns的输出以查看问题。此外,您的最终资产可能是负数,因为您只是将您的投资组合中的现金PnL加到您的初始资本中。即,一旦您的实际资产变为负数,量子计划就不会停止交易。您可以这样想:即使您的损失超过初始权益,quantstrat也只是保持交易,假设您有更多的资金可以交易。

如果您使用合理的参数,您的权益将永远不会在量化策略中出现负值(除非您的策略非常糟糕且一直失去现金,或者您在回溯测试中进行了很长时间的交易,因此破产或更糟的情况具有相当大的概率发生在允许的时间。)

这里有一些调整,即sigCrossover,并使用交易规模300,在您的策略中获得更明智的数字。也使用osMaxPos。它仍然在这里购买最多3个级别的堆叠仓位。

library(quantmod)
library(FinancialInstrument)
library(PerformanceAnalytics)
library(foreach)
library(blotter)
library(quantstrat)

options("getSymbols.yahoo.warning"=FALSE)
options("getSymbols.warning4.0"=FALSE)

initDate="1990-01-01"
from ="2009-01-01"
to ="2013-01-01"
symbols = c("SPY")
currency("USD")
getSymbols(symbols, from=from, to=to, adjust=TRUE)

stock(symbols, currency="USD", multiplier=1)
initEq=1000000

strategy.st <- portfolio.st <- account.st <- "mystrat"

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


initPortf(name=portfolio.st,
          symbols=symbols,
          initDate=initDate,
          currency='USD')
initAcct(name=account.st,
         portfolios=portfolio.st,
         initDate=initDate,
         currency='USD',
         initEq=initEq)
initOrders(portfolio=portfolio.st,
           initDate=initDate)

strategy(strategy.st, store=TRUE)

### Add Indicators

nRSI <- 21
buyThresh <- 50
sellThresh <- 50

#Indicator for EMA long medium short

nEMAL<- 200
nEMAM<- 30
nEMAS<- 13
nEMAF<- 5

tradeSize <- 100
for (sym in symbols) {
  addPosLimit(portfolio.st, sym, start(get(sym)), maxpos = 300, longlevels = 3)
}

add.indicator(strategy.st, name="RSI",
              arguments=list(price=quote(Cl(mktdata)), n=nRSI),
              label="rsi")

add.indicator(strategy.st, name="EMA",
              arguments=list(x=quote(Cl(mktdata)), n=nEMAL),
              label="EMAL")

add.indicator(strategy.st, name="EMA",
              arguments=list(x=quote(Cl(mktdata)), n=nEMAM),
              label="EMAM")

add.indicator(strategy.st, name="EMA",
              arguments=list(x=quote(Cl(mktdata)), n=nEMAS),
              label="EMAS")

add.indicator(strategy.st, name="EMA",
              arguments=list(x=quote(Cl(mktdata)), n=nEMAF),
              label="EMAF")

# important to set cross = TRUE in sigThreshold
add.signal(strategy.st, name = "sigThreshold",
           arguments = list(column = "rsi", threshold = 50, relationship = "gt", cross = TRUE),
           label = "rsiUp")

add.signal(strategy.st, name = "sigThreshold",
           arguments = list(column = "rsi", threshold = 50, relationship = "lt", cross = TRUE),
           label = "rsiDn")

upsig <- function(data) {
  sig <- data[, "rsiUp"] & data[, "EMA.EMAM"] > data[, "EMA.EMAL"]   
  colnames(sig) <- "upSig"
  sig
}

downsig <- function(data) {
  sig <- data[, "rsiDn"] & data[, "EMA.EMAM"] < data[, "EMA.EMAL"]   
  colnames(sig) <- "downSig"
  sig
}



### Add Signal- Enter

add.signal(strategy.st, name="upsig",
           arguments=list(data = quote(mktdata)),
           label = "entersig")

add.signal(strategy.st, name="downsig",
           arguments=list(data = quote(mktdata)),
           label = "exitsig")

### Add rule - Enter

add.rule(strategy.st,
         name='ruleSignal',
         arguments = list(sigcol="upSig.entersig",
                          sigval=TRUE,
                          orderqty=100,
                          ordertype='market',
                          orderside='long',
                          threshold=NULL,
                          osFUN = osMaxPos), # <- need this to cap trade levels to at most 3
         type='enter',
         path.dep=TRUE)


### Add rule- Exit

add.rule(strategy.st,
         name='ruleSignal',
         arguments = list(sigcol="downSig.exitsig",
                          sigval=TRUE,
                          orderqty= -100,
                          ordertype='market',
                          orderside='long',
                          pricemethod='market',
                          replace=FALSE),
         type='exit',
         path.dep=TRUE)

start_t<-Sys.time()
out<-try(applyStrategy(strategy=strategy.st,
                       portfolios=portfolio.st))


updatePortf(portfolio.st)
updateAcct(portfolio.st)
updateEndEq(account.st)

for(symbol in symbols) {
  chart.Posn(
    Portfolio=portfolio.st,
    Symbol=symbol,
    log=TRUE)
}

tstats <- tradeStats(portfolio.st)
t(tstats)

rets <- PortfReturns(Account = account.st)
rownames(rets) <- NULL
charts.PerformanceSummary(rets, colorset = bluefocus)

tab.perf <- table.Arbitrary(rets,
                            metrics=c(
                              "Return.cumulative",
                              "Return.annualized",
                              "SharpeRatio.annualized",
                              "CalmarRatio"),
                            metricsNames=c(
                              "Cumulative Return",
                              "Annualized Return",
                              "Annualized Sharpe Ratio",
                              "Calmar Ratio"))
tab.perf


tail(.blotter$account.mystrat$summary)
# Additions Withdrawals Realized.PL Unrealized.PL Interest Gross.Trading.PL Txn.Fees Net.Trading.PL Advisory.Fees Net.Performance  End.Eq
# 2012-12-20 19:00:00         0           0           0        -392.4        0        -392.4006        0      -392.4006             0       -392.4006 1012255
# 2012-12-23 19:00:00         0           0           0        -132.0        0        -131.9961        0      -131.9961             0       -131.9961 1012123
# 2012-12-25 19:00:00         0           0           0        -180.0        0        -180.0018        0      -180.0018             0       -180.0018 1011943
# 2012-12-26 19:00:00         0           0           0         -57.0        0         -57.0006        0       -57.0006             0        -57.0006 1011886
# 2012-12-27 19:00:00         0           0           0        -459.0        0        -458.9997        0      -458.9997             0       -458.9997 1011427
# 2012-12-30 19:00:00         0           0           0         714.0        0         714.0015        0       714.0015             0        714.0015 1012141