Skip to content

Commit

Permalink
Merge pull request #279 from tamersaadeh/patch-1
Browse files Browse the repository at this point in the history
 Allow get_product_trades() to be paginated
  • Loading branch information
danpaquin authored Mar 28, 2018
2 parents d7dfc6f + fb2f0d6 commit 6e23554
Showing 1 changed file with 49 additions and 20 deletions.
69 changes: 49 additions & 20 deletions gdax/public_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down

0 comments on commit 6e23554

Please sign in to comment.