def __init__(self): try: log.info("TickStream service started") json_path = str(tsobj.basepath) + os.path.join( tsobj.get_value("common", "masters"), tsobj.get_value("common", "stream_list")) log.info("Loading scripts details from json " + str(tsobj.get_value("common", "stream_list"))) company_dicts = None if json_path: with open(json_path, 'r') as f: company_dicts = json.load(f) log.info("List of company loaded \n" + str(company_dicts)) strmobj = streamer() log.info("Creating IB symbol mapping dictionary") for index, script in enumerate(company_dicts): tsobj.ib_subcribed_scripts.append({ "reqid": index + 1, "ibsymbol": strmobj.get_ib_symbol_from_map(script["Symbol"]) }) log.info("NSE and IB Symbol Mapping Done !! \n" + str(tsobj.ib_subcribed_scripts)) if tsobj.ib_subcribed_scripts is not None: log.info("Tick Streamer starting streaming..") strmobj.start_streaming() except Exception as ex: log.error(ex)
def stop_streaming(self): try: for script in tsobj.ib_subcribed_scripts: self.ibobj.unsuscribe_contract(script, self.ibobj) except Exception as ex: log.error(ex) log.error(traceback.format_exc())
def start_run(self, ibcon): try: log.info("IB Service started running") ibcon.run() except Exception as ex: log.error(ex) log.error(traceback.format_exc())
def __init__(self): try: IBWrapper.__init__(self) IBClient.__init__(self, wrapper=self) self._my_contract_details = {} except Exception as ex: log.error(ex) log.error(traceback.format_exc())
def subscribe_contract(self, symboldict, ibcon): try: self.contract = self.get_stk_contract( str(symboldict.get("ibsymbol"))) ibcon.reqMktData(int(symboldict.get("reqid")), self.contract, "", False, False, []) except Exception as ex: log.error(ex) log.error(traceback.format_exc())
def get_ib_symbol_from_map(self, nse_symbol): try: map_json = json.loads(open(str(SYM_MAP_PATH)).read()) for sym in map_json: if sym['NSE_Symbol'] == nse_symbol: return sym['IB_Symbol'] return nse_symbol except Exception as ex: log.error("No mapping value found for NSE Symbol:%s" % nse_symbol) log.error(traceback.format_exc())
def start_streaming(self): try: self.ibobj.connect_ib(self.ibobj) for contract in tsobj.ib_subcribed_scripts: self.ibobj.subscribe_contract(contract, self.ibobj) log.info("Sucessfully Subscribed " + str(contract)) self.ibobj.start_run(self.ibobj) except Exception as ex: log.error(ex) log.error(traceback.format_exc())
def connect_ib(ib_connection): try: if not ib_connection.isConnected(): ib_connection.connect(TWS_IP, TMS_PORT, clientId=IB_CLIENT_ID) log.info("serverVersion:%s connectionTime:%s" % (ib_connection.serverVersion(), ib_connection.twsConnectionTime())) except Exception as ex: log.error(ex) log.error(traceback.format_exc())
def get_stk_contract(self, symbol): try: contract = Contract() contract.symbol = symbol contract.secType = "STK" contract.currency = "INR" contract.exchange = "NSE" return contract except Exception as ex: log.error(ex) log.error(traceback.format_exc())
def get_hrhd_data(self, ibcon): try: # self.back_test_symbol = self.back_test_symbols[self.exe_iterator] self.contract = self.get_stk_contract(self.back_test_symbol) self.cdate = self.hdate_prev ibcon.reqHistoricalTicks(1, self.contract, str(self.hdate_prev) + " 09:15:00", "", 1000, "TRADES", 1, True, []) ibcon.run() except: log.error(traceback.format_exc())
def tickPrice(self, reqId: TickerId, tickType: TickType, price: float, attrib: TickAttrib): try: super().tickPrice(reqId, tickType, price, attrib) print("TickPrice - TickerId:", reqId, "tickType:", tickType, "Price:", price, "localtime", str(self.get_time_stamp())) data = { "reqid": reqId, "price": price, "timestamp": str(self.get_time_stamp()) } producer.send(str(reqId), value=data) except Exception as ex: log.error(ex) log.error(traceback.format_exc())
def tickPrice(self, reqId: TickerId, tickType: TickType, price: float, attrib: TickAttrib): try: super().tickPrice(reqId, tickType, price, attrib) #print("TickPrice - TickerId:", reqId, "tickType:", tickType, # "Price:", price, "localtime", str(self.get_time_stamp())) data = { "TickerId": reqId, "TopicId": str(tsobj.ib_subcribed_scripts[int(reqId) - 1]['ibsymbol']), "Price": price, "Timestamp": str(self.get_time_stamp()) } producer.send(str(tsobj.ib_subcribed_scripts[int(reqId) - 1]['ibsymbol']), value=data) except Exception as ex: log.error(ex) log.error(traceback.format_exc())
def unsuscribe_contract(self, symboldict, ibcon): try: ibcon.cancelMktData(int(symboldict.get("reqid"))) except Exception as ex: log.error(ex) log.error(traceback.format_exc())