SCM

SCM Repository

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

Annotation of /pkg/quantstrat/demo/signal.RSI.R

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1673 - (view) (download)

1 : michaelgua 1667 # Work Flow:
2 :     # Example Code 2 for Running Signal Analysis in Quantstrat.
3 :     # System: A simple RSI strategy for evaluating signals.
4 :     # Author: Michael Guan
5 :     ###########################################################################
6 :    
7 :     # Load Packages:
8 :     require(iterators)
9 :     require(quantstrat)
10 :    
11 :     suppressWarnings(rm("order_book.RSI",pos=.strategy))
12 :     suppressWarnings(rm("account.RSI","portfolio.RSI",pos=.blotter))
13 :     suppressWarnings(rm("account.st","portfolio.st","stock.str","stratRSI","initDate","initEq",'start_t','end_t'))
14 :    
15 :     #Parameters
16 :     n=2
17 :    
18 :     #Data
19 :     currency("USD")
20 :     currency("EUR")
21 : michaelgua 1673 symbols = c("SPY")
22 : michaelgua 1667 for(symbol in symbols){ # establish trade-able instruments
23 :     stock(symbol, currency="USD",multiplier=1)
24 :     getSymbols(symbol,src='yahoo')
25 :     }
26 :    
27 :     # Initialize Account, Portfolio, Strategy
28 :     stratRSI <- strategy("RSI")
29 :    
30 :     initDate='1997-12-31'
31 :     initEq=100000
32 :     port.st<-'RSI' #use a string here for easier changing of parameters and re-trying
33 :    
34 :     initPortf(port.st, symbols=symbols, initDate=initDate)
35 :     initAcct(port.st, portfolios=port.st, initDate=initDate,initEq=initEq)
36 :     initOrders(portfolio=port.st, initDate=initDate)
37 :     for(symbol in symbols){ addPosLimit(port.st, symbol, initDate, 300, 3 ) } #set max pos
38 :    
39 :     # Indicator
40 :     stratRSI <- add.indicator(strategy = stratRSI, name = "RSI", arguments = list(price = quote(getPrice(mktdata)),n=n), label="RSI")
41 :    
42 :     # There are two signals:
43 :     # The first is when RSI is greater than 90
44 :     stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = list(threshold=70, column="RSI",relationship="gt", cross=TRUE),label="RSI.gt.70")
45 :     # The second is when RSI is less than 10
46 :     stratRSI <- add.signal(strategy = stratRSI, name="sigThreshold",arguments = list(threshold=30, column="RSI",relationship="lt",cross=TRUE),label="RSI.lt.30")
47 :    
48 :     #########################################################################
49 :     #Signal Analysis
50 :    
51 :     #Entry Signal colname Label
52 :     signal.label = 'RSI.lt.30'
53 :    
54 :     .n = seq(2,10,1)
55 :    
56 :     strategy.st<-add.distribution(stratRSI,
57 :     paramset.label = 'RSI',
58 :     component.type = 'indicator',
59 :     component.label = 'RSI',
60 :     variable = list(n = .n),
61 :     label = 'nRSI')
62 :    
63 :    
64 :     # Run Study
65 :     results =apply.paramset.signal.analysis(strategy.st,
66 :     paramset.label='RSI',
67 :     port.st,
68 :     sigcol = signal.label,
69 :     sigval = 1,
70 : michaelgua 1673 on=NULL,
71 : michaelgua 1667 forward.days=10,
72 :     cum.sum=TRUE,
73 :     include.day.of.signal=F,
74 :     obj.fun=signal.obj.slope,
75 :     decreasing=T,
76 :     mktdata=NULL,
77 :     verbose=TRUE)
78 :    
79 :    
80 :     # Plot Paramset Combined Barchart [Subset list to plot a sub portion if too large]
81 : michaelgua 1673 signal.plot(results$sigret.by.asset$SPY, rows=2, columns = 5)
82 : michaelgua 1667
83 :     # Distributional Box Plot via gamlss
84 : michaelgua 1673 distributional.boxplot(results$sigret.by.asset$SPY$paramset.2)
85 : michaelgua 1667
86 : bodanker 1671 ###############################################################################
87 :     # R (http://r-project.org/) Quantitative Strategy Model Framework
88 :     #
89 :     # Copyright (c) 2009-2015
90 :     # Peter Carl, Dirk Eddelbuettel, Brian G. Peterson,
91 :     # Jeffrey Ryan, Joshua Ulrich, and Garrett See
92 :     #
93 :     # This library is distributed under the terms of the GNU Public License (GPL)
94 :     # for full details see the file COPYING
95 :     #
96 :     ###############################################################################
97 : michaelgua 1667

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