diff --git a/gdax/public_client.py b/gdax/public_client.py index 43e769d..282021a 100644 --- a/gdax/public_client.py +++ b/gdax/public_client.py @@ -119,30 +119,59 @@ def get_product_ticker(self, product_id): """ return self._get('/products/{}/ticker'.format(str(product_id))) - def get_product_trades(self, 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 - + 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" - }] + 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 = {} - """ - return self._get('/products/{}/trades'.format(str(product_id))) + 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 + + r = requests.get(url, params=params) + # r.raise_for_status() + + 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 + + # 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 def get_product_historic_rates(self, product_id, start=None, end=None, granularity=None):