Created
January 19, 2014 16:39
-
-
Save ryanh-ai/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
This file contains hidden or 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
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