Reputation: 21
This is my first post on this site and I am looking forward to becoming more involved as my skills in coding increase.
My first question involves scraping options (calls and puts) data from the web using yahoo or google finance. I am interested in running code, in R
, which will look at the asking price for a call/put on a given stock. Specifically, I hope to run a code which examines all optionable stocks ( CSV
file containing all symbols can be found at https://www.cboe.com/tradtool/symbols/symboldirectory.aspx ) and returns a list of stock symbols corresponding to those stocks which have a asking price for a given call/put at or below a certain price with a strike price within a given percentage of the current trading price.
For example, assume that I am looking at the hypothetical company ABC which is currently trading at $100 a share. If there exist a put or call option on this stock for a $1 premium or less with a strike price between $90 and $110 ( e.g. within 10% of the current price ) I would be interested in trading this stock.
Expanding this, I would be interested in generating an algorithm, which would search all optionable stocks and return a list of stock symbols corresponding to those stocks meeting this criteria.
I have extensively searched through the available resources on this site and have found some insight into methods of scraping options data, specifically using the script as presented below (gathering data for Apple) and described in https://mktstk.com/2014/12/29/start-trading-like-a-quant-download-option-chains-from-google-finance-in-r/.
library(RCurl)
library(jsonlite)
getOptionQuote <- function(symbol){
output = list()
url = paste('http://www.google.com/finance/option_chain?q=', symbol, '&output=json', sep = "")
x = getURL(url)
fix = fixJSON(x)
json = fromJSON(fix)
numExp = dim(json$expirations)[1]
for( i in 1:numExp ){
# download each expirations data
y = json$expirations[i,]$y
m = json$expirations[i,]$m
d = json$expirations[i,]$d
expName = paste(y, m, d, sep = "_")
if ( i > 1 ){
url = paste('http://www.google.com/finance/option_chain?q=', symbol, '&output=json&expy=', y, '&expm=', m, '&expd=', d, sep = "")
json = fromJSON(fixJSON(getURL(url)))
}
output[[paste(expName, "calls", sep = "_")]] = json$calls
output[[paste(expName, "puts", sep = "_")]] = json$puts
}
return( output )
fixJSON <- function(json_str){
stuff = c( 'cid','cp','s','cs','vol','expiry','underlying_id','underlying_price','p','c','oi','e','b','strike','a','name','puts','calls','expirations', 'y','m','d' )
for ( i in 1:length( stuff ) ){
replacement1 = paste( ',"', stuff[i], '":', sep = "" )
replacement2 = paste( '\\{"', stuff[i], '":', sep = "" )
regex1 = paste( ',', stuff[i], ':', sep = "" )
regex2 = paste( '\\{', stuff[i], ':', sep = "" )
json_str = gsub( regex1, replacement1, json_str )
json_str = gsub( regex2, replacement2, json_str )
}
return( json_str )
}
aapl_opt = getOptionQuote("AAPL")
However, this code does not support examining multiple stocks at once and thus has not been successful for my application.
Upvotes: 2
Views: 1322
Reputation: 5152
Doing that for multiple stocks is straight forward. Try this:
res=lapply(c("AAPL","MSFT"),getOptionQuote)
names(res)<-c("AAPL","MSFT")
#str(res)
i=2
plot(res$AAPL[[i]]$strike, res$AAPL[[i]]$oi, type = "s", main = paste("Open Interest by Strike", names(res$AAPL)[i]))
For other parts of your questions get information from res object.
Upvotes: 1