forked from mcardillo55/cbpro-trader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
engine.py
396 lines (362 loc) · 18.8 KB
/
engine.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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#
# engine.py
# Mike Cardillo
#
# Subsystem containing all trading logic and execution
import time
import cbpro
import threading
import logging
import datetime
from decimal import *
class OrderBookCustom(cbpro.OrderBook):
def __init__(self, product_id='BTC-USD'):
self.logger = logging.getLogger('trader-logger')
self.error_logger = logging.getLogger('error-logger')
super(OrderBookCustom, self).__init__(product_id=product_id)
def is_ready(self):
try:
super(OrderBookCustom, self).get_ask()
except (ValueError, AttributeError):
return False
return True
def get_ask(self):
while not self.is_ready():
time.sleep(0.01)
return super(OrderBookCustom, self).get_ask()
def get_bid(self):
while not self.is_ready():
time.sleep(0.01)
return super(OrderBookCustom, self).get_bid()
class Product(object):
def __init__(self, auth_client, product_id='BTC-USD'):
self.product_id = product_id
self.order_book = OrderBookCustom(product_id=product_id)
self.order_in_progress = False
self.buy_flag = False
self.sell_flag = False
self.open_orders = []
self.order_thread = None
self.meta = True
self.last_signal_switch = time.time()
cbpro_products = auth_client.get_products()
while not isinstance(cbpro_products, list):
# May be rate limited
time.sleep(3)
cbpro_products = auth_client.get_products()
for cbpro_product in cbpro_products:
if product_id == cbpro_product.get('id'):
self.meta = False # If product_id is in response, it must be a real product
self.quote_increment = cbpro_product.get('quote_increment')
self.min_size = cbpro_product.get('base_min_size')
class TradeEngine():
def __init__(self, auth_client, product_list=['BTC-USD', 'ETH-USD', 'LTC-USD'], fiat='USD', is_live=False, max_slippage=Decimal('0.10')):
self.logger = logging.getLogger('trader-logger')
self.error_logger = logging.getLogger('error-logger')
self.auth_client = auth_client
self.product_list = product_list
self.fiat_currency = fiat
self.is_live = is_live
self.products = []
self.stop_update_order_thread = False
self.last_order_update = time.time()
for product in self.product_list:
self.products.append(Product(auth_client, product_id=product))
self.last_balance_update = 0
self.update_amounts()
self.fiat_equivalent = 0
self.last_balance_update = time.time()
self.max_slippage = max_slippage
self.update_order_thread = threading.Thread(target=self.update_orders, name='update_orders')
self.update_order_thread.start()
def close(self, exit=False):
if exit:
self.stop_update_order_thread = True
for product in self.products:
# Setting both flags will close any open order threads
product.buy_flag = False
product.sell_flag = False
# Cancel any orders that may still be remaining
product.order_in_progress = False
try:
self.auth_client.cancel_all()
except Exception:
self.error_logger.exception(datetime.datetime.now())
def get_product_by_product_id(self, product_id='BTC-USD'):
for product in self.products:
if product.product_id == product_id:
return product
return None
def update_orders(self):
while not self.stop_update_order_thread:
need_updating = False
for product in self.products:
if product.order_in_progress:
need_updating = True
if need_updating and time.time() - self.last_order_update >= 1.0:
try:
ret = self.auth_client.get_orders()
while not isinstance(ret[0], list):
# May be rate limited, sleep to cool down
time.sleep(3)
ret = self.auth_client.get_orders()
for product in self.products:
product.open_orders = []
for order in ret[0]:
self.get_product_by_product_id(order.get('product_id')).open_orders.append(order)
self.logger.debug(self.get_product_by_product_id(order.get('product_id')).open_orders)
self.last_order_update = time.time()
except Exception:
self.error_logger.exception(datetime.datetime.now())
time.sleep(0.01)
def round_fiat(self, money):
return Decimal(money).quantize(Decimal('.01'), rounding=ROUND_DOWN)
def round_coin(self, money):
return Decimal(money).quantize(Decimal('.00000001'), rounding=ROUND_DOWN)
def update_amounts(self):
if time.time() - self.last_balance_update > 1.0:
try:
self.last_balance_update = time.time()
ret = self.auth_client.get_accounts()
if isinstance(ret, list):
for account in ret:
if account.get('currency') == 'BTC':
self.btc = self.round_coin(account.get('available'))
elif account.get('currency') == 'BCH':
self.bch = self.round_coin(account.get('available'))
elif account.get('currency') == 'ETH':
self.eth = self.round_coin(account.get('available'))
elif account.get('currency') == 'LTC':
self.ltc = self.round_coin(account.get('available'))
elif account.get('currency') == self.fiat_currency:
self.fiat = self.round_fiat(account.get('available'))
except Exception:
self.error_logger.exception(datetime.datetime.now())
self.btc = Decimal('0.0')
self.bch = Decimal('0.0')
self.eth = Decimal('0.0')
self.ltc = Decimal('0.0')
self.fiat = Decimal('0.0')
self.fiat_equivalent = Decimal('0.0')
return
self.fiat_equivalent = Decimal('0.0')
for product in self.products:
if not product.meta and product.order_book.get_current_ticker() and product.order_book.get_current_ticker().get('price'):
self.fiat_equivalent += self.get_base_currency_from_product_id(product.product_id, update=False) * Decimal(product.order_book.get_current_ticker().get('price'))
self.fiat_equivalent += self.fiat
def print_amounts(self):
self.logger.debug("[BALANCES] %s: %.2f BTC: %.8f" % (self.fiat_currency, self.fiat, self.btc))
def place_buy(self, product=None, partial='1.0'):
amount = self.get_quoted_currency_from_product_id(product.product_id) * Decimal(partial)
bid = product.order_book.get_ask() - Decimal(product.quote_increment)
amount = self.round_coin(Decimal(amount) / Decimal(bid))
if amount < Decimal(product.min_size):
amount = self.get_quoted_currency_from_product_id(product.product_id)
bid = product.order_book.get_ask() - Decimal(product.quote_increment)
amount = self.round_coin(Decimal(amount) / Decimal(bid))
if amount >= Decimal(product.min_size):
self.logger.debug("Placing buy... Price: %.8f Size: %.8f" % (bid, amount))
ret = self.auth_client.buy(type='limit', size=str(amount),
price=str(bid), post_only=True,
product_id=product.product_id)
if ret.get('status') == 'pending' or ret.get('status') == 'open':
product.open_orders.append(ret)
return ret
else:
ret = {'status': 'done'}
return ret
def buy(self, product=None, amount=None):
product.order_in_progress = True
last_order_update = 0
starting_price = product.order_book.get_ask() - Decimal(product.quote_increment)
try:
ret = self.place_buy(product=product, partial='0.5')
bid = ret.get('price')
amount = self.get_quoted_currency_from_product_id(product.product_id)
while product.buy_flag and (amount >= Decimal(product.min_size) or len(product.open_orders) > 0):
if (((product.order_book.get_ask() - Decimal(product.quote_increment)) / starting_price) - Decimal('1.0')) * Decimal('100.0') > self.max_slippage:
self.auth_client.cancel_all(product_id=product.product_id)
self.auth_client.buy(type='market', funds=str(self.get_quoted_currency_from_product_id(product.product_id)), product_id=product.product_id)
product.order_in_progress = False
return
if ret.get('status') == 'rejected' or ret.get('status') == 'done' or ret.get('message') == 'NotFound':
ret = self.place_buy(product=product, partial='0.5')
bid = ret.get('price')
elif not bid or Decimal(bid) < product.order_book.get_ask() - Decimal(product.quote_increment):
if len(product.open_orders) > 0:
ret = self.place_buy(product=product, partial='1.0')
else:
ret = self.place_buy(product=product, partial='0.5')
for order in product.open_orders:
if order.get('id') != ret.get('id'):
self.auth_client.cancel_order(order.get('id'))
bid = ret.get('price')
if ret.get('id') and time.time() - last_order_update >= 1.0:
try:
ret = self.auth_client.get_order(ret.get('id'))
last_order_update = time.time()
except ValueError:
self.error_logger.exception(datetime.datetime.now())
pass
amount = self.get_quoted_currency_from_product_id(product.product_id)
time.sleep(0.01)
self.auth_client.cancel_all(product_id=product.product_id)
amount = self.get_quoted_currency_from_product_id(product.product_id)
except Exception:
product.order_in_progress = False
self.error_logger.exception(datetime.datetime.now())
self.auth_client.cancel_all(product_id=product.product_id)
product.order_in_progress = False
def place_sell(self, product=None, partial='1.0'):
amount = self.round_coin(self.get_base_currency_from_product_id(product.product_id) * Decimal(partial))
if amount < Decimal(product.min_size):
amount = self.get_base_currency_from_product_id(product.product_id)
ask = product.order_book.get_bid() + Decimal(product.quote_increment)
if amount >= Decimal(product.min_size):
self.logger.debug("Placing sell... Price: %.2f Size: %.8f" % (ask, amount))
ret = self.auth_client.sell(type='limit', size=str(amount),
price=str(ask), post_only=True,
product_id=product.product_id)
if ret.get('status') == 'pending' or ret.get('status') == 'open':
product.open_orders.append(ret)
return ret
else:
ret = {'status': 'done'}
return ret
def sell(self, product=None, amount=None):
product.order_in_progress = True
last_order_update = 0
starting_price = product.order_book.get_bid() + Decimal(product.quote_increment)
try:
ret = self.place_sell(product=product, partial='0.5')
ask = ret.get('price')
amount = self.get_base_currency_from_product_id(product.product_id)
while product.sell_flag and (amount >= Decimal(product.min_size) or len(product.open_orders) > 0):
if (Decimal('1') - ((product.order_book.get_bid() + Decimal(product.quote_increment)) / starting_price)) * Decimal('100.0') > self.max_slippage:
self.auth_client.cancel_all(product_id=product.product_id)
self.auth_client.sell(type='market', size=str(self.get_base_currency_from_product_id(product.product_id)), product_id=product.product_id)
product.order_in_progress = False
return
if ret.get('status') == 'rejected' or ret.get('status') == 'done' or ret.get('message') == 'NotFound':
ret = self.place_sell(product=product, partial='0.5')
ask = ret.get('price')
elif not ask or Decimal(ask) > product.order_book.get_bid() + Decimal(product.quote_increment):
if len(product.open_orders) > 0:
ret = self.place_sell(product=product, partial='1.0')
else:
ret = self.place_sell(product=product, partial='0.5')
for order in product.open_orders:
if order.get('id') != ret.get('id'):
self.auth_client.cancel_order(order.get('id'))
ask = ret.get('price')
if ret.get('id') and time.time() - last_order_update >= 1.0:
try:
ret = self.auth_client.get_order(ret.get('id'))
except ValueError:
self.error_logger.exception(datetime.datetime.now())
pass
last_order_update = time.time()
amount = self.get_base_currency_from_product_id(product.product_id)
time.sleep(0.01)
self.auth_client.cancel_all(product_id=product.product_id)
amount = self.get_base_currency_from_product_id(product.product_id)
except Exception:
product.order_in_progress = False
self.error_logger.exception(datetime.datetime.now())
self.auth_client.cancel_all(product_id=product.product_id)
product.order_in_progress = False
def get_base_currency_from_product_id(self, product_id, update=True):
if update:
self.update_amounts()
if product_id == 'BTC-USD':
return self.btc
elif product_id == 'BCH-USD':
return self.bch
elif product_id == 'BCH-EUR':
return self.bch
elif product_id == 'BCH-BTC':
return self.bch
elif product_id == 'BTC-EUR':
return self.btc
elif product_id == 'ETH-USD':
return self.eth
elif product_id == 'ETH-EUR':
return self.eth
elif product_id == 'LTC-USD':
return self.ltc
elif product_id == 'LTC-EUR':
return self.ltc
elif product_id == 'ETH-BTC':
return self.eth
elif product_id == 'LTC-BTC':
return self.ltc
def get_quoted_currency_from_product_id(self, product_id):
self.update_amounts()
if product_id == 'BTC-USD':
return self.fiat
elif product_id == 'BCH-USD':
return self.fiat
elif product_id == 'BCH-EUR':
return self.fiat
elif product_id == 'BCH-BTC':
return self.btc
elif product_id == 'BTC-EUR':
return self.fiat
elif product_id == 'ETH-USD':
return self.fiat
elif product_id == 'ETH-EUR':
return self.fiat
elif product_id == 'LTC-USD':
return self.fiat
elif product_id == 'LTC-EUR':
return self.fiat
elif product_id == 'ETH-BTC':
return self.btc
elif product_id == 'LTC-BTC':
return self.btc
def determine_trades(self, product_id, period_list, indicators):
self.update_amounts()
if self.is_live:
amount_of_coin = self.get_base_currency_from_product_id(product_id)
product = self.get_product_by_product_id(product_id)
new_buy_flag = True
new_sell_flag = False
for cur_period in period_list:
if Decimal(indicators[cur_period.name]['adx']) > Decimal(25.0):
# Trending strategy
new_buy_flag = new_buy_flag and Decimal(indicators[cur_period.name]['obv']) > Decimal(indicators[cur_period.name]['obv_ema'])
new_sell_flag = new_sell_flag or Decimal(indicators[cur_period.name]['obv']) < Decimal(indicators[cur_period.name]['obv_ema'])
else:
# Ranging strategy
new_buy_flag = new_buy_flag and Decimal(indicators[cur_period.name]['stoch_slowk']) > Decimal(indicators[cur_period.name]['stoch_slowd']) and \
Decimal(indicators[cur_period.name]['stoch_slowk']) < Decimal('50.0')
new_sell_flag = new_sell_flag or Decimal(indicators[cur_period.name]['stoch_slowk']) < Decimal(indicators[cur_period.name]['stoch_slowd'])
if product_id == 'LTC-BTC' or product_id == 'ETH-BTC':
ltc_or_eth_fiat_product = self.get_product_by_product_id(product_id[:3] + '-' + self.fiat_currency)
btc_fiat_product = self.get_product_by_product_id('BTC-' + self.fiat_currency)
new_buy_flag = new_buy_flag and ltc_or_eth_fiat_product.buy_flag
new_sell_flag = new_sell_flag and btc_fiat_product.buy_flag
if new_buy_flag:
if product.sell_flag:
product.last_signal_switch = time.time()
product.sell_flag = False
product.buy_flag = True
amount = self.get_quoted_currency_from_product_id(product_id)
bid = product.order_book.get_ask() - Decimal(product.quote_increment)
amount = self.round_coin(Decimal(amount) / Decimal(bid))
if amount >= Decimal(product.min_size):
if not product.order_in_progress:
product.order_thread = threading.Thread(target=self.buy, name='buy_thread', kwargs={'product': product})
product.order_thread.start()
elif new_sell_flag:
if product.buy_flag:
product.last_signal_switch = time.time()
product.buy_flag = False
product.sell_flag = True
if amount_of_coin >= Decimal(product.min_size):
if not product.order_in_progress:
product.order_thread = threading.Thread(target=self.sell, name='sell_thread', kwargs={'product': product})
product.order_thread.start()
else:
product.buy_flag = False
product.sell_flag = False