Monday 31 December 2012

Extracting 1 minutes /5 minutes Bar from Google Finance

Many retail traders or wanna be traders hit the road block on how to get the intraday data for NSE stocks. There are many paid services/databases available which can be subscribed to get the data. However if you are just starting to test your strategy and do not want to spend on getting the historical data, then here's the alternative. This post will explain you the R code using which you shall be able to download intraday 1 min, 5 min ... data from google finance for free. You need not to be a technical person to use this.


First of all you will need to download R software which is free. Here's the link to download R for windows OS.
Once downloaded and installed R, you shall see a R icon on your desktop like below. Double click to open 

 

It will open RGui. Go to File --> New Script.

 

Copy paste the R code below in the new script window.

symbol = "CIPLA"  
 noDays = 1  
 interval = 60 #Seconds  
 dirPath = "C:/Umesh/Data/"  
 fileName = paste(dirPath,symbol,".csv",sep="")  
   
 download.file(paste("http://www.google.com/finance/getprices?q=",symbol,"&x=NSE&i=",interval,"&p=",noDays,"d&f=d,o,h,l,c,v,t",sep=""), fileName)  
   
 unix2POSIXct <- function (time)  structure(time, class = c("POSIXt", "POSIXct"))  
 data = read.table(fileName,sep=",",col.names=c("DATE1","CLOSE","HIGH","LOW","OPEN","VOLUME"),fill=TRUE)  
 data$DATE = 0  
 data$TIME = 0  
   
 for (i in 8:nrow(data))  
 {  
  if(i==8 || substr(as.vector((data$DATE1[i])),1,1) == "a")  
  {  
   tempDate = unix2POSIXct(as.numeric(substr(as.vector((data$DATE1[i])),2,nchar(as.vector((data$DATE1[i]))))))    
   data$DATE[i] = as.numeric(format(tempDate,format="%Y%m%d"))  
   data$TIME[i] = as.numeric(format(tempDate,format="%H%M"))  
  } else {  
   tempDate1 = tempDate + as.numeric(as.vector(data$DATE1[i]))*interval   
   data$DATE[i] = as.numeric(format(tempDate1,format="%Y%m%d"))  
   data$TIME[i] = as.numeric(format(tempDate1,format="%H%M"))  
  }   
 }  
 data1=as.data.frame(data)  
 data1=(data1[data1$TIME>915 & data1$TIME<=1530,])  
   
 finalData = data.frame(DATE=as.vector(data1$DATE),TIME=as.vector(data1$TIME),CLOSE=as.vector(data1$CLOSE),HIGH=as.vector(data1$HIGH),LOW=as.vector(data1$LOW),OPEN=as.vector(data1$OPEN),VOLUME=as.vector(data1$VOLUME))  
 finalData = data.frame(DATE=data1$DATE,TIME=data1$TIME,CLOSE=data1$CLOSE,HIGH=data1$HIGH,LOW=data1$LOW,OPEN=data1$OPEN,VOLUME=data1$VOLUME)  
 write.csv(finalData,file=fileName,row.names=FALSE) 
 
  

In the above code, change the first four lines shown below.

symbol = "CIPLA"  
 noDays = 1  
 interval = 60 #Seconds  
 dirPath = "C:/Umesh/Data/"  
  

1. The first line is the symbol name.

2. Second line sets the number of days for which data is to be downloaded. Note that, longer history data is not available. 

3. Third line sets the time interval/frequency of data. It's in seconds.

4. Fourth line sets the folder where data is to be stored.

Once done, select all lines and press F5 to run the code. And data will be stored in the specified directory with symbol name.

Now, enjoy downloading data. Later on I will explain how to create an automated job in windows to download daily data automatically.