Skip to content
Snippets Groups Projects
Commit 3a2e60ad authored by victarr's avatar victarr
Browse files

Upload New File

parent b26d4c6a
No related branches found
No related tags found
No related merge requests found
options(scipen=999)
library(plotly)
library(tidyquant)
library(DT)
library(formattable)
library(tidyverse)
server <- function(session, input, output) {
observeEvent(input$uno,{
updateDateRangeInput(session, inputId = "date_range", start = Sys.Date() - 31)
})
observeEvent(input$dos,{
updateDateRangeInput(session, inputId = "date_range", start = Sys.Date() - 93)
})
observeEvent(input$tres,{
updateDateRangeInput(session, inputId = "date_range", start = Sys.Date() - 183)
})
observeEvent(input$cuatro,{
updateDateRangeInput(session, inputId = "date_range", start = Sys.Date() - 365)
})
observeEvent(input$cinco,{
updateDateRangeInput(session, inputId = "date_range", start = Sys.Date() - 3650)
})
data <- read.csv("../../RData/datos_sp500.csv")
v <- data$Valor
simbolos <- data$Simbolo
ayer <- read.csv("../../RData/datos_sp500_ayer.csv")
data <- data[,-2]
ayer <- ayer[,-2]
names(data) <- c("Valor", "Precio de cierre ajustado ($)")
names(ayer) <- c("Valor", "Precio de cierre ajustado ($)")
Cambio <- ((as.numeric(data$`Precio de cierre ajustado ($)`)-as.numeric(ayer$`Precio de cierre ajustado ($)`))/as.numeric(ayer$`Precio de cierre ajustado ($)`))*100
signos <- function(elemento){
if(elemento > 0){
return(paste("+", elemento, sep = ""))
}else{
return(elemento)
}
}
colSigno <- function(elemento){
if(substr(elemento, start = 1, stop = 1) == "+"){
return(1)
}else{
return(0)
}
}
Cambio <- round(Cambio, digits = 2)
Cambio <- unlist(lapply(Cambio, signos))
colores <- unlist(lapply(Cambio, colSigno))
data <- cbind(data, Cambio)
data <- mutate(data, `Cambio` = paste(data$`Cambio`, "%"))
output$x1 <- DT::renderDataTable(datatable(data,
options = list(scrollX = TRUE, lengthChange = FALSE),
rownames = FALSE,
selection = 'single')%>%
formatStyle(3, fontWeight = "bold", color = JS("value.includes('+') ? '#4AB58A' : value.includes('-') ? '#B51D1D' : 'gray'"))
)
pdf(NULL)
output$x2 = renderPlotly({
s = input$x1_rows_selected
par(mar = c(4, 4, 1, .1))
fecha_ini <- input$date_range[1]
fecha_fin <- input$date_range[2]
if (length(s)) {
aux <- getSymbols(simbolos[s], from = fecha_ini, to = fecha_fin-1, warnings = FALSE, auto.assign = FALSE)
aux <- na.omit(aux)
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')
fig <- plot_ly(stock, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~Fecha, y = ~Precio)%>%
layout(showlegend = F, title=v[s])
high_annotations <- list(
x=stock$Fecha[stock$Precio == max(stock$Precio)],
y=max(stock$Precio),
xref='x', yref='y',
text=paste0('Máximo: ',max(stock$Precio)),
ax=0, ay=-40
)
low_annotations <- list(
x=stock$Fecha[stock$Precio == min(stock$Precio)],
y=min(stock$Precio),
xref='x', yref='y',
text=paste0('Mínimo: ',min(stock$Precio)),
ax=0, ay=40
)
updatemenus <- list(
list(
active = -1,
type= 'buttons',
buttons = list(
list(
label = "Máximo",
method = "update",
args = list(list(visible = c(TRUE, TRUE)),
list(title = v[s], annotations = list(c(), high_annotations)))),
list(
label = "Mínimo",
method = "update",
args = list(list(visible = c(TRUE, TRUE)),
list(title = v[s], annotations = list(low_annotations, c() )))),
list(
label = "Ambos",
method = "update",
args = list(list(visible = c(TRUE, TRUE)),
list(title = v[s], annotations = list(low_annotations, high_annotations)))),
list(
label = "Reset",
method = "update",
args = list(list(visible = c(TRUE, TRUE)),
list(title = v[s], annotations = list(c(), c())))))
)
)
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, updatemenus = updatemenus)
}
else{
aux <- getSymbols("^GSPC", from = fecha_ini, to = fecha_fin-1, warnings = FALSE, auto.assign = FALSE)
aux <- na.omit(aux)
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')
fig <- plot_ly(stock, type = 'scatter', mode = 'lines')%>%
add_trace(x = ~Fecha, y = ~Precio)%>%
layout(showlegend = F, title = "S&P 500")
high_annotations <- list(
x=stock$Fecha[stock$Precio == max(stock$Precio)],
y=max(stock$Precio),
xref='x', yref='y',
text=paste0('Máximo: ',max(stock$Precio)),
ax=0, ay=-40
)
low_annotations <- list(
x=stock$Fecha[stock$Precio == min(stock$Precio)],
y=min(stock$Precio),
xref='x', yref='y',
text=paste0('Mínimo: ',min(stock$Precio)),
ax=0, ay=40
)
updatemenus <- list(
list(
active = -1,
type= 'buttons',
buttons = list(
list(
label = "Máximo",
method = "update",
args = list(list(visible = c(TRUE, TRUE)),
list(title = "S&P 500", annotations = list(c(), high_annotations)))),
list(
label = "Mínimo",
method = "update",
args = list(list(visible = c(TRUE, TRUE)),
list(title = "S&P 500", annotations = list(low_annotations, c() )))),
list(
label = "Ambos",
method = "update",
args = list(list(visible = c(TRUE, TRUE)),
list(title = "S&P 500", annotations = list(low_annotations, high_annotations)))),
list(
label = "Reset",
method = "update",
args = list(list(visible = c(TRUE, TRUE)),
list(title = "S&P 500", annotations = list(c(), c())))))
)
)
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, updatemenus = updatemenus)
}
fig
})
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment