Intraday Price-Volume HeatMap Of A Stock Using R

In this article, we will see how to create price volume heatmap of any stock ticker in R. The data used is historical intra-day ticks of the stock. A heatmap is a colored representation of data where individual values are represented as the shade of color.

The code is written in simple form so as easy to understand but multiple lines can be combined into one. 

Step 1

Get input data in format as attached in the zip. A sample row being ICICIBANK,20180409,09:08,277.75,277.75,277.75,277.75,86894 where "StockName","Date","Time","Open","HIgh","Low","Close","Volume" are meaning of the data.
Step 2

Install and Import Packages
  1. install.packages("plyr")  
  2. install.packages("lubridate")  
  3. install.packages("ggplot2")  
  4. install.packages("dplyr")  
  5. library(plyr)  
  6. library(lubridate)  
  7. library(ggplot2)  
  8. library(dplyr)  
Step 3

To avoid seeing e+ in the volume legend.
  1. options(scipen=10000000)  
Step 4

Set Variables and Import Data. The colors need to be identified correctly so that the heatmap make a good gradient.
  1. tickerFile <- file.choose()  
  2. APTicker <- read.csv(tickerFile,sep=",", fill=TRUE, stringsAsFactors=F, header = F)  
  3. colnames(APTicker) <- c("StockName","Date","Time","Open","HIgh","Low","Close","Volume")   
  4. # Colour shades for volume  
  5. col1 = "#abefbf"  
  6. col2 = "#22B14C"  
Step 5

Convert hour and minute using lubridate.
  1. APTicker$Date <- ymd(APTicker$Date)  
  2. APTicker$Time <- hm(APTicker$Time)  
  3. APTicker$DateTime <- ymd_hms(paste(APTicker$Date + APTicker$Time))  
  4. APTicker$TickHour <- floor_date(APTicker$DateTime, "1 hour")  
  5. APTicker$Tick15Minute <- ceiling_date(APTicker$DateTime, "15 mins")  
  6. APTicker$Tick30Minute <- ceiling_date(APTicker$DateTime, "30 mins")  
  7. attach(APTicker)  
Step 6

Start working on HeatMaps. Create summary table for heatmap- Volume/15 min Specific
  1. Min15Volume <- ddply(APTicker, .(Tick15Minute), summarise, Min15Volume = sum(Volume),  
  2. Mean15MinPrice=mean(Close))  
  3. Min15Volume$Tick15 <- paste(hour(Min15Volume$Tick15Minute),":", minute(Min15Volume$Tick15Minute))  
  4. FactorMin15Volume <- factor(Min15Volume$Tick15,levels = Min15Volume$Tick15[order(Min15Volume$Tick15Minute)])  
  5. FactorMin15MeanPrice <- factor(as.integer(Min15Volume$Mean15MinPrice)) 
Step 7 - Plot the map
  1. ggplot(Min15Volume, aes(FactorMin15Volume, FactorMin15MeanPrice)) +  
  2. geom_tile(aes(fill = Min15Volume),colour = "white", na.rm = TRUE) +  
  3. scale_fill_gradient(low = col1, high = col2) +  
  4. guides(fill=guide_legend(title="Volume Traded")) +  
  5. theme_bw() + theme_minimal() +  
  6. labs(title = paste("Histogram of 15 min interval Volume of " , as.character(APTicker$StockName[0:1]) , " for Date: " , as.character(APTicker$Date[0:1])),  
  7. x = "Volume Per 15 mins", y = "15 mins Mean Price") +  
  8. theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())  
Step 8

Check Result


The attached code file contains code for a 30 minute chart and hourly chart which you can download and try. Please note that there are multiple ways to achieve the same result. Other libraries that support date time data can also be used to achieve the same purpose. The code can be modified to a live feed if available.


Similar Articles