Skip to content

Commit 6e23554

Browse files
authored
Merge pull request #279 from tamersaadeh/patch-1
Allow get_product_trades() to be paginated
2 parents d7dfc6f + fb2f0d6 commit 6e23554

File tree

1 file changed

+49
-20
lines changed

1 file changed

+49
-20
lines changed

gdax/public_client.py

Lines changed: 49 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -119,30 +119,59 @@ 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):
122+
def get_product_trades(self, product_id, before='', after='', limit='', result=[]):
123123
"""List the latest trades for a product.
124-
125124
Args:
126-
product_id (str): Product
127-
125+
product_id (str): Product
126+
before (Optional[str]): start time in ISO 8601
127+
after (Optional[str]): end time in ISO 8601
128+
limit (Optional[int]): the desired number of trades (can be more than 100,
129+
automatically paginated)
130+
results (Optional[list]): list of results that is used for the pagination
128131
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-
}]
132+
list: Latest trades. Example::
133+
[{
134+
"time": "2014-11-07T22:19:28.578544Z",
135+
"trade_id": 74,
136+
"price": "10.00000000",
137+
"size": "0.01000000",
138+
"side": "buy"
139+
}, {
140+
"time": "2014-11-07T01:08:43.642366Z",
141+
"trade_id": 73,
142+
"price": "100.00000000",
143+
"size": "0.01000000",
144+
"side": "sell"
145+
}]
146+
""""
147+
url = self.url + '/products/{}/trades'.format(str(product_id))
148+
params = {}
143149

144-
"""
145-
return self._get('/products/{}/trades'.format(str(product_id)))
150+
if before:
151+
params['before'] = str(before)
152+
if after:
153+
params['after'] = str(after)
154+
if limit and limit < 100:
155+
# the default limit is 100
156+
# we only add it if the limit is less than 100
157+
params['limit'] = limit
158+
159+
r = requests.get(url, params=params)
160+
# r.raise_for_status()
161+
162+
result.extend(r.json())
163+
164+
if 'cb-after' in r.headers and limit is not len(result):
165+
# update limit
166+
limit -= len(result)
167+
if limit <= 0:
168+
return result
169+
170+
# TODO: need a way to ensure that we don't get rate-limited/blocked
171+
# time.sleep(0.4)
172+
return self.get_product_trades(product_id=product_id, after=r.headers['cb-after'], limit=limit, result=result)
173+
174+
return result
146175

147176
def get_product_historic_rates(self, product_id, start=None, end=None,
148177
granularity=None):

0 commit comments

Comments
 (0)