Skip to Content
Mastering Python for Finance - Second Edition
book

Mastering Python for Finance - Second Edition

by James Ma Weiming
April 2019
Intermediate to advanced
426 pages
11h 13m
English
Packt Publishing
Content preview from Mastering Python for Finance - Second Edition

Implementing the method for streaming prices

Add the following stream_prices() method in the OandaBroker class to start streaming prices from your broker:

def stream_prices(self, symbols=[]):
    response = self.stream_api.pricing.stream(
        self.accountid,
        instruments=",".join(symbols),
        snapshot=True
    )

    for msg_type, msg in response.parts():
        if msg_type == "pricing.Heartbeat":
            continue
        elif msg_type == "pricing.ClientPrice":
            self.process_price(msg)

Since the host connection expects a continuous stream, the response object has a parts() method that listens for incoming data. The msg object is essentially a price object, which we can reuse with the process_price() method to notify the listeners of an incoming price event.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Python for Finance - Second Edition

Python for Finance - Second Edition

Yuxing Yan
Python for Finance

Python for Finance

Yves Hilpisch

Publisher Resources

ISBN: 9781789346466Supplemental Content