Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use regex to parse an OCC Option Symbol?

The OCC option symbol consists of 4 parts:

  1. Root symbol of the underlying stock or ETF, padded with spaces to 6 characters
  2. Expiration date, 6 digits in the format yymmdd
  3. Option type, either P or C, for put or call
  4. Strike price, as the price x 1000, front padded with 0s to 8 digits

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

like image 436
Shamoon Avatar asked Jun 07 '17 02:06

Shamoon


People also ask

What are the symbols used in regex?

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.

How do you check if a regex is correct?

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 ...

How to match against a string using regex?

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.")

Are there any tutorials for regular expressions?

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.


2 Answers

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]);
like image 195
Yonah Karp Avatar answered Sep 21 '22 21:09

Yonah Karp


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.

like image 23
Tim Biegeleisen Avatar answered Sep 21 '22 21:09

Tim Biegeleisen