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
51
52
53
54
55
56
57
58
59
60
61
62
63
|
import json
import datetime
import requests
from common import root
import sys
import os
import pytz
from quantlib.time.date import Date
from quantlib.time import imm
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")
else:
workdate = datetime.datetime.today()
# get next 8 futures maturities
def QLnextIMMCodes(startdate, length = 8):
code = imm.next_code(Date.from_datetime(startdate))
contracts = ["GE" + code]
for _ in range(length - 1):
code = imm.next_code(code)
contracts.append("GE" + code)
return contracts
contracts = ["GE" + monthcodes[d.month-1] + str(d.year)[-1:] for d in nextIMMDates(workdate.date())]
#putting a historic date doesn't work
if sys.version_info >= (3,3):
ts = int(datetime.datetime.timestamp(workdate) * 1000)
else:
ts = datetime.datetime.strftime(workdate, '%s') + '000'
base_uri = "http://www.cmegroup.com/CmeWS/md/MDServer/V1/Venue/G/Exchange/XCME/FOI/FUT/Product/GE"
payload = {'currentTime': ts,
'contractCDs': ",".join(contracts)}
r = requests.get(base_uri, params=payload)
quotes = json.loads(r.text)
central = pytz.timezone('US/Central')
with open(os.path.join(root, "data", "Yield Curves",
"futures-{0}.csv".format(workdate.date())), "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))
|