Streaming Prices


In [1]:
from datetime import datetime, timedelta
import pandas as pd
import oandapy
import configparser

config = configparser.ConfigParser()
config.read('../config/config_v1.ini')
account_id = config['oanda']['account_id']
api_key = config['oanda']['api_key']

oanda = oandapy.API(environment="practice", 
                    access_token=api_key)

In [2]:
class MyStreamer(oandapy.Streamer):
    def __init__(self, count=10, *args, **kwargs):
        super(MyStreamer, self).__init__(*args, **kwargs)
        self.count = count
        self.reccnt = 0

    def on_success(self, data):
        print(data, "\n")
        self.reccnt += 1
        if self.reccnt == self.count:
            self.disconnect()

    def on_error(self, data):
        self.disconnect()

In [3]:
stream = MyStreamer(environment="practice", 
                    access_token=api_key)
stream.rates(account_id, instruments="EUR_USD,EUR_JPY")


{'tick': {'time': '2017-01-27T13:55:54.881300Z', 'ask': 1.0695, 'bid': 1.06937, 'instrument': 'EUR_USD'}} 

{'tick': {'time': '2017-01-27T13:55:57.550989Z', 'ask': 123.202, 'bid': 123.184, 'instrument': 'EUR_JPY'}} 

{'tick': {'time': '2017-01-27T13:55:59.301785Z', 'ask': 123.206, 'bid': 123.188, 'instrument': 'EUR_JPY'}} 

{'tick': {'time': '2017-01-27T13:55:59.294828Z', 'ask': 1.06952, 'bid': 1.06939, 'instrument': 'EUR_USD'}} 

{'tick': {'time': '2017-01-27T13:55:59.611722Z', 'ask': 1.06953, 'bid': 1.06942, 'instrument': 'EUR_USD'}} 

{'tick': {'time': '2017-01-27T13:55:59.829196Z', 'ask': 1.06951, 'bid': 1.0694, 'instrument': 'EUR_USD'}} 

{'tick': {'time': '2017-01-27T13:55:59.917122Z', 'ask': 1.06949, 'bid': 1.06937, 'instrument': 'EUR_USD'}} 

{'heartbeat': {'time': '2017-01-27T13:56:00.013690Z'}} 

{'tick': {'time': '2017-01-27T13:56:00.163226Z', 'ask': 123.212, 'bid': 123.192, 'instrument': 'EUR_JPY'}} 

{'tick': {'time': '2017-01-27T13:56:00.621958Z', 'ask': 123.208, 'bid': 123.188, 'instrument': 'EUR_JPY'}}