Skip to content

Commit

Permalink
Merge branch 'master' into Format-method
Browse files Browse the repository at this point in the history
  • Loading branch information
danpaquin authored Jun 3, 2017
2 parents f547d19 + 10087d2 commit 9ce4c94
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 36 deletions.
22 changes: 12 additions & 10 deletions GDAX/AuthenticatedClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,20 @@ def holdsPagination(self, accountId, list, after):
self.holdsPagination(accountId, list, r.headers["cb-after"])
return list

def buy(self, buyParams):
buyParams["side"] = "buy"
if not buyParams["product_id"]:
buyParams["product_id"] = self.productId
r = requests.post(self.url + '/orders', data=json.dumps(buyParams), auth=self.auth)
#r.raise_for_status()
def buy(self, **kwargs):
kwargs["side"] = "buy"
if not "product_id" in kwargs:
kwargs["product_id"] = self.productId
r = requests.post(self.url + '/orders',
data=json.dumps(kwargs),
auth=self.auth)
return r.json()

def sell(self, sellParams):
sellParams["side"] = "sell"
r = requests.post(self.url + '/orders', data=json.dumps(sellParams), auth=self.auth)
#r.raise_for_status()
def sell(self, **kwargs):
kwargs["side"] = "sell"
r = requests.post(self.url + '/orders',
data=json.dumps(kwargs),
auth=self.auth)
return r.json()

def cancelOrder(self, orderId):
Expand Down
19 changes: 11 additions & 8 deletions GDAX/OrderBook.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@


class OrderBook(WebsocketClient):

def __init__(self, product_id='BTC-USD'):
WebsocketClient.__init__(self, products=product_id)
self._asks = RBTree()
Expand Down Expand Up @@ -44,8 +43,10 @@ def onMessage(self, message):
self._sequence = res['sequence']

if sequence <= self._sequence:
return #ignore old messages
# ignore older messages (e.g. before order book initialization from getProductOrderBook)
return
elif sequence > self._sequence + 1:
print('Error: messages missing ({} - {}). Re-initializing websocket.'.format(sequence, self._sequence))
self.close()
self.start()
return
Expand Down Expand Up @@ -73,10 +74,10 @@ def onMessage(self, message):

def add(self, order):
order = {
'id': order['order_id'] if 'order_id' in order else order['id'],
'id': order.get('order_id') or order['id'],
'side': order['side'],
'price': Decimal(order['price']),
'size': Decimal(order.get('size', order['remaining_size']))
'size': Decimal(order.get('size') or order['remaining_size'])
}
if order['side'] == 'buy':
bids = self.get_bids(order['price'])
Expand Down Expand Up @@ -163,10 +164,11 @@ def change(self, order):
return

def get_current_book(self):
result = dict()
result['sequence'] = self._sequence
result['asks'] = list()
result['bids'] = list()
result = {
'sequence': self._sequence,
'asks': [],
'bids': [],
}
for ask in self._asks:
try:
# There can be a race condition here, where a price point is removed
Expand Down Expand Up @@ -223,6 +225,7 @@ def set_bids(self, price, bids):

if __name__ == '__main__':
import time

order_book = OrderBook()
order_book.start()
time.sleep(10)
Expand Down
6 changes: 3 additions & 3 deletions GDAX/WebsocketClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ def onOpen(self):
self.url = "wss://ws-feed.gdax.com/"
self.products = ["BTC-USD", "ETH-USD"]
self.MessageCount = 0
print ("Lets count the messages!")
print("Let's count the messages!")

def onMessage(self, msg):
if 'price' in msg and 'type' in msg:
print ("Message type:", msg["type"], "\t@ {}.3f".format(float(msg["price"])))
print("Message type:", msg["type"], "\t@ %.3f" % float(msg["price"]))
self.MessageCount += 1

def onClose(self):
Expand All @@ -103,7 +103,7 @@ def onClose(self):
print(wsClient.url, wsClient.products)
# Do some logic with the data
while (wsClient.MessageCount < 500):
print ("\nMessageCount =", "{} \n".format(wsClient.MessageCount))
print("\nMessageCount =", "%i \n" % wsClient.MessageCount)
time.sleep(1)

wsClient.close()
31 changes: 16 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,12 +96,12 @@ Only available for the `PublicClient`, you may pass any function above raw JSON
import GDAX
publicClient = GDAX.PublicClient()

method1 = public.getProductHistoricRates(granularity='3000')
method1 = publicClient.getProductHistoricRates(granularity='3000')

params = {
'granularity': '3000'
}
method2 = public.getProductHistoricRates(params)
method2 = publicClient.getProductHistoricRates(params)

# Both methods will send the same request, but not always return the same data if run in series.
print (method1, method2)
Expand All @@ -110,7 +110,14 @@ print (method1, method2)


### Authenticated Client
Not all API endpoints are available to everyone. Those requiring user authentication can be reached using ```AuthenticatedClient```. You must setup API access within your [account settings](https://www.gdax.com/settings/api). The ```AuthenticatedClient``` inherits all methods from the ```PrivateClient``` class, so you will only need to initialize one if you are planning to integrate both into your script.

Not all API endpoints are available to everyone.
Those requiring user authentication can be reached using `AuthenticatedClient`.
You must setup API access within your
[account settings](https://www.gdax.com/settings/api).
The `AuthenticatedClient` inherits all methods from the `PublicClient`
class, so you will only need to initialize one if you are planning to
integrate both into your script.

```python
import GDAX
Expand Down Expand Up @@ -154,21 +161,15 @@ authClient.getAccountHolds("7d0f7d8e-dd34-4d9c-a846-06f431c381ba")
- [buy & sell](https://docs.gdax.com/#place-a-new-order)
```python
# Buy 0.01 BTC @ 100 USD
buyParams = {
'price': '100.00', #USD
'size': '0.01', #BTC
'product_id': 'BTC-USD'
}
authClient.buy(buyParams)
authClient.buy(price='100.00', #USD
size='0.01', #BTC
product_id='BTC-USD')
```
```python
# Sell 0.01 BTC @ 200 USD
sellParams = {
'price': '200.00', #USD
'size': '0.01', #BTC
#product_id not needed if default is desired
}
authClient.sell(sellParams)
authClient.sell(price='200.00', #USD
size='0.01', #BTC
product_id='BTC-USD')
```

- [cancelOrder](https://docs.gdax.com/#cancel-an-order)
Expand Down
1 change: 1 addition & 0 deletions contributors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ Daniel J Paquin
Leonard Lin
Jeff Gibson
David Caseria
Paul Mestemaker

0 comments on commit 9ce4c94

Please sign in to comment.