def get_cme_margins(http_link): # alternative way to get bulk download: 'http://www.cmegroup.com/CmeWS/mvc/Margins/OUTRIGHT.csv' x = return_web_tables(http_link)[0] x = [i for i in x if i != 'About This Report'] nx = [i for i, j in enumerate(x) if j == 'Maintenance'][0] x = x[int(nx*2+2)] x = float(x.replace(',', '').split(' ')[0]) return x
def get_ib_fut_info(entity_id, exchanges=None, mult=None, incr=None): # entity_id = 'a19207303' # VIX # entity_id = 'a19206937' # CORN # exchanges = ['CBOT', 'ECBOT'] if exchanges is None: http_link = 'https://misc.interactivebrokers.com/cstools/contract_info/v3.9/index.php?' \ 'action=Futures%20Search&entityId=' + entity_id + '&lang=en&wlId=GEN&showEntities=Y' else: http_link = 'https://misc.interactivebrokers.com/cstools/contract_info/v3.9/index.php?' \ 'action=Futures%20Search&entityId=' + entity_id + '&exchanges=' + \ ''.join([i+',' for i in exchanges]) + '&lang=en&wlId=GEN&showEntities=Y' page = requests.get(http_link) page_body = html.fromstring(page.content) page_links = page_body.xpath('//a/@href') page_links = [i for i in page_links if 'showDetails' in i] page_links = [i.replace('javascript:showDetails(', '') for i in page_links] page_links = [i.replace(')', '') for i in page_links] page_links = page_links[0] page_links = page_links[1:len(page_links)-1] http_link = 'https://misc.interactivebrokers.com/cstools/contract_info/v3.9/index.php?' \ 'action=Conid%20Info&wlId=GEN&conid=' + page_links + '&lang=en' page = return_web_tables(http_link) page = [i for i in page if len(i)> 0] page = [i for i in page if i[0] != 'Underlying Information'] page = [i for i in page if i[0] != 'Contract Information'] page = [i for i in page if i[0] != 'Contract Identifiers'] # get the multiplier if mult is not None: mult = [i for i in page if i[0] == 'Futures Features'][0] mult = float(mult[[i for i, j in enumerate(mult) if j == 'Multiplier'][0]+1]) # get the margin requirements page = [i for i in page if i[0] != 'Futures Features'] marg = [i for i in page if i[0] == 'Margin Requirements'][0] marg = [i.replace(',', '') for i in marg] marg = marg[2::2] marg = [(None if i == 'Default' else int(i)) for i in marg] page = [i for i in page if i[0] != 'Margin Requirements'] if incr is not None: incr = [i for i in page if i[0] == 'Range'] incr = [i[2:] for i in incr] incr = [i for i in incr if 'price' in i[0]] incr = incr[0][1] incr = float(incr) if (mult is None) and (incr is None): return marg elif (mult is not None) and (incr is None): return marg, mult elif (mult is None) and (incr is not None): return marg, incr else: return marg, mult, incr
def get_cde_expiry_dates(): x = return_web_tables('http://www.m-x.ca/nego_ca_en.php')[0] x = x[5:] # removing the headers # remove subheaders and keep only futures xm_ = {'January': '01', 'February': '02', 'March': '03', 'April': '04', 'May': '05', 'June': '06', 'July': '07', 'August': '08', 'September': '09', 'October': '10', 'November': '11', 'December': '12'} loc = [] for i in xm_.keys(): loc += [ii for ii, j in enumerate(x) if j.find(i) != -1] loc = sorted(list(set(loc))) loc_ = [j for j in loc if ',' not in x[j]] loc__ = [i for i, j in enumerate(x) if j == 'Equity options'] ndt = len(loc_) x_ = [] for i in range(0, ndt): x_ += x[loc_[i]+1:loc__[i]] # make it into a table loc = [] for i in xm_.keys(): loc += [ii for ii, j in enumerate(x_) if j.find(i) != -1] loc = sorted(list(set(loc))) loc = [i for i, j in enumerate(x_) if i not in loc] ndt = len(loc) loc_ = loc[1:] + [len(x_)] xd_ = [] for i in range(0, ndt): xd_.append(x_[loc[i]:loc_[i]]) ndt = len(xd_) # remove the expiration date, and keep the last trading day xpc = [] xltdt = [] xfndt = [] for i in range(0, ndt): x_ = xd_[i] xpc.append(x_[0]) xltdt.append(x_[2]) if len(x_) == 3: xfndt.append(x_[2]) else: xfndt.append(x_[3]) # converts dates into useable formats xltdt_ = [i.split(', ') for i in xltdt] xltdt_ = [i[0].split(' ')+[i[1]] for i in xltdt_] xltdt_ = [[xm_[i[0]]]+i[1:3] for i in xltdt_] xltdt_ = [i[2]+i[0]+i[1] for i in xltdt_] xfndt_ = [i.split(', ') for i in xfndt] xfndt_ = [i[0].split(' ')+[i[1]] for i in xfndt_] xfndt_ = [[xm_[i[0]]]+i[1:3] for i in xfndt_] xfndt_ = [i[2]+i[0]+i[1] for i in xfndt_] return xpc, xltdt_, xfndt_
def get_saxo_expiry_dates(): # saxo bank margin url # 'http://www.saxobank.com/Documents/futures-documents/Initial-and-Maintenance-Margin.html' http_link = 'http://www.saxobank.com/prices/futures/futures-contracts?' + \ 'int_cmpid=gl:prices:cfds:trading-conditions_futures%20contracts_1' page = return_web_tables(http_link) page_ = [i for i in page if i[0].lower() == 'tradable contracts'] page_ = [i[3:] for i in page_] stics, sfndt, sltdt = [], [], [] for ii, i in enumerate(page_): icode = i[0::3] ifndt = i[1::3] iltdt = i[2::3] if (len(icode) == len(ifndt)) and (len(icode) == len(iltdt)): stics += icode sfndt += ifndt sltdt += iltdt else: raise ValueError('Can not parse website at sym %s' % icode[0]) # fix last traded dates xm_ = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'} for i, j in enumerate(sltdt): jj = j.split('-') jj[1] = xm_[jj[1]] jj = jj[2]+jj[1]+jj[0] sltdt[i] = jj if sfndt[i] != 'NA': jj = sfndt[i].split('-') jj[1] = xm_[jj[1]] jj = jj[2]+jj[1]+jj[0] sfndt[i] = jj else: sfndt[i] = sltdt[i] x = cruf.DataFrame({'tick': stics, 'dtls': sltdt, 'dtfn': sfndt}) x = x[['tick', 'dtls', 'dtfn']] x['cont'] = [i[-2:] for i in x['tick'].values] xm_ = {'F': '01', 'G': '02', 'H': '03', 'J': '04', 'K': '05', 'M': '06', 'N': '07', 'Q': '08', 'U': '09', 'V': '10', 'X': '11', 'Z': '12'} xy = [i[1] for i in x['cont'].values] xy = [(str(2010+int(i)) if int(i) >= 5 else str(2020+int(i))) for i in xy] x['cont'] = [xy[i]+xm_[j[0]] for i, j in enumerate(x['cont'].values)] x['tick'] = [i[0:-2] for i in x['tick'].values] x = x[['tick', 'cont', 'dtls', 'dtfn']] cr_info.store('Saxo_Expiries', x) return None
def get_cme_expiry_dates(http_link): x = return_web_tables(http_link)[0] x = [i for i in x if i != 'About This Report'] nr = int(len(x)/8) xpc = x[0::8][1:] # product codes xltdt = x[2::8][1:] # last date xfndt = x[6::8][1:] # first notice date xfndt = [i[0:11] for i in xfndt] xltdt = [i[11:] for i in xltdt] xm_ = {'Jan': '01', 'Feb': '02', 'Mar': '03', 'Apr': '04', 'May': '05', 'Jun': '06', 'Jul': '07', 'Aug': '08', 'Sep': '09', 'Oct': '10', 'Nov': '11', 'Dec': '12'} xpc = [i.replace(i[0:3], xm_[i[0:3]]) for i in xpc] xltdt = [i.replace(i[3:6], xm_[i[3:6]]) for i in xltdt] xpc = [''.join(list(reversed(i.split(' ')))) for i in xpc] xltdt = [''.join(list(reversed(i.split(' ')))) for i in xltdt] if list(set(xfndt))[0] != '--': xfndt = [i.replace(i[3:6], xm_[i[3:6]]) for i in xfndt] xfndt = [''.join(list(reversed(i.split(' ')))) for i in xfndt] else: xfndt = None return xpc, xltdt, xfndt
def get_cfe_expiry_dates(tics='VIX'): x = return_web_tables('http://www.cboe.com/delayedquote/ssfquote.aspx') xn = len(x) for i in range(0, xn): # i = 0 xi = x[i] xi = xi[9:] xi = [j for j in xi if '/' in j] xil = round(len(xi)/2) xid = {} for j in range(0, xil): xid[xi[2*j]] = xi[2*j+1] x[i] = xid # retain only 'VIX' # tics = 'VIX' if isinstance(tics, str): tics = [tics] xit = [] for i in tics: # i = tics[0] jidx = None for j in range(0, xn): # j = 2 xint = list(set([k.split('/')[0] for k in list(x[j].keys())])) jchk = [kk for kk in xint if kk == i] if len(jchk) > 0: jidx = j break if jidx is not None: # reduce it further xlt_ = x[jidx] xlt__ = {} for n, v in xlt_.items(): if n.split('/')[0] == i: xlt__[n] = int(v[6:]+v[0:2]+v[3:5]) # xit.append(x[jidx]) xit.append(xlt__) return xit