top of page

Gráfico del Índice de Precios y Cotizaciones en R

Actualizado: 21 feb 2023

De acuerdo con la BMV; "el S&P/BMV Índice de Precios y Cotizaciones (S&P/BMV IPC) es el principal indicador de Mercado Mexicano de Valores; expresa el rendimiento del mercado accionario en función de las variaciones de precios de una muestra balanceada, ponderada y representativa del conjunto de Emisoras cotizadas en la Bolsa Mexicana de Valores, basado en las mejores prácticas internacionales. El S&P/BMV Índice de Precios y Cotizaciones(S&P/BMV IPC), desde octubre de 1978, tiene como principal objetivo, constituirse como un indicador representativo del Mercado Mexicano para servir como referencia y subyacente de productos financieros".




CÓDIGO EN R

library(tidyverse)

library(dplyr)

library(lubridate)

library(plotly)

library(quantmod)




MXX <- read.csv("C:\\Users\\End User\\Desktop\\Proyecto MQEconomics\\Publicaciones\\Finanzas\\Datos históricos S&P_BMV IPC.csv")

MXX <- rename(MXX, Date=ï..Fecha, MXX.High=MÃ.ximo , MXX.Low=MÃ.nimo ,

MXX.Close=Cierre, MXX.Open=Apertura, MXX.Volume=Vol.)

MXX <- select(MXX,Date, MXX.High, MXX.Low,

MXX.Close, MXX.Open, MXX.Volume)



MXX$MXX.Volume<- substr(MXX$MXX.Volume, 1, 5)


MXX$Date <- as.Date(MXX$Date,format="%d.%m.%Y")


MXX <- xts(MXX[, 2:6], order.by = MXX$Date)

MXX <- na.omit(MXX)

df <- data.frame(Date=index(MXX),coredata(MXX))

df$MXX.High <- as.numeric(df$MXX.High)

df$MXX.Low <- as.numeric(df$MXX.Low)

df$MXX.Close <- as.numeric(df$MXX.Close)



MXX$MXX.High <- as.numeric(MXX$MXX.High)

MXX$MXX.Low <- as.numeric(MXX$MXX.Low)

MXX$MXX.Close <- as.numeric(MXX$MXX.Close)



# Crear BBands


bbands <- BBands( MXX[,c("MXX.High","MXX.Low","MXX.Close")],)


# join and subset data


df <- subset(cbind(df, data.frame(bbands[,1:3])), Date >= "2020-01-03")


# colors column for increasing and decreasing

for (i in 1:length(df[,1])) {

if (df$MXX.Close[i] >= df$MXX.Open[i]) {

df$direction[i] = 'Increasing'

} else {

df$direction[i] = 'Decreasing'

}

}


i <- list(line = list(color = 'forestgreen'))

d <- list(line = list(color = 'red'))


# Gráfico de velas


fig <- df %>% plot_ly(x = ~Date, type="candlestick",

open = ~MXX.Open, close = ~MXX.Close,

high = ~MXX.High, low = ~MXX.Low, name = "MXX",

increasing = i, decreasing = d)


fig <- fig %>% add_lines(x = ~Date, y = ~up , name = "B Bands",

line = list(color = '#ccc', width = 0.5),

legendgroup = "Bollinger Bands",

hoverinfo = "none", inherit = F)

fig <- fig %>% 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")

fig <- fig %>% add_lines(x = ~Date, y = ~mavg, name = "Mv Avg",

line = list(color = '#E377C2', width = 0.5),

hoverinfo = "none", inherit = F)

fig <- fig %>% layout(yaxis = list(title = "Precio"))



# Crear botones (rangos)

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 <- fig %>% layout(title = paste("IPC MÉXICO: 2020-01-03 -",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'))


fig



RESULTADO






DATOS





Esther Quiñones Luna
25 visualizaciones0 comentarios

Entradas Recientes

Ver todo

Comentarios

Obtuvo 0 de 5 estrellas.
Aún no hay calificaciones

Agrega una calificación
Publicar: Blog2_Post
bottom of page