Skip to content

Instantly share code, notes, and snippets.

@canada4663
Created January 19, 2014 16:39
Show Gist options
  • Save canada4663/8507301 to your computer and use it in GitHub Desktop.
Save canada4663/8507301 to your computer and use it in GitHub Desktop.
Simple Method for Parsing Stock Option Symbols into Underlying, Expiration Date, Option Type, and Strike Price
def _parse_option(self,symbol):
#option_format = r'''
# ^([\D]{1,6})
# ([\d]{2})
# ([\d]{2})
# ([\d]{2})
# ([PC]{1})
# ([\d]{5})
# ([\d]{3})
# $'''
parsed = re.match(r'^([\D]{1,6})([\d]{2})([\d]{2})([\d]{2})([PC]{1})([\d]{5})([\d]{3})$',
symbol)
##TODO ADD: [Derivative._parse_option] Raise Appropriate Exception
if not parsed:
raise
self._underlying_symbol = parsed.group(1).upper()
year = int(parsed.group(2))
if year > 50: year +=1900
if year < 50: year +=2000
self.expiration = datetime(year,
int(parsed.group(3)),
int(parsed.group(4)))
self.type = parsed.group(5).upper()
self.strike = float(parsed.group(6)) + float(parsed.group(7))/1000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment