# Volume by Price charts with R – first attempt

October 21, 2011
By

(This article was first published on DataPunks.com » R, and kindly contributed to R-bloggers)

I stumbled upon this chart in the R Graph Gallery, which got me thinking someone could come up with a Volume by Price chart using R.

Such charts can be useful to determine support and resistance levels, as they illustrate amount of volume for different price ranges.

Below is my first attempt at this. Note that
1. the left axis (price) is not perfectly aligned and doesn’t show up properly
2. the horizontal barplot could be upgraded to a stacked barplot identifying which part of the volume came from a up/down price movement.

 library(quantmod) sumVol <- function(x) { sum(Vo(subset(GSPC, GSPC$t == x))) } getSymbols("^GSPC") hi <- hist(Cl(GSPC), plot=F) b <- hi$breaks GSPC\$t <- floor(Cl(GSPC)/ 100) * 100 vols <- unlist(lapply(b, sumVol)) t <- as.table(vols) names(t) <- b   plot(GSPC, col='red') par(new=T) barplot(t, horiz=T, axes=F, col=rgb(0.1,0.7,0.1,alpha=0.1))