对于长篇帖子提前抱歉,我不知道如何减少它 我已经开始使用来自Guy Yollin的this tutorial的吸墨纸/ quanstrat包。如果我按原样使用Yollin先生的代码,不用担心我会得到类似的结果。
library(blotter)
currency("USD")
initDate <- "2000-01-01"
startDate <- "2000-01-02"
endDate <- "2016-07-01"
initEq <- 1e6
我所做的唯一改变是使用系统投资者在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)
}
虽然脚本没有错误地运行,但我有三件事需要关注。
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
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(portfolio.st, account.st) [1] "portfolio P&L doesn't match sum of symbols P&L" [1] FALSE
所以我似乎要担心这些警告,但我不确定如何解决这个问题。
如果仅使用getSymbols("SPY")
运行整个事情,则没有问题。但我真的希望能够使用本地存储的数据进行回测。我住在非洲的赞比亚,互联网/电力并不总是可靠的。
提前感谢任何暗示。
答案 0 :(得分:1)
我已向quantmod github提交了pull request。关于警告#1,您现在有四个选项:
getSymbols
(没有命名空间指定)而不是quantmod::getSymbols