aboutsummaryrefslogtreecommitdiffstats
path: root/python/futures.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/futures.py')
-rw-r--r--python/futures.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/python/futures.py b/python/futures.py
new file mode 100644
index 00000000..d0968d15
--- /dev/null
+++ b/python/futures.py
@@ -0,0 +1,39 @@
+import json
+import datetime
+import requests
+import sys
+
+ticker = "GE"
+monthcodes = ["F", "G", "H", "J", "K", "M", "N", "Q", "U", "V", "X", "Z"]
+d = datetime.date.today().day
+m = datetime.date.today().month
+y = datetime.date.today().year
+
+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()==2]
+ immdate = wednesdays[0]
+ if immdate > startdate:
+ r.append(immdate)
+ if len(r) == 8:
+ return r
+
+
+# get next 8 futures maturities
+contracts = ["GE" + monthcodes[d.month-1] + str(d.year)[-1:] for d in nextIMMDates(datetime.date.today())]
+if sys.version_info >= (3,3):
+ ts = int(datetime.date.today().timestamp()*1000)
+else:
+ ts = datetime.datetime.strftime(datetime.date.today(), '%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)
+
+with open("futures-{0}.csv".format(datetime.date.today()), "w") as fh:
+ for q in quotes[u'marketDataInfoAsStringList'][u'message']:
+ fh.write("{0}, {1}\n".format(q['ticker'], q['tradePrice']))