-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathcyripto_trader.py
executable file
·564 lines (455 loc) · 19.8 KB
/
cyripto_trader.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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import decimal
import datetime
import time
import argparse
from poloniex import Poloniex
# api keys are in the settings.yaml
try:
import yaml
with open('settings.yaml') as f:
settings = yaml.safe_load(f)
if not (settings and
settings.get('polo_api', None) and
settings['polo_api'].get('key', None) and
settings['polo_api'].get('secret', None)):
raise ValueError('api keys missing')
except (ValueError, IOError):
print "settings not found."
print "create a file named settings.yaml and put them here like this:"
print "polo_api:"
print " key: super_rich_acounts_api_key"
print " secret: super_secret_top_secret"
print ""
exit()
class Trader(object):
"""Trader class
"""
def __init__(self, api_key, api_secret, coin, currency, dust_total=10,
dust_amount=100.0, min_spread=0.0001, max_trading_amount=1):
# set currency pair
self.coin = coin
self.currency = currency
self.my_pair = '%s_%s' % (currency, coin)
# we'll need this while finding buy/sell prices
self.dust_total = self.make_satoshi(dust_total)
self.dust_amount = self.make_satoshi(dust_amount)
self.min_spread = self.make_satoshi(min_spread)
self.max_trading_amount = self.make_satoshi(max_trading_amount)
# initialize the poloniex api
self.api_key = api_key
self.api_secret = api_secret
self.polo = Poloniex(api_key, api_secret)
# initialize other variables
self.coin_balance = self.make_satoshi('0.0')
self.currency_balance = self.make_satoshi('0.0')
self.total_coin_balance = self.make_satoshi('0.0')
self.total_currency_balance = self.make_satoshi('0.0')
self.open_orders_raw = []
self.open_orders = []
self.open_orders_sell = []
self.open_orders_buy = []
self.order_book_raw = []
self.order_book = {}
self.order_book["buy"] = []
self.order_book["sell"] = []
self.sell_price = self.make_satoshi('0.0')
self.buy_price = self.make_satoshi('0.0')
self.price_spread = self.make_satoshi('0.0')
self.sell_amount = self.make_satoshi('0.0')
self.buy_amount = self.make_satoshi('0.0')
self.trade = False
# those are hardcoded. may be added to settings later.
self.min_currency_balance = self.make_satoshi('0.00100000')
# seconds to wait for exchange rate limits.
self.exchange_wait_time = 0.8
def make_satoshi(self, num):
return decimal.Decimal(num).quantize(decimal.Decimal('1.00000000'))
def load_open_orders(self):
"""
loads open orders. Resets the balances so load_balances method should be called after.
"""
self.total_coin_balance = self.make_satoshi('0.0')
self.total_currency_balance = self.make_satoshi('0.0')
self.open_orders_raw = self.polo.returnOpenOrders(currencyPair=self.my_pair)
self.open_orders = []
self.open_orders_sell = []
self.open_orders_buy = []
for item in self.open_orders_raw:
order = {}
order['type'] = item['type']
order["order_number"] = item['orderNumber']
order["price"] = self.make_satoshi(item['rate'])
order["amount"] = self.make_satoshi(item['amount'])
order["total"] = self.make_satoshi(order['price'] * order['amount'])
order["starting_amount"] = self.make_satoshi(item['startingAmount'])
self.open_orders.append(order)
if order['type'] == 'sell':
self.open_orders_sell.append(order)
self.total_coin_balance = self.total_coin_balance + order["amount"]
else:
self.open_orders_buy.append(order)
self.total_currency_balance = self.total_currency_balance + order["total"]
return self.open_orders
def load_balances(self):
# first load the open orders to get the balances in open orders.
self.load_open_orders()
# request from exchange
self.balances = self.polo.returnBalances()
# set the coin balance
self.coin_balance = self.make_satoshi(self.balances[self.coin])
self.total_coin_balance = self.total_coin_balance + self.coin_balance
# '0.00000001' makes things complicated. get rid of it.
if self.coin_balance <= self.make_satoshi('0.00000001'):
self.coin_balance = self.make_satoshi('0.0')
if self.total_coin_balance <= self.make_satoshi('0.00000001'):
self.total_coin_balance = self.make_satoshi('0.0')
# set the main currency balance
self.currency_balance = self.make_satoshi(self.balances[self.currency])
self.total_currency_balance = self.total_currency_balance + self.currency_balance
# '0.00000001' makes things complicated. get rid of it.
if self.currency_balance <= self.make_satoshi('0.00000001'):
self.currency_balance = self.make_satoshi('0.0')
if self.total_currency_balance <= self.make_satoshi('0.00000001'):
self.total_currency_balance = self.make_satoshi('0.0')
# added for debugging. make the BTC amount > 0 so we can test buying.
# self.total_currency_balance = self.make_satoshi('0.10000000')
def load_order_book(self):
self.order_book_raw = self.polo.returnOrderBook(currencyPair=self.my_pair, depth='200')
self.order_book["buy"] = []
self.order_book["sell"] = []
self.process_order_book('buy', self.order_book_raw["bids"])
self.process_order_book('sell', self.order_book_raw["asks"])
return self.order_book
def process_order_book(self, order_type, order_book_raw):
self.order_book[order_type] = []
total = self.make_satoshi("0.0")
for item in order_book_raw:
order = {}
order['type'] = order_type
order["price"] = self.make_satoshi(item[0])
order["amount"] = self.make_satoshi(item[1])
order["cur"] = self.make_satoshi(order["price"] * order["amount"])
total = self.make_satoshi(total + order["cur"])
order["total"] = total
# mark the order if its my order. put order number from open_orders
order['order_number'] = None
for open_order in self.open_orders:
if (open_order['type'] == order['type']) and (open_order["price"] == order["price"]):
order["order_number"] = open_order['order_number']
print "order:", order
self.order_book[order_type].append(order)
return self.order_book[order_type]
def find_sell_price(self):
total = self.make_satoshi("0.0")
for order in self.order_book['sell']:
if order['order_number'] is None:
total = self.make_satoshi(total + order["cur"])
if (order["amount"] >= self.dust_amount) or (total >= self.dust_total):
return self.make_satoshi(order["price"] - self.make_satoshi("0.00000003"))
return False
def find_buy_price(self):
total = self.make_satoshi("0.0")
for order in self.order_book['buy']:
if order['order_number'] is None:
total = self.make_satoshi(total + order["cur"])
if (order["amount"] >= self.dust_amount) or (total >= self.dust_total):
return self.make_satoshi(order["price"] + self.make_satoshi("0.00000003"))
return False
def decide_to_trade(self):
self.sell_price = self.find_sell_price()
self.buy_price = self.find_buy_price()
self.price_spread = self.sell_price - self.buy_price
print "Sell Price:", self.sell_price
print "Buy Price:", self.buy_price
print "Price Spread:", self.price_spread
if (not self.sell_price) or (not self.buy_price):
print "Something is wrong with sell and buy prices. Will not trade!"
self.trade = False
return False
if self.total_currency_balance > self.min_currency_balance:
print 'I have %s %s, will try to buy %s.' % (
self.total_currency_balance, self.currency, self.coin)
self.trade = 'buy'
return True
else:
if self.price_spread >= self.min_spread:
print 'Spread is good. Will trade'
# sell or buy?
if self.total_coin_balance > 0:
print 'I have %s %s, will try to sell %s for %s.' % (
self.total_coin_balance, self.coin, self.coin,
self.currency)
self.trade = 'sell'
return True
else:
print 'I have nothing to trade.'
self.trade = False
else:
print 'Price Spread is not good. Stop trading.'
self.trade = False
return False
def cancel_open_orders(self, buy_sell):
for order in self.open_orders:
if order['type'] == buy_sell:
print "canceling order:", order
try:
retval = self.polo.cancelOrder(order["order_number"])
except RuntimeError:
retval = False
print 'ERROR canceling order.'
print "cancel order retval:", retval
self.open_orders.remove(order)
if buy_sell == 'sell':
self.open_orders_sell.remove(order)
else:
self.open_orders_buy.remove(order)
def cancel_open_order(self, order):
print "canceling order:", order
try:
retval = self.polo.cancelOrder(order["order_number"])
except RuntimeError:
retval = False
print 'ERROR canceling order.'
time.sleep(0.2)
print "cancel order retval:", retval
self.open_orders.remove(order)
if order['type'] == 'sell':
self.open_orders_sell.remove(order)
else:
self.open_orders_buy.remove(order)
def add_sell_order(self):
# compute the amount
if self.total_coin_balance > self.max_trading_amount:
self.sell_amount = self.max_trading_amount
else:
self.sell_amount = self.total_coin_balance
print "self.sell_amount:", self.sell_amount
# send order to exchange
try:
retval = self.polo.sell(currencyPair=self.my_pair, rate=self.sell_price, amount=self.sell_amount)
except RuntimeError:
print 'ERROR adding SELL order.'
retval = False
print retval
return retval
def sell(self):
# cancel all buy orders first.
self.cancel_open_orders('buy')
# check open sell orders if the sell price is ok.
for order in self.open_orders_sell:
if order['price'] == self.sell_price:
print "order is ok:", order
else:
# cancel the order.
retval = self.cancel_open_order(order)
print 'self.open_orders_sell:', len(self.open_orders_sell)
if len(self.open_orders_sell) == 0:
print 'adding a new sell order'
retval = self.add_sell_order()
def add_buy_order(self):
# compute the amount
if self.total_currency_balance > self.make_satoshi('0.00001000'):
self.buy_amount = self.make_satoshi(self.total_currency_balance / self.buy_price) - self.make_satoshi('0.00000001')
print "Buying amount:", self.buy_amount
else:
print "Buying amount is low. not buying:"
return False
# send order to exchange
try:
retval = self.polo.buy(currencyPair=self.my_pair, rate=self.buy_price, amount=self.buy_amount)
except RuntimeError:
print 'ERROR adding BUY order.'
retval = False
print retval
return retval
def buy(self):
# cancel all buy orders first.
self.cancel_open_orders('sell')
# check open sell orders if the sell price is ok.
for order in self.open_orders_buy:
if order['price'] == self.buy_price:
print "order is ok:", order
else:
# cancel the order.
retval = self.cancel_open_order(order)
print 'self.open_orders_buy:', len(self.open_orders_buy)
if len(self.open_orders_buy) == 0:
print 'adding a new buy order'
retval = self.add_buy_order()
def run_scalping(self):
while True:
now = datetime.datetime.now()
print str(now)
self.load_balances()
self.load_order_book()
print '%s Balance: %s' % (self.currency, self.total_currency_balance)
print '%s Balance: %s' % (self.coin, self.total_coin_balance)
# trade or not trade?
trade = self.decide_to_trade()
print "Trade?", trade
if self.trade == 'sell':
self.sell()
elif self.trade == 'buy':
self.buy()
else:
#Will not trade. Cancel all orders.
self.cancel_open_orders('buy')
self.cancel_open_orders('sell')
print "."
print "open orders:", self.open_orders
print "."
now = datetime.datetime.now()
print str(now)
print "Will wait a little bit for not to flood the exchange..."
time.sleep(self.exchange_wait_time)
def add_sell_all_order(self):
# compute the amount
self.sell_amount = self.total_coin_balance
print "self.sell_amount:", self.sell_amount
if self.sell_amount:
# send order to exchange
try:
retval = self.polo.sell(currencyPair=self.my_pair,
rate=self.sell_price,
amount=self.sell_amount)
except RuntimeError:
print 'ERROR adding SELL ALL order.'
retval = False
print retval
return retval
else:
return False
def sell_all(self):
# cancel all buy orders first.
self.cancel_open_orders('buy')
# check open sell orders if the sell price is ok.
for order in self.open_orders_sell:
if order['price'] == self.sell_price:
print "order is ok:", order
else:
# cancel the order.
retval = self.cancel_open_order(order)
print 'self.open_orders_sell:', len(self.open_orders_sell)
if len(self.open_orders_sell) == 0:
print 'adding a new sell all order'
retval = self.add_sell_all_order()
def run_sell_all(self):
self.trade = 'sell'
while self.trade == 'sell':
now = datetime.datetime.now()
print str(now)
self.load_balances()
self.load_order_book()
print '%s Balance: %s' % (self.currency, self.total_currency_balance)
print '%s Balance: %s' % (self.coin, self.total_coin_balance)
self.sell_price = self.find_sell_price()
print "Sell Price:", self.sell_price
if self.total_coin_balance > 0.0:
print 'I have %s %s, will try to sell %s for %s.' % (
self.total_coin_balance, self.coin, self.coin,
self.currency)
self.trade = 'sell'
else:
print 'I have nothing to trade.'
self.trade = False
if self.trade == 'sell':
self.sell_all()
else:
#Will not trade. Cancel all orders.
self.cancel_open_orders('buy')
self.cancel_open_orders('sell')
print "."
print "open orders:", self.open_orders
print "."
now = datetime.datetime.now()
print str(now)
print "Will wait a little bit for not to flood the exchange..."
time.sleep(self.exchange_wait_time)
def add_buy_all_order(self):
# compute the amount
if self.total_currency_balance > self.make_satoshi('0.00001000'):
top = self.total_currency_balance / self.buy_price
self.buy_amount = self.make_satoshi(top) - self.make_satoshi('0.00000001')
print "Buying amount:", self.buy_amount
else:
print "Buying amount is low. not buying:"
return False
# send order to exchange
try:
retval = self.polo.buy(currencyPair=self.my_pair, rate=self.buy_price, amount=self.buy_amount)
except:
print 'ERROR adding BUY ALL order.'
retval = False
print retval
return retval
def buy_all(self):
# cancel all buy orders first.
self.cancel_open_orders('sell')
# check open sell orders if the sell price is ok.
for order in self.open_orders_buy:
if order['price'] == self.buy_price:
print "order is ok:", order
else:
# cancel the order.
retval = self.cancel_open_order(order)
print 'self.open_orders_buy:', len(self.open_orders_buy)
if len(self.open_orders_buy) == 0:
print 'adding a new buy all order'
retval = self.add_buy_all_order()
def run_buy_all(self):
self.trade = 'buy'
while self.trade == 'buy':
now = datetime.datetime.now()
print str(now)
self.load_balances()
self.load_order_book()
print '%s Balance: %s' % (self.currency, self.total_currency_balance)
print '%s Balance: %s' % (self.coin, self.total_coin_balance)
self.buy_price = self.find_buy_price()
print "Buy Price:", self.buy_price
if self.total_currency_balance > self.min_currency_balance:
print 'I have %s %s, will try to buy %s.' % (self.total_currency_balance, self.currency, self.coin)
self.trade = 'buy'
else:
print 'I have nothing to trade.'
self.trade = False
if self.trade == 'buy':
self.buy_all()
else:
#Will not trade. Cancel all orders.
self.cancel_open_orders('buy')
self.cancel_open_orders('sell')
print "."
print "open orders:", self.open_orders
print "."
now = datetime.datetime.now()
print str(now)
print "Will wait a little bit for not to flood the exchange..."
time.sleep(self.exchange_wait_time)
if __name__ == "__main__":
PARSER = argparse.ArgumentParser()
PARSER.add_argument(
'action',
choices=['sell_all', 'buy_all', 'scalp'],
help="What to do? sell_all, buy_all or scalp",
)
ARGS = PARSER.parse_args()
ACTION = ARGS.action
POLO_API_KEY = settings['polo_api']['key']
POLO_API_SECRET = settings['polo_api']['secret']
TRACE_CURRENCY = settings['coin']['my_coin']
RETURN_CURRENCY = settings['coin']['currency']
DUST_TOTAL = settings['dust_total']
DUST_AMOUNT = settings['dust_amount']
MIN_SPREAD = settings['min_spread']
MAX_TRADING_AMOUNT = settings['max_trading_amount']
TRADER = Trader(POLO_API_KEY, POLO_API_SECRET, TRACE_CURRENCY, RETURN_CURRENCY,
DUST_TOTAL, DUST_AMOUNT, MIN_SPREAD, MAX_TRADING_AMOUNT)
if ACTION == "scalp":
TRADER.run_scalping()
elif ACTION == "sell_all":
TRADER.run_sell_all()
elif ACTION == "buy_all":
TRADER.run_buy_all()