def get_message_handler(self, message: BaseMessage): if message.get_type() is None: self.logger.warn("Can't handle message: can't find message type") return None elif message.get_type() in self.handlers: return self.handlers[message.get_type()] else: self.logger.warn("Can't handle message: handler {0} not registered".format(message.get_type())) return None
def __init__(self, username, password, heartbeat=3, session=None): BaseMessage.__init__(self, [ (Field.EncryptMethod, 0), (Field.HeartBtInt, heartbeat), (Field.ResetSeqNum, 'Y'), (Field.Username, username), (Field.Password, password) ], session) self.msg_type = Message.Types.Logon
def __init__(self, order_id, symbol, side, size, session=None): BaseMessage.__init__(self, [ (Field.ClOrdID, order_id), (Field.Symbol, symbol), (Field.Side, side), (Field.TransactTime, get_time()), (Field.OrderQty, size), (Field.OrdType, 1), (Field.TimeInForce, 3), ], session) self.msg_type = Message.Types.NewOrder
def __init__(self, request_id, symbol, unsubscribe=False, refresh=False, session=None): BaseMessage.__init__(self, [ (Field.MDReqID, request_id), (Field.SubscriptionRequestType, 2 if unsubscribe else 1), (Field.MarketDepth, 0 if refresh else 1), (Field.MDUpdateType, 1), (Field.NoRelatedSym, 1), (Field.Symbol, symbol), (Field.NoMDEntryTypes, 2), (Field.MDEntryType, 0), (Field.MDEntryType, 1), ], session) self.msg_type = Message.Types.MarketDataRequest
def market_data_snapshot_handler(self, message: BaseMessage): prices = message.get_group(Field.Groups.MDEntry_Snapshot) if len(prices) < 2 or Field.MDEntryPx not in prices[0] or Field.MDEntryPx not in prices[1]: self.logger.warn("No ask or bid in price update.") return ask_idx = 1 if prices[0][Field.MDEntryType] == '0' else 0 bid_idx = (ask_idx + 1) % 2 spread = calculate_spread( prices[bid_idx][Field.MDEntryPx], prices[ask_idx][Field.MDEntryPx], self.session.symbol_table[int(message.get_field(Field.Symbol))]['pip_position'] ) name = self.session.symbol_table[int(message.get_field(Field.Symbol))]['name'] self.logger.info("Symbol: {0: <7}\tBID: {1: <10}\tASK: {2: <10}\tSPREAD: {3}\t\tBID_VOL: {4}\tASK_VOL: {5}".format( name, prices[bid_idx][Field.MDEntryPx], prices[ask_idx][Field.MDEntryPx], spread, int(self.session.symbol_table[int(message.get_field(Field.Symbol))]['bid_volume'] / 1000000), int(self.session.symbol_table[int(message.get_field(Field.Symbol))]['ask_volume'] / 1000000), ))
def market_data_refresh_handler(self, message: BaseMessage): results = message.get_group(Field.Groups.MDEntry_Refresh) actions = {'0': 'New', '2': 'Delete'} types = {'0': 'BID', '1': 'ASK'} message = "Price Update:" for r in results: if actions[r[Field.MDUpdateAction]] == 'New': if Field.MDEntryPx in r: name = self.session.symbol_table[int(r[Field.Symbol])]['name'] message += "\n\t\t\tSymbol: {0: <7}, Type: {1}, ID: {2}, Price: {3: <10}, Size: {4}, Action: {5}".format( name, types[r[Field.MDEntryType]], r[Field.MDEntryID], r[Field.MDEntryPx], r[Field.MDEntrySize], actions[r[Field.MDUpdateAction]] ) else: message += "\n\t\t\tSymbol: {0: <7}, ID: {1}, Action: {2}".format( 'none', r[Field.MDEntryID], actions[r[Field.MDUpdateAction]] ) self.logger.info(message)
def __init__(self, text, session=None): BaseMessage.__init__(self, [(Field.TestReqID, text)], session) self.msg_type = Message.Types.Heartbeat
def __init__(self, session=None): BaseMessage.__init__(self, [], session) self.msg_type = Message.Types.Heartbeat
def logout_handler(self, message: BaseMessage): self.logger.critical('Logout reason: {0}'.format(message.get_field(Field.Text))) self.close()
def reject_handler(self, message: BaseMessage): self.logger.warn("MESSAGE REJECTED: {0}".format(message.get_field(Field.Text)))
def test_request_handled(self, message: BaseMessage): self.send(TestResponseMessage(message.get_field(Field.TestReqID), self.session))
def heartbeat_handler(self, message: BaseMessage): self.logger.debug( "Received heartbeat, sending it back. Server time: {0}.".format(message.get_field(Field.SendingTime)) ) self.send(HeartbeatMessage(self.session))
def logon_handler(self, message: BaseMessage): self.authorized = True self.logger.info("Logged in at {0} as {1}".format(message.get_field(Field.SendingTime), self.session.sender_id))