Esempio n. 1
0
 async def _send(self, params):
     """helper method to encode a message and write to stream"""
     message = simplefix.FixMessage()
     # standard header
     message.append_pair(simplefix.TAG_BEGINSTRING,
                         f"FIX.{self.fix_version}")
     message.append_pair(simplefix.TAG_SENDER_COMPID, self.sender_comp_id)
     message.append_pair(simplefix.TAG_TARGET_COMPID, self.target_comp_id)
     message.append_pair(simplefix.TAG_MSGSEQNUM, self.next_send_seq_num)
     if not message.get(simplefix.TAG_SENDING_TIME):
         message.append_utc_timestamp(simplefix.TAG_SENDING_TIME)
     # body
     for key, value in params.items():
         if isinstance(value, datetime):
             header = key == simplefix.TAG_SENDING_TIME
             message.append_utc_timestamp(key, value, header=header)
         else:
             message.append_pair(key, value)
     self.next_send_seq_num += 1
     # encode
     buffer = message.encode()
     # send
     print(f"Sending: {buffer}")
     self.writer.write(buffer)
     await self.writer.drain()
Esempio n. 2
0
def main():
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    s.connect(("127.0.0.1", 10102))

    p = simplefix.FixParser()

    # Logon
    m = simplefix.FixMessage()
    m.append_pair(8, "FIX.4.2")
    m.append_pair(35, "A")
    m.append_pair(49, "CLIENT")
    m.append_pair(56, "SERVER")
    m.append_pair(52, datetime.datetime.utcnow().isoformat('-')[:-3])
    m.append_pair(98, 0)  # No encrytion
    m.append_pair(108, 30)

    s.sendall(m.encode())

    while True:
        data = s.recv(8192)
        p.append_buffer(data)
        r = p.get_message()
        if r:
            break

    print m.encode().replace(simplefix.SOH, "|")
Esempio n. 3
0
def test_create_server():
    proxy = fixtool.spawn_agent()
    assert proxy is not None

    s1 = proxy.create_server("s1")
    s1.listen(23456)

    c1 = proxy.create_client("c1")
    c1.connect('localhost', 23456)

    assert s1.pending_accept_count() == 1
    cs1 = s1.accept("cs1")
    assert c1.is_connected()
    assert cs1.is_connected()

    fix_msg = simplefix.FixMessage()
    fix_msg.append_pair(8, "FIX.4.2")
    fix_msg.append_pair(35, 0)
    fix_msg.append_pair(34, 1)
    fix_msg.append_utc_timestamp(52)
    send_buf = fix_msg.encode()

    c1.send(send_buf)
    assert cs1.receive_queue_length() == 1

    m1 = cs1.receive()

    c1.destroy()
    s1.destroy()
    proxy.shutdown()
    return
Esempio n. 4
0
    def baseSend(self, symbol, side, qty, ordType):
        '''
        REQD:    msg / header | 1  | 11  | 55 | 21 (handleInst)? |54 | 38 qty | 40 ordType
        Opt:     204 *? CustOrFirm Done | 100 + 15 *IF using SMART routing 
        Extras:  pkt.append_pair(21,'2')pkt.append_pair(49,'client 1') pkt.append_pair(204,'0')
        #SMART:  pkt.append_pair(100,'SMART')pkt.append_pair(15,'USD')
        '''
        pkt = simplefix.FixMessage()

        pkt.append_pair(8, self.FV)
        pkt.append_pair(1, self.acct_number)
        pkt.append_pair(35, 'D')  #Always
        pkt.append_pair(56, self.TgtID)

        newID = self.getClOrdId()
        #cur_ord = ordId + 1
        pkt.append_pair(11, newID)  #self.getClOrdId)

        pkt.append_pair(55, symbol)
        pkt.append_pair(54, side)
        pkt.append_pair(38, qty)
        pkt.append_pair(40, ordType)

        buffer = pkt.encode()
        return buffer
Esempio n. 5
0
    def test_create_server(self):
        proxy = fixtool.spawn_agent()
        self.assertIsNotNone(proxy)

        s1 = proxy.create_server("s1")
        s1.listen(23456)

        c1 = proxy.create_client("c1")
        c1.connect('localhost', 23456)

        self.assertEqual(1, s1.pending_accept_count())
        cs1 = s1.accept("cs1")
        self.assertTrue(c1.is_connected())
        self.assertTrue(cs1.is_connected())

        fix_msg = simplefix.FixMessage()
        fix_msg.append_pair(8, "FIX.4.2")
        fix_msg.append_pair(35, 0)
        fix_msg.append_pair(34, 1)
        fix_msg.append_utc_timestamp(52)
        send_buf = fix_msg.encode()

        c1.send(send_buf)
        self.assertEqual(1, cs1.receive_queue_length())

        #m1 = cs1.receive()
        #self.assertEqual("hello", m1)

        c1.destroy()
        s1.destroy()
        proxy.shutdown()
        return
Esempio n. 6
0
    def __init__(self, seqObj):  # Initialize the Fix message objects here
        self.seqObj = seqObj
        self.message = simplefix.FixMessage()
        self.resp_mesg = simplefix.FixParser()
        self.seqObj.updateReqSequence(sys.argv[1])
        print(self.seqObj.getReqSequence())

        return
Esempio n. 7
0
 async def _subscribe_security_status(self, exchange, symbol):
     message = simplefix.FixMessage()
     self.request_id = self.request_id + 1
     message.append_pair(simplefix.TAG_MSGTYPE,
                         simplefix.MSGTYPE_SECURITY_STATUS_REQUEST)
     message.append_pair(324,
                         f"REQ:{self.request_id}")  # SECURITY_STATUS_REQ_ID
     message.append_pair(simplefix.TAG_SYMBOL, symbol)  # SYMBOL
     message.append_pair(207, exchange)  # SECURITY_EXCHANGE
     await self.client.send(message)
Esempio n. 8
0
    def leanSS(self, symbol, qty):
        msg = simplefix.FixMessage()

        self.testID = self.testID + 1
        newID = str(self.testID)

        FIX_str = '8=FIX.4.2|9=51|35=D|1=U123456|56=IB|11=' + newID + \
        '|55=' + symbol + '|54=5|38=' + qty + '|40=0|10=1663'
        msg.append_string(FIX_str)
        msg.append_pair(35, '8')
        b = msg.encode()
        return b
Esempio n. 9
0
    def create_message(self, message_type: bytes) -> simplefix.FixMessage:
        """Boilerplates a new message which the caller may populate as desired."""
        assert isinstance(message_type, bytes)
        assert 1 <= len(message_type) <= 2

        m = simplefix.FixMessage()
        m.append_pair(simplefix.TAG_BEGINSTRING, self.fix_version)
        m.append_pair(simplefix.TAG_MSGTYPE, message_type)
        m.append_pair(simplefix.TAG_SENDER_COMPID, self.senderCompId, header=True)
        m.append_pair(simplefix.TAG_TARGET_COMPID, self.targetCompId, header=True)
        m.append_utc_timestamp(simplefix.TAG_SENDING_TIME, header=True)
        return m
Esempio n. 10
0
 async def send_test_request(self, test_req_id):
     """send a logout message"""
     now = datetime.now()
     message = simplefix.FixMessage()
     message.append_pair(simplefix.TAG_MSGTYPE,
                         simplefix.MSGTYPE_TEST_REQUEST)
     message.append_pair(simplefix.TAG_TESTREQID, test_req_id)
     await self.send(message)
     self.next_test_request = now + timedelta(seconds=self.heart_bt_int)
     if self.remote_heartbeat_timeout is not None:
         logging.warning("Missed a heartbeat")
     self.remote_heartbeat_timeout = now + timedelta(seconds=10)
Esempio n. 11
0
 def header(self):  # this is header message
     self.message.append_pair(8, "FIX.4.2")  #update if you want to
     self.message.append_pair(49, "banzai")  #update sender
     self.message.append_pair(56, "fiximulator")  #update target
     self.message.append_pair(34, self.seqObj.getReqSequence())
     # self.message.append_pair(52,"20200611-16:37:33.764") #faking the timestamp,
     self.message.append_utc_timestamp(
         52, precision=3, header=True
     )  #comment it out, if you are commenting the above line to send fake time
     _message = self.message
     del self.message
     self.message = simplefix.FixMessage()
     self.seqObj.updateReqSequence()
     return _message
Esempio n. 12
0
 async def _subscribe_security_definition(self, exchange, symbol):
     message = simplefix.FixMessage()
     self.request_id = self.request_id + 1
     message.append_pair(simplefix.TAG_MSGTYPE,
                         simplefix.MSGTYPE_SECURITY_DEFINITION_REQUEST)
     message.append_pair(320,
                         f"REQ:{self.request_id}")  # SECURITY_STATUS_REQ_ID
     message.append_pair(
         321, 1
     )  # SECURITY_REQUEST_TYPE 1 = REQUEST_SECURITY_IDENTITY_FOR_THE_SPECIFICATION_PROVIDED
     message.append_pair(simplefix.TAG_SYMBOL, symbol)  # SYMBOL
     message.append_pair(207, exchange)  # SECURITY_EXCHANGE
     message.append_pair(263,
                         1)  # SUBSCRIPTION_REQUEST_TYPE = SNAPSHOT + UPDATE
     await self.client.send(message)
Esempio n. 13
0
    def send_heartbeat(self, test_request_id=None):

        fix = simplefix.FixMessage()
        fix.set_message_type('0')  # Heartbeat
        fix.append_pair(34, self.next_seq())
        fix.append_pair(49, self._my_compid)
        fix.append_pair(56, self._peer_compid)
        fix.append_pair(52, self.get_fix_time())
        if test_request_id:
            fix.append_pair(112, test_request_id)

        self._session.send(fix.encode())
        logging.info("Sent FIX heartbeat" +
                     " id = [%s]" % test_request_id if test_request_id else "")
        return
Esempio n. 14
0
    def basic_message(self):
        try:
            print("in", self.msg_seq_num_lock.locked())
            self.msg_seq_num_lock.acquire()
            msg = simplefix.FixMessage()
            msg.append_pair(BeginString, self.BEGIN_STRING, header=True)
            msg.append_pair(SenderCompID, self.SENDER, header=True)
            msg.append_pair(TargetCompID, self.TARGET, header=True)
            msg.append_pair(MsgSeqNum, self.msg_seq_num)

            self.msg_seq_num = self.msg_seq_num + 1

            return msg
        finally:
            self.msg_seq_num_lock.release()
            print("out", self.msg_seq_num_lock.locked())
Esempio n. 15
0
 async def _unsubscribe_top_of_book(self, md_req_id, exchange, symbol):
     message = simplefix.FixMessage()
     self.request_id = self.request_id + 1
     message.append_pair(simplefix.TAG_MSGTYPE,
                         simplefix.MSGTYPE_MARKET_DATA_REQUEST)
     message.append_pair(262, md_req_id)  # MD_REQ_ID
     message.append_pair(263,
                         2)  # SUBSCRIPTION_REQUEST_TYPE = SNAPSHOT + UPDATE
     message.append_pair(264, 0)  # MARKET_DEPTH 1 = ToB
     message.append_pair(266, "Y")  # AGGREGATED_BOOK true = ToB
     message.append_pair(267, 2)  # NO_MD_ENTRY_TYPES
     message.append_pair(269, 0)  # MD_ENTRY_TYPE 0 = BID
     message.append_pair(269, 1)  # MD_ENTRY_TYPE 1 = OFFER
     message.append_pair(146, 1)  # NO_RELATED_SYM
     message.append_pair(simplefix.TAG_SYMBOL, symbol)  # SYMBOL
     message.append_pair(207, exchange)  # SECURITY_EXCHANGE
     await self.client.send(message)
Esempio n. 16
0
    def logOff(self):
        '''If ISSUE STILL PRESENT (|'s) -- append_pair for ALL'''
        msg = simplefix.FixMessage()

        fix_str = '8=FIX.4.2'

        msg.append_pair(49, 'qafix67', header=True)
        msg.append_pair(56, 'IB', header=True)
        msg.append_pair(34, '30', header=True)

        msg.append_string(fix_str)
        #msg.append_pair(9,'54') #Was 65, duplicate
        msg.append_pair(35, 5)
        msg.append_utc_timestamp(52, precision=6, header=True)

        b = msg.encode()
        return b
Esempio n. 17
0
    def logOff(self):
        '''create logOff msg -- returns bytes'''
        msg = simplefix.FixMessage()
        
        fix_str = '8=FIX.4.2'
        
        msg.append_pair(49,'qafix67',header=True)
        msg.append_pair(56,'IB',header=True)
        msg.append_pair(34,'30',header=True)

        
        msg.append_string(fix_str)
        #msg.append_pair(9,'54') #duplicate
        msg.append_pair(35,5)
        msg.append_utc_timestamp(52, precision=6, header=True)
        
        b = msg.encode()
        return b
Esempio n. 18
0
    def heartBeat(self):
        '''If ISSUE STILL PRESENT (|'s) -- append_pair for ALL'''
        msg = simplefix.FixMessage()

        fix_str = '8=FIX.4.2'  # 1=U01067 Moved out of header                   #New Addit (bc on works)

        msg.append_string(fix_str)

        msg.append_pair(49, 'qafix67', header=True)
        msg.append_pair(56, 'IB', header=True)
        msg.append_pair(34, 31, header=True)
        msg.append_utc_timestamp(52, precision=6, header=True)
        msg.append_pair(1, 'U01067')

        msg.append_pair(35, 0)

        b = msg.encode()
        return b
Esempio n. 19
0
    def baseBuy(self, symbol, qty):
        pkt = simplefix.FixMessage()

        pkt.append_pair(8, self.FV)
        pkt.append_pair(1, self.acct_number)
        pkt.append_pair(35, 'D')  #Always
        pkt.append_pair(56, self.TgtID)

        newID = self.getClOrdId()
        pkt.append_pair(11, newID)

        pkt.append_pair(55, symbol)
        pkt.append_pair(54, 1)  #buy
        pkt.append_pair(38, qty)
        pkt.append_pair(40, 1)  #mkt

        buf = pkt.encode()
        return buf
Esempio n. 20
0
    def heartBeat(self):
        '''Create heartbeat msg -- returns bytes'''
        msg = simplefix.FixMessage()
        
        fix_str = '8=FIX.4.2' # 1=U01067 Moved out of header                   
        
        msg.append_string(fix_str)

        msg.append_pair(49,'qafix67',header=True)
        msg.append_pair(56,'IB',header=True)
        msg.append_pair(34,31,header=True)
        msg.append_utc_timestamp(52,precision=6,header=True)
        msg.append_pair(1,'U01067')

        msg.append_pair(35,0)
        
        
        b = msg.encode()
        return b
Esempio n. 21
0
 def logOn(self):
     '''create logOn msg -- returns bytes'''
     msg = simplefix.FixMessage()
     
     fix_str = '8=FIX.4.2'
     
     msg.append_pair(49,'qafix67',header=True)
     msg.append_pair(56,'IB',header=True)
     msg.append_pair(34,'30',header=True)
     
     msg.append_string(fix_str)
     msg.append_pair(35,'A')
     msg.append_pair(108,'30')
     
     msg.append_utc_timestamp(52, precision=6, header=True)
     msg.append_pair(98,0)
     
     b = msg.encode()
     return b
Esempio n. 22
0
    def logOn(self):
        '''If ISSUE STILL PRESENT (|'s) -- append_pair for ALL'''
        msg = simplefix.FixMessage()

        fix_str = '8=FIX.4.2'

        msg.append_pair(49, 'qafix67', header=True)
        msg.append_pair(56, 'IB', header=True)
        msg.append_pair(34, '30', header=True)

        msg.append_string(fix_str)
        msg.append_pair(35, 'A')
        msg.append_pair(108, '30')

        msg.append_utc_timestamp(52, precision=6, header=True)

        msg.append_pair(98, 0)

        b = msg.encode()
        return b
Esempio n. 23
0
    def createOrder(self, ticker, side, qty):
        '''Side: Buy = 1; SellShort = 5'''
        msg = simplefix.FixMessage()

        #fix_str = '8=FIX.4.2 1=U01067'
        msg.append_pair(8, 'FIX.4.2')  #These may need header=True
        msg.append_pair(1, 'U01067')

        msg.append_pair(49, 'qafix67', header=True)
        msg.append_pair(56, 'IB', header=True)
        msg.append_pair(34, 31, header=True)
        msg.append_utc_timestamp(52, precision=6, header=True)

        msg.append_pair(35, 'D')
        msg.append_pair(55, ticker)
        msg.append_pair(54, side)  #1 Buy, 5 = Sellshort
        msg.append_pair(38, qty)

        b = msg.encode()

        return b
Esempio n. 24
0
    def send_login_ack(self, message):

        # In FIX, login acknowledgement is sent with a Logon()
        # message.  In addtion to confirming authentication, it also
        # returns the agree heartbeat timeout period.

        fix = simplefix.FixMessage()
        fix.append_pair(35, "A")
        fix.append_pair(34, self.next_seq())
        fix.append_pair(49, self._my_comp_id)  # Sender
        fix.append_pair(56, self._peer_comp_id)  # Target
        fix.append_pair(52, self.get_fix_time())
        fix.append_pair(98, 0)  # No encryption
        fix.append_pair(108, self._heartbeat_interval)
        self._session.send(fix.encode())

        # FIXME: set timer for heartbeats.
        #self._gateway.add_timeout(time.time() + self._heartbeat_interval, self.send_heartbeat)

        logging.info("Sent FIX logon (login_ack)")
        return
Esempio n. 25
0
    def header_msg(self, tag):
        """
        Return standard FIX message header with MsgType = tag.
        """

        # Build msg seq_nu from all messages sent to server.
        global seq_num

        msg = simplefix.FixMessage()
        seq_num[0] += 1
        sendingTime = datetime.now().strftime('%Y%m%d-%H:%M:%S.%f')[:-3]

        msg.append_pair(8, protocol_type)  # Protocol Type
        msg.append_pair(35, tag)  # MsgType
        msg.append_pair(34, seq_num[0])  # MsgSeqNum
        msg.append_pair(49, "fedejbrun5018")  # SenderCompID
        msg.append_pair(52, sendingTime)  # SendingTime
        msg.append_pair(56, "ROFX")  # TargetCompID
        msg.append_pair(115, User[0])  # OnBehalfOfCompID
        msg.append_pair(128, xchange_name[0])  # DeliverToCompID
        return msg
Esempio n. 26
0
def main():
    msg = simplefix.FixMessage()
    msg.append_pair(Field.BEGIN_STRING.value.get_number(), 'FIX.4.4')
    msg.append_pair(Field.MSG_TYPE.value.get_number(),
                    MsgType.ORDER_SINGLE.value)
    msg.append_pair(Field.PRICE.value.get_number(),
                    "%0.2f" % (random.random() * 2 + 10))
    msg.append_pair(Field.ORDER_QTY.value.get_number(),
                    int(random.random() * 100))
    msg.append_pair(Field.SYMBOL.value.get_number(), "VOD.L")
    msg.append_pair(Field.SECURITY_ID.value.get_number(), "GB00BH4HKS39")
    msg.append_pair(Field.SECURITY_ID_SOURCE.value.get_number(),
                    SecurityIDSource.ISIN_NUMBER.value)
    msg.append_pair(Field.ACCOUNT.value.get_number(), "TEST")
    msg.append_pair(
        Field.HANDL_INST.value.get_number(), HandlInst.
        AUTOMATED_EXECUTION_ORDER_PRIVATE_NO_BROKER_INTERVENTION.value)
    msg.append_pair(Field.EX_DESTINATION.value.get_number(), "XLON")
    msg.append_pair(Field.SIDE.value.get_number(), Side.BUY.value)
    msg.append_pair(Field.CL_ORD_ID.value.get_number(), str(uuid.uuid1()))
    msg.append_pair(Field.CURRENCY.value.get_number(), "GBP")

    print(str(msg.encode()).replace('\\x01', '|'))
Esempio n. 27
0
 async def send_logon(self, heart_bt_int):
     """send a logon message"""
     message = simplefix.FixMessage()
     message.append_pair(simplefix.TAG_MSGTYPE, simplefix.MSGTYPE_LOGON)
     message.append_pair(simplefix.TAG_HEARTBTINT, heart_bt_int)
     await self.send(message)
Esempio n. 28
0
 async def send_heartbeat(self, test_req_id):
     """send a logout message"""
     message = simplefix.FixMessage()
     message.append_pair(simplefix.TAG_MSGTYPE, simplefix.MSGTYPE_HEARTBEAT)
     message.append_pair(simplefix.TAG_TESTREQID, test_req_id)
     await self.send(message)
Esempio n. 29
0
 async def send_logout(self, text):
     """send a logout message"""
     message = simplefix.FixMessage()
     message.append_pair(simplefix.TAG_MSGTYPE, simplefix.MSGTYPE_LOGOUT)
     message.append_pair(simplefix.TAG_TEXT, text)
     await self.send(message)
Esempio n. 30
0
"""
Created on Wed May 30 20:50:39 2018

@author: TRISHA
"""

import simplefix
import datetime
import sys
import time
import warnings
from simplefix import *


 # message consists oof tag-value pairs
msg = simplefix.FixMessage()

# fix value for string
def fix_val(value):
 if type(value) == bytes:
        return value

    if sys.version_info[0] == 2:
        return bytes(value)
    elif type(value) == str:
        return bytes(value, 'UTF-8')
    else:
        return bytes(str(value), 'ASCII')
    
    
    # fix value for tag