From 70cc173f022603b532f374137e054327f15a68c0 Mon Sep 17 00:00:00 2001 From: Derek Colley Date: Fri, 28 Sep 2018 15:00:10 +0100 Subject: [PATCH 1/2] Add instrument.orderBook() to the API --- src/v20/instrument.py | 113 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) diff --git a/src/v20/instrument.py b/src/v20/instrument.py index 99020a9..ced974a 100644 --- a/src/v20/instrument.py +++ b/src/v20/instrument.py @@ -1140,3 +1140,116 @@ def position_book( return response + def orderBook(self, instrument, **kwargs): + print("orderBook() starting...") + request = Request( + 'GET', + '/v3/instruments/{instrument}/orderBook' + ) + + request.set_path_param( + 'instrument', + instrument + ) + + request.set_param( + 'time', + kwargs.get('time') + ) + + response = self.ctx.request(request) + + if response.content_type is None: + return response + + if not response.content_type.startswith("application/json"): + return response + + jbody = json.loads(response.raw_body) + + parsed_body = {} + + # + # Parse responses as defined by the API specification + # + if str(response.status) == "200": + if jbody.get('orderBook') is not None: + parsed_body['orderBook'] = \ + jbody.get('orderBook') + + if jbody.get('instrument') is not None: + parsed_body['instrument'] = \ + jbody.get('instrument') + + if jbody.get('time') is not None: + print("found time") + parsed_body['time'] = \ + jbody.get('time') + + if jbody.get('unixTime') is not None: + print("found unixTime") + parsed_body['unixTime'] = \ + jbody.get('unixTime') + + if jbody.get('price') is not None: + print("found price") + parsed_body['price'] = \ + jbody.get('price') + + if jbody.get('bucketWidth') is not None: + print("found bucketWidth") + parsed_body['bucketWidth'] = \ + jbody.get('bucketWidth') + + if jbody.get('buckets') is not None: + print("found buckets") + parsed_body['buckets'] = [ + self.ctx.instrument.Candlestick.from_dict(d, self.ctx) + for d in jbody.get('buckets') + ] + + elif str(response.status) == "400": + if jbody.get('errorCode') is not None: + parsed_body['errorCode'] = \ + jbody.get('errorCode') + + if jbody.get('errorMessage') is not None: + parsed_body['errorMessage'] = \ + jbody.get('errorMessage') + + elif str(response.status) == "401": + if jbody.get('errorCode') is not None: + parsed_body['errorCode'] = \ + jbody.get('errorCode') + + if jbody.get('errorMessage') is not None: + parsed_body['errorMessage'] = \ + jbody.get('errorMessage') + + elif str(response.status) == "404": + if jbody.get('errorCode') is not None: + parsed_body['errorCode'] = \ + jbody.get('errorCode') + + if jbody.get('errorMessage') is not None: + parsed_body['errorMessage'] = \ + jbody.get('errorMessage') + + elif str(response.status) == "405": + if jbody.get('errorCode') is not None: + parsed_body['errorCode'] = \ + jbody.get('errorCode') + + if jbody.get('errorMessage') is not None: + parsed_body['errorMessage'] = \ + jbody.get('errorMessage') + + # + # Unexpected response status + # + else: + parsed_body = jbody + + response.body = parsed_body + + return response From 327054e0c8666d7df930eb73cf63401c740c71dc Mon Sep 17 00:00:00 2001 From: Derek Colley Date: Fri, 28 Sep 2018 15:02:21 +0100 Subject: [PATCH 2/2] increment version for instrument.orderBook() --- src/setup.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/setup.py b/src/setup.py index 6e162e2..73ef46b 100644 --- a/src/setup.py +++ b/src/setup.py @@ -25,7 +25,7 @@ def main(): # setup dictionary setup_options = { "name": "v20", - "version": "3.0.25.0", + "version": "3.0.25.1", "description": "OANDA v20 bindings for Python", "long_description": read("README.rst"), "author": "OANDA Corporation",