很抱歉,很长的帖子,我不知道如何减少它。
我已经开始使用Guy Yollin 的教程中的 blotter / quanstrat 包。如果我按原样使用 Yollin 先生的代码,不用担心我会得到类似的结果。
library(blotter)
currency("USD")
initDate <- "2000-01-01"
startDate <- "2000-01-02"
endDate <- "2016-07-01"
initEq <- 1e6
我所做的唯一更改是使用 Systematic Investor 在其Github上提供的稍作修改的函数使用本地存储在 .csv 中的数据。
这是功能。
getSymbols.sit <- function(
Symbols,
env = .GlobalEnv,
auto.assign = TRUE,
stock.folder = 'Google Drive/Software/TechnicalAnalysis/StockData',
stock.date.format = '%Y-%m-%d',
...)
{
require(quantmod)
for(i in 1:length(Symbols)) {
s = Symbols[i]
temp = list()
temp[[ s ]] = list(src='csv', format=stock.date.format, dir=stock.folder)
setSymbolLookup(temp)
temp = quantmod::getSymbols(s, env = env, auto.assign = auto.assign)
if (!auto.assign) {
cat(s, format(range(index(temp)), '%d-%b-%Y'), '\n', sep='\t')
return(temp)
}
if(!is.null(env[[ s ]]))
cat(i, 'out of', length(Symbols), 'Reading', s, format(range(index(env[[ s ]])), '%d-%b-%Y'), '\n', sep='\t')
else
cat(i, 'out of', length(Symbols), 'Missing', s, '\n', sep='\t')
}
}
然后调用它。
getSymbols.sit("SPY", source = "yahoo", from=startDate, to=endDate, adjust=T)
到目前为止,一切似乎都有效。现在继续基于 Faber 的 10 个月 SMA 构建回溯测试。
stock("SPY", currency = "USD", multiplier = 1)
SPY=to.monthly(SPY, indexAt = 'endof', drop.time = FALSE)
SPY$SMA10m <- SMA(Cl(SPY), n=10)
portfolio.st <- "portf.faber"
account.st <- "acct.faber"
initPortf(portfolio.st, "SPY", initDate = initDate)
initAcct(account.st, portfolios = portfolio.st, initDate = initDate, initEq = initEq)
现在创建策略并运行它。
for(i in 1:nrow(SPY))
{
#set up all the values for the specific date and update them in the loop
actualDate <- time(SPY)[i]
equity = getEndEq(Account = account.st, Date = actualDate)
closePrice <- as.numeric(Cl(SPY[i]))
posn <- getPosQty(Portfolio = portfolio.st, Symbol = "SPY", Date = actualDate)
unitSize = as.numeric(trunc(equity/closePrice))
ma <- as.numeric(SPY$SMA10m[i])
#Take market decision
if( !is.na(ma) ) { #we have to wait to have our first 10sma
if( posn == 0 ) { #if no position then we go long
if( closePrice > ma ) {
addTxn(Portfolio = portfolio.st, Symbol = "SPY", TxnDate = actualDate,
TxnQty = unitSize, TxnPrice = closePrice, TxnFees = 0) }
} else {
if( closePrice < ma ) { #sell share and go cash if closing price < 10sma
addTxn(Portfolio = portfolio.st, Symbol = "SPY", TxnDate = actualDate,
TxnQty = -posn, TxnPrice = closePrice, TxnFees = 0)
} else {
if( i == nrow(SPY) ) #last recorded price, we close the system
addTxn(Portfolio = portfolio.st, Symbol = "SPY", TxnDate = actualDate,
TxnQty = -posn, TxnPrice = closePrice, TxnFees = 0)
}
}
}
updatePortf(portfolio.st, Dates = actualDate)
updateAcct(name = account.st, Dates = actualDate)
updateEndEq(Account = account.st, actualDate)
}
尽管脚本运行没有错误,但我开始关注 3 件事。
- 使用时的警告
getSymbols.sit()
。我不知道该怎么办。这是警告。
1 out of 1 Reading SPY 03-Jan-2000 19-Jul-2016 Warning message: In if (as.character(sc[[1]]) != calling.fun) return() : the condition has length > 1 and only the first element will be used
- 运行最后 3 个更新函数时的警告。
There were 50 or more warnings (use warnings() to see the first 50) 1: In updatePortf(portfolio.st, Dates = actualDate) : Incompatible methods ("Ops.POSIXt", "Ops.Date") for ">="
- 如果我使用 checkBlotterUpdate(如教程中给出的)函数,我会收到错误消息。
checkBlotterUpdate(portfolio.st, account.st) [1] "portfolio P&L doesn't match sum of symbols P&L" [1] FALSE
所以看来我不得不担心这些警告,但我不知道如何解决这个问题。
如果用 just 运行整个事情,就getSymbols("SPY")
没有问题。但我真的很希望能够使用本地存储的数据进行回测。我住在赞比亚、非洲,互联网/电力并不总是可靠的。提前感谢您的任何提示。