def nextValidId(self, orderId: int): print('API初始化完成!') print('nextValidId:', orderId) zs = make_contract(symbol='ZS', conID=341629352, secType='FUT', exchange='ECBOT') le = make_contract(symbol='LE', conID=373227052, secType='FUT', exchange='GLOBEX') now = datetime.now().strftime("%Y%m%d %H:%M:%S") self.reqHistoricalData(0, zs, now, '2 Y', '1 hour', 'BID_ASK', 0, 1, False, [])
def make_future_spread_combo(symbol: str, buysideID: int, sellsideID: int, exchange: str, currency='USD'): comcon = make_contract(symbol, secType='BAG', exchange=exchange, currency=currency) legbuy = ComboLeg() legbuy.conId = buysideID legbuy.ratio = 1 legbuy.action = 'BUY' legbuy.exchange = exchange legsell = ComboLeg() legsell.conId = sellsideID legsell.ratio = 1 legsell.action = 'SELL' legsell.exchange = exchange comcon.comboLegs = [] comcon.comboLegs.append(legbuy) comcon.comboLegs.append(legsell) return comcon
def ReqContractsDetail(self, symbols: list): reqn = 0 for si in symbols: self.reqConsdict[reqn] = si reqCon = make_contract(symbol=si) print('请求合约详情:', si) self.reqContractDetails(reqn, reqCon) reqn += 1 self.scan()
def reqContract_STK_batch(self): UDfiles = os.listdir(UDpath) reqCSBbuff = [] for si in self.dfsymbols: if si + '-Details.json' not in UDfiles: con = make_contract(symbol=si, secType='STK') reqCSBbuff.append((self.reqID, deepcopy(con))) self.contractDetails_STK_batch_buff[self.reqID] = si self.reqID += 1 self.req0 = True for ri in reqCSBbuff: self.reqContractDetails(ri[0], ri[1]) self.control_reqtime_gap()
def reqOptionContractDetails_bulk(self, params: dict): for sym, para in params.items(): self.optreqidinfo[sym] = {} optdelfile = ospath + os.sep + sym + '-specs.json' if os.path.exists(optdelfile): self.superoptdel[sym] = Json_to_Dict(optdelfile) else: self.superoptdel[sym] = {} self.opthasdel[sym] = [] self.opthaonotdel[sym] = [] self.reciveoptdelstatus[sym] = False for parai in para: exs = parai[0] strs = parai[1] if len(exs) != 0 and len(strs) != 0: for ei in exs: for si in strs: self.reqID += 1 con_C = make_contract(symbol=sym, secType='OPT', right='C', lastTradeDateOrContractMonth=ei, strike=si) self.optsupercon.append((self.reqID, deepcopy(con_C))) self.optreqidinfo[sym][self.reqID] = [sym, ei, si, 'C'] self.reqID += 1 con_P = make_contract(symbol=sym, secType='OPT', right='P', lastTradeDateOrContractMonth=ei, strike=si) self.optsupercon.append((self.reqID, deepcopy(con_P))) self.optreqidinfo[sym][self.reqID] = [sym, ei, si, 'P'] self.req8 = True for coi in self.optsupercon: self.reqContractDetails(coi[0], coi[1]) print(coi[0], '批量请求期权Contract Details 数据:', coi[1].__str__()) self.control_reqtime_gap_2() self.optsupercon = []
def reqOPT_Params_Details_batch_2(self): optdelbuff = [] for sym, param in self.OPT_Params.items(): self.OPT_Detail_reqIDs[sym] = {} self.OPT_Detail_recived_IDs[sym] = [] self.OPT_Detail_error_IDs[sym] = [] self.OPT_Detail_Done[sym] = False osfile = OSpath + os.sep + sym + '-specs.json' existed_OPT_details_brif = [] if os.path.exists(osfile): self.existed_OPT_details[sym] = Json_to_Dict(osfile) for month, rig in self.existed_OPT_details[sym].items(): for rigi, st in rig.items(): for sti in st.keys(): existed_OPT_details_brif.append( (sym, month, rigi, sti)) else: self.existed_OPT_details[sym] = {} stricklimit = 0.15 idx = self.dfsymbols.index(sym) last = self.dfPrice[idx] pdiff = 100 - last if pdiff > 0: stricklimit = 0.15 + (pdiff / 80) * ( self.pick_OPT_strickes_paramters['maxstricklimit'] - 0.15) exs = param['expirations'] strs = param['strikes'] today = date.today() for ei in exs: exdate = IBdate_to_Date(ei) if (exdate - today ).days <= self.pick_OPT_strickes_paramters['lessthanday']: for strsi in strs: if last * (1 - stricklimit) <= strsi <= last * ( 1 + stricklimit): brif_C = (sym, ei, 'C', strsi) if brif_C not in existed_OPT_details_brif: con_C = make_contract( symbol=sym, secType='OPT', right='C', lastTradeDateOrContractMonth=ei, strike=strsi) optdelbuff.append( (self.reqID, deepcopy(con_C))) self.OPT_Detail_reqIDs[sym][ self.reqID] = brif_C self.reqID += 1 brif_P = (sym, ei, 'P', strsi) if brif_P not in existed_OPT_details_brif: con_P = make_contract( symbol=sym, secType='OPT', right='P', lastTradeDateOrContractMonth=ei, strike=strsi) optdelbuff.append( (self.reqID, deepcopy(con_P))) self.OPT_Detail_reqIDs[sym][ self.reqID] = brif_P self.reqID += 1 self.req2 = True # locker = Lock() # locker.acquire() for oi in optdelbuff: self.reqContractDetails(oi[0], oi[1]) self.control_reqtime_gap()
f.write(pdata) f.close() if __name__ == '__main__': specs = [('EUR', 12087792, 'USD'), ('GBP', 12087797, 'USD'), ('USD', 15016059, 'JPY')] # ('CHF', 12087802, 'USD'), # ('AUD', 14433401, 'USD'), # ('NZD', 39453441, 'USD'), # ('USD', 113342317, 'CNH'), # ('USD', 15016062, 'CAD'), # ] conlist = [] # xau = make_contract(symbol='XAUUSD', conID=69067924, currency='USD', secType='CMDTY', exchange='SMART') # conlist.append(xau) # xag = make_contract(symbol='XAGUSD', conID=77124483, currency='USD', secType='CMDTY', exchange='SMART') # conlist.append(xag) for i in specs: con = make_contract(symbol=i[0], conID=i[1], secType='CASH', currency=i[2], exchange='IDEALPRO') conlist.append(copy.deepcopy(con)) app = myClient_get_FX_Tickdata(conlist) app.connect('127.0.0.1', 4003, 1) app.run()
def nextValidId(self, orderId: int): con = make_contract(symbol='AMZN', secType='OPT') self.timrstart = datetime.now() self.reqContractDetails(1, con)
self.plotstart = True else: x = [self.plotnums, self.plotnums + 1] bidy = [self.spread_tab_p[1], self.spread_tab[1]] asky = [self.spread_tab_p[2], self.spread_tab[2]] self.plotnums += 1 plt.plot(x, bidy, color='green') plt.plot(x, asky, color='red') plt.draw() plt.pause(0.001) if __name__ == '__main__': ES202012 = make_contract(symbol='ES', conID=383974339, secType='FUT', exchange='GLOBEX', localSymbol='ESZ0') ES202103 = make_contract(symbol='ES', conID=396336017, secType='FUT', exchange='GLOBEX', localSymbol='ESH1') ES = {'back': ES202103, 'front': ES202012} NQ202012 = make_contract(symbol='NQ', conID=383974419, secType='FUT', exchange='GLOBEX', localSymbol='NQZ0') NQ202103 = make_contract(symbol='NQ', conID=396335999, secType='FUT', exchange='GLOBEX', localSymbol='NQH1') NQ = {'back': NQ202103, 'front': NQ202012} YM202012 = make_contract(symbol='YM', conID=396335960, secType='FUT', exchange='ECBOT', localSymbol='YM DEC 20') YM202103 = make_contract(symbol='YM', conID=412888950, secType='FUT', exchange='ECBOT', localSymbol='YM MAR 21') YM = {'back': YM202103, 'front': YM202012} RTY202012 = make_contract(symbol='RTY', conID=383974422, secType='FUT', exchange='GLOBEX', localSymbol='RTYZ0') RTY202103 = make_contract(symbol='RTY', conID=396336027, secType='FUT', exchange='GLOBEX', localSymbol='RTYH1') RTY = {'back': RTY202103, 'front': RTY202012}
else: x = [self.plotnums, self.plotnums + 1] bidy = [self.combo_tick_pre[1], self.combo_tick[1]] asky = [self.combo_tick_pre[2], self.combo_tick[2]] self.plotnums += 1 plt.plot(x, bidy, color='green') plt.plot(x, asky, color='red') plt.draw() plt.pause(0.001) if __name__ == '__main__': coilk1 = make_contract(symbol='COIL', conID=153452020, secType='FUT', exchange='IPE', localSymbol='COILK1') coilh1 = make_contract(symbol='COIL', conID=153452051, secType='FUT', exchange='IPE', localSymbol='COILH1') ES202012 = make_contract(symbol='ES', conID=383974339, secType='FUT', exchange='GLOBEX', localSymbol='ESZ0') ES202103 = make_contract(symbol='ES', conID=396336017,
if tickType == 88: sdatetime = datetime.fromtimestamp(int(value), estz).strftime("%Y-%m-%d %H:%M:%S") self.id_tick_tab[reqId][0] = sdatetime symbol = self.id_sy_tab[reqId] print('时间:', symbol, sdatetime) # filename = datapath + os.sep + symbol + '-tick.txt' # f = open(filename, 'a+') # pdata = ','.join([str(i) for i in self.id_tick_tab[reqId]]) + '\n' # f.write(pdata) # f.close() if __name__ == '__main__': conlist = [] sb = make_contract(symbol='SB', conID=320220010, secType='FUT', exchange='NYBOT') cc = make_contract(symbol='CC', conID=348296999, secType='FUT', exchange='NYBOT') gc = make_contract(symbol='GC', conID=442474779, secType='FUT', exchange='NYMEX') zs = make_contract(symbol='ZS', conID=341629352, secType='FUT', exchange='ECBOT') le = make_contract(symbol='LE', conID=373227052, secType='FUT', exchange='GLOBEX') vix = make_contract(symbol='VIX', conID=394987017, secType='FUT', exchange='CFE') zg = make_contract(symbol='ZG', conID=217704900, secType='FUT', exchange='NYSELIFFE') coil = make_contract(symbol='COIL', conID=153452027, secType='FUT', exchange='IPE') w = make_contract(symbol='W', conID=374752952, secType='FUT', exchange='ICEEUSOFT') conlist.append(copy.deepcopy(w)) app = myClient_get_FUT_Tickdata(conlist) app.connect('127.0.0.1', 4003, 1) app.run()