Reputation: 751
Sorry in advance for the long post, I'm not sure how to reduce it though.
I have started using the blotter / quanstrat package from this tutorial of Guy Yollin. If I use Mr. Yollin code as it is, no worries I'm getting similar results.
library(blotter)
currency("USD")
initDate <- "2000-01-01"
startDate <- "2000-01-02"
endDate <- "2016-07-01"
initEq <- 1e6
The only change I am making is to use data locally stored on a .csv using a slightly modified function given by the Systematic Investor on his Github.
Here is the function.
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')
}
}
Then calling it.
getSymbols.sit("SPY", source = "yahoo", from=startDate, to=endDate, adjust=T)
Everything seems to work so far. and now continuing building the back test based on the 10 months SMA from Faber.
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)
Now creatingn the strategy and running it.
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)
}
Although the script run through without error, there are 3 things that I am getting concern about.
getSymbols.sit()
. and I am not sure what to do about it. Here is the warning.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
So it seems I have to be worry about these warnings but I'm not sure how to fix the problem.
If run the whole thing with just getSymbols("SPY")
, there is no issues. But I would really like to be able to backtest using locally stored data. I live in Zambia, Africa and internet / power is not always reliable.
Thanks in advance for any hint.
Upvotes: 1
Views: 429
Reputation: 2026
I've submitted a pull request to the quantmod github. You now have four options with regard to Warning #1:
getSymbols
(without the namespace designation) instead of quantmod::getSymbols
Upvotes: 1