-
Notifications
You must be signed in to change notification settings - Fork 61
/
stop-loss-optimization.Rmd
231 lines (196 loc) · 7.5 KB
/
stop-loss-optimization.Rmd
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# Stop Loss Optimization {#stop-loss-optimization}
We take the code from \@ref(stop-loss) and reuse it here this time to test parameters for our stop-loss just as we did in \@ref(parameter-optimization). This time though instead of optimizing indicators we're going to optimize `.stopLoss` in a new integer vector `.StopLoss`.
```{r stop-loss-optimization-strategy-vars}
.StopLoss = seq(0.05, 0.6, length.out = 48)/100
```
We'll name this strategy `Luxor.Stop.Loss.Opt`.
```{r stop-loss-optimization-strategy}
strategy.st <- "Luxor.Stop.Loss.Opt"
rm.strat(portfolio.st)
rm.strat(account.st)
initPortf(name = portfolio.st,
symbols = symbols,
initDate = init_date)
initAcct(name = account.st,
portfolios = portfolio.st,
initDate = init_date)
initOrders(portfolio = portfolio.st,
initDate = init_date)
strategy(strategy.st, store = TRUE)
```
## Add Indicators
```{r stop-loss-optimization-add-indicators}
add.indicator(strategy.st,
name = "SMA",
arguments = list(x = quote(Cl(mktdata)),
n = .fast),
label = "nFast")
add.indicator(strategy.st,
name = "SMA",
arguments = list(x = quote(Cl(mktdata)),
n = .slow),
label = "nSlow")
```
## Add Signals
```{r stop-loss-optimization-add-signals}
add.signal(strategy.st,
name = "sigCrossover",
arguments = list(columns = c("nFast", "nSlow"),
relationship = "gte"),
label = "long"
)
add.signal(strategy.st,
name = "sigCrossover",
arguments = list(columns = c("nFast", "nSlow"),
relationship = "lt"),
label = "short")
```
## Add Rules
```{r stop-loss-optimization-add-rules}
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "long" ,
sigval = TRUE,
replace = FALSE,
orderside = "long" ,
ordertype = "stoplimit",
prefer = "High",
threshold = .threshold,
TxnFees = .txnfees,
orderqty = +.orderqty,
osFUN = osMaxPos,
orderset = "ocolong"),
type = "enter",
label = "EnterLONG")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "short",
sigval = TRUE,
replace = FALSE,
orderside = "short",
ordertype = "stoplimit",
prefer = "Low",
threshold = .threshold,
TxnFees = .txnfees,
orderqty = -.orderqty,
osFUN = osMaxPos,
orderset = "ocoshort"),
type = "enter",
label = "EnterSHORT")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "short",
sigval = TRUE,
replace = TRUE,
orderside = "long" ,
ordertype = "market",
TxnFees = .txnfees,
orderqty = "all",
orderset = "ocolong"),
type = "exit",
label = "Exit2SHORT")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "long",
sigval = TRUE,
replace = TRUE,
orderside = "short",
ordertype = "market",
TxnFees = .txnfees,
orderqty = "all",
orderset = "ocoshort"),
type = "exit",
label = "Exit2LONG")
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "long" ,
sigval = TRUE,
replace = FALSE,
orderside = "long",
ordertype = "stoplimit",
tmult = TRUE,
threshold = quote(.stoploss),
TxnFees = .txnfees,
orderqty = "all",
orderset = "ocolong"),
type = "chain",
parent = "EnterLONG",
label = "StopLossLONG",
enabled = FALSE)
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "short",
sigval = TRUE,
replace = FALSE,
orderside = "short",
ordertype = "stoplimit",
tmult = TRUE,
threshold = quote(.stoploss),
TxnFees = .txnfees,
orderqty = "all",
orderset = "ocoshort"),
type = "chain",
parent = "EnterSHORT",
label = "StopLossSHORT",
enabled = FALSE)
```
## Add Position Limit
```{r stop-loss-optimization-add-pos-limit}
for(symbol in symbols){
addPosLimit(portfolio = portfolio.st,
symbol = symbol,
timestamp = init_date,
maxpos = .orderqty)
}
```
## Add Distribution
We use `add.distribution` again to assign our `.StopLoss` vector as values for the **StopLossLONG** and **StopLossSHORT** rule chains.
```{r stop-loss-optimization-add-distribution}
add.distribution(strategy.st,
paramset.label = "StopLoss",
component.type = "chain",
component.label = "StopLossLONG",
variable = list(threshold = .StopLoss),
label = "StopLossLONG")
add.distribution(strategy.st,
paramset.label = "StopLoss",
component.type = "chain",
component.label = "StopLossSHORT",
variable = list(threshold = .StopLoss),
label = "StopLossSHORT")
```
## Add Distribution Constraint
We set a distribution constraint to keep the **StopLossLONG** and **StopLossSHORT** parameters the same.
```{r stop-loss-optimization-add-distribution-constraint}
add.distribution.constraint(strategy.st,
paramset.label = "StopLoss",
distribution.label.1 = "StopLossLONG",
distribution.label.2 = "StopLossSHORT",
operator = "==",
label = "StopLoss")
```
## Enable Rules
```{r stop-loss-optimization-enable-rules}
enable.rule(strategy.st, 'chain', 'StopLoss')
```
## Apply Paramset
```{r stop-loss-optimization-apply-paramset, results = "hide"}
cwd <- getwd()
setwd("./_data/")
results_file <- paste("results", strategy.st, "RData", sep = ".")
if( file.exists(results_file) ) {
load(results_file)
} else {
results <- apply.paramset(strategy.st,
paramset.label = "StopLoss",
portfolio.st = portfolio.st,
account.st = account.st,
nsamples = .nsamples,
verbose = TRUE)
if(checkBlotterUpdate(portfolio.st, account.st, verbose = TRUE)) {
save(list = "results", file = results_file)
save.strategy(strategy.st)
}
}
setwd(cwd)
```