Example #1
0
async def process_row(receive_channel, prc_file):
    """receive price feed, process it and store in a np.array"""
    prc_list = []
    async for value in receive_channel:
        # differentiate between 112 and 117

        if value[0] == 117:
            m_resp = Dtc.MarketDataUpdateBidAskCompact()
            m_resp.ParseFromString(value[1])
            msg = np.array([
                np.uint32(value[0]),
                np.uint32(m_resp.DateTime),
                np.uint32(m_resp.SymbolID),
                np.uint32(m_resp.BidPrice * 100),
                np.uint32(m_resp.BidQuantity),
                np.uint32(m_resp.AskPrice * 100),
                np.uint32(m_resp.AskQuantity)
            ])
            np.savetxt(prc_file, msg[None, :], fmt="%d", delimiter=', ')

        elif value[0] == 112:
            m_resp = Dtc.MarketDataUpdateTradeCompact()
            m_resp.ParseFromString(value[1])
            msg = np.array([
                np.uint32(value[0]),
                np.uint32(m_resp.DateTime),
                np.uint32(m_resp.SymbolID),
                np.uint32(m_resp.Price * 100),
                np.uint32(m_resp.Volume),
                np.uint32(m_resp.AtBidOrAsk)
            ])
            np.savetxt(prc_file, msg[None, :], fmt="%d", delimiter=', ')
Example #2
0
async def heartbeater(client_stream, heartbeat_interval):
    # loggen.debug("Heartbeater started")
    hrtbt = Dtc.Heartbeat()
    while True:
        logStdout.info('Sending Heartbeat')
        await send_message(hrtbt, Dtc.HEARTBEAT, client_stream)
        await trio.sleep(heartbeat_interval)
Example #3
0
def create_mktdat_req(symbolID, symbol, exchange):
    data_req = Dtc.MarketDataRequest()
    data_req.RequestAction = Dtc.SUBSCRIBE
    data_req.SymbolID = symbolID  # Note: Make sure this is unique when requesting multiple symbols
    data_req.Symbol = symbol
    data_req.Exchange = exchange
    return data_req
Example #4
0
async def trigger(event,scope, sock, loggen):
    loggen.info("  trigger waiting now...")
    logStdout.info("  trigger waiting now...")
    await event.wait()
    """ Gracefully logoff and close the connection """
    loggen.info(" QUIT!!! : Gracefully logoff and close the connection ")
    logStdout.info(" QUIT!!! : Gracefully logoff and close the connection ")
    logoff = Dtc.Logoff()
    logoff.Reason = "John-Client quit"
    await send_message(logoff, Dtc.LOGOFF, sock)
    loggen.info('--------------------------------------------------------------')
    logStdout.info('--------------------------------------------------------------')
    scope.cancel()
Example #5
0
async def parent(addr,
                 symbols,
                 logfile,
                 encoding=Dtc.PROTOCOL_BUFFERS,
                 heartbeat_interval=15):
    event = trio.Event()
    #logprc = create_logprc(logfile)
    prc_file = open(logfile + "_prc", 'a')
    loggen = create_loggen(logfile)
    loggen.info(f"connecting to {addr[0]}:{addr[1]}")
    logStdout.info(f"connecting to {addr[0]}:{addr[1]}")
    client_stream = await trio.open_tcp_stream(addr[0], addr[1])
    # client_stream = await trio.open_ssl_over_tcp_stream(addr[0], addr[1], ssl_context=ssl.SSLContext())
    async with client_stream:
        # encoding request
        loggen.info("spawing encoding request ...")
        # construct encoding request
        enc_req = Dtc.EncodingRequest()
        enc_req.Encoding = encoding
        enc_req.ProtocolType = "DTC"
        enc_req.ProtocolVersion = Dtc.CURRENT_VERSION
        await send_message(enc_req, Dtc.ENCODING_REQUEST,
                           client_stream)  # send encoding request
        mtype, mbody = await get_response(client_stream)
        msg = read_response(mtype, mbody)
        loggen.info("%s, %s", mtype, msg)

        # # logon request
        loggen.info("spawing logon request ...")
        logon_req = Dtc.LogonRequest()
        logon_req.ProtocolVersion = Dtc.CURRENT_VERSION
        # logon_req.Username = "******"
        # logon_req.Password = "******"
        logon_req.GeneralTextData = "John's Test"
        logon_req.HeartbeatIntervalInSeconds = heartbeat_interval
        logon_req.ClientName = "John_Tester"
        await send_message(logon_req, Dtc.LOGON_REQUEST, client_stream)
        mtype, mdata = await get_response(client_stream)
        msg = read_response(mtype, mbody)
        loggen.info("%s, %s", mtype, msg)

        # Start nursery
        async with trio.open_nursery() as nursery:
            # trigger
            loggen.info("spawning trigger...")
            nursery.start_soon(trigger, event, nursery.cancel_scope,
                               client_stream, loggen, prc_file)

            # Open Channels to separate receiving and writing jobs from feed
            send_channel, receive_channel = trio.open_memory_channel(4096)
            # Start a producer and a consumer, passing one end of the channel to each of them:
            #nursery.start_soon(producer, send_channel)
            #nursery.start_soon(consumer, receive_channel)

            # mkt data request
            loggen.info("spawning mkt data request ...")
            for m in symbols:
                data_req = create_mktdat_req(*m)
                await send_message(data_req, Dtc.MARKET_DATA_REQUEST,
                                   client_stream)
                mtype, mdata = await get_response(client_stream)
                msg = read_response(mtype, mbody)
                loggen.info("%s, %s", mtype, msg)

            loggen.info("spawning heartbeater ...")
            nursery.start_soon(heartbeater, client_stream,
                               heartbeat_interval)  # task to send heartbeat

            loggen.info("spawning prc_feeder ...")
            nursery.start_soon(prc_feeder, client_stream, send_channel,
                               loggen)  # task to grab price from feed
            nursery.start_soon(process_row, receive_channel,
                               prc_file)  # task to write to disk

            # -- NEW
            async for key in keyboard():
                if key == 'q':
                    # nursery.cancel_scope.cancel()
                    event.set()

            loggen.info("waiting for tasks to finish...")
            logStdout.info("waiting for tasks to finish...")

        loggen.info("  Logoff Successful!!!")
        logStdout.info("  Logoff Successful!!!")

    loggen.info(
        "  Socket Closed!!! --------------------------------------------------------"
    )
    logStdout.info(
        "  Socket Closed!!!------------------------------------------------------"
    )
Example #6
0
async def parent(addr, symbols, logfile, encoding=Dtc.PROTOCOL_BUFFERS, heartbeat_interval=10):
    event = trio.Event()
    logprc = create_logprc(logfile)
    loggen = create_loggen(logfile)
    loggen.info(f"connecting to {addr[0]}:{addr[1]}")
    logStdout.info(f"connecting to {addr[0]}:{addr[1]}")
    client_stream = await trio.open_tcp_stream(addr[0], addr[1])
    # client_stream = await trio.open_ssl_over_tcp_stream(addr[0], addr[1], ssl_context=ssl.SSLContext())

    async with client_stream:
        # encoding request
        loggen.info("spawing encoding request ...")
        # construct encoding request
        enc_req = Dtc.EncodingRequest()
        enc_req.Encoding = encoding
        enc_req.ProtocolType = "DTC"
        enc_req.ProtocolVersion = Dtc.CURRENT_VERSION
        await send_message(enc_req, Dtc.ENCODING_REQUEST,client_stream) # send encoding request
        response = await get_response(client_stream)
        loggen.info("%s ", response)

        # # logon request
        loggen.info("spawing logon request ...")
        logon_req = Dtc.LogonRequest()
        logon_req.ProtocolVersion = Dtc.CURRENT_VERSION
        # logon_req.Username = "******"
        # logon_req.Password = "******"
        logon_req.GeneralTextData = "John's Test"
        logon_req.HeartbeatIntervalInSeconds = heartbeat_interval
        logon_req.ClientName = "John_Tester"
        await send_message(logon_req, Dtc.LOGON_REQUEST, client_stream)
        response = await get_response(client_stream)
        loggen.info("%s ", response)

        # Start nursery
        async with trio.open_nursery() as nursery:
            # trigger
            loggen.info("spawning trigger...")
            nursery.start_soon(trigger, event, nursery.cancel_scope, client_stream, loggen)
            loggen.info("spawned trigger ...")

            # mkt data request
            loggen.info("spawning mkt data request ...")
            for m in symbols:    
                data_req = create_mktdat_req(*m)
                await send_message(data_req, Dtc.MARKET_DATA_REQUEST, client_stream)
                response = await get_response(client_stream)
                loggen.info("%s ", response)


            loggen.info("spawning heartbeater ...")
            nursery.start_soon(heartbeater, client_stream, heartbeat_interval)

            loggen.info("spawning mkt_updater ...")
            nursery.start_soon(mkt_updater, client_stream, logprc)

            # -- NEW
            async for key in keyboard():
                if key == 'q':
                    # nursery.cancel_scope.cancel()
                    event.set()
                    
            loggen.info("waiting for tasks to finish...")
            logStdout.info("waiting for tasks to finish...")

        loggen.info("  Logoff Successful!!!")
        logStdout.info("  Logoff Successful!!!")

    loggen.info("  Socket Closed!!! --------------------------------------------------------")
    logStdout.info("  Socket Closed!!!------------------------------------------------------")