Plotting Cumulative FII & DII Inflow against Nifty spot index

August 10, 2011

(This article was first published on My Paper Trades, and kindly contributed to R-bloggers)

Now that I have the FII and DII Inflow along with Nifty Index, I tried my hands on charting!

The objective is to plot the Cumulative FII, DII and Net Inflow and Nifty Index for current year (2011) on a single graph. This post takes inspiration from Deepak Shenoy’s post, wherein he has plotted some nice graphs.

The approach is to read the file created earlier for FII & DII Stats, convert the object as zoo (package used is quantmod), filter the data to contain only current years figure values, and then calculate cumulative sum using cumsum() fuction.

The extra effort done here is the use of right axis to display Nifty Index values using axis() function.

The graph contains 4 lines on the same plot, with the legends. If you observe, there is a high co-relation between Nifty spot and Net FII inflow. 68% is what the following line tell. Does this mean our markets trends are dictated by FIIs?

Cumulative Institutional inflow in 2011

The code to generate this plot is available here

The code is as below:


Dir = "D:\\FII Stats"
filename = "FII-DII-Nifty.csv"
#Read the csv file created earlier
temp <- read.csv(file.path(Dir, filename), header=TRUE, sep=",")
temp$Date <- as.Date(temp$Date, format="%d-%m-%Y")
temp <- temp[order(temp$Date, decreasing=FALSE),]
#Define Date as row name
rownames(temp) <- temp$Date
temp$ChangePct <- temp$Change*100 / Lag(temp$Close)#[2:length(temp$Close)]
#temp$ChangePct[$ChangePct)] <- 0
z <- as.zoo(temp)
#Filter for 2011
z <- z[z$Date > "2011-01-01"]
z$cumFIINetValue <- cumsum(z[,"FIINetValue"])
z$cumDIINetValue <- cumsum(z[,"DIINetValue"])
z$cumEffectiveInflow <- cumsum(z[,"EffectiveInflow"])
# Remove NAs
z[] <- 0
plot(cbind(z$cumDIINetValue,z$cumFIINetValue,z$cumEffectiveInflow), screens=c(1,1,1), type="l",
col=c("blue","red", "green"), xlab="Time",
ylab="Rs (In Cr)",
ylim = list(20100:-100,-20100:100), lwd=2,
main="Cumulative Institutional inflow in 2011", sub="")
plot(z$Close, screens=1,type="l", ann=FALSE, yaxt="n", ylim = 4500:6500, lwd=3, ylab="Nifty")
legend(x="topleft", bty="n", lty=c(1,1,1,1), col=c("blue","red","green","black"),
legend=c("DII Inflow", "FII Inflow", "Effective Inflow","Nifty"), lwd=2)

Created by Pretty R at

To leave a comment for the author, please follow the link and comment on their blog: My Paper Trades. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, 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...

Tags: , ,

Comments are closed.


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)