R - Quantstrat - back testing is not working

一笑奈何 提交于 2019-12-11 07:25:24

问题


I used the following code from Guy Yollin's lecture. The result I got was not correct. The result is posted below the code.

library(blotter)
library(quantstrat)

currency("USD")
stock("SPY", currency = "USD", multiplier = 1)

initDate = "1997-12-31"
startDate = "1998-01-01"
endDate = "2014-06-30"
initEQ = 1e6

Sys.setenv(TZ = "UTC")

getSymbols('SPY', from = startDate, to = endDate, index.class = "POSIXct", adjust = T)

SPY = to.monthly(SPY, indexAt = "endof", drop.time = FALSE)
SPY$SMA10m <- SMA(Cl(SPY),10)

qs.strategy <- "qsFaber"

initPortf(qs.strategy, "SPY", initDate = initDate)
initAcct(qs.strategy, portfolios = qs.strategy, initDate = initDate, initEq = initEQ)
initOrders(portfolio = qs.strategy, initDate = initDate)

strategy(qs.strategy, store = TRUE)

strat <- getStrategy(qs.strategy)

add.indicator(
  strategy = qs.strategy,
  name = "SMA",
  arguments = list(
    x = quote(Cl(mktdata)), 
    n = 10),
  label = "SMA10"
)

add.signal(
  qs.strategy,
  name = "sigCrossover",
  arguments = list (
    columns = c("Close", "SMA10"),
    relationship = "gt"),
  label = "Cl.gt.SMA"
  )

add.signal (
  qs.strategy,
  name = "sigCrossover",
  arguments = list(
    columns = c("Close", "SMA10"),
    relationship = "lt"),
  label = "Cl.lt.SMA"
)

add.rule(
  qs.strategy,
  name = "ruleSignal",
  arguments = list(
    sicol = "Cl.gt.SMA",
    sigval = TRUE,
    orderqty = 900,
    ordertype = "market",
    orderside = "long"),
  type = "enter"
)

add.rule(
  qs.strategy,
  name = "ruleSignal",
  arguments = list(
    sicol = "Cl.lt.SMA",
    sigval = TRUE,
    orderqty = "all",
    ordertype = "market",
    orderside = "long"),
  type = "exit"
)

applyStrategy(strategy = qs.strategy, portfolios = qs.strategy)

getTxns(Portfolio = qs.strategy, "SPY")

getTxns(Portfolio = qs.strategy, "SPY")

Txn.Qty Txn.Price Txn.Fees Txn.Value Txn.Avg.Cost Net.Txn.Realized.PL 1997-12-31

 0         0        0         0            0                   0

I would appreciate any feedbacks or replies! Thanks in advance!


回答1:


Check like 70. it should read sigcol not sicol.



来源:https://stackoverflow.com/questions/44816766/r-quantstrat-back-testing-is-not-working

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!