SCM

SCM Repository

[blotter] Annotation of /pkg/quantstrat/demo/bbands.R
ViewVC logotype

Annotation of /pkg/quantstrat/demo/bbands.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1376 - (view) (download)

1 : braverock 258 require(quantstrat)
2 : gsee 621 suppressWarnings(rm("order_book.bbands",pos=.strategy))
3 :     suppressWarnings(rm("account.bbands","portfolio.bbands",pos=.blotter))
4 :     suppressWarnings(rm("account.st","portfolio.st","stock.str","stratBBands","initDate","initEq",'start_t','end_t'))
5 : peter_carl 222
6 : braverock 282 # some things to set up here
7 :     stock.str='IBM' # what are we trying it on
8 : braverock 360
9 :     # we'll pass these
10 : braverock 282 SD = 2 # how many standard deviations, traditionally 2
11 : braverock 360 N = 20 # how many periods for the moving average, traditionally 20
12 : braverock 282
13 :    
14 : braverock 265 currency('USD')
15 : braverock 282 stock(stock.str,currency='USD',multiplier=1)
16 : braverock 265
17 : milktrader 1376 ##### PLACE DEMO AND TEST DATES HERE #################
18 :     #
19 :     #if(isTRUE(options('in_test')$in_test))
20 :     # # use test dates
21 :     # {initDate="2011-01-01"
22 :     # endDate="2012-12-31"
23 :     # } else
24 :     # # use demo defaults
25 :     # {initDate="1999-12-31"
26 :     # endDate=Sys.Date()}
27 : milktrader 1370
28 : bodanker 281 initDate='2006-12-31'
29 : braverock 255 initEq=1000000
30 :    
31 : braverock 272 portfolio.st='bbands'
32 :     account.st='bbands'
33 : braverock 255
34 : braverock 282 initPortf(portfolio.st,symbols=stock.str, initDate=initDate)
35 : braverock 272 initAcct(account.st,portfolios='bbands', initDate=initDate)
36 : braverock 255 initOrders(portfolio=portfolio.st,initDate=initDate)
37 : braverock 1097 addPosLimit(portfolio.st, stock.str, initDate, 200, 2 ) #set max pos
38 : braverock 370 stratBBands <- strategy("bbands")
39 : braverock 282
40 : braverock 360 #one indicator
41 : opentrades 1217 stratBBands <- add.indicator(strategy = stratBBands, name = "BBands", arguments = list(HLC = quote(HLC(mktdata)), maType='SMA'), label='BBands')
42 : peter_carl 222
43 : braverock 224
44 : braverock 360 #add signals:
45 : braverock 371 stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","up"),relationship="gt"),label="Cl.gt.UpperBand")
46 :     stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("Close","dn"),relationship="lt"),label="Cl.lt.LowerBand")
47 :     stratBBands <- add.signal(stratBBands,name="sigCrossover",arguments = list(columns=c("High","Low","mavg"),relationship="op"),label="Cross.Mid")
48 : braverock 255
49 :     # lets add some rules
50 : braverock 1097 stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cl.gt.UpperBand",sigval=TRUE, orderqty=-100, ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='enter')
51 :     stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cl.lt.LowerBand",sigval=TRUE, orderqty= 100, ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='enter')
52 :     stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(sigcol="Cross.Mid",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL,osFUN=osMaxPos),type='exit')
53 : braverock 370
54 :     #alternately, to exit at the opposite band, the rules would be...
55 : braverock 371 #stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Lo.gt.UpperBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
56 :     #stratBBands <- add.rule(stratBBands,name='ruleSignal', arguments = list(data=quote(mktdata),sigcol="Hi.lt.LowerBand",sigval=TRUE, orderqty= 'all', ordertype='market', orderside=NULL, threshold=NULL),type='exit')
57 : braverock 370
58 : braverock 255 #TODO add thresholds and stop-entry and stop-exit handling to test
59 :    
60 : braverock 1097 getSymbols(stock.str,from=initDate,index.class=c('POSIXt','POSIXct'))
61 : braverock 260 start_t<-Sys.time()
62 : braverock 370 out<-try(applyStrategy(strategy=stratBBands , portfolios='bbands',parameters=list(sd=SD,n=N)) )
63 : braverock 360
64 : braverock 263 # look at the order book
65 : braverock 272 #getOrderBook('bbands')
66 : braverock 260 end_t<-Sys.time()
67 : braverock 374 print("strat execution time:")
68 :     print(end_t-start_t)
69 :    
70 :     start_t<-Sys.time()
71 : braverock 272 updatePortf(Portfolio='bbands',Dates=paste('::',as.Date(Sys.time()),sep=''))
72 : braverock 374 end_t<-Sys.time()
73 :     print("updatePortf execution time:")
74 :     print(end_t-start_t)
75 :    
76 : braverock 309 chart.Posn(Portfolio='bbands',Symbol=stock.str)
77 :     plot(add_BBands(on=1,sd=SD,n=N))
78 : braverock 370
79 : braverock 263 ###############################################################################
80 :     # R (http://r-project.org/) Quantitative Strategy Model Framework
81 :     #
82 : braverock 1194 # Copyright (c) 2009-2012
83 : braverock 263 # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson, Jeffrey Ryan, and Joshua Ulrich
84 :     #
85 :     # This library is distributed under the terms of the GNU Public License (GPL)
86 :     # for full details see the file COPYING
87 :     #
88 :     # $Id$
89 :     #
90 :     ###############################################################################
91 : opentrades 1217
92 : milktrader 1370 ##### PLACE THIS BLOCK AT END OF DEMO SCRIPT ###################
93 :     # book = getOrderBook(port)
94 :     # stats = tradeStats(port)
95 :     # rets = PortfReturns(acct)
96 :     ################################################################

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