-
Notifications
You must be signed in to change notification settings - Fork 0
/
gdax_priv.py
304 lines (241 loc) · 9.47 KB
/
gdax_priv.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
import numpy as np
import GDAX
from dateutil import parser
import time
import eth.num_utils as num_utils
class Account():
"""
Represents a single user account.
"""
def __init__(self, msg):
self.original_msg = msg
if('message' in msg):
self.isError = True
self.message = msg['message']
else:
self.id = msg['id']
self.currency = msg['currency']
self.balance = float(msg['balance'])
self.available = float(msg['available'])
self.hold = float(msg['hold'])
self.profile_id = msg['profile_id']
def __str__(self):
return "(%s) Balance: %f\tAvailable: %f\t [%s]" % (self.currency, self.balance, self.available, self.id)
def __repr__(self):
return self.__str__()
class AccountHistoryItem():
"""Represents a single record in an account history"""
def __init__(self, msg):
self.original_msg = msg
if('message' in msg):
self.isError = True
self.message = msg['message']
else:
self.id = msg['id']
self.created_at = parser.parse(msg['created_at'])
self.amount = float(msg['amount'])
self.balance = float(msg['balance'])
self.type = msg['type']
self.details = msg.get('details')
def __str__(self):
return "(%s) %s: %f\t [%s]" % (self.created_at, self.type, self.amount, self.id)
def __repr__(self):
return self.__str__()
class AccountHholdItem():
"""Represents a single account hold"""
def __init__(self, msg):
self.original_msg = msg
self.id = msg['id']
self.account_id = msg['account_id']
self.created_at = parser.parse(msg['created_at'])
self.updated_at = parser.parse(msg['updated_at'])
self.amount = float(msg['amount'])
self.type = msg['type']
self.ref = msg['ref']
def __str__(self):
return "(%s) %s: %f\t [%s]" % (self.created_at, self.type, self.amount, self.id)
def __repr__(self):
return self.__str__()
class LimitOrderParams():
def __init__(self, price, size, side = 'buy', product_id = 'ETH-USD'):
self.side = side
self.product_id = product_id
self.size = size
self.price = price
self.post_only = True
def ToDict(self):
res = dict()
res['side'] = self.side
res['product_id'] = self.product_id
res['type'] = 'limit'
res['size'] = "%.6f" % self.size
res['price'] = "%.2f" % self.price
res['post_only'] = self.post_only
return res;
def __str__(self):
return "%s: %f ETH @ $%f (limit)" % (self.side, self.size, self.price)
def __repr__(self):
return self.__str__()
class MarketOrderParams():
def __init__(self, funds = None, size = None, side = 'buy', product_id = 'ETH-USD'):
self.side = side
self.product_id = product_id
if((not size and not funds) or (size and funds)):
raise "Error: only size xor funds may be specified."
if(size):
self.size = size
elif(funds):
self.funds = funds
def ToDict(self):
res = dict()
res['side'] = self.side
res['product_id'] = self.product_id
res['type'] = 'market'
if('size' in self.__dict__):
res['size'] = self.size
else:
res['funds'] = self.funds
return res;
def __str__(self):
if('size' in self.__dict__):
return "%s: %f ETH (market)" % (self.side, self.size)
else:
return "%s: $%f (market)" % (self.side, self.funds)
def __repr__(self):
return self.__str__()
class StopOrderParams():
def __init__(self, price, size = None, funds = None, side = 'buy', product_id = 'ETH-USD'):
self.side = side
self.product_id = product_id
self.price = price
if((not size and not funds) or (size and funds)):
raise "Error: only size xor funds may be specified."
if(size):
self.size = size
elif(funds):
self.funds = funds
def ToDict(self):
res = dict()
res['side'] = self.side
res['product_id'] = self.product_id
res['type'] = 'stop'
res['price'] = self.price
if('size' in self.__dict__):
res['size'] = self.size
else:
res['funds'] = self.funds
return res;
def __str__(self):
if('size' in self.__dict__):
return "%s: %f ETH @ %f (stop)" % (self.side, self.size, self.price)
else:
return "%s: $%f @ %f(stop)" % (self.side, self.funds, self.price)
def __repr__(self):
return self.__str__()
class OrderResponse():
def __init__(self, msg):
self.original_msg = msg
if('message' in msg):
self.isError = True
self.message = msg['message']
else:
self.isError = False
self.id = msg['id']
self.price = float(msg['price'])
self.size = float(msg['size'])
self.product_id = msg['product_id']
self.side = msg['side']
self.stp = msg.get('stp')
self.type = msg['type']
self.time_in_force = msg.get('time_in_force')
self.post_only = bool(msg.get('post_only'))
self.created_at = parser.parse(msg['created_at'])
self.fill_fees = float(msg['fill_fees'])
self.filled_size = float(msg['filled_size'])
self.executed_value = float(msg['executed_value'])
self.status = msg['status']
self.settled = bool(msg['settled'])
def __str__(self):
if(self.isError):
return "Error: %s" % self.message
else:
return "(%s) %s: %f ETH @ $%f\t(%s)" % (self.type, self.side, self.size, self.price, self.status)
def __repr__(self):
return self.__str__()
class Fill():
def __init__(self, msg):
self.original_msg = msg
self.trade_id = (msg['trade_id'])
self.product_id = (msg['product_id'])
self.price = float(msg['price'])
self.size = float(msg['size'])
self.order_id = (msg['order_id'])
self.created_at = parser.parse(msg['created_at'])
self.liquidity = (msg['liquidity'])
self.fee = float(msg['fee'])
self.settled = bool(msg['settled'])
self.side = (msg['side'])
def __str__(self):
return "%s: %f ETH @ $%f" % (self.side, self.size, self.price)
def __repr__(self):
return self.__str__()
class Trade():
def __init__(self, msg):
self.original_msg = msg
self.price = float(msg['price'])
self.side = msg['side']
self.size = float(msg['size'])
self.time = parser.parse(msg['time'])
self.trade_id = msg['trade_id']
def __str__(self):
return "(%s) %f ETH @ $%f" % (self.time, self.size, self.price)
def __repr__(self):
return self.__str__()
class Ticker():
def __init__(self, msg):
self.original_msg = msg
self.ask = float(msg['ask'])
self.buy = float(msg['bid'])
self.price = float(msg['price'])
self.volume = float(msg['volume'])
self.size = float(msg['size'])
self.time = parser.parse(msg['time'])
self.trade_id = msg['trade_id']
def __str__(self):
return "%s: $%f (A/B: $%f/$%f) (V: %f)" % (self.time, self.price, self.ask, self.buy, self.volume)
def __repr__(self):
return self.__str__()
#
class RateInfo():
def __init__(self, msg):
self.time = int(msg[0])
self.low = float(msg[1])
self.high = float(msg[2])
self.open = float(msg[3])
self.close = float(msg[4])
self.volume = float(msg[5])
def __str__(self):
return "H:%f|L:%f|O:%f|C:%f|V:%f (%s)" % (self.high, self.low, self.open, self.close, self.volume, self.time)
def __repr__(self):
return self.__str__()
class SimpleOrder():
def __init__(self, msg = None, level = 2):
self.price = float(msg[0]) if msg else 0.0
self.size = float(msg[1]) if msg else 0.0
if(level == 2):
self.num_orders = int(msg[2]) if msg else 0
if(level == 3):
self.order_id = msg[2] if msg else None
class OrderBook():
def __init__(self, msg):
self.original_msg = msg
self.sequence = int(msg['sequence'])
self.bids = [SimpleOrder(x, level=2) for x in msg['bids']]
self.asks = [SimpleOrder(x, level=2) for x in msg['asks']]
def getBestPrices(self):
"""Returns best bid/ask"""
return (np.max([x.price for x in self.bids]),
np.min([x.price for x in self.asks]))
def getMidMarketPrice(self):
return (np.min([x.price for x in self.asks])
+ np.max([x.price for x in self.bids])) / 2.0