From da5c38bc14edd8973bb364361c07631e382512fe Mon Sep 17 00:00:00 2001 From: Tamer Saadeh Date: Fri, 23 Mar 2018 17:40:30 +0100 Subject: [PATCH 1/3] Allow get_product_trades to be paginated This implements the pagination of trades, as per the docs (https://docs.gdax.com/#get-trades). --- gdax/public_client.py | 47 ++++++++++++++++++++++++------------------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/gdax/public_client.py b/gdax/public_client.py index 43e769d..3ce4fd2 100644 --- a/gdax/public_client.py +++ b/gdax/public_client.py @@ -119,30 +119,35 @@ def get_product_ticker(self, product_id): """ return self._get('/products/{}/ticker'.format(str(product_id))) - def get_product_trades(self, product_id): - """List the latest trades for a product. + def get_product_trades(self, product_id, before='', after='', limit='', result=[]): + url = self.url + '/products/{}/trades'.format(str(product_id)) + params = {} - Args: - product_id (str): Product + if before: + params['before'] = str(before) + if after: + params['after'] = str(after) + if limit and limit < 100: + # the default limit is 100 + # we only add it if the limit is less than 100 + params['limit'] = limit - Returns: - list: Latest trades. Example:: - [{ - "time": "2014-11-07T22:19:28.578544Z", - "trade_id": 74, - "price": "10.00000000", - "size": "0.01000000", - "side": "buy" - }, { - "time": "2014-11-07T01:08:43.642366Z", - "trade_id": 73, - "price": "100.00000000", - "size": "0.01000000", - "side": "sell" - }] + r = requests.get(url, params=params) + r.raise_for_status() - """ - return self._get('/products/{}/trades'.format(str(product_id))) + result.extend(r.json()) + + if 'cb-after' in r.headers and limit is not len(result): + # update limit + limit -= len(result) + if limit <= 0: + return result + + # ensure that we don't get rate-limited/blocked + time.sleep(0.4) + return self.get_product_trades(product_id=product_id, after=r.headers['cb-after'], limit=limit, result=result) + + return result def get_product_historic_rates(self, product_id, start=None, end=None, granularity=None): From 23f8949b24773405ce9fffdbe123f7365ee6695b Mon Sep 17 00:00:00 2001 From: Tamer Saadeh Date: Fri, 23 Mar 2018 17:51:04 +0100 Subject: [PATCH 2/3] add docs --- gdax/public_client.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/gdax/public_client.py b/gdax/public_client.py index 3ce4fd2..0c970e5 100644 --- a/gdax/public_client.py +++ b/gdax/public_client.py @@ -120,6 +120,30 @@ def get_product_ticker(self, product_id): return self._get('/products/{}/ticker'.format(str(product_id))) def get_product_trades(self, product_id, before='', after='', limit='', result=[]): + """List the latest trades for a product. + Args: + product_id (str): Product + before (Optional[str]): start time in ISO 8601 + after (Optional[str]): end time in ISO 8601 + limit (Optional[int]): the desired number of trades (can be more than 100, + automatically paginated) + results (Optional[list]): list of results that is used for the pagination + Returns: + list: Latest trades. Example:: + [{ + "time": "2014-11-07T22:19:28.578544Z", + "trade_id": 74, + "price": "10.00000000", + "size": "0.01000000", + "side": "buy" + }, { + "time": "2014-11-07T01:08:43.642366Z", + "trade_id": 73, + "price": "100.00000000", + "size": "0.01000000", + "side": "sell" + }] + """" url = self.url + '/products/{}/trades'.format(str(product_id)) params = {} From fb2f0d6d0775e341f39ce161efbb920c0684514a Mon Sep 17 00:00:00 2001 From: Tamer Saadeh Date: Sat, 24 Mar 2018 11:41:33 +0100 Subject: [PATCH 3/3] remove raise and comment out time.sleep --- gdax/public_client.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/gdax/public_client.py b/gdax/public_client.py index 0c970e5..282021a 100644 --- a/gdax/public_client.py +++ b/gdax/public_client.py @@ -157,7 +157,7 @@ def get_product_trades(self, product_id, before='', after='', limit='', result=[ params['limit'] = limit r = requests.get(url, params=params) - r.raise_for_status() + # r.raise_for_status() result.extend(r.json()) @@ -167,8 +167,8 @@ def get_product_trades(self, product_id, before='', after='', limit='', result=[ if limit <= 0: return result - # ensure that we don't get rate-limited/blocked - time.sleep(0.4) + # TODO: need a way to ensure that we don't get rate-limited/blocked + # time.sleep(0.4) return self.get_product_trades(product_id=product_id, after=r.headers['cb-after'], limit=limit, result=result) return result