[Blotter-commits] r370 - pkg/quantstrat/demo
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Thu Aug 12 15:52:05 CEST 2010
Author: braverock
Date: 2010-08-12 15:52:05 +0200 (Thu, 12 Aug 2010)
New Revision: 370
Modified:
pkg/quantstrat/demo/bbands.R
pkg/quantstrat/demo/faber.R
pkg/quantstrat/demo/maCross.R
pkg/quantstrat/demo/macd.R
pkg/quantstrat/demo/rsi.R
Log:
- update demos
- fix RSI demo, which was broken
Modified: pkg/quantstrat/demo/bbands.R
===================================================================
--- pkg/quantstrat/demo/bbands.R 2010-08-12 11:44:00 UTC (rev 369)
+++ pkg/quantstrat/demo/bbands.R 2010-08-12 13:52:05 UTC (rev 370)
@@ -1,7 +1,7 @@
require(quantstrat)
try(rm("order_book.bbands",pos=.strategy),silent=TRUE)
try(rm("account.bbands","portfolio.bbands",pos=.blotter),silent=TRUE)
-try(rm("account.st","portfolio.st","stock.str","s","initDate","initEq",'start_t','end_t'),silent=TRUE)
+try(rm("account.st","portfolio.st","stock.str","stratBBands","initDate","initEq",'start_t','end_t'),silent=TRUE)
# some things to set up here
stock.str='IBM' # what are we trying it on
@@ -24,29 +24,31 @@
initAcct(account.st,portfolios='bbands', initDate=initDate)
initOrders(portfolio=portfolio.st,initDate=initDate)
-s <- strategy("bbands")
+stratBBands <- strategy("bbands")
#one indicator
-s <- add.indicator(strategy = s, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'))
+stratBBands <- add.indicator(strategy = s, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'))
#add signals:
-s<- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")
-s<- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")
-s<- add.signal(s,name="sigCrossover",arguments = list(columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")
+stratBBands <- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")
+stratBBands <- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")
+stratBBands <- add.signal(s,name="sigCrossover",arguments = list(columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")
# lets add some rules
-s
-s <- add.rule(s,name='ruleSignal', arguments = list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market', orderside=NULL, threshold=NULL),type='enter')
-s <- add.rule(s,name='ruleSignal', arguments = list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market', orderside=NULL, threshold=NULL),type='enter')
-s <- add.rule(s,name='ruleSignal', arguments = list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
-#s <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Lo.gt.UpperBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
-#s <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Hi.lt.LowerBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
+stratBBands <- add.rule(s,name='ruleSignal', arguments = list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market', orderside=NULL, threshold=NULL),type='enter')
+stratBBands <- add.rule(s,name='ruleSignal', arguments = list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market', orderside=NULL, threshold=NULL),type='enter')
+stratBBands <- add.rule(s,name='ruleSignal', arguments = list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
+
+#alternately, to exit at the opposite band, the rules would be...
+#stratBBands <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Lo.gt.UpperBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
+#stratBBands <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Hi.lt.LowerBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
+
#TODO add thresholds and stop-entry and stop-exit handling to test
getSymbols(stock.str,from=initDate)
start_t<-Sys.time()
-out<-try(applyStrategy(strategy='s' , portfolios='bbands',parameters=list(sd=SD,n=N)) )
+out<-try(applyStrategy(strategy=stratBBands , portfolios='bbands',parameters=list(sd=SD,n=N)) )
# look at the order book
#getOrderBook('bbands')
@@ -55,6 +57,7 @@
updatePortf(Portfolio='bbands',Dates=paste('::',as.Date(Sys.time()),sep=''))
chart.Posn(Portfolio='bbands',Symbol=stock.str)
plot(add_BBands(on=1,sd=SD,n=N))
+
###############################################################################
# R (http://r-project.org/) Quantitative Strategy Model Framework
#
Modified: pkg/quantstrat/demo/faber.R
===================================================================
--- pkg/quantstrat/demo/faber.R 2010-08-12 11:44:00 UTC (rev 369)
+++ pkg/quantstrat/demo/faber.R 2010-08-12 13:52:05 UTC (rev 370)
@@ -47,7 +47,7 @@
# Try to clean up in case the demo was run previously
try(rm("account.faber","portfolio.faber",pos=.blotter),silent=TRUE)
-try(rm("ltaccount","ltportfolio","ClosePrice","CurrentDate","equity","GSPC","i","initDate","initEq","Posn","UnitSize","verbose"),silent=TRUE)
+try(rm("ltaccount","ltportfolio","ClosePrice","CurrentDate","equity","GSPC","stratFaber","initDate","initEq","Posn","UnitSize","verbose"),silent=TRUE)
try(rm("order_book.faber",pos=.strategy),silent=TRUE)
# Set initial values
@@ -82,26 +82,26 @@
print("setup completed")
# Initialize a strategy object
-s <- strategy("faber")
+stratFaber <- strategy("faber")
# Add an indicator
-s <- add.indicator(strategy = s, name = "SMA", arguments = list(x = quote(Cl(mktdata)), n=10), label="SMA10")
+stratFaber <- add.indicator(strategy = s, 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
-s<- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="gt"),label="Cl.gt.SMA")
+stratFaber <- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="gt"),label="Cl.gt.SMA")
# The second is when the monthly price crosses under the 10-month SMA
-s<- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="lt"),label="Cl.lt.SMA")
+stratFaber <- add.signal(s,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
-s <- add.rule(s, name='ruleSignal', arguments = list(sigcol="Cl.gt.SMA", sigval=TRUE, orderqty=1000, ordertype='market', orderside='long', pricemethod='market'), type='enter', path.dep=TRUE)
+stratFaber <- add.rule(s, name='ruleSignal', arguments = list(sigcol="Cl.gt.SMA", sigval=TRUE, orderqty=1000, ordertype='market', orderside='long', pricemethod='market'), type='enter', path.dep=TRUE)
# The second is to sell when the price crosses below the SMA
-s <- add.rule(s, name='ruleSignal', arguments = list(sigcol="Cl.lt.SMA", sigval=TRUE, orderqty='all', ordertype='market', orderside='long', pricemethod='market'), type='exit', path.dep=TRUE)
+stratFaber <- add.rule(s, name='ruleSignal', arguments = list(sigcol="Cl.lt.SMA", sigval=TRUE, orderqty='all', ordertype='market', orderside='long', pricemethod='market'), type='exit', path.dep=TRUE)
# Process the indicators and generate trades
start_t<-Sys.time()
-out<-try(applyStrategy(strategy='s' , portfolios='faber'))
+out<-try(applyStrategy(strategy=stratFaber , portfolios='faber'))
end_t<-Sys.time()
print("Strategy Loop:")
print(end_t-start_t)
Modified: pkg/quantstrat/demo/maCross.R
===================================================================
--- pkg/quantstrat/demo/maCross.R 2010-08-12 11:44:00 UTC (rev 369)
+++ pkg/quantstrat/demo/maCross.R 2010-08-12 13:52:05 UTC (rev 370)
@@ -45,3 +45,15 @@
add_SMA(n=50 , on=1,col='blue')
add_SMA(n=200, on=1)
+###############################################################################
+# 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: faber.R 369 2010-08-12 11:44:00Z braverock $
+#
+###############################################################################
\ No newline at end of file
Modified: pkg/quantstrat/demo/macd.R
===================================================================
--- pkg/quantstrat/demo/macd.R 2010-08-12 11:44:00 UTC (rev 369)
+++ pkg/quantstrat/demo/macd.R 2010-08-12 13:52:05 UTC (rev 370)
@@ -56,4 +56,15 @@
chart.Posn(Portfolio=portfolio.st,Symbol=stock.str)
plot(add_MACD(fast=fastMA, slow=slowMA, signal=signalMA,maType="EMA"))
-
+###############################################################################
+# 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: faber.R 369 2010-08-12 11:44:00Z braverock $
+#
+##############################################################################
\ No newline at end of file
Modified: pkg/quantstrat/demo/rsi.R
===================================================================
--- pkg/quantstrat/demo/rsi.R 2010-08-12 11:44:00 UTC (rev 369)
+++ pkg/quantstrat/demo/rsi.R 2010-08-12 13:52:05 UTC (rev 370)
@@ -6,18 +6,18 @@
# There are two signals:
# The first is when RSI is greater than 90
-stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = list(data=quote(mktdata),threshold=90, column="RSI",relationship="gt", cross=TRUE),label="RSI.gt.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(data=quote(mktdata),threshold=10, column="RSI",relationship="lt",cross=TRUE),label="RSI.lt.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(mktdata), sigcol="RSI.gt.90", sigval=TRUE, orderqty=-1000, ordertype='market', orderside='short', pricemethod='market'), type='enter', path.dep=TRUE)
-stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(data=quote(mktdata), sigcol="RSI.lt.10", sigval=TRUE, orderqty='all', ordertype='market', orderside='short', pricemethod='market'), type='exit', path.dep=TRUE)
+stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(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(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(mktdata), sigcol="RSI.lt.10", sigval=TRUE, orderqty= 1000, ordertype='market', orderside='long', pricemethod='market'), type='enter', path.dep=TRUE)
-stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(data=quote(mktdata), sigcol="RSI.gt.90", sigval=TRUE, orderqty='all', ordertype='market', orderside='long', pricemethod='market'), type='enter', path.dep=TRUE)
+stratRSI <- add.rule(strategy = stratRSI, name='ruleSignal', arguments = list(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(sigcol="RSI.gt.70", sigval=TRUE, orderqty='all', ordertype='market', orderside='long', pricemethod='market', replace=FALSE), type='exit', path.dep=TRUE)
#add changeable parameters
# add level in/out
@@ -31,6 +31,7 @@
symbols = c("XLF", "XLP", "XLE", "XLY", "XLV", "XLI", "XLB", "XLK", "XLU")
for(symbol in symbols){ # establish trade-able instruments
stock(symbol, currency="USD",multiplier=1)
+ getSymbols(symbol)
}
@@ -38,9 +39,9 @@
# applySignals(strategy=stratRSI, mktdata=applyIndicators(strategy=stratRSI, mktdata=symbols[1]))
-initDate='2009-02-06'
+initDate='1997-12-31'
initEq=100000
-port.st<-'RSISPX' #use a string here for easier changing of parameters and re-trying
+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)
@@ -49,9 +50,8 @@
print("setup completed")
# Process the indicators and generate trades
-portfolios=c("RSISPX","RSIGDAXI","RSIboth")
start_t<-Sys.time()
-out<-try(applyStrategy(strategy=stratRSI , portfolios=c(port.st),parameters=list(n=2) ) )
+out<-try(applyStrategy(strategy=stratRSI , portfolios=port.st, parameters=list(n=2) ) )
end_t<-Sys.time()
print("Strategy Loop:")
print(end_t-start_t)
@@ -74,3 +74,16 @@
chart.Posn(Portfolio=port.st,Symbol=symbol,theme=themelist)
plot(add_RSI(n=2))
}
+
+###############################################################################
+# 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: faber.R 369 2010-08-12 11:44:00Z braverock $
+#
+###############################################################################
\ No newline at end of file
More information about the Blotter-commits
mailing list