streaming_prices.open()

HI i have the following code:

def display_refreshed_fields(streaming_price, instrument_name, fields): current_time = datetime.datetime.now().time() print(current_time, "- Refresh received for", instrument_name, ":", fields) def display_updated_fields(streaming_price, instrument_name, fields): current_time = datetime.datetime.now().time() print(current_time, "- Update received for", instrument_name, ":", fields) def display_status(streaming_price, instrument_name, status): current_time = datetime.datetime.now().time() print(current_time, "- Status received for", instrument_name, ":", status) streaming_prices = ek.StreamingPrices( instruments = ['EUR=','GBP=','JPY=', 'CAD='], fields = ['SALTIM', 'CF_BID','CF_ASK','OPEN_PRC', 'CF_HIGH','CF_LOW', 'CF_CLOSE'], on_refresh = lambda streaming_price, instrument_name, fields : display_refreshed_fields(streaming_price, instrument_name, fields), on_update = lambda streaming_price, instrument_name, fields : display_updated_fields(streaming_price, instrument_name, fields), on_status = lambda streaming_price, instrument_name, status : display_status(streaming_price, instrument_name, status) ) streaming_prices.open()

I use Pycharm and running this code i get:

16:20:03.342917 - Status received for GBP= : {'status': <StreamState.Open: 3>, 'code': 'Open', 'message': 'All is well'} 16:20:03.343917 - Refresh received for GBP= : {'CF_BID': 1.2503, 'CF_ASK': 1.2507, 'OPEN_PRC': 1.2473, 'CF_HIGH': 1.2529, 'CF_LOW': 1.2462, 'CF_CLOSE': 1.247} 16:20:03.343917 - Status received for JPY= : {'status': <StreamState.Open: 3>, 'code': 'Open', 'message': 'All is well'} 16:20:03.343917 - Refresh received for JPY= : {'CF_BID': 107.5, 'CF_ASK': 107.53, 'OPEN_PRC': 107.45, 'CF_HIGH': 107.72, 'CF_LOW': 107.34, 'CF_CLOSE': 107.46} 16:20:03.343917 - Status received for CAD= : {'status': <StreamState.Open: 3>, 'code': 'Open', 'message': 'All is well'} 16:20:03.343917 - Refresh received for CAD= : {'CF_BID': 1.3576, 'CF_ASK': 1.358, 'OPEN_PRC': 1.3582, 'CF_HIGH': 1.3611, 'CF_LOW': 1.356, 'CF_CLOSE': 1.3585} 16:20:03.343917 - Status received for EUR= : {'status': <StreamState.Open: 3>, 'code': 'Open', 'message': 'All is well'} 16:20:03.343917 - Refresh received for EUR= : {'CF_BID': 1.1264, 'CF_ASK': 1.1268, 'OPEN_PRC': 1.125, 'CF_HIGH': 1.1302, 'CF_LOW': 1.1248, 'CF_CLOSE': 1.125} Process finished with exit code 0


As you can see, the program terminates almost immediatly, I would like the streaming prices to keep open and the updates getting handled when they come in.

What works is putting the streaming_prices.open() in a loop:

while True: 
streaming_prices.open()
streaming_prices.open()while True:      pass

this doesnt work either unfortunatly, then the program doesnt terminate, but the updates dont get handled by the function.

But I cant imagine this is the way to do it? Any help would be appreciated, thanks!


Jan


Best Answer

  • Hi,

    streaming_prices.open() does just the streaming connection with the server.
    It's up to you to keep it streaming the duration you want/need.
    The solution you propose is correct but you should define the way to stop it

    while True:
    ...
    if <condition is True>:
    break

    ...

    You can also run your code in a Jupyter notebook because the stream will continue until you close your book.

Answers

  • streaming_prices.open()
    while True:
    pass

    this doesnt work either unfortunatly, then the program doesnt terminate, but the updates dont get handled by the function.

  • somehow the readability got lost, here is the code again:

    def display_refreshed_fields(streaming_price, instrument_name, fields):
    current_time = datetime.datetime.now().time()
    print(current_time, "- Refresh received for", instrument_name, ":", fields)



    def display_updated_fields(streaming_price, instrument_name, fields):
    current_time = datetime.datetime.now().time()
    print(current_time, "- Update received for", instrument_name, ":", fields)

    def display_status(streaming_price, instrument_name, status):
    current_time = datetime.datetime.now().time()
    print(current_time, "- Status received for", instrument_name, ":", status)



    streaming_prices = ek.StreamingPrices(
    instruments = ['EUR=','GBP=','JPY=', 'CAD='],
    fields = ['SALTIM', 'CF_BID','CF_ASK','OPEN_PRC', 'CF_HIGH','CF_LOW', 'CF_CLOSE'],
    on_refresh = lambda streaming_price, instrument_name, fields :
    display_refreshed_fields(streaming_price, instrument_name, fields),
    on_update = lambda streaming_price, instrument_name, fields :
    display_updated_fields(streaming_price, instrument_name, fields),
    on_status = lambda streaming_price, instrument_name, status :
    display_status(streaming_price, instrument_name, status)

    )


    streaming_prices.open()
  • that is clear then, thank you Pierre!