-
Notifications
You must be signed in to change notification settings - Fork 32
/
TA - Bollinger Bands - Trading Strategy.R
59 lines (44 loc) · 1.57 KB
/
TA - 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
#Bollinger Bands - Trading Strategy
setwd("~/Quant Finance")
library(quantmod)
library(TTR)
library(PerformanceAnalytics)
library(tseries)
library(zoo)
library(xts)
library(dplyr)
library(knitr)
#Get the data
getSymbols('GOOG', src = 'yahoo', from = "2018-01-01", to = '2019-04-01')
#Plot and add indicator utilised
chartSeries(GOOG)
addBBands()
GOOG <- na.omit(merge(GOOG, BBands(Cl(GOOG))))
#Create strategy
#Flat when Close crosses the Moving Average
GOOG$sig[c(FALSE, diff(sign(Cl(GOOG) - GOOG$mavg), na.pad=FALSE) != 0)] <- 0
#Short when Close is above Upper Bollinger Band
GOOG$sig[Cl(GOOG) > GOOG$up] <- -1
#Long when Close is under Lower Bollinger Band
GOOG$sig[Cl(GOOG) < GOOG$dn] <- 1
#Flat on first day and last day
GOOG$sig[1] <- 0
GOOG$sig[nrow(GOOG)] <- 0
#Fill in the signal for other times
#Wherever signal is NA, copy previous value to next row
GOOG$sig <- na.locf(GOOG$sig)
#Lag signal so that you don't trade on the same bar that your signal fires
GOOG$sig <- Lag(GOOG$sig)
#Replace NA with zero position on first row
GOOG$sig[1] <- 0
#Plot your positions
plot(GOOG$GOOG.Close)
addEventLines(events = GOOG$sig, lwd = 1, col = "red")
#Create a table with your returns
Returns <- na.omit(GOOG$sig) * dailyReturn(Cl(GOOG))
#Plot the benchmark of the derivative with the performance of your strategy
charts.PerformanceSummary(cbind(dailyReturn(Cl(GOOG)),Returns))
#Create a calendar table of returns
kable(table.CalendarReturns(Returns), caption = "Calendar Returns")
#Create a table of drawdowns
kable(table.Drawdowns(Returns, top=10), caption = "Table of Drawdowns")