Skip to content

Extract option IDs from option chain

9 messages · rex, Jeff Ryan, Gabor Grothendieck +1 more

rex
#
I'm using Jeff Ryan's excellent QuantMod and its getOptionChain
to download option data. I've fruitlessly spent hours searching
for a way to extract option ID's as strings.

Format is:
$calls                                                                      
                    Strike   Last   Chg    Bid    Ask   Vol    OI           
AAPL100918C00150000    150 108.50 16.50 106.80 108.85     3    13           
AAPL100918C00155000    155  96.77  0.00 102.40 103.85     4    10           
AAPL100918C00160000    160  95.75  4.50  97.40  98.85    10    30           
[...]

$puts
                    Strike  Last   Chg   Bid   Ask   Vol    OI
AAPL100918P00150000    150  0.01  0.00    NA  0.01     6   876
AAPL100918P00155000    155  0.02  0.00    NA  0.01    30   666
AAPL100918P00160000    160  0.02  0.00    NA  0.01    79  1535
[...]

$symbol
[1] "AAPL"

The obvious thing fails to produce the desired result:
[1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

What I need are the indexes of both $calls and $puts as a set of
strings that can be split, etc. (I need the expiration date of the
options as a Date to be used to calculate the days to expiration.)

Thanks in advance for any help on this very frustrating problem.

The R world is too vast for my half-vast brain... :(

-rex
rex
#
rex <rex at nosyntax.net> [2010-09-06 16:11]:
A solution (fugly, for sure):
[[1]]
 [1] "AAPL100918P00150000" "AAPL100918P00155000" "AAPL100918P00160000"
 [4] "AAPL100918P00165000" "AAPL100918P00170000" "AAPL100918P00175000"
 [7] "AAPL100918P00180000" "AAPL100918P00185000" "AAPL100918P00190000"
[10] "AAPL100918P00195000" "AAPL100918P00200000" "AAPL100918P00210000"
[13] "AAPL100918P00220000" "AAPL100918P00230000" "AAPL100918P00240000"
[16] "AAPL100918P00250000" "AAPL100918P00260000" "AAPL100918P00270000"
[19] "AAPL100918P00280000" "AAPL100918P00290000" "AAPL100918P00300000"
[22] "AAPL100918P00310000" "AAPL100918P00320000" "AAPL100918P00330000"

[[2]]
[1] "Strike" "Last"   "Chg"    "Bid"    "Ask"    "Vol"    "OI"
[[1]]
 [1] "AAPL100918P00150000" "AAPL100918P00155000" "AAPL100918P00160000"
 [4] "AAPL100918P00165000" "AAPL100918P00170000" "AAPL100918P00175000"
 [7] "AAPL100918P00180000" "AAPL100918P00185000" "AAPL100918P00190000"
[10] "AAPL100918P00195000" "AAPL100918P00200000" "AAPL100918P00210000"
[13] "AAPL100918P00220000" "AAPL100918P00230000" "AAPL100918P00240000"
[16] "AAPL100918P00250000" "AAPL100918P00260000" "AAPL100918P00270000"
[19] "AAPL100918P00280000" "AAPL100918P00290000" "AAPL100918P00300000"
[22] "AAPL100918P00310000" "AAPL100918P00320000" "AAPL100918P00330000"
[1] "list"
[1] "AAPL100918P00155000"
[1] "100918"
< dat
[1] "20100918"
[1] "2010-09-18"

The above has got to be about the most convoluted and arcane
method to get the expiration date one can imagine.

Surely there's a simple way? All I want is the number of trading
days to expiration of options of a particular type that expire in
a given month.

-rex
#
On Mon, Sep 6, 2010 at 8:37 PM, rex <rex at nosyntax.net> wrote:
Here are a few approaches and variations:
[1] "2010-09-18" "2010-09-18" "2010-09-18"
[1] "2010-09-18" "2010-09-18" "2010-09-18"
[1] "2010-09-18" "2010-09-18" "2010-09-18"
[1] "2010-09-18" "2010-09-18" "2010-09-18"
#
Yahoo hasn't done a stellar job with the OSI initiative as far as I
can tell. They seem to be getting better, but as Marc points out, it
is far from perfect.

You could possibly go from right to left to avoid the symbol width
issue (which *should* be 6 wide).

One other comment though --- if you are just looking to get days to
expiry, you are specifically requesting that in the call to retrieve
the chains - so all the parsing isn't really needed for that part.

Best,
Jeff
On Mon, Sep 6, 2010 at 10:29 PM, Marc Delvaux <mdelvaux at gmail.com> wrote:

  
    
#
On Mon, Sep 6, 2010 at 11:29 PM, Marc Delvaux <mdelvaux at gmail.com> wrote:
If we can assume that its garbage followed by 6 good digits followed
by P then just replace \d+ with \d{6} in the strapply solution like
this:
+ "BHI101016P00020000", "BHI1101016P00020000", "BHI1101016P00021000",
+ "BHI1101016P00022000", "BHI101016P00022500", "BHI1101016P00023000",
+ "BHI1101016P00024000", "BHI101016P00025000", "BHI1101016P00025000",
+ "BHI1101016P00026000", "BHI101016P00030000", "BHI101016P00034000",
+ "BHI101016P00035000", "BHI101016P00036000", "BHI101016P00037000",
+ "BHI101016P00038000", "BHI101016P00039000", "BHI101016P00040000",
+ "BHI101016P00041000", "BHI101016P00042000", "BHI101016P00043000",
+ "BHI101016P00044000", "BHI101016P00045000", "BHI101016P00046000",
+ "BHI101016P00047000", "BHI101016P00048000", "BHI101016P00049000",
+ "BHI101016P00050000", "BHI101016P00055000", "BHI101016P00060000",
+ "BHI101016P00065000")
[1] "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16"
 [6] "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16"
[11] "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16"
[16] "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16"
[21] "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16"
[26] "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16"
[31] "2010-10-16" "2010-10-16" "2010-10-16" "2010-10-16"
#
It is not Yahoo for BHI, this is the arguably strange method that was
specified for contract changes, and
OCC should/could have selected something that makes a little bit more
sense.  The relevant text from
http://www.optionsclearing.com/components/docs/initiatives/symbology/contract_adjustments_and_the_osi.pdf
reads like this

"Under OSI, the option symbol in most cases will be the same as the
stock symbol. For
example, ?MSFT? will be the option symbol for the underlying security
?MSFT?. If an
option symbol change is necessary in a contract adjustment (e.g., 3
for 2 split), option
symbol MSFT will change to MSFT1, with the numeric suffix identifying
this option as
an adjusted, ?non-standard? contract."

This mostly guarantees that you need extra information about the exact
change to interpret the non standard
quotes, so IMO it is simpler to drop all of these non standard symbols
when using automated scripts.  OCC choice
was poor as it may be difficult to spot the extra "1" especially for
years 201x, it literally took me 10+ minutes to
spot the difference while trying to understand why my script was
failing to correctly calculate the expiration date :-(
On Mon, Sep 6, 2010 at 8:45 PM, Jeff Ryan <jeff.a.ryan at gmail.com> wrote:
1 day later
rex
#
Marc Delvaux <mdelvaux at gmail.com> [2010-09-06 20:30]:
Thanks for the heads up.

As Jeff mentioned, this is a Yahoo implementation problem, not
OSI. While OSI provides for symbol changes as you pointed out, OSI
also specifies field widths, and the symbol field should be padded
if the symbol < 6 chars. Unfortunately, the padding character is
apparently not specified, and longer fields are permitted. (!) 

http://www.optionsclearing.com/components/docs/initiatives/symbology/symbology_initiative_v1_8.pdf

(Page 4)
Minimum field sizes
      Symbol ? 6 bytes
      Year ? 2 bytes
      Month ? 2 bytes
      Day ? 2 bytes
      Call/Put indicator ? 1 byte
Your command above fails for me:
Error in as.yearmon.character(allExp) :
  yearmon variable can only have one format

So does your next line when used on a successful getOptionChain call:
Error in Options[[2]]$puts : $ operator is invalid for atomic
vectors

Both rownames(Options[[2]]) and rownames(Options$puts) return the
desired result.

I don't see how the commands below could possibly have worked for you.
-rex
rex
#
Jeff Ryan <jeff.a.ryan at gmail.com> [2010-09-06 20:46]:
The expiration day is not specified in my call. The call to getOptionChain is:
allOpts <- getOptionChain(stkSym, Exp=optExpire) where optExpire
<- "2010-09". So, parsing the returned option data is required to
get the expiration day unless some other solution is used.

The obscure part of my code is this line:
I only discovered it provided what's needed after a lot of
fiddling with things that didn't work. I now use the much clearer:

id <- rownames(allOpts$puts)

It's obvious in retrospect. :(

The rest is straightforward:
Jeff, thanks again for your many and continuing contributions to
the R community. I use QuantMod constantly and recently started
exploring your IBrokers package. 

-rex