Data Visualisation: Correlation Analysis of Stock Market Data

Correlation Analysis and Portfolio Management: Correlation is a statistical tool for finding the strength of the type of the relationship among two variables. Correlation analysis helps you in diversifying your portfolio and at the same time minimize the risk. But how can diversification help in minimizing the risk?

Let’s imagine you buy two stocks, one of a company which produces woolen clothes and another which produces knit wear, you may gain from these two shares in the winter season when the demand for warm clothes is high but will lose a lot of money otherwise because of which it becomes important to diversify your portfolio. Correlation helps in diversifying the portfolio by measuring the status and strength of relationship among stocks.
Here's the step wise coding in R

# Get stock prices and other relevant information from yahoo

>getSymbols(c("TTM","DLF.NS", "PARAGMILK.BO", "SBIN.NS", "ITC.NS", "APOLLOTYRE.BO", "COALINDIA.NS" ), src="yahoo" , from ="2016-01-01", to = Sys.Date())

# Save the closing prices of each Stock as vector

>ClosingPriceTTM <- as.numeric(TTM$TTM.Close)
>ClosingPriceDLF <- as.numeric(DLF.NS$DLF.NS.Close)
>ClosingPricePARAGMILK <- as.numeric(PARAGMILK.BO$PARAGMILK.BO.Close)
>ClosingPriceSBI <- as.numeric(SBIN.NS$SBIN.NS.Close)
>ClosingPriceITC <- as.numeric(ITC.NS$ITC.NS.Close)
>ClosingPriceCOALIND <- as.numeric(COALINDIA.NS$COALINDIA.NS.Close)

# Make an empty Vector

>ClosePrice <- vector(mode="numeric", length=0)

# Build a matrix of all the stocks with closing price

>ClosePrice$TTM <- ClosingPriceTTM[1:300]
>ClosePrice$PARAGMILK <- ClosingPricePARAGMILK[1:300]
>ClosePrice$DLF <- ClosingPriceDLF[1:300]
>ClosePrice$SBI <- ClosingPriceSBI[1:300]
>ClosePrice$ITC <- ClosingPriceITC[1:300]
>ClosePrice$APOLLOty <- ClosingPriceAPOLLOTYRES[1:300]
>ClosePrice$COALIND <- ClosingPriceCOALIND[1:300]

>ClosePriceDF <-

# Visualise the stocks Closing price

>ClosePriceDFDummy <- ClosePriceDF # Make a dummy variable for visualisation
>ClosePriceDFDummy$Day <- c(1:300)

>qplot(data = ClosePriceDFDummy, x = ClosePriceDFDummy$Day,  y = ClosePriceDFDummy$PARAGMILK, geom = "line",  xlab = "Days", ylab = "Stock Prices")+
geom_line(y = ClosePriceDFDummy$TTM, colour = "blue")+
geom_line(y = ClosePriceDFDummy$APOLLOty, colour = "red")+
geom_line(y = ClosePriceDFDummy$DLF, colour = "pink")+
geom_line(y = ClosePriceDFDummy$SBI, colour = "dark green")+
geom_line(y = ClosePriceDFDummy$ITC, colour = "yellow")+
geom_line(y = ClosePriceDFDummy$COALIND, colour = "brown")+
scale_y_continuous(limits = c(0,400), breaks = seq(0,400,50))


# Find Correlation among all the stocks

>StocksCorr <- rcorr(as.matrix(ClosePriceDF))

# Visualise the correlation

>col <- colorRampPalette(c("#BB4444", "#EE9988", "#FFFFFF", "#77AADD", "#4477AA"))
>corrplot(StocksCorr$r, method = "color", col = col(10),  type = "upper", order = "hclust", addCoef.col = "black",tl.col = "darkblue", = 45,  p.mat = StocksCorr$P, sig.level = 0.05, diag = FALSE)

For any query contact us at

Connect With Us