I am trying to use Mathematica to create a stock options database of sorts. That is I wish to write a function that imports the option chain of a given stock. Unfortunately Wolfram has yet to put stock options on the data server associated with FinancialData[]
so I decided to obtain the necessary data from Yahoo Finance. Here is the function I wrote to do this:
rules = {"Jan" -> "01", "Feb" -> "02", "Mar" -> "03", "Apr" -> "04",
"May" -> "05", "Jun" -> "06", "Jul" -> "07", "Aug" -> "08",
"Sep" -> "09", "Oct" -> "10", "Nov" -> "11", "Dec" -> "12"};
acquireOptions[stock_String] :=
Module[{z =
StringSplit[Import["http://finance.yahoo.com/q/op?s=" <> stock <>"+Options"]],index1, index2},
index1 = Position[z, "Expiration:"][[1, 1]] + 1;
index2 = Position[z, "Call"][[1, 1]] - 1;
ArrayFlatten[(Partition[(DeleteCases[z[[index1 ;; index2]], "|"] /.
rules), 2] /. {x_String,
y_String} :> {"20" <> y <> "-" <> x}) /. {x_String :>
Cases[Import[
"http://finance.yahoo.com/q/op?s=" <> stock <> "&m=" <> x,
"Data"], {_?NumberQ, _String, __?NumberQ}, \[Infinity]]}]]
I should note that I had to study the structure of the Yahoo Finance website hard core to make this work. Although it is functional the problem I have is that it is simply too slow. Running this function for a single stock takes about 30 seconds. Lets assume there are 1000 stocks that have options that I would want to acquire (there are probably a lot more than 1000, I would literally want to be able to acquire every last option). This would take about 8 and a half hours which is just too long. So I'm interested in how I can do this more efficiently. If I used parallel table on my 8 core machine could I expect this to only take about 2 hours?
Besides for the code I've written, I saw that Yahoo has a query language YQL that can be used to pull data off their servers. I then saw that Mathematica has Database Link SQL operations. I don't know much about SQL/YQL but would this have been a better direction to go in? If so would anyone be able to show how to link Mathematica and YQL and provide an example where YQL is used in Mathematica to obtain options data?
Oh and in case anyone was wondering why I am doing this, it is for an investment model I'm working on.
Thanks
IN
and a list of years. As for type, you can useOR
I think:SELECT * FROM yahoo.finance.option_chain WHERE symbol='GOOG' AND expiration=2013 AND (type='C' OR type='P')
. However, you cannot omit either symbol, expiration or type. They are all required. $\endgroup$