Last active
March 4, 2022 20:56
-
-
Save prerak-proof/22b2d8dc00af52609073874bc6c7dc9d to your computer and use it in GitHub Desktop.
Symbology Conversion
This file contains 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
import re | |
def cms_to_inet(sym): | |
splits = sym.split(' ', 2) | |
if len(splits) == 1: | |
return sym | |
root, suffix = splits | |
suffix = suffix.replace('PR', '-') | |
suffix = suffix.replace('WD', '$') | |
suffix = suffix.replace('WS', '+') | |
suffix = suffix.replace('CL', '*') | |
suffix = suffix.replace('WI', '#') | |
suffix = suffix.replace('EC', '!') | |
suffix = suffix.replace('PP', '@') | |
suffix = suffix.replace('CV', '%') | |
suffix = suffix.replace('RT', '^') | |
suffix = suffix.replace('TEST', '~') | |
if suffix == 'U': | |
suffix = '=' | |
elif re.match('[A-Z][*%#]?', suffix): | |
suffix = '.' + suffix | |
return root + suffix | |
# more correctly, this is for the Bloomberg Tradebook symbology | |
# (which differs only for preferred symbols) | |
def cms_to_bbg(sym): | |
splits = sym.split(' ', 2) | |
if len(splits) == 1: | |
return sym | |
root, suffix = splits | |
suffix = suffix.replace('PR', '/P') | |
suffix = suffix.replace('WD', '/WD') | |
suffix = suffix.replace('WS', '/WS') | |
# suffix = suffix.replace('CL', '*') # we don't know yet | |
suffix = suffix.replace('WI', '-W') | |
# suffix = suffix.replace('EC', '!') # ? | |
# suffix = suffix.replace('PP', '@') # ? | |
# suffix = suffix.replace('CV', '%') # ? | |
suffix = suffix.replace('RT', '-R') | |
# suffix = suffix.replace('TEST', '~') # ? | |
# convert /WSA and /WDA to /WS/A and /WD/A | |
suffix = re.sub('/W([DS])([A-Z]+)', '/W\\1/\\2', suffix) | |
# convert BRK B to BRK/B (or generally, any other suffix as /suffix) | |
suffix = re.sub('^([A-Z])', '/\\1', suffix) | |
return root + suffix | |
def inet_to_yf_symbology(ticker): | |
ticker = ticker.replace('-', '-P') | |
ticker = ticker.replace('.', '-') | |
ticker = replace_suffix(ticker, '$', 'WD') | |
ticker = replace_suffix(ticker, '+', 'WT') | |
ticker = replace_suffix(ticker, '*', 'CL') | |
ticker = replace_suffix(ticker, '^', 'RI') | |
ticker = replace_suffix(ticker, '#', 'WI') | |
ticker = replace_suffix(ticker, '=', 'UN') | |
return ticker | |
def replace_suffix(ticker, sfx_from, sfx_to): | |
if sfx_from in ticker: | |
if '-' not in ticker: | |
sfx_to = '-' + sfx_to | |
return ticker.replace(sfx_from, sfx_to) | |
return ticker |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment