This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(data.table) | |
library(RQuantLib) | |
#### 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 in mm/dd/yyyy | |
dates = c("01/06/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)) | |
# get NIFTY 50 Index price | |
indprice = 0 | |
# Dataframe for put/call options | |
optionindxdf = NULL | |
otmput = NULL | |
otmcall =NULL | |
# find NIFTY index price | |
if(length(indexdf)>0) { | |
colmhead = "Nifty 50" | |
if(colmhead %in% indexdf[, 1]){ | |
indprice = indexdf[indexdf$Index.Name =="Nifty 50","Closing.Index.Value"] | |
} | |
else{ | |
indprice = indexdf[indexdf$Index.Name =="CNX Nifty","Closing.Index.Value"] | |
} | |
} | |
if(length(indexdf)>0) { | |
# Near expirty date | |
optiondfEDT = as.Date(optiondfEXPIRY_DT, "%d-%b-%Y") | |
# Days to maturity | |
optiondfdtm = as.numeric(optiondfEDT - tdata) | |
nearexp = min(optiondf[(optiondfdtm>20) & (optiondfdtm<100), "EDT"]) | |
optselindx = (optiondfINSTRUMENT=="OPTIDX") & (optiondfSYMBOL=="NIFTY") & (optiondfCONTRACTS>0) & (optiondfEDT==nearexp) | |
optionindxdf = data.table(optiondf[optselindx, ]) | |
# Out of money call and put options | |
otmput = optionindxdf[(optionindxdfOPTION_TYP=="PE") & (optionindxdfSTRIKE_PR<indprice), ][order(STRIKE_PR)] | |
otmcall = optionindxdf[(optionindxdfOPTION_TYP=="CE") & (optionindxdfSTRIKE_PR>indprice), ][order(STRIKE_PR)] | |
otmput$X = NULL | |
otmcall$X = NULL | |
print("Call option details:") | |
print(head(otmcall)) | |
print("Put option details:") | |
print(head(otmput)) | |
# Calculate implied volatility | |
idx = seq(1, dim(otmput)[1]) | |
otmput$IV = 0.01 | |
for(j in idx){ | |
oval = otmput[j, CLOSE] | |
stk = otmput[j, STRIKE_PR] | |
# Annualized days | |
matrty = otmput[j, dtm]/360.0 | |
dvdyld = 0 | |
expvol = 0.1 | |
civ = EuropeanOptionImpliedVolatility(type="put", value=oval, underlying=indprice, strike=stk, dividendYield=dvdyld, riskFreeRate=riskfree, maturity=matrty, volatility=expvol) | |
otmput$IV[j] = civ | |
} | |
idx = seq(1, dim(otmcall)[1]) | |
otmcall$IV = 0.01 | |
for(j in idx){ | |
oval = otmcall[j, CLOSE] | |
stk = otmcall[j, STRIKE_PR] | |
matrty = otmcall[j, dtm]/360.0 | |
dvdyld = 0 | |
expvol = 0. | |
civ = EuropeanOptionImpliedVolatility(type="call", value=oval, underlying=indprice, strike=stk, dividendYield=dvdyld, riskFreeRate=riskfree, maturity=matrty, volatility=expvol) | |
otmcall$IV[j] = civ | |
} | |
# Plot the volatility smile/smirk | |
smirkval = c(otmputIV, otmcallIV) | |
smirkstk = c(otmputSTRIKE_PR, otmcallSTRIKE_PR) | |
plot(smirkstk, smirkval, xlab="Strike", ylab= "Implied volatility", main ="Volatility smile", type="l", col = "dark red") | |
abline(v=indprice, col="blue") | |
text(indprice, 0.2, "Stock Price") | |
} | |
No comments:
Post a Comment