SCM

SCM Repository

[blotter] Diff of /pkg/quantstrat/demo/faber.R
ViewVC logotype

Diff of /pkg/quantstrat/demo/faber.R

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 370, Thu Aug 12 13:52:05 2010 UTC revision 639, Fri Jun 24 14:29:06 2011 UTC
# Line 46  Line 46 
46  require(quantstrat)  require(quantstrat)
47    
48  # Try to clean up in case the demo was run previously  # Try to clean up in case the demo was run previously
49  try(rm("account.faber","portfolio.faber",pos=.blotter),silent=TRUE)  suppressWarnings(rm("account.faber","portfolio.faber",pos=.blotter))
50  try(rm("ltaccount","ltportfolio","ClosePrice","CurrentDate","equity","GSPC","stratFaber","initDate","initEq","Posn","UnitSize","verbose"),silent=TRUE)  suppressWarnings(rm("ltaccount", "ltportfolio", "ClosePrice", "CurrentDate", "equity",
51  try(rm("order_book.faber",pos=.strategy),silent=TRUE)              "GSPC", "stratFaber", "initDate", "initEq", "Posn", "UnitSize", "verbose"))
52    suppressWarnings(rm("order_book.faber",pos=.strategy))
53    
54  # Set initial values  # Set initial values
55  initDate='1997-12-31'  initDate='1997-12-31'
# Line 76  Line 77 
77    
78  # Initialize portfolio and account  # Initialize portfolio and account
79  initPortf('faber', symbols=symbols, initDate=initDate)  initPortf('faber', symbols=symbols, initDate=initDate)
80  initAcct('faber', portfolios='faber', initDate=initDate)  initAcct('faber', portfolios='faber', initDate=initDate, initEq=100000)
81  initOrders(portfolio='faber', initDate=initDate)  initOrders(portfolio='faber', initDate=initDate)
82    
83  print("setup completed")  print("setup completed")
# Line 85  Line 86 
86  stratFaber <- strategy("faber")  stratFaber <- strategy("faber")
87    
88  # Add an indicator  # Add an indicator
89  stratFaber <- add.indicator(strategy = s, name = "SMA", arguments = list(x = quote(Cl(mktdata)), n=10), label="SMA10")  stratFaber <- add.indicator(strategy = stratFaber, name = "SMA", arguments = list(x = quote(Cl(mktdata)), n=10), label="SMA10")
90    
91  # There are two signals:  # There are two signals:
92  # The first is when monthly price crosses over the 10-month SMA  # The first is when monthly price crosses over the 10-month SMA
93  stratFaber <- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="gt"),label="Cl.gt.SMA")  stratFaber <- add.signal(stratFaber,name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="gte"),label="Cl.gt.SMA")
94  # The second is when the monthly price crosses under the 10-month SMA  # The second is when the monthly price crosses under the 10-month SMA
95  stratFaber <- add.signal(s,name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="lt"),label="Cl.lt.SMA")  stratFaber <- add.signal(stratFaber,name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="lt"),label="Cl.lt.SMA")
96    
97  # There are two rules:  # There are two rules:
98  # The first is to buy when the price crosses above the SMA  # The first is to buy when the price crosses above the SMA
99  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)  stratFaber <- add.rule(stratFaber, name='ruleSignal', arguments = list(sigcol="Cl.gt.SMA", sigval=TRUE, orderqty=1000, ordertype='market', orderside='long', pricemethod='market',TxnFees=-5), type='enter', path.dep=TRUE)
100  # The second is to sell when the price crosses below the SMA  # The second is to sell when the price crosses below the SMA
101  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)  stratFaber <- add.rule(stratFaber, 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)
102    
103  # Process the indicators and generate trades  # Process the indicators and generate trades
104  start_t<-Sys.time()  start_t<-Sys.time()
# Line 117  Line 118 
118    
119  # hack for new quantmod graphics, remove later  # hack for new quantmod graphics, remove later
120  themelist<-chart_theme()  themelist<-chart_theme()
121  themelist$up.col<-'lightgreen'  themelist$col$up.col<-'lightgreen'
122  themelist$down.col<-'pink'  themelist$col$dn.col<-'pink'
123  for(symbol in symbols){  for(symbol in symbols){
124      dev.new()      dev.new()
125      chart.Posn(Portfolio='faber',Symbol=symbol,theme=themelist)      chart.Posn(Portfolio='faber',Symbol=symbol,theme=themelist)
126      plot(add_SMA(n=10,col='darkgreen', on=1))      plot(add_SMA(n=10,col='darkgreen', on=1))
127  }  }
128    
129    ret1 <- PortfReturns('faber')
130    ret1$total<-rowSums(ret1)
131    ret1
132    
133    if("package:PerformanceAnalytics" %in% search() || require("PerformanceAnalytics",quietly=TRUE)){
134            getSymbols("SPY", src='yahoo', index.class=c("POSIXt","POSIXct"), from='1999-01-01')
135            SPY<-to.monthly(SPY)
136            SPY.ret<-Return.calculate(SPY$SPY.Close)
137            index(SPY.ret)<-index(ret1)
138            dev.new()
139            charts.PerformanceSummary(cbind(ret1$total,SPY.ret), geometric=FALSE, wealth.index=TRUE)
140    }
141    
142    faber.stats<-tradeStats('faber')[,c('Net.Trading.PL','maxDrawdown','Num.Trades','Profit.Factor','Std.Dev.Trade.PL','Largest.Winner','Largest.Loser','Max.Equity','Min.Equity')]
143    faber.stats
144    
145  ###############################################################################  ###############################################################################
146  # R (http://r-project.org/) Quantitative Strategy Model Framework  # R (http://r-project.org/) Quantitative Strategy Model Framework
147  #  #
148  # Copyright (c) 2009-2010  # Copyright (c) 2009-2010
149  # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua Ulrich  # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson,
150    # Jeffrey Ryan, Joshua Ulrich, and Garrett See
151  #  #
152  # This library is distributed under the terms of the GNU Public License (GPL)  # This library is distributed under the terms of the GNU Public License (GPL)
153  # for full details see the file COPYING  # for full details see the file COPYING

Legend:
Removed from v.370  
changed lines
  Added in v.639

root@r-forge.r-project.org
ViewVC Help
Powered by ViewVC 1.0.0  
Thanks to:
Vienna University of Economics and Business Powered By FusionForge