Hi Roger, I don't get an error using either data object. Ensure you have the latest quantstrat and blotter from R-Forge and try running the example in the email again. If you still get an error, please provide the output from traceback() and sessionInfo(). Best, Josh
On Fri, Feb 26, 2016 at 11:28 AM, Bos, Roger <roger.bos at rothschild.com> wrote:
Dear all quantstrat users, I am trying to learn how to use quantstrat with pricing data read in from a csv file using getSymbols(symbols, src='csv'). I think I have that part working, but I am having a problem that I cannot debug myself. The following code is from the faber_rebal demo. To make it reproducible I have used dput() to generate a truncated version of the csv pricing data for my example symbol G12 (actually XLF). I have truncated the data to make it easier to post this message. The first version of G12 does not work and the second version does. The only difference is the starting point. If the G12 data starts in 1/31/1999 the code works fine, but if the G12 data starts in 12/31/1998 the code gives an error in applyStrategy.rebalancing:
out<-applyStrategy.rebalancing(strategy='faber' , portfolios='faber')
Error in if (is.na(endDate)) endDate <- NULL : argument is of length zero
Thanks in advance for any advice on this, Roger
# Not Working (starting at 12/31/1998)
G12 <- structure(c(23.4375, 23.84375, 24.21875, 24.9375, 26.6875, 25.078125,
26.09375, 24.5, 23.421875, 22.125, 25.5625, 24.4375, 23.765625,
23.0625, 20.59375, 24.265625, 24.5, 25.046875, 23.75, 25.984375,
28.46875, 29, 28.75, 27.28125, 29.5, 23.8125, 25.0625, 24.875,
26.3125, 29.28125, 27.28125, 26.3125, 26.84375, 25.796875, 24.25,
26.15625, 26.78125, 25.40625, 24.078125, 23.234375, 25.1875,
25.796875, 25.1875, 27.15625, 26.34375, 28.625, 29.625, 29.625,
29.21875, 30.0625, 23.21875, 22.71875, 22.0625, 24.015625, 24.75,
24.375, 24.15625, 24.46875, 22.65625, 21.5, 20.875, 24.03125,
22.71875, 21.46875, 19.65625, 19.46875, 22.125, 22.65625, 23.40625,
23.5625, 25.875, 27.125, 24.875, 25.75, 26.71875, 23.4375, 23.84375,
24.21875, 24.9375, 26.6875, 25.078125, 26.09375, 24.5, 23.421875,
22.125, 25.5625, 24.4375, 23.765625, 23.0625, 20.59375, 24.265625,
24.5, 25.046875, 23.75, 25.984375, 28.46875, 29, 28.75, 27.28125,
29.5, 447800, 2057500, 2334300, 3122300, 2662900, 3307500, 2091900,
2333800, 3945700, 5653600, 9955200, 5993600, 14715300, 8663400,
8425500, 15117400, 8647800, 5391700, 9438800, 4609900, 4224100,
9112300, 18210900, 17317600, 21173200, 23.4375, 23.84375, 24.21875,
24.9375, 26.6875, 25.078125, 26.09375, 24.5, 23.421875, 22.125,
25.5625, 24.4375, 23.765625, 23.0625, 20.59375, 24.265625, 24.5,
25.046875, 23.75, 25.984375, 28.46875, 29, 28.75, 27.28125, 29.5),
.Dim = c(25L, 6L), .Dimnames = list(NULL, c("G12.Open", "G12.High",
"G12.Low", "G12.Close", "G12.Volume", "G12.Adjusted")), index = structure(c(915062400,
917740800, 920160000, 922838400, 925430400, 928108800, 930700800,
933379200, 936057600, 938649600, 941328000, 943920000, 946598400,
949276800, 951782400, 954460800, 957052800, 959731200, 962323200,
965001600, 967680000, 970272000, 972950400, 975542400, 978220800
), tclass = "Date"), tclass = "Date", tzone = "UTC", src = "csv", updated = structure(1456505311.48799, class = c("POSIXct",
"POSIXt")), .indexCLASS = "Date", .indexTZ = "UTC", .indexFORMAT = "%Y-%m-%d", class = c("xts","zoo"))
# Working (starting at 1/31/1999)
G12 <- structure(c(23.84375, 24.21875, 24.9375, 26.6875, 25.078125,
26.09375, 24.5, 23.421875, 22.125, 25.5625, 24.4375, 23.765625,
23.0625, 20.59375, 24.265625, 24.5, 25.046875, 23.75, 25.984375,
28.46875, 29, 28.75, 27.28125, 29.5, 25.0625, 24.875, 26.3125,
29.28125, 27.28125, 26.3125, 26.84375, 25.796875, 24.25, 26.15625,
26.78125, 25.40625, 24.078125, 23.234375, 25.1875, 25.796875,
25.1875, 27.15625, 26.34375, 28.625, 29.625, 29.625, 29.21875,
30.0625, 22.71875, 22.0625, 24.015625, 24.75, 24.375, 24.15625,
24.46875, 22.65625, 21.5, 20.875, 24.03125, 22.71875, 21.46875,
19.65625, 19.46875, 22.125, 22.65625, 23.40625, 23.5625, 25.875,
27.125, 24.875, 25.75, 26.71875, 23.84375, 24.21875, 24.9375,
26.6875, 25.078125, 26.09375, 24.5, 23.421875, 22.125, 25.5625,
24.4375, 23.765625, 23.0625, 20.59375, 24.265625, 24.5, 25.046875,
23.75, 25.984375, 28.46875, 29, 28.75, 27.28125, 29.5, 2057500,
2334300, 3122300, 2662900, 3307500, 2091900, 2333800, 3945700,
5653600, 9955200, 5993600, 14715300, 8663400, 8425500, 15117400,
8647800, 5391700, 9438800, 4609900, 4224100, 9112300, 18210900,
17317600, 21173200, 23.84375, 24.21875, 24.9375, 26.6875, 25.078125,
26.09375, 24.5, 23.421875, 22.125, 25.5625, 24.4375, 23.765625,
23.0625, 20.59375, 24.265625, 24.5, 25.046875, 23.75, 25.984375,
28.46875, 29, 28.75, 27.28125, 29.5), .Dim = c(24L, 6L), .Dimnames = list(
NULL, c("G12.Open", "G12.High", "G12.Low", "G12.Close", "G12.Volume",
"G12.Adjusted")), index = structure(c(917740800, 920160000,
922838400, 925430400, 928108800, 930700800, 933379200, 936057600,
938649600, 941328000, 943920000, 946598400, 949276800, 951782400,
954460800, 957052800, 959731200, 962323200, 965001600, 967680000,
970272000, 972950400, 975542400, 978220800), tclass = "Date"), tclass = "Date", tzone = "UTC", src = "csv",
updated = structure(1456505447.82159, class = c("POSIXct",
"POSIXt")), .indexCLASS = "Date", .indexTZ = "UTC", .indexFORMAT = "%Y-%m-%d", class = c("xts","zoo"))
# Load required libraries
require(quantstrat)
#correct for TZ issues if they crop up
oldtz <- Sys.getenv('TZ')
if(oldtz=='') {
Sys.setenv(TZ="UTC")
}
# Try to clean up in case the demo was run previously
suppressWarnings(rm("account.faber","portfolio.faber",pos=.blotter))
suppressWarnings(rm("ltaccount", "ltportfolio", "ClosePrice", "CurrentDate", "equity",
"GSPC", "stratFaber", "startDate", "initEq", "Posn", "UnitSize", "verbose"))
suppressWarnings(rm("order_book.faber",pos=.strategy))
# Set initial values
startDate='1997-12-31'
initEq=100000
# Set up instruments with FinancialInstruments package
currency("USD")
#symbols = c("XLF", "XLP", "XLE", "XLY", "XLV", "XLI", "XLB", "XLK", "XLU")
symbols = c("G12")
for(symbol in symbols){ # establish tradable instruments
stock(symbol, currency="USD",multiplier=1)
}
# Load data with quantmod
#getSymbols(symbols, src='yahoo', index.class=c("POSIXt","POSIXct"), from='1998-01-01')
### Download monthly data instead?
### GSPC=to.monthly(GSPC, indexAt='endof')
#getSymbols(symbols, src='yahoo', index.class=c("POSIXt","POSIXct"), from='1999-01-01')
#getSymbols(symbols, src='csv')
for(symbol in symbols) {
x<-get(symbol)
x<-to.monthly(x,indexAt='lastof',drop.time=TRUE)
indexFormat(x)<-'%Y-%m-%d'
colnames(x)<-gsub("x",symbol,colnames(x))
assign(symbol,x)
}
# Initialize portfolio and account
initPortf('faber', symbols=symbols)
initAcct('faber', portfolios='faber', initEq=100000)
initOrders(portfolio='faber')
# set intial position limits
posval <- initEq/length(symbols)
for(symbol in symbols){
#pos <- round((posval/first(getPrice(get(symbol)))[,1]),-2)
pos <- round((posval/first(getPrice(get(symbol)))),-2)
addPosLimit('faber', symbol, startDate, maxpos=pos, minpos=-pos)
}
print("setup completed")
# Initialize a strategy object
strategy("faber", store=TRUE)
# Add an indicator
add.indicator('faber', name = "SMA", arguments = list(x = quote(Cl(mktdata)), n=10), label="SMA10")
# There are two signals:
# The first is when monthly price crosses over the 10-month SMA
add.signal('faber',name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="gte"),label="Cl.gt.SMA")
# The second is when the monthly price crosses under the 10-month SMA
add.signal('faber',name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="lt"),label="Cl.lt.SMA")
# There are two rules:
# The first is to buy when the price crosses above the SMA
add.rule('faber', name='ruleSignal', arguments = list(sigcol="Cl.gt.SMA", sigval=TRUE, orderqty=100000, osFUN='osMaxPos', ordertype='market', orderside='long', pricemethod='market',TxnFees=-5), type='enter', path.dep=TRUE)
# The second is to sell when the price crosses below the SMA
add.rule('faber', name='ruleSignal', arguments = list(sigcol="Cl.lt.SMA", sigval=TRUE, orderqty='all', ordertype='market', orderside='long', pricemethod='market',TxnFees=-5), type='exit', path.dep=TRUE)
# add quaterly rebalancing
add.rule('faber', 'rulePctEquity',
arguments=list(rebalance_on='quarters',
trade.percent=1/length(symbols),
refprice=quote(last(getPrice(mktdata)[paste('::',as.character(curIndex),sep='')][,1])),
digits=0
),
type='rebalance',
label='rebalance'
)
# Process the strategy and generate trades
start_t<-Sys.time()
out<-applyStrategy.rebalancing(strategy='faber' , portfolios='faber')
end_t<-Sys.time()
print("Strategy Loop:")
print(end_t-start_t)
_______________________________________________ R-SIG-Finance at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-finance -- Subscriber-posting only. If you want to post, subscribe first. -- Also note that this is not the r-help list where general R questions should go.
Joshua Ulrich | about.me/joshuaulrich FOSS Trading | www.fosstrading.com R/Finance 2016 | www.rinfinance.com