[R-SIG-Finance] quantstrat and nonstandard column names in the price series

Vladimir Egorin vegorin at yahoo.com
Wed Feb 9 20:08:28 CET 2011


Here is a hopefully small standalone example (a hacked script from one of the 
demos.).
Running it produces an error:
[1] "setup completed"
Error in .xts(e, .index(e1), .indexCLASS = indexClass(e1), .indexFORMAT = 
indexFormat(e1),  : 

  index length must match number of observations

In general, how would you specify the name of the underlying when 
adding rules, in case, for example, the signal is on the underlying X
and the order is on underlying Y.

Thank you.


try(rm("order_book.RSI",pos=.strategy),silent=TRUE)
try(rm("account.RSI","portfolio.RSI",pos=.blotter),silent=TRUE)
try(rm("account.st","portfolio.st","stock.str","stratRSI","initDate","initEq",'start_t','end_t'),silent=TRUE)


library('xts')
library('quantstrat')
alldates <- c("2011-01-03","2011-01-04","2011-01-05","2011-01-06","2011-01-07",
"2011-01-10","2011-01-11","2011-01-12","2011-01-13","2011-01-14",
"2011-01-18","2011-01-19","2011-01-20","2011-01-21","2011-01-24",
"2011-01-25","2011-01-26","2011-01-27","2011-01-28","2011-01-31",
"2011-02-01","2011-02-02","2011-02-03","2011-02-04","2011-02-07",
"2011-02-08")
values <- 
c(117.6562,117.6875,117.0156,117.3359,117.9922,118.1172,117.9531,117.9531,
118.2734,118.1719,118.1016,118.2344,117.6562,117.8438,117.8672,118.2422,
117.9688,118.2891,118.5547,118.4141,118.0703,117.6875,117.3984,116.8516,
116.8828,116.3438)

FV = xts(values, as.POSIXct(alldates, format="%Y-%m-%d"))
colnames(FV) <- 'FV'

# Initialize a strategy object
stratRSI <- strategy("RSI")

# Add an indicator
stratRSI <- add.indicator(strategy = stratRSI, name = "RSI", arguments = 
list(price = quote(getPrice(mktdata, prefer='FV'))), label="RSI")

# There are two signals:
# The first is when RSI is greater than 90
stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = 
list(threshold=70, column="RSI",relationship="gt", 
cross=TRUE),label="RSI.gt.70")
# The second is when RSI is less than 10
stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = 
list(threshold=30, column="RSI",relationship="lt",cross=TRUE),label="RSI.lt.30")

# There are two rules:
#'## we would Use osMaxPos to put trade on in layers, or to a maximum position. 
# The first is to sell when the RSI crosses above the threshold
stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = 
list(data=quote(getPrice(mktdata, prefer='FV')), sigcol="RSI.gt.70", 
sigval=TRUE, orderqty=-1000, ordertype='market', orderside='short', 
pricemethod='market', replace=FALSE), type='enter', path.dep=TRUE)
stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = 
list(data=quote(getPrice(mktdata, prefer='FV')), sigcol="RSI.lt.30", 
sigval=TRUE, orderqty='all', ordertype='market', orderside='short', 
pricemethod='market', replace=FALSE), type='exit', path.dep=TRUE)
# The second is to buy when the RSI crosses below the threshold
stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = 
list(data=quote(getPrice(mktdata, prefer='FV')), sigcol="RSI.lt.30", 
sigval=TRUE, orderqty= 1000, ordertype='market', orderside='long', 
pricemethod='market', replace=FALSE), type='enter', path.dep=TRUE)
stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = 
list(data=quote(getPrice(mktdata, prefer='FV')),sigcol="RSI.gt.70", sigval=TRUE, 
orderqty='all', ordertype='market', orderside='long', pricemethod='market', 
replace=FALSE), type='exit', path.dep=TRUE)

currency("USD")
symbols = c("FV")
for(symbol in symbols){ # establish trade-able instruments
    stock(symbol, currency="USD",multiplier=1)
}
initDate='2011-01-02'
initEq=100000
port.st<-'RSI' #use a string here for easier changing of parameters and 
re-trying

initPortf(port.st, symbols=symbols, initDate=initDate)
initAcct(port.st, portfolios=port.st, initDate=initDate)
initOrders(portfolio=port.st, initDate=initDate)

print("setup completed")

# Process the indicators and generate trades
start_t<-Sys.time()
out<-try(applyStrategy(strategy=stratRSI , portfolios=port.st, 
parameters=list(n=2) ) )
end_t<-Sys.time()
print("Strategy Loop:")
print(end_t-start_t)




----- Original Message ----
From: Vladimir Egorin <vegorin at yahoo.com>
To: r-sig-finance at r-project.org
Sent: Wed, February 9, 2011 12:24:11 PM
Subject: quantstrat and nonstandard column names in the price series

Does anyone have a short demo for quantstrat that does not use standard column 
names for price data in mktdata?  I am curious what the syntax would be in case, 

for example, when xts price series has a column named "XXX".
I figured so far that I need to provide a "prefer" value for the getPrice 
function to indicate the column name with the price, but the running backtest 
fails, apparently when parsing the orders.

Thanks, Vladimir



More information about the R-SIG-Finance mailing list