Skip to content

Commit da5c38b

Browse files
authored
Allow get_product_trades to be paginated
This implements the pagination of trades, as per the docs (https://docs.gdax.com/#get-trades).
1 parent d7dfc6f commit da5c38b

File tree

1 file changed

+26
-21
lines changed

1 file changed

+26
-21
lines changed

gdax/public_client.py

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,35 @@ def get_product_ticker(self, product_id):
119119
"""
120120
return self._get('/products/{}/ticker'.format(str(product_id)))
121121

122-
def get_product_trades(self, product_id):
123-
"""List the latest trades for a product.
122+
def get_product_trades(self, product_id, before='', after='', limit='', result=[]):
123+
url = self.url + '/products/{}/trades'.format(str(product_id))
124+
params = {}
124125

125-
Args:
126-
product_id (str): Product
126+
if before:
127+
params['before'] = str(before)
128+
if after:
129+
params['after'] = str(after)
130+
if limit and limit < 100:
131+
# the default limit is 100
132+
# we only add it if the limit is less than 100
133+
params['limit'] = limit
127134

128-
Returns:
129-
list: Latest trades. Example::
130-
[{
131-
"time": "2014-11-07T22:19:28.578544Z",
132-
"trade_id": 74,
133-
"price": "10.00000000",
134-
"size": "0.01000000",
135-
"side": "buy"
136-
}, {
137-
"time": "2014-11-07T01:08:43.642366Z",
138-
"trade_id": 73,
139-
"price": "100.00000000",
140-
"size": "0.01000000",
141-
"side": "sell"
142-
}]
135+
r = requests.get(url, params=params)
136+
r.raise_for_status()
143137

144-
"""
145-
return self._get('/products/{}/trades'.format(str(product_id)))
138+
result.extend(r.json())
139+
140+
if 'cb-after' in r.headers and limit is not len(result):
141+
# update limit
142+
limit -= len(result)
143+
if limit <= 0:
144+
return result
145+
146+
# ensure that we don't get rate-limited/blocked
147+
time.sleep(0.4)
148+
return self.get_product_trades(product_id=product_id, after=r.headers['cb-after'], limit=limit, result=result)
149+
150+
return result
146151

147152
def get_product_historic_rates(self, product_id, start=None, end=None,
148153
granularity=None):

0 commit comments

Comments
 (0)