-
Notifications
You must be signed in to change notification settings - Fork 62
/
_03-basic-strategy-ii.Rmd
103 lines (86 loc) · 2.69 KB
/
_03-basic-strategy-ii.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
# Basic Strategy II {#basic-strategy-ii}
In [Basic Strategy](#basic-strategy) we did a simple strategy of buying n number of shares on a fixed moving average.
In this slightly modified example we'll pass the SMA `n` parameter to `applyStrategy()`.
```{r 3-rm-strat}
rm.strat(portfolio.st)
rm.strat(account.st)
```
```{r 3-init-portf}
initPortf(name = portfolio.st,
symbols = symbols,
initDate = init_date)
```
```{r 3-init-acct}
initAcct(name = account.st,
portfolios = portfolio.st,
initDate = init_date,
initEq = init_equity)
```
```{r 3-init-orders}
initOrders(portfolio = portfolio.st,
symbols = symbols,
initDate = init_date)
```
```{r 3-strategy}
strategy(strategy.st, store = TRUE)
```
```{r 3-add-indicators}
add.indicator(strategy = strategy.st,
name = "SMA",
arguments = list(x = quote(Cl(mktdata))),
label = "SMA20")
```
Notice in `add.indicator` we did not supply the `n` parameter as we did before.
```{r 3-add-signals}
add.signal(strategy = strategy.st,
name="sigCrossover",
arguments = list(columns = c("Close", "SMA20"),
relationship = "gte"),
label = "Cl.gte.SMA20")
add.signal(strategy = strategy.st,
name="sigCrossover",
arguments = list(columns = c("Close", "SMA20"),
relationship = "lt"),
label = "Cl.lt.SMA20")
```
```{r 3-add-rules}
# BTO when Cl crosses above SMA(20)
add.rule(strategy = strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "Cl.gte.SMA20",
sigval = TRUE,
orderqty = 100,
ordertype = "market",
orderside = "long"),
type = "enter",
label = "BTO")
# STC when Cl crosses under SMA(20)
add.rule(strategy.st,
name = "ruleSignal",
arguments = list(sigcol = "Cl.lt.SMA20",
sigval = TRUE,
orderqty = "all",
ordertype = "market",
orderside = "long"),
type = "exit",
label = "STC")
```
```{r 3-apply-strategy, results = "hide"}
# Results hidden to save space
applyStrategy(strategy.st,
portfolios = portfolio.st,
parameters = list(n = 20))
```
```{r 3-update}
updatePortf(portfolio.st)
updateAcct(account.st)
updateEndEq(account.st)
```
```{r 3-checkBlotterUpdate}
checkBlotterUpdate(portfolio.st, account.st, verbose = TRUE)
```
```{r 3-equity-curve}
a <- getAccount(account.st)
equity <- a$summary$End.Eq
plot(equity, main = "Consolidated Equity Curve")
```