Volume by Price charts with R – first attempt

[This article was first published on DataPunks.com » R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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 their blog: DataPunks.com » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)