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 362, Wed Jul 28 16:47:39 2010 UTC revision 450, Sat Nov 13 18:30:40 2010 UTC
# Line 47  Line 47 
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)  try(rm("account.faber","portfolio.faber",pos=.blotter),silent=TRUE)
50  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)
51  try(rm("order_book.faber",pos=.strategy),silent=TRUE)  try(rm("order_book.faber",pos=.strategy),silent=TRUE)
52    
53  # Set initial values  # Set initial values
# Line 82  Line 82 
82  print("setup completed")  print("setup completed")
83    
84  # Initialize a strategy object  # Initialize a strategy object
85  s <- strategy("faber")  stratFaber <- strategy("faber")
86    
87  # Add an indicator  # Add an indicator
88  s <- 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")
89    
90  # There are two signals:  # There are two signals:
91  # The first is when monthly price crosses over the 10-month SMA  # The first is when monthly price crosses over the 10-month SMA
92  s<- add.signal(s,name="sigCrossover",arguments = list(data=quote(mktdata),columns=c("Close","SMA10"),relationship="gt"),label="Cl.gt.SMA")  stratFaber <- add.signal(stratFaber,name="sigCrossover",arguments = list(columns=c("Close","SMA10"),relationship="gt"),label="Cl.gt.SMA")
93  # 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
94  s<- add.signal(s,name="sigCrossover",arguments = list(data=quote(mktdata),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")
95    
96  # There are two rules:  # There are two rules:
97  # The first is to buy when the price crosses above the SMA  # The first is to buy when the price crosses above the SMA
98  s <- add.rule(s, name='ruleSignal', arguments = list(data=quote(mktdata), 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'), type='enter', path.dep=TRUE)
99  # The second is to sell when the price crosses below the SMA  # The second is to sell when the price crosses below the SMA
100  s <- add.rule(s, name='ruleSignal', arguments = list(data=quote(mktdata), 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'), type='exit', path.dep=TRUE)
101    
102  # Process the indicators and generate trades  # Process the indicators and generate trades
103  start_t<-Sys.time()  start_t<-Sys.time()
104  out<-try(applyStrategy(strategy='s' , portfolios='faber'))  out<-try(applyStrategy(strategy=stratFaber , portfolios='faber'))
105  end_t<-Sys.time()  end_t<-Sys.time()
106  print("Strategy Loop:")  print("Strategy Loop:")
107  print(end_t-start_t)  print(end_t-start_t)
# Line 117  Line 117 
117    
118  # hack for new quantmod graphics, remove later  # hack for new quantmod graphics, remove later
119  themelist<-chart_theme()  themelist<-chart_theme()
120  themelist$up.col<-'lightgreen'  themelist$col$up.col<-'lightgreen'
121  themelist$down.col<-'pink'  themelist$col$dn.col<-'pink'
122  for(symbol in symbols){  for(symbol in symbols){
123      dev.new()      dev.new()
124      chart.Posn(Portfolio='faber',Symbol=symbol,theme=themelist)      chart.Posn(Portfolio='faber',Symbol=symbol,theme=themelist)

Legend:
Removed from v.362  
changed lines
  Added in v.450

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