├── LICENCE.md ├── README.md └── streaming.py /LICENCE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 OANDA Corporation 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | py-api-streaming 2 | ================ 3 | 4 | A sample Python application that connects to OANDA's HTTP based rates stream. 5 | 6 | ### Setup 7 | 8 | Clone this repo to the location of your choice 9 | 10 | Update the following values in the connect_to_stream method in streaming.py 11 | 12 | * domain 13 | * account_id 14 | * access_token (Authorization) 15 | * instruments 16 | 17 | ### Usage 18 | 19 | ~~~ 20 | python streaming.py [options] 21 | ~~~ 22 | 23 | #### Options 24 | 25 | **-b**, **--displayHeartBeat** 26 | : _Optional_ Toggles the displaying of the stream's heartbeats. No heartbeats are displayed by default. 27 | 28 | ### Sample Output 29 | 30 | {"tick":{"instrument":"EUR_USD","time":"2014-03-07T20:58:07.461445Z","bid":1.38701,"ask":1.38712}} 31 | {"tick":{"instrument":"EUR_USD","time":"2014-03-07T20:58:09.345955Z","bid":1.38698,"ask":1.38709}} 32 | {"tick":{"instrument":"USD_CAD","time":"2014-03-07T20:58:12.320218Z","bid":1.10906,"ask":1.10922}} 33 | {"tick":{"instrument":"USD_CAD","time":"2014-03-07T20:58:12.360615Z","bid":1.10904,"ask":1.10925}} 34 | 35 | ### More Information 36 | 37 | [http://developer.oanda.com/](http://developer.oanda.com/docs/v1/stream/#rates-streaming) 38 | -------------------------------------------------------------------------------- /streaming.py: -------------------------------------------------------------------------------- 1 | """ 2 | Demonstrates streaming feature in OANDA open api 3 | 4 | To execute, run the following command: 5 | 6 | python streaming.py [options] 7 | 8 | To show heartbeat, replace [options] by -b or --displayHeartBeat 9 | """ 10 | 11 | import requests 12 | import json 13 | 14 | from optparse import OptionParser 15 | 16 | def connect_to_stream(): 17 | 18 | """ 19 | Environment Description 20 | fxTrade (Live) The live (real money) environment 21 | fxTrade Practice (Demo) The demo (simulated money) environment 22 | """ 23 | domainDict = { 'live' : 'stream-fxtrade.oanda.com', 24 | 'demo' : 'stream-fxpractice.oanda.com' } 25 | 26 | # Replace the following variables with your personal values 27 | environment = "demo" # Replace this 'live' if you wish to connect to the live environment 28 | domain = domainDict[environment] 29 | access_token = 'REPLACE THIS WITH YOUR ACCESS TOKEN' 30 | account_id = 'REPLACE THIS WITH YOUR ACCOUNT ID, ie 2252344' 31 | instruments = 'REPLACE THIS WITH THE INSTRUMENTS YOU WOULD LIKE TO SUBSCRIBE TO. ie "EUR_USD,USD_JPY,...' 32 | 33 | try: 34 | s = requests.Session() 35 | url = "https://" + domain + "/v1/prices" 36 | headers = {'Authorization' : 'Bearer ' + access_token, 37 | # 'X-Accept-Datetime-Format' : 'unix' 38 | } 39 | params = {'instruments' : instruments, 'accountId' : account_id} 40 | req = requests.Request('GET', url, headers = headers, params = params) 41 | pre = req.prepare() 42 | resp = s.send(pre, stream = True, verify = True) 43 | return resp 44 | except Exception as e: 45 | s.close() 46 | print("Caught exception when connecting to stream\n" + str(e)) 47 | 48 | def demo(displayHeartbeat): 49 | response = connect_to_stream() 50 | if response.status_code != 200: 51 | print(response.text) 52 | return 53 | for line in response.iter_lines(1): 54 | if line: 55 | try: 56 | line = line.decode('utf-8') 57 | msg = json.loads(line) 58 | except Exception as e: 59 | print("Caught exception when converting message into json\n" + str(e)) 60 | return 61 | 62 | if "instrument" in msg or "tick" in msg or displayHeartbeat: 63 | print(line) 64 | 65 | def main(): 66 | usage = "usage: %prog [options]" 67 | parser = OptionParser(usage) 68 | parser.add_option("-b", "--displayHeartBeat", dest = "verbose", action = "store_true", 69 | help = "Display HeartBeat in streaming data") 70 | displayHeartbeat = False 71 | 72 | (options, args) = parser.parse_args() 73 | if len(args) > 1: 74 | parser.error("incorrect number of arguments") 75 | if options.verbose: 76 | displayHeartbeat = True 77 | demo(displayHeartbeat) 78 | 79 | 80 | if __name__ == "__main__": 81 | main() 82 | 83 | 84 | --------------------------------------------------------------------------------