没有交易/排名到Chart Quantstrat,“逻辑”错误?

时间:2018-07-15 06:47:54

标签: r quantstrat

我收到一条错误消息,尽管使用了信号和规则,但仍然没有要绘制的交易/位置。 有人可以解释我在做什么吗? 也许是在规则的逻辑发展中? 我希望我的规则可以执行以下操作: 如果CNOwma超过0.6,则输入多头并退出空头。 如果CNOwma跌破0.6,则输入空头并退出多头。 请参考以下代码。 错误在最后一行。

谢谢。

if (!require("TTR")) {
  install.packages("TTR")
  library(TTR)
}
if (!require("quantstrat")) {
  if(!require("devtools")) {
    install.packages("devtools")
    require(devtools)
  }
  install_github("braverock/blotter") # dependency
  install_github("braverock/quantstrat")
}

if (!require("IKTrading")){
  install_github("IlyaKipnis/IKTrading", force=TRUE)
}

library(devtools)
library(quantmod)
library(quantstrat)
library(TTR)
library(IKTrading)

rm(list = ls(.blotter), envir = .blotter)
initdate <- "2010-01-01"
from <- "2012-01-01" #start of backtest
to <- "2017-31-12" #end of backtest

Sys.setenv(TZ= "EST") #Set up environment for timestamps

currency("USD") #Set up environment for currency to be used

symbols <- c("RUT") #symbols used in our backtest
getSymbols("^RUT",src="yahoo", from="2012-01-01", to="2017-12-31", periodicity="daily")

stock(symbols, currency = "USD", multiplier = 1) #tells quanstrat what instruments present and what currency to use

n <- 30

wma <-  WMA(Cl(RUT), n=4, wts=c(1:4))
wmamaxt <-  rollmaxr(wma, n, fill = NA)
wmamint <- - rollmaxr(- wma, n, fill = NA)
CNOwma <- function (RUT) {(wma - wmamint) / (wmamaxt - wmamint)}

tradesize <-10000 #default trade size
initeq <- 100000 #default initial equity in our portfolio

strategy.st <- portfolio.st <- account.st <- "firststrat" #naming strategy, portfolio and account

#removes old portfolio and strategy from environment
rm.strat(portfolio.st)
rm.strat(strategy.st) 

#initialize portfolio, account, orders and strategy objects
initPortf(portfolio.st, symbols = symbols, initDate = initdate, currency = "USD")

initAcct(account.st, portfolios = portfolio.st, initDate = initdate, currency = "USD", initEq = initeq)

initOrders(portfolio.st, initDate = initdate)
strategy(strategy.st, store=TRUE)

add.indicator(strategy = strategy.st,
name = 'CNOwma',
arguments = list(x = quote(Cl(mktdata)), n=4),
label = 'CNOwma4')





add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CNOwma4", threshold = 0.6,
relationship = "gt", cross = TRUE),
label = "longthreshold")


add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "CNOwma4", threshold = 0.6,
relationship = "lt", cross = TRUE),
label = "shortthreshold")




add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"), osFUN = IKTrading::osMaxDollar,
tradeSize = tradesize, maxSize = tradesize, type = "enter")


add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "long", replace = FALSE,
prefer = "Open"),
type = "exit")

add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "shortthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),osFUN = IKTrading::osMaxDollar, 
tradeSize = tradesize, maxSize = tradesize, type = "enter")

add.rule(strategy.st, name = "ruleSignal",
arguments = list(sigcol = "longthreshold", sigval = TRUE,
orderqty = "all", ordertype = "market",
orderside = "short", replace = FALSE,
prefer = "Open"),
type = "exit")



out <- applyStrategy(strategy = strategy.st, portfolios = portfolio.st)
updatePortf(portfolio.st)
daterange <- time(getPortfolio(portfolio.st)$summary)[-1]

updateAcct(account.st, daterange)
updateEndEq(account.st)


for(symbol in symbols){

chart.Posn(Portfolio = portfolio.st, Symbol = symbol, 
TA= c("add_SMA(n=50, col='blue')", "add_SMA(n=200, col='red')"))
}

Error in chart.Posn(Portfolio = portfolio.st, Symbol = symbol, TA = c("add_SMA(n=50, col='blue')",  : 
  no transactions/positions to chart

1 个答案:

答案 0 :(得分:2)

我解决了这个问题。 如果对将来的某个人有帮助,我将发布如何解决该问题的方法。

在Quantstrat中进行回测会导致很多错误,这是由于列名的规范不正确所致。

此问题已通过将代码更改为:

add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "X1.CNOwma4", threshold = 0.6,
relationship = "gt", cross = TRUE),
label = "longthreshold")

add.signal(strategy.st, name = "sigThreshold",
arguments = list(column = "X1.CNOwma4", threshold = 0.6,
relationship = "lt", cross = TRUE),
label = "shortthreshold")

也就是说,列=“ CNOwma4”被列=“ X1.CNOwma4”代替