-
Notifications
You must be signed in to change notification settings - Fork 24
/
chart_macd.R
180 lines (155 loc) · 3.56 KB
/
chart_macd.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
175
176
177
178
179
180
# script: Moving Average Convergence Divergence (MACD)
# indicators
# date: 2024-01-29
# author: Serkan Korkmaz, serkor1@duck.com
# objective: Create expressions to be evaluted in
# the charting function
# script start;
#' @title
#' Chart the Moving Average Convergence Divergence (MACD) indicator
#'
#' @description
#' `r lifecycle::badge("experimental")`
#'
#' A high-level [plotly::plot_ly()]- and [plotly::add_lines()]-function that
#' interacts with the [TTR::MACD()]-function. The function adds subchart with a
#' [TTR::MACD()]-indicator.
#'
#' @usage macd(
#' nFast = 12,
#' nSlow = 26,
#' nSig = 9,
#' maType = "SMA",
#' percent = TRUE,
#' ...
#' )
#'
#' @inheritParams TTR::MACD
#' @param ... For internal use. Please ignore.
#'
#' @inherit kline
#'
#' @example man/examples/scr_charting.R
#'
#' @family chart indicators
#' @family subchart indicators
#' @family momentum indicators
#' @author Serkan Korkmaz
#' @export
macd <- function(
nFast = 12,
nSlow = 26,
nSig = 9,
maType = "SMA",
percent = TRUE,
...){
# 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 = "close",
.f = TTR::MACD,
nFast = nFast,
nSlow = nSlow,
nSig = nSig,
maType = maType,
percent = percent
)
data$direction <- as.logical(
data$signal >= data$macd
)
# 2) create plot
# without using pipes;
p <- plotly::plot_ly(
data = data,
showlegend = FALSE,
name = 'MACD',
x = ~ index,
y = ~ (macd - signal),
color = ~ direction,
colors = c(
# The first color always
# applies to the true condition
args$candle_color$bullish,
args$candle_color$bearish),
type = 'bar',
marker = list(line = list(
color = "black",
width = 0.5
))
)
layers <- list(
list(
type = "add_lines",
params = list(
name = "MACD: Signal",
data = data,
showlegend = FALSE,
x = ~ index,
y = ~ signal,
inherit = FALSE,
line = list(width = linewidth)
)
),
list(
type = "add_lines",
params = list(
name = "MACD: MACD",
data = data,
showlegend = FALSE,
x = ~ index,
y = ~ macd,
inherit = FALSE,
line = list(width = linewidth)
)
)
)
p <- build(
plot = p,
layers = layers,
annotations = list(
list(
text = paste0(
"MACD(",paste(c(nFast, nSlow,nSig), collapse = ', '), ")"
),
x = 0,
y = 1,
font = list(
size = 16
),
xref = 'paper',
yref = 'paper',
showarrow = FALSE
)
),
yaxis = list(
title = NA
)
)
p
},
class = c(
"subchart",
"plotly",
"htmlwidget"
)
)
}
# script end;