Skip to content

Commit

Permalink
Allow get_product_trades to be paginated
Browse files Browse the repository at this point in the history
This implements the pagination of trades, as per the docs (https://docs.gdax.com/#get-trades).
  • Loading branch information
tamersaadeh authored Mar 23, 2018
1 parent d7dfc6f commit da5c38b
Showing 1 changed file with 26 additions and 21 deletions.
47 changes: 26 additions & 21 deletions gdax/public_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit da5c38b

Please sign in to comment.