Hi listers,
I'm having some trouble to get quantstrat work on intraday data I'm
pulling out of Bloomberg. The orginal demo (rsi.R) came with quantstrat.pkg
works fine on daily data, but when I try to replace the daily time series
with intraday data (30min OHLC):
currency("USD")
currency("EUR")
symbols = c("GCA" )#, "XAGUSD", "XPTUSD", "XPDUSD")
for(symbol in symbols){ # establish trade-able instruments
stock(symbol, currency="USD",multiplier=1)
getSymbols(symbol,src="Bloomberg", *bb.interval = "30"*,
bb.suffix="Curncy",*from=as.POSIXlt(Sys.time()-60*4000,"GMT"),*
* *
*to=as.POSIXlt(Sys.time(),"GMT")*)
}
> head(GCA)
GCA.Open GCA.High GCA.Low GCA.Close GCA.Volume
2012-01-23 20:07:00 1678.8 1681.1 1678.3 1679.9 65
2012-01-23 20:37:00 1680.0 1681.5 1680.0 1680.3 84
2012-01-23 21:07:00 1680.6 1680.6 1679.7 1679.7 15
2012-01-23 21:37:00 1680.3 1680.6 1680.3 1680.4 4
2012-01-23 22:07:00 1679.7 1681.1 1679.3 1681.1 3
2012-01-23 23:00:00 1680.6 1680.6 1680.4 1680.6 3
applyStrategy() seems generating bogus pnl even though applySignals and
applyRules seems working properly. Anyone can tell me how I can work around
with this and get it works around intraday data?
Regards,
Yuanhang
[code]
require(quantstrat)
require(RBloomberg)
suppressWarnings(rm("order_book.RSI",pos=.strategy))
suppressWarnings(rm("account.RSI","portfolio.RSI",pos=.blotter))
suppressWarnings(rm("account.st","portfolio.st
","stock.str","stratRSI","initDate","initEq",'start_t','end_t'))
# Initialize a strategy object
stratRSI <- strategy("RSI")
# Add an indicator
stratRSI <- add.indicator(strategy = stratRSI, name = "RSI", arguments =
list(price = quote(getPrice(mktdata)),n=2), label="RSI")
# There are two signals:
stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments =
list(threshold=55, column="RSI",relationship="gt",
cross=TRUE),label="RSI.gt.55")
stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments =
list(threshold=55,
column="RSI",relationship="lt",cross=TRUE),label="RSI.lt.55")
stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments =
list(threshold=45, column="RSI",relationship="gt",
cross=TRUE),label="RSI.gt.45")
# The second is when RSI is less than 10
stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments =
list(threshold=45,
column="RSI",relationship="lt",cross=TRUE),label="RSI.lt.45")
# 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(sigcol="RSI.gt.55", 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(sigcol="RSI.lt.55", sigval=TRUE, orderqty='all', ordertype='market',
orderside='short', pricemethod='market', replace=FALSE), type='exit',
path.dep=TRUE)
stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments =
list(sigcol="RSI.lt.45", 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(sigcol="RSI.gt.45", sigval=TRUE, orderqty='all', ordertype='market',
orderside='short', pricemethod='market', replace=FALSE), type='exit',
path.dep=TRUE)
#add changeable parameters
currency("USD")
currency("EUR")
symbols = c("GCA" )#, "XAGUSD", "XPTUSD", "XPDUSD")
for(symbol in symbols){ # establish trade-able instruments
stock(symbol, currency="USD",multiplier=1)
getSymbols(symbol,src="Bloomberg", bb.interval = "30",
bb.suffix="Curncy",from=as.POSIXlt(Sys.time()-60*4000,"GMT"),
to=as.POSIXlt(Sys.time(),"GMT"))
}
# you can test with something like this:
# applySignals(strategy=stratRSI,
mktdata=applyIndicators(strategy=stratRSI, mktdata=symbols[1]))
initDate=Sys.Date()-1
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), verbose =FALSE ) )
end_t<-Sys.time()
print("Strategy Loop:")
print(end_t-start_t)
# look at the order book
#print(getOrderBook(port.st))
start_t<-Sys.time()
updatePortf(Portfolio=port.st,Dates=paste('::',as.Date(Sys.time()),sep=''))
end_t<-Sys.time()
print("trade blotter portfolio update:")
print(end_t-start_t)
# hack for new quantmod graphics, remove later
themelist<-chart_theme()
themelist$col$up.col<-'lightgreen'
themelist$col$dn.col<-'pink'
for(symbol in symbols){
dev.new()
chart.Posn(Portfolio=port.st,Symbol=symbol,theme=themelist)
plot(add_RSI(n=2,RSIup=55,RSIdn=45))
}
###############################################################################
# R (http://r-project.org/) Quantitative Strategy Model Framework
#
# Copyright (c) 2009-2010
# Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and
Joshua Ulrich
#
# This library is distributed under the terms of the GNU Public License
(GPL)
# for full details see the file COPYING
#
# $Id: rsi.R 621 2011-06-09 23:18:04Z gsee $
#
###############################################################################
[[alternative HTML version deleted]]
_______________________________________________
[email protected] 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.