-
Notifications
You must be signed in to change notification settings - Fork 1
/
gdax.py
executable file
·369 lines (326 loc) · 10.5 KB
/
gdax.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#!/usr/bin/python3
import sys
import json
import hmac, hashlib, time, base64
import requests
from requests.auth import AuthBase
from decimal import Decimal, ROUND_DOWN
API_URL = 'https://api.gdax.com/'
#ANSI Colors
colors = {
'black': "\x1b[30m",
'red': "\x1b[31m",
'green': "\x1b[32m",
'yellow': "\x1b[33m",
'blue': "\x1b[34m",
'magenta': "\x1b[35m",
'cyan': "\x1b[36m",
'white': "\x1b[37m",
'reset': "\x1b[0m",
'clear': "\x1b[2J"
}
# Global auth object
auth = None
# JSON serializer for Decimal
def decimal_default(obj):
if isinstance(obj, Decimal):
return str(obj)
raise TypeError
# Custom authentication for GDAX
class GDAXAuth(AuthBase):
def __init__(self, api_key, secret_key, passphrase):
self.api_key = api_key
self.secret_key = secret_key
self.passphrase = passphrase
def __call__(self, request):
timestamp = str(time.time())
message = timestamp + request.method + request.path_url + (request.body or '')
hmac_key = base64.b64decode(self.secret_key)
signature = hmac.new(hmac_key, message.encode('ascii'), hashlib.sha256)
signature_b64 = base64.b64encode(signature.digest())
request.headers.update({
'CB-ACCESS-SIGN': signature_b64,
'CB-ACCESS-TIMESTAMP': timestamp,
'CB-ACCESS-KEY': self.api_key,
'CB-ACCESS-PASSPHRASE': self.passphrase,
'Content-Type': 'application/json'
})
return request
# API call wrapper function
def api(endpoint, params=None, delete=False, notFoundOK=False):
if(params != None):
r = requests.post(API_URL + endpoint, auth=auth, data=json.dumps(params, default=decimal_default), headers={"Content-Type": "text/javascript"})
elif(delete):
r = requests.delete(API_URL + endpoint, auth=auth)
else:
r = requests.get(API_URL + endpoint, auth=auth)
if(r.status_code == 200 or (notFoundOK and r.status_code == 404)):
if(r.text == ''):
return {}
else:
return r.json()
else:
message = 'Error getting data from API: ' + endpoint + '\n'
try:
message += 'Response: ' + r.json()['message'] + '\n'
except(ValueError):
pass
message += 'Params: ' + (json.dumps(params, default=decimal_default, indent=4) if params != None else 'None') + '\n'
message += 'Raw: ' + r.text + '\n'
print(message)
return None
# Get current market ticker
def getTicker(silent=True):
tick = api('products/BTC-USD/ticker')
if(not silent):
print('Market price: {:.2f}'.format(Decimal(tick['price'])))
return tick
# Watch the market ticker
def watchTicker():
last = Decimal(0.0)
while(True):
tick = Decimal(getTicker()['price'])
if(tick > last):
print('Market price: {}{:.2f}{}'.format(colors['green'], tick, colors['reset']))
elif(tick < last):
print('Market price: {}{:.2f}{}'.format(colors['red'], tick, colors['reset']))
else:
print('Market price: {:.2f}'.format(tick))
last = tick
return
# Get the full order book
def getOrderBook(silent=True, clear=False):
book = api('products/BTC-USD/book?level=2')
if(silent):
return book
if(clear):
print(colors['clear'])
spreadstr = 'Spread: {}${:.2f}{}'.format(
colors['yellow'],
Decimal(book['asks'][0][0]) - Decimal(book['bids'][0][0]),
colors['reset']
)
mmpstr = 'Mid-Market Price: {}${:.2f}{}'.format(
colors['yellow'],
(Decimal(book['asks'][0][0]) + Decimal(book['bids'][0][0])) / 2,
colors['reset']
)
padding = 40 - len(spreadstr)
print(spreadstr + (' ' * padding) + mmpstr)
print('')
bidtotal = Decimal(0.0)
asktotal = Decimal(0.0)
for i in range (0, 24):
bidtotal += Decimal(book['bids'][i][1])
asktotal += Decimal(book['asks'][i][1])
total = bidtotal + asktotal
print((' ' * 8) + 'Bid' + (' ' * 28) + 'Ask')
for i in range(0, 24):
bidstr = '{}${:.2f}{}: {:.8f} {}'.format(
colors['green'],
Decimal(book['bids'][i][0]),
colors['reset'],
Decimal(book['bids'][i][1]),
'\u2588' * min(int(64 * Decimal(book['bids'][i][1]) / total), 8)
)
askstr = '{}${:.2f}{}: {:.8f} {}'.format(
colors['red'],
Decimal(book['asks'][i][0]),
colors['reset'],
Decimal(book['asks'][i][1]),
'\u2588' * min(int(64 * Decimal(book['asks'][i][1]) / total), 8)
)
padding = 40 - len(bidstr)
print(bidstr + (' ' * padding) + askstr)
bidtotalstr = 'Total: {:.8f}'.format(bidtotal)
asktotalstr = 'Total: {:.8f}'.format(asktotal)
padding = 27 - len(bidtotalstr)
dirstr = '>>>>' if bidtotal > asktotal else '<<<<'
print(bidtotalstr + (' ' * int(padding/2)) + dirstr + (' ' * int(padding - padding/2)) + asktotalstr)
return book
# Watch live order book updates
def watchOrderBook():
while(True):
getOrderBook(silent=False, clear=True)
time.sleep(1)
return
# Get account balances
def getAccounts(silent=True):
out = {};
accounts = api('accounts');
for account in accounts:
if(account['currency'] == 'BTC' or account['currency'] == 'USD'):
if(not silent):
print('{}: {:.8f}'.format(account['currency'], Decimal(account['balance'])))
out[account['currency']] = Decimal(account['balance'])
return out
# Get all current orders
def getOrderList(silent=True):
orders = api('orders?status=open')
if(not silent):
for i, order in enumerate(orders):
print('{}: {} ({}): {} {} {:.8f}BTC at ${:.2f}'.format(
i,
order['id'],
order['status'],
order['type'],
order['side'],
Decimal(order['size']),
Decimal(order['price'])
))
return orders
# Get an order and check if it order has completed
def getOrder(oid, silent=True):
order = api('orders/' + oid, notFoundOK=True)
if(order == None or ('message' in order and order['message'] == 'NotFound')):
if(not silent):
print('Order not found')
return None
#Order completed
if(not silent and (order['status'] == 'done' or order['status'] == 'settled')):
verb = 'Sold' if order['side'] == 'sell' else 'Bought'
print(verb + '{:.8f} BTC at ${:.2f}'.format(Decimal(order['filled_size']), order['funds']))
#Order not placed
elif(not silent and order['status'] == 'rejected'):
print('Order was rejected')
#Error with order
elif(not silent and order['status'] != 'open' and order['status'] != 'pending'):
print('Error processing order (status: ' + order['status'] + ')')
#Order pending
elif(not silent):
print('{} {} {:.8f}BTC at ${:.2f} (pending)'.format(
order['type'],
order['side'],
Decimal(order['size']),
Decimal(order['price'])
))
return order
# Place an order
def placeOrder(otype, side, size, price, silent=False):
if(otype == 'market'):
order = {
'product_id': 'BTC-USD',
'type': otype,
'side': side,
'size': '{:.8f}'.format(Decimal(size)),
}
if(not silent):
action = input('Place {} {} order for {:.8f} BTC at $MKT (y/N)? '.format(
order['type'],
order['side'],
Decimal(order['size']),
))
else:
order = {
'product_id': 'BTC-USD',
'type': otype,
'side': side,
'size': '{:.8f}'.format(Decimal(size)),
'price': '{:.8f}'.format(Decimal(price)),
'post_only': True
}
if(not silent):
action = input('Place {} {} order for {:.8f} BTC at ${:.2f}/coin (y/N)? '.format(
order['type'],
order['side'],
Decimal(order['size']),
Decimal(order['price'])
))
if(action.lower() == 'y' or silent):
result = api('orders', order)
else:
result = None
if(not silent):
if(result == None):
print('Failed to place order!')
else:
print('Order placed successfully (ID ' + result['id'] + ')')
return result
# Cancel an order
def cancelOrder(oid, silent=True):
order = getOrder(oid)
if(order == None):
if(not silent):
print('Order does not exist')
return None
result = api('orders/' + oid, delete=True, notFoundOK=True)
if(not silent):
if(result == {} or result[0] == oid):
print('Cancelled {} {} order for {:.8f} BTC at ${:.2f}/coin'.format(
order['type'],
order['side'],
Decimal(order['size']),
Decimal(order['price'])
))
else:
print('Failed to cancel order!')
return result
# Watch the status of an order
def watchOrder(oid, silent=True):
order = getOrder(oid, silent)
while(order != None and (order['status'] == 'open' or order['status'] == 'pending')):
time.sleep(1)
order = getOrder(oid, silent)
return
# Program entry point
def main(argv):
global auth
authfile = open(sys.path[0] + '/auth.json', 'r')
authdata = json.load(authfile)
authfile.close()
auth = GDAXAuth(authdata['API_KEY'], authdata['API_SECRET'], authdata['API_PASS'])
if(len(argv) == 3 and len(argv[2]) == 1):
oid = getOrderList()[int(argv[2])]['id']
argv[2] = oid
# Main program loop
if(len(argv) < 2):
help()
elif(argv[1] == 'ticker'):
getTicker(silent=False)
elif(argv[1] == 'orderbook'):
getOrderBook(silent=False)
elif(argv[1] == 'balance'):
getAccounts(silent=False)
elif(argv[1] == 'orders'):
getOrderList(silent=False)
elif(argv[1] == 'order' and len(argv) == 3):
getOrder(argv[2], silent=False)
elif(argv[1] == 'watch' and len(argv) == 3):
watchOrder(argv[2], silent=False)
elif(argv[1] == 'buy' or argv[1] == 'sell' and len(argv) == 4):
placeOrder('market', argv[1], argv[2], argv[3], silent=False)
elif(argv[1] == 'market' and len(argv) == 4):
placeOrder('market', argv[2], argv[3], None, silent=False)
elif((argv[1] == 'limit' or argv[1] == 'stop') and len(argv) == 5):
placeOrder(argv[1], argv[2], argv[3], argv[4], silent=False)
elif(argv[1] == 'cancel' and len(argv) == 3):
cancelOrder(argv[2], silent=False)
elif(argv[1] == 'live'):
watchOrderBook()
elif(argv[1] == 'liveticker'):
watchTicker()
else:
help()
return
# Print usage instructions
def help():
print("Usage: gdax <command> [arguments]")
print(" ticker Get current market ticker")
print(" orderbook Get current order book data")
print(" balance Get current account balance")
print(" orders Get list of existing orders")
print(" order [order ID] Get details of existing order")
print(" watch [order ID] Watch order for completion")
print(" buy/sell [BTC amount] Market buy/sell BTC")
print(" market buy/sell [BTC amount] Alias for buy/sell")
print(" limit buy/sell [BTC amount] [limit price] Limit buy/sell BTC")
print(" stop buy/sell [BTC amount] [stop price] Stop buy/sell BTC")
print(" live Live stream of order book data")
print(" liveticker Live stream of ticker data")
return
#Shell entry point
if(__name__ == '__main__'):
try:
main(sys.argv)
except KeyboardInterrupt:
sys.exit(0)