Streaming data (prices & events)

The REST-V20 API offers streaming prices and streaming events. Both can be simply accessed as the next example will show.


In [2]:
import json
import oandapyV20
import oandapyV20.endpoints.pricing as pricing
from oandapyV20.exceptions import StreamTerminated
from exampleauth import exampleauth

accountID, access_token = exampleauth.exampleAuth()
client = oandapyV20.API(access_token=access_token)

instruments = ["EUR_USD", "EUR_JPY"]
r = pricing.PricingStream(accountID=accountID, params={"instruments": ",".join(instruments)})

n = 0
stopAfter = 3 # let's terminate after receiving 3 ticks
try:
    # the stream requests returns a generator so we can do ...
    for tick in client.request(r):
        print(tick)
        if n >= stopAfter:
            r.terminate()
        n += 1
        
except StreamTerminated as err:
    print("Stream processing ended because we made it stop after {} ticks".format(n))


{'asks': [{'liquidity': 10000000.0, 'price': '1.05534'}, {'liquidity': 10000000.0, 'price': '1.05536'}], 'tradeable': True, 'instrument': 'EUR_USD', 'type': 'PRICE', 'closeoutAsk': '1.05538', 'closeoutBid': '1.05518', 'time': '2017-03-09T13:37:46.048197280Z', 'status': 'tradeable', 'bids': [{'liquidity': 10000000.0, 'price': '1.05522'}, {'liquidity': 10000000.0, 'price': '1.05520'}]}
{'asks': [{'liquidity': 1000000.0, 'price': '120.991'}, {'liquidity': 2000000.0, 'price': '120.992'}, {'liquidity': 5000000.0, 'price': '120.993'}, {'liquidity': 10000000.0, 'price': '120.995'}], 'tradeable': True, 'instrument': 'EUR_JPY', 'type': 'PRICE', 'closeoutAsk': '120.995', 'closeoutBid': '120.965', 'time': '2017-03-09T13:37:44.958026355Z', 'status': 'tradeable', 'bids': [{'liquidity': 1000000.0, 'price': '120.969'}, {'liquidity': 2000000.0, 'price': '120.968'}, {'liquidity': 5000000.0, 'price': '120.967'}, {'liquidity': 10000000.0, 'price': '120.965'}]}
{'asks': [{'liquidity': 1000000.0, 'price': '120.995'}, {'liquidity': 2000000.0, 'price': '120.996'}, {'liquidity': 5000000.0, 'price': '120.997'}, {'liquidity': 10000000.0, 'price': '120.999'}], 'tradeable': True, 'instrument': 'EUR_JPY', 'type': 'PRICE', 'closeoutAsk': '120.999', 'closeoutBid': '120.971', 'time': '2017-03-09T13:37:47.550550833Z', 'status': 'tradeable', 'bids': [{'liquidity': 1000000.0, 'price': '120.975'}, {'liquidity': 2000000.0, 'price': '120.974'}, {'liquidity': 5000000.0, 'price': '120.973'}, {'liquidity': 10000000.0, 'price': '120.971'}]}
{'asks': [{'liquidity': 10000000.0, 'price': '1.05527'}, {'liquidity': 10000000.0, 'price': '1.05529'}], 'tradeable': True, 'instrument': 'EUR_USD', 'type': 'PRICE', 'closeoutAsk': '1.05531', 'closeoutBid': '1.05510', 'time': '2017-03-09T13:37:49.349544372Z', 'status': 'tradeable', 'bids': [{'liquidity': 10000000.0, 'price': '1.05514'}, {'liquidity': 10000000.0, 'price': '1.05512'}]}
Stream processing ended because we made it stop after 3 ticks

In [4]:
import json
import oandapyV20
import oandapyV20.endpoints.transactions as trans
from oandapyV20.exceptions import StreamTerminated
from exampleauth import exampleauth

accountID, access_token = exampleauth.exampleAuth()
client = oandapyV20.API(access_token=access_token)

instruments = ["EUR_USD", "EUR_JPY"]
r = trans.TransactionsStream(accountID=accountID)

n = 0
stopAfter = 3 # let's terminate after receiving 3 ticks
try:
    # the stream requests returns a generator so we can do ...
    for T in client.request(r):
        print(T)
        if n >= stopAfter:
            r.terminate()
        n += 1
        
except StreamTerminated as err:
    print("Stream processing ended because we made it stop after {} ticks".format(n))


{'lastTransactionID': '7582', 'time': '2017-03-09T15:08:57.512620407Z', 'type': 'HEARTBEAT'}
{'lastTransactionID': '7582', 'time': '2017-03-09T15:09:02.594588344Z', 'type': 'HEARTBEAT'}
{'lastTransactionID': '7582', 'time': '2017-03-09T15:09:07.657436396Z', 'type': 'HEARTBEAT'}
{'lastTransactionID': '7582', 'time': '2017-03-09T15:09:12.721645564Z', 'type': 'HEARTBEAT'}
Stream processing ended because we made it stop after 3 ticks