-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathQuantstrat - Bollinger Bands - Trading Strategy.R
182 lines (141 loc) · 4.93 KB
/
Quantstrat - Bollinger Bands - Trading Strategy.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
setwd("~/Quant Finance")
install.packages("quantmod")
install.packages("FinancialInstrument")
install.packages("PerformanceAnalytics")
install.packages("foreach")
install.packages("devtools")
# Install from github directly
install_github("braverock/blotter")
install_github("braverock/quanstrat")
library(devtools)
library(blotter)
library(quantstrat)
library(quantmod)
library(TTR)
library(PerformanceAnalytics)
library(foreach)
update.packages('blotter', repos="http://R-Forge.R-project.org")
options("getSymbols.warning4.0"=FALSE)
from ="2017-1-01"
to ="2019-01-01"
symbols = c("GOOG","IBM")
getSymbols(symbols, from=from, to=to, adjust=TRUE)
currency("USD")
stock(symbols, currency="USD", multiplier=1)
Sys.setenv(TZ="UTC") #setting up the timezone
GOOG <- merge(GOOG, SAR(GOOG))
GOOG <- na.omit(merge(GOOG, MACD(Cl(GOOG))))
IBM <- merge(IBM, SAR(IBM))
IBM <- na.omit(merge(IBM, MACD(Cl(IBM))))
initEq=1000000
strategy.st <- portfolio.st <- account.st <- "BBands"
rm.strat(strategy.st)
initPortf(portfolio.st, symbols)
initAcct(account.st, portfolios=portfolio.st, initEq = initEq)
initOrders(portfolio.st)
strategy(strategy.st, store=TRUE)
add.indicator(
strategy.st, name="BBands",
arguments=list(HLC = quote(Cl(mktdata)), n=20, sd=2),
label="bbands")
#In quantstrat, there are three ways one can use a signal. It is refer to as 'name':
#sigThreshold: more or less than a fixed value
#sigCrossover: when two signals cross over
#sigComparsion: compare two signals
#The 'column' refers to the data for calculation of signal. There are five possible 'relationships':
#gt = greater than
#gte = greater than or equal to
#lt = less than
#lte = less than or equal to
#eq = equal to
# Bull market if Cl < BBands(dn)
add.signal(
strategy.st,
name= 'sigCrossover',
arguments = list(column=c("mavg","dn"),
relationship="lt"),
label="long")
# Bear market if Cl > BBands(up)
add.signal(
strategy.st,
name= 'sigCrossover',
arguments = list(column=c("mavg.bbands","up.bbands"),
relationship="gt"),
label="short")
#While trading signals tell us buy or sell, but it does not specify the execution details.
#Trading rules will specify the following seven elements:
#SigCol: Name of Signal
#SigVal: implement when there is signal (or reverse)
#Ordertype: market, stoplimit
#Orderside: long, short
#Pricemethod: market
#Replace: whether to replace other others
#Type: enter or exit the order
add.rule(strategy.st,
name ='ruleSignal',
arguments = list(sigcol = 'long',
sigval = TRUE,
orderqty = 1000,
ordertype = 'market',
orderside = 'long',
pricemethod = 'market',
osFUN = osMaxPos,
orderset = "ocolong",
replace = FALSE),
type='enter',
path.dep = TRUE,
label= 'EnterLONG')
add.rule(strategy.st,
name = 'ruleSignal',
arguments = list(sigcol = 'short',
sigval = TRUE,
orderqty = -1000,
ordertype = 'market',
orderside = 'short',
pricemethod ='market',
osFUN = osMaxPos,
orderset = "ocolong",
replace = FALSE),
type='enter',
path.dep = TRUE,
label= 'EnterSHORT')
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = 'mavg.bbands',
sigval = TRUE,
orderqty = "all",
ordertype = "market",
orderside = "long",
pricemethod ='market',
replace = TRUE),
type = "exit",
path.dep =TRUE,
label = "Exit2SHORT")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = 'Cross.mid',
sigval = TRUE,
orderqty = "all",
ordertype = "market",
orderside = "short",
pricemethod ='market',
replace = TRUE),
type = "exit",
path.dep =TRUE,
label = "Exit2LONG")
for(symbols in symbols){
addPosLimit(portfolio = portfolio.st,
symbol = symbols,
timestamp = from,
maxpos = 1000)
}
out <- applyStrategy(strategy.st, portfolios=portfolio.st)
updatePortf(Portfolio = portfolio.st)
updateAcct(name = portfolio.st)
updateEndEq(Account = account.st)
chart.Posn(Portfolio=portfolio.st, Symbol = "GOOG",
TA = 'add_BBands(n=20, sd=2)
add_RSI(n=14)')
chart.Posn(Portfolio=portfolio.st, Symbol = "IBM",
TA = 'add_BBands(n=20, sd=2)
add_RSI(n=14)')