Select Git revision
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
server.R 33.41 KiB
library(shinydashboard)
library(plotly)
library(tidyquant)
library(shinyalert)
library(tidyverse)
load("../../RData/activos.RData")
server <- function(session, input, output) {
# No aparece ningun indicador tecnico seleccionado por defecto en el grafico de la serie temporal
observeEvent(input$empresas,{
empresas <- paste(input$empresas)
#save(empresas, file = "../../RData/current_values.RData")
if(input$grap == "Serie temporal"){
updateSelectInput(session, inputId = "indicadores", selected = "")
}
})
# Solo se permite la periodicidad mensual si al menos el intervalo temporal supera los 3 meses
observeEvent(input$date_range,{
if((input$date_range[2] - input$date_range[1]) <= 93){ # Algo mas de 3 meses
updateSelectInput(session, "period", choices = c("diaria", "semanal"))
}
else{
updateSelectInput(session, "period", choices = c("diaria", "semanal", "mensual"))
}
})
# El grafico de la serie temporal es el unico que permite seleccionar mas de un valor
observeEvent(input$grap,{
if(input$grap == "Serie temporal"){
updateSelectizeInput(session, "empresas", "Seleccione uno o varios valores", options = list(maxItems = 20), selected = input$empresas)
}
if(input$grap != "Serie temporal"){
updateSelectizeInput(session, "empresas", "Seleccione un valor", options = list(maxItems = 1), selected = input$empresas)
}
})
pdf(NULL)
output$selected_var <- renderPlotly({
# Si el intervalo temporal es de menos de 30 dias no se muestra ninguna salida
if(input$date_range[2] - input$date_range[1] >= 30) {
par(mar = c(4, 4, 1, .1))
# Lectura empresas introducidas
empresas <- paste(input$empresas)
paste(input$empresas)
b <- strsplit(empresas, "\" \"")
c <- activos$simbolo[activos$activo == b][1]
# Conversion periodicidad
priodicidad <- NULL
if(input$period == "diaria"){
periodicidad <- "daily"
}else{
if(input$period == "semanal"){
periodicidad <- "weekly"
}else{
if(input$period == "mensual"){
periodicidad <- "monthly"
}
}
}
# Si hay algun activo introducida...
if(!is.na(c)){
valores <- NULL
names_valores <- NULL
# Obtencion de los simbolos de los activos introducidos
for(i in 1:length(empresas)){
valores <- c(valores, activos$simbolo[activos$activo == empresas[i]])
}
aux <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1],
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux) <- c("open", "high", "low", "close", "volume", "adjusted")
stock <- data.frame(aux$adjusted)
stock <- data.frame(stock,rownames(stock))
colnames(stock) <- append('Precio','Fecha')
stock$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", stock$Fecha)))
#############################################
# Grafico de la serie temporal de los valores
#############################################
if(input$grap == "Serie temporal") {
fig <- plot_ly(stock, type = 'scatter', mode = 'lines', height=1000)%>%
add_trace(x = ~Fecha, y = ~Precio, name = empresas[1])%>%
layout(showlegend = T, title="",
xaxis = list(rangeslider = list(visible = T),
rangeselector=list(
buttons=list(
list(count=1, label="1M", step="month", stepmode="backward"),
list(count=3, label="3M", step="month", stepmode="backward"),
list(count=6, label="6M", step="month", stepmode="backward"),
list(count=1, label="1Y", step="year", stepmode="backward"),
list(step="all")
))))
# Si hay indicadores tecnicos, se calculan y se añaden
if(length(input$indicadores) > 0){
aux <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1],
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux) <- c("open", "high", "low", "close", "volume", "adjusted")
if("Media móvil simple" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 2 - 14): dim(aux2)[1],]
MMA <- apply(aux2$adjusted, 2, SMA, n = 14)
MMA <- MMA[14:dim(MMA)[1],]
datos <- data.frame(MMA)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('MMA', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$MMA, name = "Media móvil simple")
}
if("Media móvil ponderada" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 2 - 14): dim(aux2)[1],]
MMP <- apply(aux2$adjusted, 2, WMA, n = 14)
MMP <- MMP[14:dim(MMP)[1],]
datos <- data.frame(MMP)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('MMP', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$MMP, name = "Media móvil ponderada")
}
if("Media móvil exponencial" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 2 - 14): dim(aux2)[1],]
MME <- apply(aux2$adjusted, 2, EMA, n = 14)
MME <- MME[14:dim(MME)[1],]
datos <- data.frame(MME)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('MME', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$MME, name = "Media móvil exponencial")
}
if("Bandas de Bollinger" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 2 - 14): dim(aux2)[1],]
bollinger <- BBands(aux2$adjusted, n = 14)
bollinger <- bollinger[14:dim(bollinger)[1],]
bollingerdn <- data.frame(bollinger$dn)
bollingerup <- data.frame(bollinger$up)
datos <- data.frame(bollingerdn, bollingerup, rownames(data.frame(aux)))
colnames(datos) <- c('bollingerdn', "bollingerup", 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$bollingerdn, name = "",
mode = "lines", line = list(color = 'transparent'), showlegend = FALSE)
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$bollingerup, name = "Bandas de Bollinger",
fill = 'tonexty', fillcolor='rgba(0,100,80,0.2)', line = list(color = 'transparent'))
}
if("Momento" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 1 - 14): dim(aux2)[1],]
momento <- apply(aux2$adjusted, 2, momentum, n = 14)
momento <- momento[(14 + 1):dim(momento)[1],]
datos <- data.frame(momento)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('momento', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$momento, name = "Momento", yaxis = 'y2')
}
if("ROC" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 1 - 14): dim(aux2)[1],]
ROC <- apply(aux2$adjusted, 2, ROC, n = 14)
ROC <- ROC[(14 + 1):dim(ROC)[1],]
datos <- data.frame(ROC)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('ROC', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$ROC, name = "ROC", yaxis = 'y4')
}
if("MACD" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 1 - 25): dim(aux2)[1],]
MACD <- MACD(aux2$adjusted)
MACD <- MACD[(26):dim(MACD)[1],]
datos <- data.frame(MACD$macd)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('MACD', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$MACD, name = "MACD", yaxis = 'y4')
}
if("RSI" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 1 - 14): dim(aux2)[1],]
RSI <- apply(aux2$adjusted, 2, RSI, n = 14)
RSI <- RSI[(14 + 1):dim(RSI)[1],]
datos <- data.frame(RSI)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('RSI', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$RSI, name = "RSI", yaxis = 'y3')
}
if("Stochastic %K" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 2 - 14): dim(aux2)[1],]
hlc <- cbind(aux2$high, aux2$low, aux2$close)
stochastic_k <- stoch(hlc)$fastK
stochastic_k <- stochastic_k[14:dim(stochastic_k)[1],]
datos <- data.frame(stochastic_k)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('stochastic_k', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$stochastic_k, name = "Stochastic %K", yaxis = 'y4')
}
if("Stochastic %D" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 2 - 14): dim(aux2)[1],]
hlc <- cbind(aux2$high, aux2$low, aux2$close)
stochastic_d <- stoch(hlc)$fastD
stochastic_d <- stochastic_d[14:dim(stochastic_d)[1],]
datos <- data.frame(stochastic_d)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('stochastic_d', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$stochastic_d, name = "Stochastic %D", yaxis = 'y4')
}
if("A/D" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1]): dim(aux2)[1],]
hlc <- cbind(aux2$high, aux2$low, aux2$close)
AD <- williamsAD(hlc)
AD <- AD[2:dim(AD)[1],]
datos <- data.frame(AD)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('AD', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$AD, name = "A/D", yaxis = 'y2')
}
if("CCI" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 2 - 14): dim(aux2)[1],]
hlc <- cbind(aux2$high, aux2$low, aux2$close)
CCI <- CCI(hlc, n = 14)
CCI <- CCI[14:dim(CCI)[1],]
datos <- data.frame(CCI)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('CCI', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$CCI, name = "CCI", yaxis = 'y2')
}
if("%B" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 2 - 14): dim(aux2)[1],]
bollinger <- BBands(aux2$adjusted, n = 14)
bollinger <- bollinger[14:dim(bollinger)[1],]
B <- bollinger$pctB
datos <- data.frame(B)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('B', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$B, name = "%B", yaxis = 'y4')
}
if("OBV" %in% input$indicadores){
OBV <- OBV(aux$adjusted, aux$volume)
datos <- data.frame(OBV)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('OBV', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$OBV, name = "OBV", yaxis = 'y5')
}
if("ATR" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1]): dim(aux2)[1],]
hlc <- cbind(aux2$high, aux2$low, aux2$close)
ATR <- ATR(hlc, n = 14)
ATR <- ATR[2:dim(ATR)[1],]
datos <- data.frame(ATR$tr)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('ATR', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$ATR, name = "ATR", yaxis = 'y2')
}
if("ADX" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 1 - 14): dim(aux2)[1],]
hlc <- cbind(aux2$high, aux2$low, aux2$close)
ADX <- ADX(hlc, n = 14)
ADX <- ADX[(14 + 1):dim(ADX)[1],]
datos <- data.frame(ADX$DX)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('ADX', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$ADX, name = "ADX", yaxis = 'y2')
}
if("Volatilidad" %in% input$indicadores){
aux2 <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1] - 365,
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux2) <- c("open", "high", "low", "close", "volume", "adjusted")
aux2 <- aux2[(dim(aux2)[1] - dim(aux)[1] + 2 - 14): dim(aux2)[1],]
ohlc <- cbind(aux2$open, aux2$high, aux2$low, aux2$close)
volatility <- volatility(ohlc, n = 14)
volatility <- volatility[14:dim(volatility)[1],]
datos <- data.frame(volatility)
datos <- data.frame(datos, rownames(data.frame(aux)))
colnames(datos) <- c('Volatilidad', 'Fecha')
datos$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", datos$Fecha)))
fig <- fig %>% add_trace(x = datos$Fecha, y = datos$Volatilidad, name = "Volatilidad", yaxis = 'y4')
}
}
# Si hay mas de un activo introducido, se añaden al grafico
if(length(valores) > 1){
for(i in 2:length(valores)){
aux <- getSymbols(valores[i], periodicity = periodicidad, from = input$date_range[1],
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux) <- c("open", "high", "low", "close", "volume", "adjusted")
stock <- data.frame(aux$adjusted)
stock <- data.frame(stock,rownames(stock))
colnames(stock) <- append('Precio','Fecha')
stock$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", stock$Fecha)))
fig <- fig %>%
add_trace(x = stock$Fecha, y = stock$Precio, name = empresas[i])%>%
layout(showlegend = T, title="",
xaxis = list(rangeslider = list(visible = T),
rangeselector=list(
buttons=list(
list(count=1, label="1M", step="month", stepmode="backward"),
list(count=3, label="3M", step="month", stepmode="backward"),
list(count=6, label="6M", step="month", stepmode="backward"),
list(count=1, label="1Y", step="year", stepmode="backward"),
list(step="all")
))))
}
}
fig <- fig %>%
layout(
xaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff',
domain = c(0.8, 1),
fixedrange = FALSE),
yaxis2 = list(domain = c(0.6, 0.79),
fixedrange = FALSE),
yaxis3 = list(domain = c(0.4, 0.59),
fixedrange = FALSE),
yaxis4 = list(domain = c(0.2, 0.39),
fixedrange = FALSE),
yaxis5 = list(domain = c(0., 0.19),
fixedrange = FALSE),
plot_bgcolor='#e5ecf6', margin = 0.1)
} else {
#############################################
# Grafico de velas de los valores
#############################################
if(input$grap == "Velas japonesas") {
aux <- getSymbols(valores[1], periodicity = periodicidad, from = input$date_range[1],
to = input$date_range[2], warnings = FALSE, auto.assign = FALSE)
names(aux) <- c("open", "high", "low", "close", "volume", "adjusted")
stock <- data.frame(aux$adjusted)
stock <- data.frame(stock,rownames(stock))
colnames(stock) <- append('Precio','Fecha')
stock$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", stock$Fecha)))
stock <- data.frame(aux$adjusted, aux$volume, aux$open, aux$close, aux$low, aux$high)
stock <- data.frame(stock,rownames(stock))
colnames(stock)[7] <- 'Fecha'
stock$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", stock$Fecha)))
df <- stock
# Creciente: color verde
# Decreciente: color rojo
for (i in 1:length(df[,1])) {
if (df$close[i] >= df$open[i]) {
df$direction[i] = 'Increasing'
} else {
df$direction[i] = 'Decreasing'
}
}
i <- list(line = list(color = '#4AB58A'))
d <- list(line = list(color = '#B51D1D'))
fig <- df %>% plot_ly(x = ~Fecha, type = "candlestick", height = 600,
open = ~df$open, close = ~df$close,
high = ~df$high, low = ~df$low, name = input$empresas,
increasing = i, decreasing = d)
fig <- fig %>% layout(yaxis = list(title = "Precio"))
fig2 <- df
fig2 <- fig2 %>% plot_ly(x = ~Fecha, y = ~df$volume, type = 'bar', name = input$empresas, height = 600,
color = ~direction, colors = c('#B51D1D', '#4AB58A'))
fig2 <- fig2 %>% layout(yaxis = list(title = "Volumen"))
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')
))
fig <- subplot(fig, fig2, heights = c(0.8,0.2), nrows=2,
shareX = TRUE, titleY = TRUE)
fig <- fig %>% layout(title = list(xanchor="center",
yanchor="middle",
pad=list(b=10, l=5, r=5, t=80),
text=paste(input$empresas, input$date_range[1], "-", input$date_range[2])),
xaxis = list(rangeselector = rs),
legend = list(orientation = 'h', x = 0.5, y = 1,
xanchor = 'center', yref = 'paper',
font = list(size = 9),
bgcolor = 'transparent'))
}
#############################################
# Grafico de barras de los valores
#############################################
if(input$grap == "Barras"){
stock <- data.frame(aux$adjusted, aux$volume, aux$open, aux$close, aux$low, aux$high)
stock <- data.frame(stock,rownames(stock))
colnames(stock)[7] <- 'Fecha'
df <- stock
i <- list(line = list(color = '#4AB58A'))
d <- list(line = list(color = '#B51D1D'))
fig <- df %>% plot_ly(x = ~Fecha, type = "ohlc",
open = ~open, close = ~close,
high = ~high, low = ~low,
increasing = i, decreasing = d,
name = c("creciente/descendiente"))
fig <- fig %>% layout(title = "",
xaxis = list(rangeslider = list(visible = F)),
showlegend = TRUE)
}
#############################################
# Serie de rendimientos de los valores
#############################################
if(input$grap == "Serie de rendimientos"){
stock <- data.frame(aux$adjusted)
returns <- Return.calculate(stock)
stock <- data.frame(stock,rownames(stock),data.frame(returns$adjusted))
colnames(stock) <- c('Precio','Fecha','retornos')
stock$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", stock$Fecha)))
stock <- stock[-1,]
fig <- plot_ly(stock, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~Fecha, y = ~retornos, name = empresas[1])%>%
layout(showlegend = T, title="",
xaxis = list(rangeslider = list(visible = T),
rangeselector=list(
buttons=list(
list(count=1, label="1M", step="month", stepmode="backward"),
list(count=3, label="3M", step="month", stepmode="backward"),
list(count=6, label="6M", step="month", stepmode="backward"),
list(count=1, label="1Y", step="year", stepmode="backward"),
list(step="all")
))))
fig <- fig %>%
layout(
xaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
plot_bgcolor='#e5ecf6', margin = 0.1)
}
#############################################
# Serie de disminuciones de los valores
#############################################
if(input$grap == "Serie de disminuciones"){
#stock <- data.frame(aux)
#stock <- drop_na(stock)
stock <- na.omit(aux)
names(stock) <- c("open", "high", "low", "close", "volume", "adjusted")
returns <- Return.calculate(stock)
returns <- drop_na(data.frame(returns))
cum <- cumsum(returns)
max_cum <- max(cum$adjusted)
disminuciones <- cum - max_cum
stock <- data.frame(stock[-1,])
stock <- data.frame(rownames(stock),data.frame(disminuciones$adjusted))
colnames(stock) <- c('Fecha','disminuciones')
stock$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", stock$Fecha)))
fig <- plot_ly(stock, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~Fecha, y = ~disminuciones, name = empresas[1])%>%
layout(showlegend = T, title="",
xaxis = list(rangeslider = list(visible = T),
rangeselector=list(
buttons=list(
list(count=1, label="1M", step="month", stepmode="backward"),
list(count=3, label="3M", step="month", stepmode="backward"),
list(count=6, label="6M", step="month", stepmode="backward"),
list(count=1, label="1Y", step="year", stepmode="backward"),
list(step="all")
))))
fig <- fig %>%
layout(
xaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
yaxis = list(zerolinecolor = '#ffff',
zerolinewidth = 2,
gridcolor = 'ffff'),
plot_bgcolor='#e5ecf6', margin = 0.1)
}
####################################
# Boxplot de rendimientos
####################################
if(input$grap == "Boxplot de rendimientos"){
stock <- data.frame(aux$adjusted)
returns <- Return.calculate(stock)
stock <- data.frame(stock,rownames(stock),data.frame(returns$adjusted))
colnames(stock) <- c('Precio','Fecha','retornos')
stock$Fecha <- gsub("\\.", "-", unlist(gsub("X", "", stock$Fecha)))
stock <- stock[-1,]
fig <- plot_ly(x = stock$retornos, type = 'box', name = input$empresas)
}
}
}
if(length(input$empresas) >= 1){
fig
}
}
})
}