---
title: "Análise Técnica de Ações"
date: "Atualização: `r format(Sys.time(), '%d')`/`r format(Sys.time(), '%m')`/`r format(Sys.time(), '%Y')` às `r format(Sys.time(), '%H:%M')`h"
output:
flexdashboard::flex_dashboard:
theme:
bg: "#273746"
fg: "#F8F9F9"
primary: "#F8F9F9"
base_font:
google: Blinker
code_font:
google: Ubuntu
orientation: rows
vertical_layout: scroll
social: menu
source_code: embed
---
```{r setup, include=FALSE}
#---------- Pacotes
library(flexdashboard)
library(tidyquant)
library(tidyverse)
#---------- Dados
ticket <- "LREN3.SA"
df <- tq_get(x = ticket,
get = "stock.prices",
from = "2018-01-01",
to = Sys.Date())
#---------- Processamento
df <- df |>
mutate(amplitude = high - low,
candle = case_when(close > open ~ "Positivo",
close < open ~ "Negativo"),
var_1dia = round((close - lag(close)) / lag(close) * 100, digits = 2),
var_7dias = round((close - lag(close, n = 7)) / lag(close, n = 7) * 100, digits = 2),
var_15dias = round((close - lag(close, n = 15)) / lag(close, n = 15) * 100, digits = 2),
var_30dias = round((close - lag(close, n = 30)) / lag(close, n = 30) * 100, digits = 2),
var_60dias = round((close - lag(close, n = 60)) / lag(close, n = 60) * 100, digits = 2),
preco_medio = round((close + open) / 2, digits = 2),
ano = lubridate::year(date),
mes = lubridate::month(date, label = TRUE, abbr = TRUE))
```
Row
-----------------------------------------------------------------------
###
```{r}
df |>
filter(date == Sys.Date()-1) |>
select(symbol) |>
valueBox(icon = 'fa-store', caption = "Código do ativo")
```
###
```{r}
df |>
filter(date == Sys.Date()-1) |>
select(var_7dias) |>
valueBox(icon = 'fa-percent', caption = "Variação nos últimos 7 dias")
```
###
```{r}
df |>
filter(date == Sys.Date()-1) |>
select(var_15dias) |>
valueBox(icon = 'fa-percent', caption = "Variação nos últimos 15 dias")
```
###
```{r}
df |>
filter(date == Sys.Date()-1) |>
select(var_30dias) |>
valueBox(icon = 'fa-percent', caption = "Variação nos últimos 30 dias")
```
###
```{r}
df |>
filter(date == Sys.Date()-1) |>
select(var_60dias) |>
valueBox(icon = 'fa-percent', caption = "Variação nos últimos 60 dias")
```
Row
-----------------------------------------------------------------------
### Tendência nos últimos 6 meses
```{r fig.height=4, fig.width=8}
df |>
filter(date >= Sys.Date()-180) |>
ggplot() +
geom_candlestick(aes(x = date,
open = open,
high = high,
low = low,
close = close),
fill_up = "#28B463",
fill_down = "#CB4335",
colour_up = "#28B463",
colour_down = "#CB4335") +
geom_smooth(aes(y = close, x = date), method = "gam", color = "grey30") +
hrbrthemes::theme_ft_rc() +
labs(y = NULL, x = NULL) +
theme(panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(linetype = "dotted"),
panel.grid.minor.x = element_blank())
```
### Candlesticks do último mês
```{r fig.height=4, fig.width=8}
df |>
filter(date >= Sys.Date()-30) |>
ggplot(aes(y = close, x = date)) +
geom_candlestick(aes(x = date,
open = open,
high = high,
low = low,
close = close),
fill_up = "#28B463",
fill_down = "#CB4335",
colour_up = "#28B463",
colour_down = "#CB4335") +
scale_x_date(breaks = "3 day") +
hrbrthemes::theme_ft_rc() +
labs(y = NULL, x = NULL) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(linetype = "dotted"),
panel.grid.minor.x = element_blank())
```
Row
-----------------------------------------------------------------------
### Proporção de pregões com candles positivos
```{r fig.height=4, fig.width=8}
df |>
filter(ano >= 2021) |>
group_by(ano, mes) |>
summarise(positivo = length(candle[candle == "Positivo"]),
negativo = length(candle[candle == "Negativo"]),
total = positivo + negativo,
proporcao = round(positivo / total * 100, digits = 2)) |>
ggplot(aes(y = proporcao, x = mes)) +
geom_segment(aes(x = mes, xend = mes, y = 0, yend = proporcao),
size = 1, color = "#28B463", alpha = 0.5) +
geom_point(color = "#28B463", size = 6) +
geom_text(aes(label = round(proporcao, digits = 0)), color = "black", size = 3) +
facet_wrap(~ ano) +
hrbrthemes::theme_ft_rc() +
labs(y = NULL, x = NULL) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
panel.grid.minor.y = element_blank(),
panel.grid.major.y = element_line(linetype = "dotted"),
panel.grid.major.x = element_blank())
```
### Acumulado mensal da variação % entre fechamentos
```{r fig.height=4, fig.width=8}
df |>
filter(ano >= 2021) |>
group_by(ano, mes) |>
summarise(var_1dia = sum(var_1dia)) |>
mutate(resultado = case_when(var_1dia >= 0.000 ~ "positivo",
var_1dia < 0.000 ~ "negativo")) |>
ggplot(aes(y = var_1dia, x = mes, fill = resultado)) +
geom_col(alpha = 0.7, color = "grey20") +
hrbrthemes::theme_ft_rc() +
facet_grid(~ ano) +
scale_fill_manual(values = c("#CB4335", "#28B463")) +
theme(panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(linetype = "dotted"),
panel.grid.minor.x = element_blank(),
axis.text.x = element_text(angle = 90, vjust = 0.5)) +
labs(y = NULL, x = NULL) +
guides(fill = "none")
```
Row
-----------------------------------------------------------------------
### Comparativo anual do preço médio do fechamento e desvio padrão
```{r fig.height=4, fig.width=8}
df |>
group_by(ano) |>
summarise(media = mean(close),
desvio = sd(close)) |>
ggplot(aes(y = media, x = ano)) +
geom_col(fill = "#28B463", color = "#28B463") +
geom_errorbar(aes(ymin = media - desvio,
ymax = media + desvio),
width = 0.1, color = "white", size = 0.8) +
hrbrthemes::theme_ft_rc() +
theme(panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank()) +
labs(y = NULL, x = NULL) +
scale_x_continuous(n.breaks = 10)
```
### Acumulado semanal da variação % entre fechamentos em 2023
```{r fig.height=4, fig.width=8}
df |>
filter(ano == 2023) |>
mutate(semana = week(date)) |>
group_by(semana) |>
summarise(var_1dia = round(sum(var_1dia), digits = 2)) |>
mutate(resultado = case_when(var_1dia >= 0.000 ~ "positivo",
var_1dia < 0.000 ~ "negativo")) |>
ggplot(aes(y = var_1dia, x = semana, fill = resultado)) +
geom_col(alpha = 0.7, color = "grey20") +
hrbrthemes::theme_ft_rc() +
scale_fill_manual(values = c("#CB4335", "#28B463")) +
theme(panel.grid.minor.y = element_blank(),
panel.grid.major.x = element_line(linetype = "dotted"),
panel.grid.minor.x = element_blank()) +
labs(y = NULL, x = NULL) +
guides(fill = "none") +
scale_x_continuous(n.breaks = 10)
```