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 287, Tue Mar 16 14:37:31 2010 UTC revision 370, Thu Aug 12 13:52:05 2010 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.longtrend","portfolio.longtrend",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.longtrend",pos=.strategy),silent=TRUE)  try(rm("order_book.faber",pos=.strategy),silent=TRUE)
52    
53  # Set initial values  # Set initial values
54  initDate='1997-12-31'  initDate='1997-12-31'
# Line 75  Line 75 
75  }  }
76    
77  # Initialize portfolio and account  # Initialize portfolio and account
78  initPortf('longtrend', symbols=symbols, initDate=initDate)  initPortf('faber', symbols=symbols, initDate=initDate)
79  initAcct('longtrend', portfolios='longtrend', initDate=initDate)  initAcct('faber', portfolios='faber', initDate=initDate)
80  initOrders(portfolio='longtrend', initDate=initDate)  initOrders(portfolio='faber', initDate=initDate)
81    
82  print("setup completed")  print("setup completed")
83    
84  # Initialize a strategy object  # Initialize a strategy object
85  s <- strategy("longtrend")  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 = s, 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(s,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(s,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(s, 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(s, 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='longtrend'))  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)
108    
109  # look at the order book  # look at the order book
110  #print(getOrderBook('longtrend'))  #print(getOrderBook('faber'))
111    
112  start_t<-Sys.time()  start_t<-Sys.time()
113  updatePortf(Portfolio='longtrend',Dates=paste('::',as.Date(Sys.time()),sep=''))  updatePortf(Portfolio='faber',Dates=paste('::',as.Date(Sys.time()),sep=''))
114  end_t<-Sys.time()  end_t<-Sys.time()
115  print("trade blotter portfolio update:")  print("trade blotter portfolio update:")
116  print(end_t-start_t)  print(end_t-start_t)
117    
118    # hack for new quantmod graphics, remove later
119    themelist<-chart_theme()
120    themelist$up.col<-'lightgreen'
121    themelist$down.col<-'pink'
122  for(symbol in symbols){  for(symbol in symbols){
123      dev.new()      dev.new()
124      chart.Posn(Portfolio='longtrend',Symbol=symbol,theme=chartTheme('white', up.col='lightgreen', dn.col='pink'), type='bar')      chart.Posn(Portfolio='faber',Symbol=symbol,theme=themelist)
125      plot(addSMA(n=10,col='darkgreen', on=1))      plot(add_SMA(n=10,col='darkgreen', on=1))
126  }  }
127    
128    

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

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