Beispiel #1
0
    def encode_message(self, message: FIXMessage) -> bytes:
        """
        :param message: The message to encode.

        :return: The FIX-compliant, raw binary string representation for this message with freshly
        generated header tags.
        """
        try:
            message = message.result()
        except AttributeError:
            # Not an Unfuture - use as-is
            pass
        message.validate()  # Make sure the message is valid before attempting to encode.

        if message.sender_id is None:
            message.sender_id = self.pipeline.apps[ClientSessionApp.name].sender

        if message.target_id is None:
            message.target_id = self.pipeline.apps[ClientSessionApp.name].target

        body = (
            utils.encode(f"{connection.protocol.Tag.MsgType}=")
            + utils.encode(message.type)
            + settings.SOH
            + utils.encode(f"{connection.protocol.Tag.MsgSeqNum}=")
            + utils.encode(message.seq_num)
            + settings.SOH
            + utils.encode(f"{connection.protocol.Tag.SenderCompID}=")
            + utils.encode(message.sender_id)
            + settings.SOH
            + utils.encode(f"{connection.protocol.Tag.SendingTime}=")
            + utils.encode(str(message.SendingTime))
            + settings.SOH
            + utils.encode(f"{connection.protocol.Tag.TargetCompID}=")
            + utils.encode(message.target_id)
            + settings.SOH
        )

        for field in message.fields:
            if field.tag in self.DYNAMIC_TAGS:  # These tags will be generated - ignore.
                continue
            body += bytes(field)

        header = (
            utils.encode(f"{connection.protocol.Tag.BeginString}=")
            + utils.encode(settings.BEGIN_STRING)
            + settings.SOH
            + utils.encode(f"{connection.protocol.Tag.BodyLength}=")
            + utils.encode(len(body))
            + settings.SOH
        )

        trailer = (
            utils.encode(f"{connection.protocol.Tag.CheckSum}=")
            + utils.encode(f"{utils.calculate_checksum(header + body):03}")
            + settings.SOH
        )

        return header + body + trailer
Beispiel #2
0
    async def on_send(self, message: FIXMessage) -> FIXMessage:
        """
        Inject MsgSeqNum for every message to be sent, except duplicates.
        """
        try:
            is_duplicate = bool(message.PossDupFlag)
        except TagNotFound:
            is_duplicate = False

        if not is_duplicate:
            # Set sequence number
            self.send_seq_num += 1
            message.seq_num = self.send_seq_num

        message.SendingTime = datetime.utcnow().strftime(settings.DATETIME_FORMAT)[:-3]

        return await super().on_send(message)
Beispiel #3
0
    async def on_send(self, message: FIXMessage) -> FIXMessage:
        message.TestReqID = f"{message.TestReqID} s3"

        return await super().on_send(message)
Beispiel #4
0
    async def on_receive(self, message: FIXMessage) -> FIXMessage:
        message.TestReqID = f"{message.TestReqID} r3"

        return await super().on_receive(message)