Quantstrat:根据股票代码Y中的信号交易股票代码X.

时间:2017-09-26 07:30:25

标签: r quantmod quantstrat

我无法弄清楚如何根据来自X和Y的组合创建的合成资产的信号来回溯测试交易股票X和股票代码Y的策略。

数据背景是XTS系列代码的列表。现在我通过交易合成资产而不是单个资产来解决它:

initDate = "1990-01-01"
from = "2010-07-22"
to = "2016-12-31"
initeq = 1000000
NBDG <- lXTS[[1]]
UKPSPIR <- lXTS[[2]]
CoIntV <- list(1, -9.90)
Diff <- NBDG - as.numeric(CoIntV[2])*UKPSPIR
colnames(Diff) <- "Close"

strategy.st <- portfolio.st <- account.st <- "test"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols = list("Diff"), initDate=initDate)
initAcct(account.st, portfolios=portfolio.st)
initOrders(portfolio.st)
strategy(strategy.st, store = TRUE)

Diff <- cbind(Diff, BBands(Diff, maType="SMA", n=12, sd=2))

add.signal(strategy=strategy.st, name="sigCrossover",
           arguments = list(columns=c("Close", "up"),
                            relationship="gt"),
           label="cl.gt.up")

add.signal(strategy=strategy.st, name="sigCrossover",
           arguments = list(columns=c("Close", "dn"),
                            relationship="lt"),
           label="cl.lt.dn")

add.signal(strategy=strategy.st, name="sigCrossover",
           arguments = list(columns=c("Close", "mavg"),
                            relationship="gte"),
           label="mid.cross.frombelow")

add.signal(strategy=strategy.st, name="sigCrossover",
           arguments = list(columns=c("Close", "mavg"),
                            relationship="lte"),
           label="mid.cross.fromabove")

tmp <- applySignals(strategy = strategy.st, mktdata=Diff)

add.rule(stratBBands,name='ruleSignal',
         arguments = list(sigcol="cl.gt.up",
                          sigval=TRUE, 
                          orderqty=-1, 
                          ordertype='market', 
                          orderside=NULL, 
                          threshold=NULL),
         type='enter')

add.rule(stratBBands,name='ruleSignal',
         arguments = list(sigcol="cl.lt.dn",
                          sigval=TRUE, 
                          orderqty=1, 
                          ordertype='market', 
                          orderside=NULL, 
                          threshold=NULL),
         type='enter')

add.rule(stratBBands,name='ruleSignal',
         arguments = list(sigcol="mid.cross.frombelow",
                          sigval=TRUE, 
                          orderqty='all', 
                          ordertype='market', 
                          orderside=NULL, 
                          threshold=NULL),
         type='exit')

add.rule(stratBBands,name='ruleSignal',
         arguments = list(sigcol="mid.cross.fromabove",
                          sigval=TRUE, 
                          orderqty='all', 
                          ordertype='market', 
                          orderside=NULL, 
                          threshold=NULL),
         type='exit')

out <- applyStrategy(strategy=strategy.st,portfolios=portfolio.st)

updatePortf(portfolio.st)
dateRange <- time(getPortfolio(portfolio.st)$summary)[-1]
updateAcct(portfolio.st,dateRange)
updateEndEq(account.st)

这样做时会收到以下警告:

1: In getInstrument(symbol) :
  instrument Diff not found, please create it first.
2: In getInstrument(Symbol) :
  instrument Diff not found, please create it first.
3: In .updatePosPL(Portfolio = pname, Symbol = as.character(symbol),  :
  Instrument Diff  not found, things may break

但我得到了结果。

有人知道吗?

1 个答案:

答案 0 :(得分:2)

您还没有定义您的仪器,这是quantstrat所期望的(尽管您的模拟可能仍然运行正常)。正如警告告诉你的那样......在你运行策略之前定义合成工具(在上面的代码中rm.strat之前,比如说)。

您还应该定义您的货币(不确定它是GBP,但默认情况下它是USD,我在这里假设:

currency(c("USD"))
spread(primary_id = 'Diff', currency = "USD", members = c('NBDG','UKPSPIR'), memberratio = c(1, -9.90))

使用这些更改运行代码,警告将消失。

(此外,在您发布的代码中,您已在strategy.st处从stratBBandsadd.rules任意更改。)