[Blotter-commits] r255 - pkg/quantstrat/demo
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Tue Feb 23 23:43:44 CET 2010
Author: braverock
Date: 2010-02-23 23:43:43 +0100 (Tue, 23 Feb 2010)
New Revision: 255
Modified:
pkg/quantstrat/demo/simplestrat.R
Log:
- now add order rules
Modified: pkg/quantstrat/demo/simplestrat.R
===================================================================
--- pkg/quantstrat/demo/simplestrat.R 2010-02-23 22:43:29 UTC (rev 254)
+++ pkg/quantstrat/demo/simplestrat.R 2010-02-23 22:43:43 UTC (rev 255)
@@ -1,16 +1,36 @@
+try(rm("account.simplestrat","portfolio.simplestrat",pos=.blotter),silent=TRUE)
+try(rm("account.st","portfolio.st","IBM","s","initDate","initEq"),silent=TRUE)
+initDate='1997-12-31'
+initEq=1000000
+
+portfolio.st='simplestrat'
+account.st='simplestrat'
+
+initPortf(portfolio.st,'IBM', initDate=initDate)
+initAcct(account.st,portfolios='simplestrat', initDate=initDate)
+initOrders(portfolio=portfolio.st,initDate=initDate)
+
s <- strategy("simplestrat")
-s <- add.indicator(strategy = s, name = "SMA", arguments = list(x = quote(Cl(mktdata)), n=10), label="SMA10")
-s <- add.indicator(strategy = s, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), sd = 1.8,maType=quote(SMA)))
+#s <- add.indicator(strategy = s, name = "SMA", arguments = list(x = quote(Cl(mktdata)), n=10), label="SMA10")
+s <- add.indicator(strategy = s, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), sd = 1.5,maType=quote(SMA)))
-getSymbols("IBM")
-IBM.mod=applyIndicators(s,mktdata=IBM)
-#manually apply a signal function for demonstration
-cbind(IBM.mod,sigComparison(label="Close.gt.Open",data=IBM.mod,columns=c("Close","Open"),">"))
-cbind(IBM.mod,sigComparison(label="Adjusted.gt.SMA",data=IBM.mod,columns=c("Adjusted","SMA10"),">"))
+#if you wanted to manually apply a signal function for demonstration
+#cbind(IBM.mod,sigComparison(label="Close.gt.Open",data=IBM.inds,columns=c("Close","Open"),">"))
+#cbind(IBM.mod,sigComparison(label="Adjusted.gt.SMA",data=IBM.inds,columns=c("Adjusted","SMA10"),">"))
-#or, do it properly and add it to the strategy:
-s<- add.signal(s,name="sigComparison",arguments = list(data=quote(mktdata),columns=c("Close","Open"),relationship="gt"),label="Cl.gt.Op")
-s<- add.signal(s,name="sigComparison",arguments = list(data=quote(mktdata),columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperdBand")
-IBM.mod<-applySignals(s,mktdata=IBM.mod)
\ No newline at end of file
+#do it properly and add it to the strategy:
+#s<- add.signal(s,name="sigComparison",arguments = list(data=quote(mktdata),columns=c("Close","Open"),relationship="gt"),label="Cl.gt.Op")
+s<- add.signal(s,name="sigCrossover",arguments = list(data=quote(mktdata),columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")
+s<- add.signal(s,name="sigCrossover",arguments = list(data=quote(mktdata),columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")
+
+IBM.sigs<-applySignals(s,mktdata=IBM.inds)
+
+# lets add some rules
+s <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='sell', orderside=NULL, threshold=NULL),type='order')
+s <- add.rule(s,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='buy' , orderside=NULL, threshold=NULL),type='order')
+#TODO add thresholds and stop-entry and stop-exit handling to test
+
+#getSymbols("IBM")
+# applyStrategy(strategy='s' , portfolios='simplestrat', mktdata="IBM")
\ No newline at end of file
More information about the Blotter-commits
mailing list