The OCC option symbol consists of 4 parts:
As an example SPX 141122P00019500
means a put on SPX, expiring on 11/22/2014, with a strike price of $19.50.
Is it possible to use regex to parse this out automatically? I'm using JavaScript
Regex symbol list and regex examples Period, matches a single character of any single character, except the end of a line. For example, the below regex matches shirt, short and any character between sh and rt. sh.rt ^ Carat, matches a term if the term appears at the beginning of a paragraph or a line.
A regex expression is really trying to find what you've asked it to search for. When there's a regex match, it's verification your expression is correct. You could simply type 'set' into a Regex parser, and it would find the word "set" in the first sentence. You could also use 's.t' in the parser, which will find all words ...
A pattern defined using RegEx can be used to match against a string. Matched? Python has a module named re to work with RegEx. Here's an example: import re pattern = '^a...s$' test_string = 'abyss' result = re.match (pattern, test_string) if result: print("Search successful.") else: print("Search unsuccessful.")
Whether a regex pattern is very simple or extremely sophisticated, it is built using the common syntax. This tutorial does not aim to teach you regular expressions. For this, there are plenty of resources online, from free tutorials for beginners to premium courses for advanced users.
Here is the regex (I highly recommend http://regexr.com)
([\w ]{6})((\d{2})(\d{2})(\d{2}))([PC])(\d{8})
Group1: ETF
Group2: Year
Group3:Month
Group4: Day
Group5: put/call
Group6: strike price
Your js would look something like this (somewhat psuedo-code. Not tested)
var myString = "SPX 141122P00019500";
var myRegexp = /([\w ]{6})((\d{2})(\d{2})(\d{2}))([PC])(\d{8})/g;
var match = myRegexp.exec(myString);
console.log("a " + match[5] + " on " + match[1].trim() + ", expiring on " + match[3] + "/" + match[4] + "/20" + match[2] + " with a strike price of $" + match[6]);
I don't think you even need a regex, assuming the OCC option string has a fixed format. Instead, you can try just using substring()
to extract the various components.
var occ = 'SPX 141122P00019500';
var symbol = occ.substring(0, 3);
var year = parseInt(occ.substring(6, 8)) + 2000;
var month = occ.substring(8, 10);
var day = occ.substring(10, 12);
var date = month + '/' + day + '/' + year;
var type = occ.substring(12, 13) == 'P' ? 'put' : 'call';
var price = parseFloat(occ.substring(13, 21)) / 1000.0;
var output = 'a ' + type + ' on ' + symbol + ', expiring on ' + date +
', with a strike price of $' + price.toFixed(2); + '.';
console.log(output);
I would expect using substring to build your output string would generally perform better than using a regex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With