Processing math: 100%

Dec 31, 2015

R code to download Options and Futures data from NSE

The code below can download daily bhav for Futures and Options from the NSE website

I have also include sample code that downloads indices level from the National Stock Exchange

#### For OS x libcurl may not work###
# This code work without quantmode#
NSEIndex = "^NSEI"
# Risk free interest rate fixed at 7.5%
# Source NSE Mibor
riskfree = 0.075
#Specify date
dates = c("12/28/2015")
tdata <- as.Date(dates, format = "%m/%d/%Y")
# Find URL link at NSE website for the bhavcopy of the day
urllink = paste("http://www.nseindia.com/content/historical/DERIVATIVES/",(format(tdata, "%Y")),"/",toupper(format(tdata, "%b")),"/fo",toupper(format(tdata, "%d%b%Y")),"bhav.csv.zip", sep="")
tempzip = paste(toupper(format(tdata, "%d%b%Y")),"bhav.csv.zip", sep="")
tempfilename = paste("fo",toupper(format(tdata, "%d%b%Y")), "bhav.csv", sep="")
# download the compressed file and extract
download.file(urllink,tempzip, mode="wb" , method = "libcurl")
unzip(tempzip, tempfilename, overwrite = T)
optiondf <- read.table(tempfilename, sep=",", header=T)
print("Downloaded derivative market data for the day")
print(head(optiondf))
# Download or read indices' close price
indexlnk = paste("http://www.nseindia.com/content/indices/ind_close_all_", toupper(format(tdata, "%d%m%Y")),".csv", sep="")
tempindexzip = paste(toupper(format(tdata, "%d%m%Y")),"index.csv", sep="")
download.file(indexlnk,tempindexzip, mode="wb" , method = "libcurl")
indexdf <- read.table(tempindexzip, sep=",", header=T)
print("Downloaded market index data for the day")
print(head(indexdf))
view raw NSEData.R hosted with ❤ by GitHub

No comments:

Post a Comment