Skip to content

Instantly share code, notes, and snippets.

@tristanwietsma
Created April 30, 2013 02:15
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tristanwietsma/5486236 to your computer and use it in GitHub Desktop.
Save tristanwietsma/5486236 to your computer and use it in GitHub Desktop.
CME settlement price fetcher Ugly, but effective, script for getting CME settlement prices off their FTP side. Could use a clean up...
import sys, string, urllib
GROUPS = dict([('ags','ftp://ftp.cmegroup.com/pub/settle/stlags'),
('rates','ftp://ftp.cmegroup.com/pub/settle/stlint'),
('fx','ftp://ftp.cmegroup.com/pub/settle/stlcur'),
('nymex','ftp://ftp.cmegroup.com/pub/settle/stlnymex'),
('comex','ftp://ftp.cmegroup.com/settle/stlcomex'),
('clearport','ftp://ftp.cmegroup.com/settle/stlcpc')])
# month codes
MONTHDICT = dict([(i+1, 'FGHJKMNQUVXZ'[i]) for i in range(12)])
MONTHDICT.update(dict([('FGHJKMNQUVXZ'[i], i+1) for i in range(12)]))
MONTH_NAMES = 'JAN FEB MAR APR MAY JUN JLY AUG SEP OCT NOV DEC'.split()
MONTHDICT.update(dict(zip(MONTH_NAMES, 'F G H J K M N Q U V X Z'.split())))
def month_code(month):
return MONTHDICT[month]
def remove_word(sentence, word, separator):
return string.join(sentence.split(word), separator).strip()
def Settlements(grp):
path = GROUPS[grp]
u = urllib.urlopen(path)
lines = u.readlines()
print lines[0].strip()
u.close()
RES = {}
key = None
for l in lines:
if len(l.strip()) == 0: continue
tokens = [t for t in l.split() if len(t.strip()) > 0]
if tokens[0] == 'TOTAL': continue
if len(tokens) > 5 and tokens[1] == tokens[2] == tokens[3] \
== tokens[4] == '----': continue
try: int(tokens[0]); continue
except: pass
if tokens[-1] in ['CALL', 'PUT', '***']: continue
if tokens[1] == 'SYNTH': continue
if tokens[-1].lower() in ['future', 'futures', 'swap', 'swp']:
symbol = tokens[0]
if tokens[1] == 'Mini-sized': tokens[1] = 'Mini'
key = (symbol, string.join(tokens[1:], ' ').upper().strip())
RES[key] = []
if symbol == 'ED':
count = 24
else: count = 3
continue
if key != None:
x = dict(zip('EXP OPEN HI LO LAST SETT CHNG'.split(), tokens[:7]))
try: x['EXP'] = month_code(x['EXP'][:3]) + x['EXP'][-1]
except:
key = None
continue
RES[key].append(x)
count -= 1
if count == 0: key = None
continue
return RES
def DisplaySettlements(grp):
result = Settlements(grp)
keys = result.keys()
keys.sort()
output = []
for (symbol, description) in keys:
if len(result[(symbol, description)]) == 0: continue
if description == 'MINNEAPOLIS HARD RED SPRING WHEAT FUTURES':
output.append('MN SPRING WHEAT FUTURES')
elif description == 'MINNEAPOLIS SOFT RED WINTER WHEAT INDEX FUTURES':
output.append('MN WINTER WHEAT IDX FUTURES')
elif description == 'KANSAS CITY WHEAT FUTURES':
output.append('KC WHEAT FUTURES')
else:
d = remove_word(description, 'CME', '')
output.append(d)
for d in result[(symbol, description)]:
line = string.join([' ', (symbol + d['EXP']).ljust(10),
d['SETT'].ljust(8), d['CHNG'].ljust(8)])
output.append(line)
return output
if __name__ == '__main__':
op = DisplaySettlements('ags')
print string.join(op, '\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment