-
Notifications
You must be signed in to change notification settings - Fork 0
/
technical_analysis_with_plotly.Rmd
139 lines (111 loc) · 3.63 KB
/
technical_analysis_with_plotly.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
---
title: "Analisis Técnico usando Plotly"
author: <a href="https://gcabrerag.rbind.io/"> <h3> Gabriel Cabrera </h3> </a> \newline Facultad de Economía y Negocios, Universidad de Chile, Chile
date: "2018/08/09 (updated: `r Sys.Date()`)"
output:
bookdown::html_document2:
toc: true
toc_float: true
toc_depth: 3
number_sections: true
theme: united
highlight: tango
df_print: paged
code_folding: show
self_contained: false
keep_md: false
encoding: "UTF-8"
---
```{r, eval = TRUE}
if(!require("pacman")) install.packages("pacman")
p_load("plotly", "quantmod", "IRdisplay")
```
```{r, eval = TRUE, message=TRUE, warning = FALSE}
getSymbols("AMZN", src = 'yahoo', from = "2018-07-01", periodicity = "daily")
```
```{r, eval = TRUE}
amazon <- data.frame(date = index(AMZN), coredata(AMZN))
```
```{r, eval = TRUE}
bbands <- data.frame(BBands(HLC(AMZN))) %>%
na.omit() %>%
select(dn, mavg, up)
```
```{r, eval = TRUE}
amazon <- amazon %>%
filter(date >= as.Date(rownames(bbands)[1]))
```
```{r, eval = TRUE}
df <- cbind(amazon,bbands)
```
```{r, eval = TRUE}
for (i in 1:nrow(df)){
if (df$AMZN.Close[i] >= df$AMZN.Open[i]) {
df$direction[i] = "Aumento"
} else {
df$direction[i] = "Disminución"
}
}
```
```{r, eval = TRUE}
i <- list(line = list(color = '#17BECF'))
d <- list(line = list(color = '#7F7F7F'))
```
```{r, eval = TRUE}
p <- df %>%
plot_ly(x = ~date, type="candlestick", open = ~AMZN.Open, close = ~AMZN.Close,
high = ~AMZN.High, low = ~AMZN.Low, name = "AMZN",
increasing = i, decreasing = d) %>%
add_lines(x = ~date, y = ~up , name = "B Bands",line = list(color = '#ccc', width = 0.5),
legendgroup = "Bollinger Bands", hoverinfo = "none", inherit = F) %>%
add_lines(x = ~date, y = ~dn, name = "B Bands", line = list(color = '#ccc', width = 0.5),
legendgroup = "Bollinger Bands", inherit = F, showlegend = FALSE, hoverinfo = "none") %>%
add_lines(x = ~date, y = ~mavg, name = "Mv Avg", line = list(color = '#E377C2', width = 0.5),
hoverinfo = "none", inherit = F) %>%
layout(yaxis = list(title = "Precio"))
```
```{r, eval = TRUE}
pp <- df %>%
plot_ly(x = ~date, y = ~AMZN.Volume, type='bar', name = "AMZN Volumen",
color = ~ direction, colors = c('#17BECF','#7F7F7F')) %>%
layout(yaxis = list(title = "Volume"))
```
```{r, eval = TRUE}
rs <- list(visible = TRUE, x = 0.5, y = -0.055, xanchor = 'center', yref = 'paper',
font = list(size = 9),
buttons = list(
list(count=1,
label='RESET',
step='all'),
list(count=1,
label='1 YR',
step='year',
stepmode='backward'),
list(count=3,
label='3 MO',
step='month',
stepmode='backward'),
list(count=1,
label='1 MO',
step='month',
stepmode='backward')
))
```
```{r, eval = TRUE}
finale_plotly <- subplot(p, pp, heights = c(0.7,0.2), nrows=2, shareX = TRUE, titleY = TRUE) %>%
layout(title = paste("S&P 500: 2018-07-01",Sys.Date()), xaxis = list(rangeselector = rs),
legend = list(orientation = 'h', x = 0.5, y = 1, xanchor = 'center', yref = 'paper',
font = list(size = 10),
bgcolor = 'transparent'))
```
```{r, eval = TRUE}
htmlwidgets::saveWidget(finale_plotly, "finale_plotly.html")
```
```{r, echo = TRUE, eval = FALSE}
finale_plotly
```
<center>
```{r, echo = FALSE, fig.alin='center'}
finale_plotly
```
</center>