1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import json
import datetime
import requests
import common
import sys
import os
import pytz
ticker = "GE"
monthcodes = ["F", "G", "H", "J", "K", "M", "N", "Q", "U", "V", "X", "Z"]
def nextIMMDates(startdate, length = 8):
y = startdate.year
r = []
for y in range(startdate.year, startdate.year + 10):
for m in [3, 6, 9, 12]:
wednesdays = [datetime.date(y, m, i) for i in range(15, 22) \
if datetime.date(y, m, i).weekday()==0]
immdate = wednesdays[0]
if immdate > startdate:
r.append(immdate)
if len(r) == 8:
return r
if len(sys.argv)>1:
workdate = datetime.datetime.strptime(sys.argv[1], "%Y-%m-%d").date()
else:
workdate = datetime.date.today()
# get next 8 futures maturities
contracts = ["GE" + monthcodes[d.month-1] + str(d.year)[-1:] for d in nextIMMDates(workdate)]
#putting a historic date doesn't work
if sys.version_info >= (3,3):
ts = int(workdate*1000)
else:
ts = datetime.datetime.strftime(workdate, '%s') + '000'
uri = "http://www.cmegroup.com/CmeWS/md/MDServer/V1/Venue/G/Exchange/XCME/FOI/FUT/Product/GE?currentTime={0}&contractCDs={1}".format(ts, ",".join(contracts))
r = requests.get(uri)
quotes = json.loads(r.text)
central = pytz.timezone('US/Central')
with open(os.path.join(common.root, "data", "Yield Curves",
"futures-{0}.csv".format(workdate)), "w") as fh:
for q in quotes[u'marketDataInfoAsStringList'][u'message']:
q['tradeDate'] = q['tradeDate'].strip(' ').strip()
tradedate = datetime.datetime.strptime(q['tradeDate'],
"%I:%M:%S %p CT<br/>%m/%d/%Y")
tradedate = central.localize(tradedate)
fh.write("{0},{1},{2}\n".format(q['ticker'], q['tradePrice'], tradedate))
|