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))

To leave a comment for the author, please follow the link and comment on his blog: DataPunks.com » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.