R:Quantstrat的例子是盖伊·约林

R:Quantstrat的例子是盖伊·约林,r,quantstrat,blotter,R,Quantstrat,Blotter,我正在浏览Guy Yollin为Quanstart blotter等制作的幻灯片。以下是我试图执行的代码: #According to quantstrat lectures 1-3 von Guy Yollin library(blotter) library(FinancialInstrument) source("chart_Posn.R") currency("USD") stock("SPY",currency="USD",multiplier=1) getSymbols('SPY

我正在浏览Guy Yollin为Quanstart blotter等制作的幻灯片。以下是我试图执行的代码:

#According to quantstrat lectures 1-3 von Guy Yollin

library(blotter)
library(FinancialInstrument)
source("chart_Posn.R")

currency("USD")
stock("SPY",currency="USD",multiplier=1)
getSymbols('SPY', from='1998-01-01', to='2011-07-31', adjust=T)
SPY=to.monthly(SPY, indexAt='endof')
SPY$SMA10m <- SMA(Cl(SPY), 10)

####################################
# Initialize portfolio and account #
####################################
#Initialize portfolio and account
b.strategy <- "bFaber" #Is only the name for the portfolio strategy
initPortf(b.strategy,'SPY', initDate='1997-12-31')
initAcct(b.strategy,portfolios=b.strategy, initDate='1997-12-31', initEq=1e6)

#######################
# Formating the chart #
#######################
theme<-chart_theme()
theme$col$up.col<-'lightgreen'
theme$col$up.border<-'lightgreen'
theme$col$dn.col<-'pink'
theme$col$dn.border<-'pink'
chart_Series(SPY,theme=theme,name="SPY")
plot(add_SMA(n=10,col=4,lwd=2))

#################
# Trading logic # (buy when monthly price > 10-month SMA, sell when monthly price < 10-month SMA)
#################
for( i in 1:nrow(SPY) ) {
  CurrentDate <- time(SPY)[i]
  ClosePrice <- as.numeric(Cl(SPY[i,]))
  Posn <- getPosQty(b.strategy, Symbol='SPY', Date=CurrentDate)
  if( !is.na(as.numeric(SPY[i,'SMA10m'])) ) {
    if( Posn == 0 ) { # No position, test to go Long
      if( ClosePrice > as.numeric(SPY[i,'SMA10m']) ) {
        # enter long position
        addTxn(b.strategy, Symbol='SPY', TxnDate=CurrentDate,
               TxnPrice=ClosePrice, TxnQty = 1000 , TxnFees=0) }
    } else { # Have a position, so check exit
      if( ClosePrice < as.numeric(SPY[i,'SMA10m']) ) {
        # exit position
        addTxn(b.strategy, Symbol='SPY', TxnDate=CurrentDate,
               TxnPrice=ClosePrice, TxnQty = -Posn , TxnFees=0) }
    }
  }
  # Calculate P&L and resulting equity with blotter
  updatePortf(b.strategy, Dates = CurrentDate)
  updateAcct(b.strategy, Dates = CurrentDate)
  updateEndEq(b.strategy, Dates = CurrentDate)
} # End dates loop

chart.Posn(b.strategy, Symbol='SPY', Dates='1998::')
plot(add_SMA(n=10,col=4,on=1,lwd=2))
这两行代码会产生以下错误:

chart.Posn(b.strategy, Symbol='SPY', Dates='1998::')
plot(add_SMA(n=10,col=4,on=1,lwd=2)
> chart.Posn(b.strategy, Symbol='SPY', Dates='1998::')
Error in as.POSIXct.numeric(first(index(Position))) : 
  'origin' must be supplied
> plot(add_SMA(n=10,col=4,on=1,lwd=2))
Warning message:
In mapply(function(name, value) { :
  longer argument not a multiple of length of shorter

我忽略了什么

首先,直接回答两点:

indicators are not necessary to a strategy, the only hard and fast requirement is that you need at least one rule that will create orders, or at least one rule in the order slot that will create transactions.

the strategy object contains only the specification of the strategy, nothing more, see below.
接下来,解释发生了什么:

quantstrat广泛使用延迟执行,以允许代码重用。策略对象是策略规范的存储位置。它可以通过使用portfolions=参数应用于一个或多个公文包(由initPortf()创建)

策略规范只是一个仓库,用来存放您以后如何应用策略。在调用applyStrategy(…)之前,不会计算任何内容。这允许使用有用的属性,例如使用相同的策略对象来测试多个不同的参数集,或者在不改变策略规范的情况下应用不同的投资组合结构和成分

applyStrategy不会更改策略对象本身。在applyStrategy内部,将创建一个名为mktdata的特殊内部对象,该对象将通过执行策略规范中包含的指标、信号和规则进行修改

默认情况下,mktdata对象是通过从.GlobalEnv或用户指定的其他环境检索包含历史数据的对象来构造的。将为公文包中的每个符号创建其中一个对象,并在applyStrategy的功能范围内进行维护

应用指示器和信号时,这些函数最常见的模式是返回与mktdata时间序列长度相同的向量,或返回与mktdata时间序列具有相同索引的时间序列对象。如果遵循此模式,这些列将添加到mktdata对象中,并可供以后的指示器、信号和规则函数使用。指示器和信号被假定为始终不依赖于路径,而规则在默认情况下依赖于路径

示例信号函数(如SIGCROSSION和SIGSTHORD)利用此模式访问和比较mktdata中存在的列。ruleSignal是一个示例规则,它查找信号具有特定值的点,然后根据该信息创建订单

外部参考:

使用quantstrat(R/Finance 2013)

quantstrat简介(自由和开放源码软件交易博客)


我很确定这是最近修复的吸墨纸中的一个bug。在R-Forge上将blotter更新为最新版本,然后重试。我面临一个非常类似的错误,我正在使用最新版本的require包。如果有人感兴趣的话,这里有一个我发布的问题的链接:这两个都不适合我……我有最新版本的吸墨纸
indicators are not necessary to a strategy, the only hard and fast requirement is that you need at least one rule that will create orders, or at least one rule in the order slot that will create transactions.

the strategy object contains only the specification of the strategy, nothing more, see below.