我正在处理Guy Yollin的quanstrat、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))然而,我不能得到它,在working..there循环之后总是出现这个错误:
Error in periodicity(table) :
can not calculate periodicity of 1 Observation这两行代码会产生以下错误:
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我忽略了什么?
发布于 2014-12-09 14:44:13
首先,直接回答两点:
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广泛使用延迟执行来允许代码重用。策略对象是策略规范的存储位置。它可以通过使用portfolios=参数应用于一个或多个公文包(由initPortf()创建)。
策略规范只是您稍后如何应用策略的仓库。在调用applyStrategy(...)之前,不会对任何内容进行计算。这允许使用有用的属性,例如使用相同的策略对象来测试多个不同的参数集,或者应用于不同的投资组合结构和组成部分,而不需要更改策略规范。
策略对象本身不会被applyStrategy更改。在applyStrategy内部,创建了一个名为mktdata的特殊内部对象,该对象将通过执行策略规范中包含的指示器、信号和规则进行修改。
默认情况下,mktdata对象是通过从.GlobalEnv或用户指定的其他环境中检索包含历史数据的对象来构造的。将为项目组合中的每个符号创建一个这些对象,并在applyStrategy的函数范围内进行维护。
当应用指示符和信号时,这些函数最常见的模式是返回与mktdata时间序列相同长度的向量,或者返回与mktdata时间序列具有相同索引的时间序列对象。如果遵循此模式,这些列将被添加到mktdata对象中,并可供以后的指示器、信号和规则函数使用。假定指示符和信号始终不依赖于路径,而默认情况下,规则依赖于路径。
示例信号函数(如sigCrossover和sigThreshold )利用此模式来访问和比较mktdata中存在的列。ruleSignal是一个示例规则,它查找信号具有某些特定值的点,然后根据该信息创建订单。
外部参照:
使用quantstrat (R/Finance 2013)
quantstrat简介(自由/开源软件交易博客)
https://stackoverflow.com/questions/18187085
复制相似问题