python api streaming price can't work on pycharm?

Hello there,

I use sample code your tutorial provide to run python eikon api streaming prices function. It works well on jupyter as I separate my code in different cells for streaming_prices.open() & streaming_prices.close() function. It keeps receiving newly updated data during the time I run these two cells.

But, if I run same code in the same script in pycharm, it doesn't work that well. It can only runs for the first refresh function and then the program go into end. I tried some different approach such as adding time.sleep or systems.("pause") while still remains the problem. Do you have any advice on this? thanks!

import eikon as ek
import os
ek.set_app_key('key')

def display_fields_refresh(streaming_prices, instrument_name, fields):
print("Fields refresh received for", instrument_name, ":", fields)
print(streaming_prices)

def display_fields_update(streaming_prices, instrument_name, fields):
print("Fields updated received for", instrument_name, ":", fields)

if __name__ == '__main__':
streaming_prices = ek.StreamingPrices(
        instruments=['CNH='],
        fields=['SALTIM', 'CF_BID', 'CF_ASK', 'OPEN_PRC', 'CF_HIGH', 'CF_LOW', 'CF_CLOSE', 'VALUE_TS1'],
        on_refresh_refresh=lambda streaming_prices, instrument_name, fields:
        display_fields(streaming_prices, instrument_name, fields),
        on_update=lambda streaming_prices, instrument_name, fields:
        display_fields_update(streaming_prices, instrument_name, fields)
    )
    print("start streaming.......")
    streaming_prices.open()
    #time.sleep(5)
    #os.system('pause')
    #streaming_prices.close()
    print("stop streaming.......")

Best Answer

  • Hi @lawrence.chiang

    Can you please try replacing your time.sleep() / adding the following after the streaming_prices open()

    streaming_prices.open() 
    asyncio.get_event_loop().run_until_complete(asyncio.sleep(120))

    OR something like

    streaming_prices.open()
    asyncio.get_event_loop().run_forever()

    You will need to import asyncio off course


Answers

  • sorry, correct some code and provide you with the running result as below. thanks

    def display_fields_refresh(streaming_prices, instrument_name, fields):
    print("Fields refresh received for", instrument_name, ":", fields)
    print(streaming_prices)


    def display_fields_update(streaming_prices, instrument_name, fields):
    print("Fields updated received for", instrument_name, ":", fields)


    if __name__ == '__main__':
    streaming_prices = ek.StreamingPrices(
    instruments=['CNH='],
    fields=['SALTIM', 'CF_BID', 'CF_ASK', 'OPEN_PRC', 'CF_HIGH', 'CF_LOW', 'CF_CLOSE', 'VALUE_TS1'],
    on_refresh=lambda streaming_prices, instrument_name, fields:
    display_fields_refresh(streaming_prices, instrument_name, fields),
    on_update=lambda streaming_prices, instrument_name, fields:
    display_fields_update(streaming_prices, instrument_name, fields)
    )
    print("start streaming.......")
    streaming_prices.open()
    # time.sleep(5)
    # os.system('pause')
    # streaming_prices.close()
    print("stop streaming.......")


    start streaming.......

    Fields refresh received for CNH= : {'CF_BID': 6.5174, 'CF_ASK': 6.518, 'OPEN_PRC': 6.516, 'CF_HIGH': 6.5327, 'CF_LOW': 6.5116, 'CF_CLOSE': 6.5165, 'VALUE_TS1': '11:05:25'}

    <eikon.streaming_session.streamingprices.StreamingPrices object at 0x0000015B722631C8>

    stop streaming.......


    Process finished with exit code 0