-
Notifications
You must be signed in to change notification settings - Fork 24
/
chart_bollingerbands.R
174 lines (150 loc) · 3.34 KB
/
chart_bollingerbands.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
# script: Bollinger Bands
# date: 2024-01-29
# author: Serkan Korkmaz, serkor1@duck.com
# objective: Create expressions to be evaluted in
# the charting function
# script start;
#' @title
#' Add Bollinger Bands
#' to the chart
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' A high-level [plotly::add_lines()]-wrapper function that interacts
#' with the [TTR::BBands()]-function. The function adds bollinger bands
#' to the main [chart()].
#'
#' @usage bollinger_bands(
#' n = 20,
#' sd = 2,
#' maType = "SMA",
#' color = '#4682b4',
#' ...
#' )
#'
#' @inheritParams TTR::BBands
#' @param color A [character]-vector of [length] 1. "#4682b4" by default.
#'
#' @inherit kline
#'
#' @family chart indicators
#' @family main chart indicators
#' @author Serkan Korkmaz
#' @export
bollinger_bands <- function(
n = 20,
sd = 2,
maType = "SMA",
color = '#4682b4',
...){
# check if the indicator is called
# from the chart-function
#
# stops the function if not
check_indicator_call()
structure(
.Data = {
# 0) construct arguments
# via chart function
args <- list(
...
)
# # 1) unpack all elements
# # of the args
# data <- args$data
# 0.4) linewidth
linewidth <- 0.90
# 1) calculate MACD
# indicator
data <- indicator(
x = args$data,
columns = c("high", "low", "close"),
.f = TTR::BBands,
n = n,
sd = sd,
maType = maType
)
layers <- list(
# middle line
list(
type = "add_lines",
params = list(
showlegend = FALSE,
legendgroup = 'bb',
name = "Lower",
inherit = FALSE,
data = data,
x = ~index,
y = ~dn,
line =list(
color = color,
width = linewidth
)
)
),
list(
type = "add_lines",
params = list(
showlegend = FALSE,
legendgroup = 'bb',
name = "Upper",
inherit = FALSE,
data = data,
x = ~index,
y = ~up,
line = list(
color = color,
width = linewidth
)
)
),
list(
type = "add_lines",
params = list(
showlegend = FALSE,
legendgroup = 'bb',
name = paste(maType),
inherit = FALSE,
data = data,
x = ~index,
y = ~mavg,
line = list(
color =color,
dash ='dot',
width = linewidth
)
)
)
)
plot <- plotly::add_ribbons(
showlegend = TRUE,
legendgroup = 'bb',
p = args$plot,
inherit = FALSE,
x = ~index,
ymin = ~dn,
ymax = ~up,
data = data,
fillcolor = as_rgb(alpha = 0.1, hex_color = color),
line = list(
color = "transparent"
),
name = paste0("BB(", paste(c(n, sd, maType), collapse = ", "), ")")
)
plot <- build(
plot = plot,
layers = layers
)
invisible(
plot
)
},
class = c(
"indicator",
"plotly",
"htmlwidget"
)
)
}
# script end;