-
Notifications
You must be signed in to change notification settings - Fork 62
/
trailing-stop.Rmd
150 lines (125 loc) · 3.93 KB
/
trailing-stop.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
# Trailing Stops {#trailing-stops}
```{block type = "strategy"}
If Signal Line crosses above 0:
BTO
Else If Signal Line crosses below 0:
STC
```
```{r trailing-stop-strategy-vars}
trailingStopPercent <- 0.07
trade_size <- init_equity/length(symbols)
```
```{r trailing-stop-create-objects}
portfolio.st <- "Quantstrat"
account.st <- "Strategies"
strategy.st <- "MACD.TS"
```
```{r trailing-stop-rm-strat}
rm.strat(portfolio.st)
rm.strat(account.st)
```
```{r trailing-stop-init-portf}
initPortf(name = portfolio.st,
symbols = symbols,
initDate = init_date)
```
```{r trailing-stop-init-acct}
initAcct(name = account.st,
portfolios = portfolio.st,
initDate = init_date,
initEq = init_equity)
```
```{r trailing-stop-init-orders}
initOrders(portfolio = portfolio.st,
symbols = symbols,
initDate = init_date)
```
```{r trailing-stop-strategy}
strategy(strategy.st, store = TRUE)
```
## osFixedDollar
quantstratIII pg. 11/66
$$ \text{orderqty} = \frac{\text{trade_size}}{\text{Cl}} $$
```{r}
osFixedDollar <- function(timestamp, orderqty, portfolio, symbol, ruletype, ...) {
if(!exists("trade_size")) stop("You must set trade_size")
ClosePrice <- as.numeric(Cl(mktdata[timestamp,]))
orderqty <- round(trade_size/ClosePrice,-2)
return(orderqty)
}
```
## Add Indicators
```{r trailing-stop-add-indicators}
add.indicator(strategy = strategy.st,
name = "MACD",
arguments = list(x = quote(Cl(mktdata))),
label = "osc")
```
## Add Signals
```{r trailing-stop-add-signals}
add.signal(strategy = strategy.st,
name="sigThreshold",
arguments = list(column ="signal.osc",
relationshipo = "gt",
threshold = 0,
cross = TRUE),
label = "signal.gt.zero")
add.signal(strategy = strategy.st,
name="sigThreshold",
arguments = list(column = "signal.osc",
relationship = "lt",
threshold = 0,
cross = TRUE),
label = "signal.lt.zero")
```
## Add Rules
```{r trailing-stop-add-rule}
add.rule(strategy = strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "signal.gt.zero",
sigval = TRUE,
orderqty = 100,
orderside = "long",
ordertype = "market",
osFUN = "osFixedDollar",
orderset = "ocolong"),
type = "enter",
label = "LE")
add.rule(strategy = strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "signal.lt.zero",
sigval = TRUE,
replace = TRUE,
orderside = "long",
ordertype = "market",
orderqty = "all",
orderset = "ocolong"),
type = "exit",
label = "LX")
add.rule(strategy = strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "signal.gt.zero",
sigval = TRUE,
replace = FALSE,
orderside = "long",
ordertype = "stoptrailing",
tmult = TRUE,
threshold = quote(trailingStopPercent),
orderqty = "all",
orderset = "ocolong"),
type = "chain",
parent = "LE",
label = "StopTrailingLong",
enabled = FALSE)
```
## Enable Rules
```{r trailing-stop-enable-rules}
enable.rule(strategy.st, type = "chain", label = "StopTrailingLong")
```
## Save Strategy
```{r trailing-stop-save-strategy}
cwd <- getwd()
setwd("./_data/")
save.strategy(strategy.st)
setwd(cwd)
```