Tutorial Source Code |
Examples referenced: |
Last Update | July 2021 |
Interpreter | Python 3.7.x or greater |
Pre-requisites | Familiarity with Python If accessing content from:
This example is built to work from a python console. |
Whether accessing from the desktop, directly to the cloud, or through your deployed managed streaming services, the Refinitiv Data Libraries provide ease-of-use interfaces to retrieve real-time streaming content from the platform. By utilizing streaming interfaces, developers can easily define properties to request, receive, and process market data in real-time.
The following example requests for a small watchlist of instruments, retrieving level 1 (MarketPrice) streaming content that captures real-time events in user-defined callbacks. The Pricing interface provides the ability to define callback methods to capture market data events including the initial refresh, status events as well as updates, such as trades and quotes, based on changes in the market. The advantage of these capabilities is for applications to immediately respond to critical changes that drive real-time decisions within their application workflow.
Whilst all the other tutorials are in Jupyter Notebook form (for ease of demonstration) - we often need to run Python code in a .py script file.
If you are using any synchronous method/function call, then normally this is not an issue as the call blocks and returns when the response has been received from the server.
However, when using the Streaming Price interface in the event-driven mode we need to ensure the script does not terminate once we have opened the stream.
So, for this tutorial, we implement the Pricing-StreamingEvents.ipynb notebook in a .py format. If you compare the code, you will note that there is very little difference in the code.
The only significant difference is in the last few lines of the script:
# Loop until specified end time
while (time.time() < exit_time):
# The following line ensures the async event callback mechanism works
asyncio.get_event_loop().run_until_complete(asyncio.sleep(1))
The above final line of code ensures
The while loop is used to allow the example to run for a limited time before the application terminates.
As with the Notebook version of the example. the output provided below dumps the entire initial image captured within the refresh callback. In addition, as market conditions change, events will be sent to an update callback that will demonstrate how to pull out individual elements from the response for display purposes. Each update displays a simple line of text detailing the instrument as well as the fields updated.
Within the source code package, select the source code example within the Content section. To execute the example:
The expected output should look similar to this: