def test_encode_no_8(self): msg = FixMessage() msg.append_pair(35, "D") try: buf = msg.encode() except ValueError: pass
def test_time_float(self): msg = FixMessage() t = 1484581872.933458 msg.append_time(52, t) self.assertEqual("2017-01-16-15:51:12.933", msg.get(52)) return
def test_encode_no_35(self): msg = FixMessage() msg.append_pair(8, "FIX.4.2") try: buf = msg.encode() except ValueError: pass
def test_tzto_parts_15_51_12_270(self): """Test TZTimeOnly with hour, minute and second components, partial hour offset.""" msg = FixMessage() msg.append_tz_time_only_parts(1, 15, 51, 12, offset=-270) self.assertEqual(fix_str("15:51:12-04:30"), msg.get(1)) return
def parse_order_book(message: simplefix.FixMessage) -> dict: order_count = int(message.get(268)) buy_orders = [] sell_orders = [] orders = {} for i in range(1, order_count): side = 'SELL' if message.get( simplefix.TAG_SYMBOL).decode('utf-8') == '1' else 'BUY' order = { 'time': int(time.time()), # No timestamp available 'side': side, 'contract_symbol': message.get(simplefix.TAG_SYMBOL).decode('utf-8'), 'qty': message.get(271).decode('utf-8'), 'price': message.get(270).decode('utf-8') } if side == 'SELL': sell_orders.append(order) else: buy_orders.append(order) buy_orders.sort(key=lambda o: float(o['price'])) sell_orders.sort(key=lambda o: float(o['price'])) orders['buy'] = buy_orders orders['sell'] = sell_orders return orders
def test_utcto_bad_precision(self): """Test UTCTimeOnly bad time precision values""" msg = FixMessage() t = 1484581872.933458 with self.assertRaises(ValueError): msg.append_utc_time_only(273, t, 9) return
def test_tzts_seconds_only(self): """Test formatting of TZTimestamp values with seconds only""" msg = FixMessage() t = 1484581872.933458 msg.append_tz_timestamp(1253, t, 0) test = time.localtime(t) s = "%04u%02u%02u-%02u:%02u:%02u" % \ (test.tm_year, test.tm_mon, test.tm_mday, test.tm_hour, test.tm_min, test.tm_sec) offset = int( (datetime.datetime.fromtimestamp(t) - datetime.datetime.utcfromtimestamp(t)).total_seconds() / 60) if offset == 0: s += "Z" else: offset_hours = abs(offset) / 60 offset_mins = abs(offset) % 60 s += "%c%02u" % ("+" if offset > 0 else "-", offset_hours) if offset_mins > 0: s += ":%02u" % offset_mins self.assertEqual(fix_str(s), msg.get(1253)) return
def test_time_bad_precision(self): msg = FixMessage() t = 1484581872.933458 with self.assertRaises(ValueError): msg.append_time(52, t, 9) return
def test_empty_message(self): try: msg = FixMessage() buf = msg.encode() except Exception as e: self.assertEqual(ValueError, type(e)) return
def test_append_tzts_datetime(self): msg = FixMessage() t = 1484581872.933458 local = datetime.datetime.fromtimestamp(t) msg.append_tz_timestamp(1132, local) test = time.localtime(t) s = "%04u%02u%02u-%02u:%02u:%02u.%03u" % \ (test.tm_year, test.tm_mon, test.tm_mday, test.tm_hour, test.tm_min, test.tm_sec, int((t - int(t)) * 1000)) offset = int((datetime.datetime.fromtimestamp(t) - datetime.datetime.utcfromtimestamp(t)).total_seconds() / 60) if offset == 0: s += "Z" else: offset_hours = abs(offset) / 60 offset_mins = abs(offset) % 60 s += "%c%02u" % ("+" if offset > 0 else "-", offset_hours) if offset_mins > 0: s += ":%02u" % offset_mins self.assertEqual(fix_str(s), msg.get(1132)) return
def test_utcto_seconds_only(self): """Test UTCTimeOnly formatting of seconds only""" msg = FixMessage() t = 1484581872.933458 msg.append_utc_time_only(273, t, 0) self.assertEqual(fix_str("15:51:12"), msg.get(273)) return
def _handle_application_message(self, message: simplefix.FixMessage): """ If application message is an order processing message, add it to the order_book keyed by the messages client_order_id. Otherwise, add marketdata messages to application_messages queue. """ assert isinstance(message, simplefix.FixMessage) # handle ORDER MASS STATUS REQUEST messages if message.get(b'584') != None: self.application_messages.put(message) return order_processing_types = [simplefix.MSGTYPE_EXECUTION_REPORT, simplefix.MSGTYPE_ORDER_CANCEL_REJECT] # handle order processing messages if message.get(simplefix.TAG_MSGTYPE) in order_processing_types: # ensure the correct client id is used, depending on order execution type use_origclordid_types = [simplefix.EXECTYPE_CANCELED, simplefix.EXECTYPE_REPLACE, simplefix.EXECTYPE_PENDING_CANCEL, simplefix.EXECTYPE_PENDING_REPLACE] if message.get(simplefix.TAG_EXECTYPE) in use_origclordid_types: client_order_id = f"{message.get(simplefix.TAG_ORIGCLORDID).decode('utf-8')}" else: client_order_id = f"{message.get(simplefix.TAG_CLORDID).decode('utf-8')}" if client_order_id not in self.order_book: self.order_book[client_order_id] = queue.Queue() self.order_book[client_order_id].put(message) # keep marketdata messages in application queue else: self.application_messages.put(message)
def test_utcto_float(self): """Test UTCTimeOnly with floating point value""" msg = FixMessage() t = 1484581872.933458 msg.append_utc_time_only(273, t) self.assertEqual(fix_str("15:51:12.933"), msg.get(273)) return
def test_tzto_parts_15_51_240(self): """Test TZTimeOnly with hour and minute components, full hour offset""" msg = FixMessage() msg.append_tz_time_only_parts(1, 15, 51, offset=-240) self.assertEqual(fix_str("15:51-04"), msg.get(1)) return
def test_time_microseconds(self): msg = FixMessage() t = 1484581872.933458 msg.append_time(52, t, 6) self.assertEqual("2017-01-16-15:51:12.933458", msg.get(52)) return
def test_tzto_parts_15_51_12_933_458_150(self): """Test TZTimeOnly with h, m, s, ms, and us components, partial hour offset.""" msg = FixMessage() msg.append_tz_time_only_parts(1, 15, 51, 12, 933, 458, offset=150) self.assertEqual(fix_str("15:51:12.933458+02:30"), msg.get(1)) return
def test_utcto_datetime(self): """Test UTCTimeOnly with datetime timestamp values""" msg = FixMessage() t = 1484581872.933458 dt = datetime.datetime.utcfromtimestamp(t) msg.append_utc_time_only(273, dt) self.assertEqual(fix_str("15:51:12.933"), msg.get(273))
def test_utcts_datetime(self): """Test UTCTimestamp with datetime timestamp values""" msg = FixMessage() t = 1484581872.933458 dt = datetime.datetime.utcfromtimestamp(t) msg.append_utc_timestamp(52, dt) self.assertEqual(fix_str("20170116-15:51:12.933"), msg.get(52))
def test_time_datetime(self): """Test use of built-in datetime timestamp values""" msg = FixMessage() t = 1484581872.933458 dt = datetime.datetime.utcfromtimestamp(t) msg.append_time(52, dt) self.assertEqual(fix_str("20170116-15:51:12.933"), msg.get(52))
def test_none_value(self): """Test encoding of None value""" if VERSION == 26: return msg = FixMessage() msg.append_pair(99999, None) self.assertNotIn(b'99999', msg)
def test_utcts_seconds_only(self): """Test UTCTimestamp formatting of seconds only""" msg = FixMessage() t = 1484581872.933458 msg.append_utc_timestamp(52, t, 0) self.assertEqual(fix_str("20170116-15:51:12"), msg.get(52)) return
def test_utcts_float(self): """Test UTCTimestamp with floating point value""" msg = FixMessage() t = 1484581872.933458 msg.append_utc_timestamp(52, t) self.assertEqual(fix_str("20170116-15:51:12.933"), msg.get(52)) return
def test_time_seconds_only(self): """Test formatting of time values with no decimal component""" msg = FixMessage() t = 1484581872.933458 msg.append_time(52, t, 0) self.assertEqual(fix_str("20170116-15:51:12"), msg.get(52)) return
def test_time_microseconds(self): """Test formatting of time values with microseconds""" msg = FixMessage() t = 1484581872.933458 msg.append_time(52, t, 6) self.assertEqual(fix_str("20170116-15:51:12.933458"), msg.get(52)) return
def test_time_float(self): """Test floating point timestamp values""" msg = FixMessage() t = 1484581872.933458 msg.append_time(52, t) self.assertEqual(fix_str("20170116-15:51:12.933"), msg.get(52)) return
def test_empty_message(self): """Test encoding of empty message""" if VERSION == 26: return msg = FixMessage() with self.assertRaises(ValueError): msg.encode()
def test_string_without_equals(self): """Test field set with string not containing equals sign""" if VERSION == 26: return msg = FixMessage() with self.assertRaises(ValueError): msg.append_string("FIX.4.2")
def test_time_datetime(self): msg = FixMessage() t = 1484581872.933458 dt = datetime.datetime.utcfromtimestamp(t) msg.append_time(52, dt) self.assertEqual("2017-01-16-15:51:12.933", msg.get(52)) return
def test_string_with_bad_tag(self): """Test field set with bad tag in tag=value string""" if VERSION == 26: return msg = FixMessage() with self.assertRaises(ValueError): msg.append_string("foo=bar")
def test_contains(self): """Test use of 'in' and 'not in' operators""" msg = FixMessage() msg.append_strings(["8=FIX.4.4", "35=0"]) self.assertIn(8, msg) self.assertIn(35, msg) self.assertNotIn(9, msg) self.assertNotIn(10, msg) return