-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinanceAPI.py
597 lines (502 loc) · 22.3 KB
/
BinanceAPI.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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
from binance.client import Client
from binance.exceptions import BinanceAPIException, BinanceWithdrawException
from binance.websockets import BinanceSocketManager
import config
import DateUtil
import threading
import Utils
import time
from datetime import datetime
from binance.enums import *
import decimal
asset = "asset"
coin_qty = "free"
OPEN_TIME = 0
OPEN_PRICE = 1
HIGH_PRICE = 2
LOW_PRICE = 3
CLOSE_PRICE = 4
VOLUME = 5
CLOSE_TIME = 6
Q_ASSET_VOL = 7
NUM_TRADES = 8
T_B_B_A_VOL = 9 # Taker buy base asset volume
T_B_Q_A_VOL = 10 # Taker buy quote asset volume
class BinanceBot:
client = Client(config.api_key, config.api_secret)
bm = BinanceSocketManager(client)
buying_price = 0.0
selling_price = 0.0
profit = 30 # percentages
interval = Client.KLINE_INTERVAL_1HOUR
quantity = 0
wait_time = 10
short_symbol = ""
btcOrBnb = "BTC"
profitPercentage = 1 # that means %1 profit
walletPercentage = 100 # default btcOrBnb coin percentages
test_total = 1 # 0.01 # btc sample for test
stop_loss = 0
stop_loss_percentage = 6 # % 6 stop loss default
binance_fee = 0.00201
def __init__(self, short_symbol, btcOrBnb='BTC', profitPercentage=2, walletPercentage=100, stop_loss_pecentage=5):
self.short_symbol = short_symbol
self.btcOrBnb = btcOrBnb
self.profitPercentage = profitPercentage
self.walletPercentage = walletPercentage
self.stop_loss_percentage = stop_loss_pecentage
def getMarketDepth(self, short_symbol, btcOrBnb="BTC"):
symbol = self.getSymbol(short_symbol, btcOrBnb)
arr = self.client.get_order_book(symbol=symbol)
bids = [] # buying price and quantity
for e in arr["bids"]:
bids.append(("price", float(e[0]), "qty", float(e[1])))
asks = [] # selling price and quantity
for e in arr["asks"]:
asks.append(("price", float(e[0]), "qty", float(e[1])))
return self.calc(bids=bids, asks=asks)
def calc(self, bids, asks):
bitSum = 0.0;
askSum = 0.0;
for (prc, val_prc, qty, val_qty) in bids:
bitSum = bitSum + (val_prc * val_qty)
for (prc, val_prc, qty, val_qty) in asks:
askSum = askSum + (val_prc * val_qty)
return bitSum - askSum;
"""
[{'symbol': 'XRPBTC',
'orderId': 16045865,
'clientOrderId': 'n1QfVLgIw9V5TpEtHOQzg3',
'price': '0.00012609',
'origQty': '279.00000000',
'executedQty': '279.00000000',
'status': 'FILLED',
'timeInForce': 'GTC',
'type': 'LIMIT',
'side': 'BUY',
'stopPrice': '0.00000000',
'icebergQty': '0.00000000',
'time': 1515571015453,
'isWorking': True},
{'symbol': 'XRPBTC',
'orderId': 16048343,
'clientOrderId': 'Jh9RFY2hmyqjt2mReOF6Ce',
'price': '0.00022400',
'origQty': '278.00000000',
'executedQty': '0.00000000',
'status': 'CANCELED',
'timeInForce': 'GTC',
'type': 'LIMIT',
'side': 'SELL',
'stopPrice': '0.00000000',
'icebergQty': '0.00000000',
'time': 1515571382261,
'isWorking': True},
{'symbol': 'XRPBTC',
'orderId': 17683047,
'clientOrderId': 'Ar03ZzG2XQJMmRS1HV33sI',
'price': '0.00016007',
'origQty': '278.00000000',
'executedQty': '0.00000000',
'status': 'CANCELED',
'timeInForce': 'GTC',
'type': 'LIMIT',
'side': 'SELL',
'stopPrice': '0.00000000',
'icebergQty': '0.00000000',
'time': 1515798744762,
'isWorking': True}]
"""
def get_all_orders(self, short_symbol, btcOrBnb="BTC"):
symbol = self.getSymbol(short_symbol, btcOrBnb)
print(symbol)
param = {}
param["symbol"] = symbol
return self.client.get_all_orders(**param)
def checkSymbol(self, symbol):
result = self.client.get_symbol_info(symbol)
if result is None:
return False
else:
return True
def getSymbol(self, short_symbol, btcOrBnb="BTC"):
symbol = short_symbol + btcOrBnb
return symbol
""" res = self.checkSymbol(symbol)
if res:
return symbol
else:
return None;
"""
"""{'asset': 'XRP', 'free': '278.79960000', 'locked': '0.00000000'}"""
def getDepositBalance(self, short_symbol):
return self.client.get_asset_balance(short_symbol)
"""{'address': 'rEb8TK3gBgk5auZkwc6sHnwr',
'success': True, 'addressTag': '11111',
'asset': 'XRP'}
"""
def getDepositAddress(self, short_symbol):
params = {}
params["asset"] = short_symbol
return self.client.get_deposit_address(**params)
""".. code-block:: python
[
{
"symbol": "LTCBTC",
"orderId": 1,
"clientOrderId": "myOrder1",
"price": "0.1",
"origQty": "1.0",
"executedQty": "0.0",
"status": "NEW",
"timeInForce": "GTC",
"type": "LIMIT",
"side": "BUY",
"stopPrice": "0.0",
"icebergQty": "0.0",
"time": 1499827319559
}
]
"""
def getOpenOrders(self):
params = {}
return self.client.get_open_orders(**params)
def getExchangeInfo(self, short_symbol, btcOrBnb="BTC", filterType="PRICE_FILTER"):
list = self.client.get_exchange_info()
list = list["symbols"]
retval = ()
symbol = self.getSymbol(short_symbol, btcOrBnb)
for data in list:
print(data)
if data["symbol"] == symbol:
filters = data["filters"]
print(filters)
for filter in filters:
if filter["filterType"] == filterType:
print(filter)
retval = (("minPrice", filter["minPrice"]), ("maxPrice", filter["maxPrice"]),
("tickSize", filter["tickSize"]))
return retval
"""
.. code-block:: python
[
[
1499040000000, # Open time
"0.01634790", # Open
"0.80000000", # High
"0.01575800", # Low
"0.01577100", # Close
"148976.11427815", # Volume
1499644799999, # Close time
"2434.19055334", # Quote asset volume
308, # Number of trades
"1756.87402397", # Taker buy base asset volume
"28.46694368", # Taker buy quote asset volume
"17928899.62484339" # Can be ignored
]
]
"""
def getHistoricDatafromCoin(self, short_symbol, btcOrBnb="BTC", limit=1):
params = {}
params["symbol"] = self.getSymbol(short_symbol, btcOrBnb)
params["limit"] = limit
params["interval"] = self.interval
arr = self.client.get_klines(**params)
tmp = arr[0]
return tmp
"""{'symbol': 'XRPBTC', 'bidPrice': '0.00010665', 'bidQty': '1676.00000000', 'askPrice': '0.00010680', 'askQty': '20.00000000'}
"""
def getCurrentDataOfTheCoin(self, short_symbol, btcOrBtn="BTC"):
symbol = self.getSymbol(short_symbol, btcOrBtn)
param = {}
param["symbol"] = symbol
retVal = self.client.get_orderbook_ticker(**param)
return retVal
"""{'symbol': 'XRPBTC', 'orderId': 24296649, 'clientOrderId': 'M5MABE75W9u5JoksLhjt7i',
'transactTime': 1517756438796, 'price': '0.01391320', 'origQty': '1.00000000',
'executedQty': '0.00000000', 'status': 'NEW', 'timeInForce': 'GTC', 'type': 'LIMIT',
'side': 'SELL'}
"""
def sellOrder(self, short_symbol, quantity, price, btcOrBnb="BTC"):
resp = ()
symbol = self.getSymbol(short_symbol, btcOrBnb)
price = self.formatNumbers(price)
quantity = self.formatNumbers(quantity)
param = {}
param["symbol"] = symbol
param["quantity"] = quantity
param["price"] = str(price)
try:
resp = self.client.order_limit_sell(**param)
except BinanceAPIException as e:
print(e)
except BinanceWithdrawException as e:
print(e)
else:
print("Success")
def buyOrder(self, short_symbol, quantity, price, btcOrBnb="BTC"):
symbol = self.getSymbol(short_symbol, btcOrBnb)
price = self.formatNumbers(price)
quantity = self.formatNumbers(quantity)
try:
order = self.client.create_order(
symbol=symbol,
side=SIDE_BUY,
type=ORDER_TYPE_LIMIT,
timeInForce=TIME_IN_FORCE_GTC,
quantity=quantity,
price=str(price))
except BinanceAPIException as e:
print(e)
except BinanceWithdrawException as e:
print(e)
else:
print("Success")
def formatNumbers(self, price):
if self.btcOrBnb == 'BTC':
return str(decimal.Decimal(price))[:8]
else:
return str(decimal.Decimal(price))[:6]
def cancelOrder(self, orderId, short_symbol, btcOrBnb="BTC"):
resp = ()
symbol = self.getSymbol(short_symbol, btcOrBnb)
param = {}
param["symbol"] = symbol
param["orderId"] = orderId
try:
resp = self.client.cancel_order(**param)
except BinanceAPIException as e:
print(e)
except BinanceWithdrawException as e:
print(e)
else:
print("Success")
def getBidPrice(self, short_symbol, btcOrBnb="BTC"):
curentData = self.getCurrentDataOfTheCoin(short_symbol, btcOrBnb)
bidPrice = curentData["bidPrice"]
return bidPrice
def getAskPrice(self, short_symbol, btcOrBnb="BTC"):
curentData = self.getCurrentDataOfTheCoin(short_symbol, btcOrBnb)
askPrice = curentData["askPrice"]
return askPrice
def pumpBuyAndSell(self, short_symbol, btcOrBnb="BTC", profitPercentage=20, buyingPercentage=5):
self.btcOrBnb = btcOrBnb
totalCoin = self.getTotalCoin(btcOrBnb=btcOrBnb)
askPrice = self.getBidPrice(short_symbol, btcOrBnb)
calculatedBidPrice = self.calcPriceWithPercentage(askPrice, buyingPercentage) # %5 in order to buy
sellingPrice = self.calcPriceWithPercentage(calculatedBidPrice, profitPercentage)
# calculates the number of PUMP_COIN(s) to buy, taking into
# consideration Binance's 0.20% fee.
c_fee = 0.00201
binance_fee = totalCoin * c_fee
quantity = float((totalCoin - binance_fee)) / calculatedBidPrice
precision = 6
quantity = "{:0.0{}f}".format(quantity, precision)
# quantity = float(totalCoin) / float(calculatedBidPrice)
# todo quantity value is float or int ???
# quantity = int(quantity)
print("Total coins: " + str(self.formatNumbers(totalCoin)))
buyingOrder = self.buyOrder(short_symbol=short_symbol, quantity=quantity, price=calculatedBidPrice,
btcOrBnb=btcOrBnb)
print('\n Buy order placed for ' + str(quantity) + ' coins at ' + str(
self.formatNumbers(askPrice)) + ' ' + btcOrBnb + ' each for a total of ' + str(
self.formatNumbers(calculatedBidPrice)) + ' ' + btcOrBnb)
balances = self.getDepositBalance(short_symbol=short_symbol)
COINS_OWNED = balances["free"]
while COINS_OWNED == 0:
time.sleep(0.1)
balances = self.getDepositBalance(short_symbol=short_symbol)
COINS_OWNED = balances["free"]
print('\nPlacing sell order at ' + COINS_OWNED + ' coins with ' + str(profitPercentage) + '%...')
sellingOrder = self.sellOrder(short_symbol=short_symbol, quantity=COINS_OWNED, price=sellingPrice,
btcOrBnb=btcOrBnb)
SELL_PRICE = float(COINS_OWNED) * float(sellingPrice)
print('Sell order placed of ' + str(
COINS_OWNED) + ' coins at ' + short_symbol + ' ' + str(
self.formatNumbers(sellingPrice)) + ' ' + btcOrBnb + ' each for ' + 'a total of ' + str(
self.formatNumbers(SELL_PRICE)) + ' ' + btcOrBnb)
PROFIT = float(sellingPrice) - float(calculatedBidPrice)
print('PROFIT if sell order fills: ' + str(self.formatNumbers(PROFIT) + ' ' + btcOrBnb))
return buyingOrder, sellingOrder
def calculateQuantity(self, totalCoin, coinPrice):
c_fee = 0.00201
binance_fee = totalCoin * c_fee
quantity = float((totalCoin - binance_fee)) / float(coinPrice)
return quantity
def getAvailableQuantity(self, short_symbol):
balances = self.getDepositBalance(short_symbol=short_symbol)
COINS_OWNED = balances["free"]
return COINS_OWNED
def getTotalCoin(self, btcOrBnb="BTC", walletPercantage=100):
balance = self.getDepositBalance(btcOrBnb)
totalCoin = (float(balance[coin_qty]) * walletPercantage) / 100
return totalCoin
# percentage example: %10
def calcPriceWithPercentage(self, price, percentage):
return float(price) * ((percentage + 100) / 100)
def testSellOrder(self, short_symbol, btcOrBnb="BTC"):
curentData = self.getCurrentDataOfTheCoin(short_symbol, btcOrBnb)
askPrice = curentData["askPrice"]
sellingPrice = self.calcPriceWithPercentage(askPrice, 40)
resp = self.sellOrder(short_symbol, 1, sellingPrice)
return resp
def checkOpenOrders(self, short_symbol, btcOrBnb="BTC"):
openOrders = self.getOpenOrders()
symbol = self.getSymbol(short_symbol=short_symbol, btcOrBnb=btcOrBnb)
retResult = []
for order in openOrders:
if order["symbol"] == symbol:
retResult.append(order["orderId"])
return retResult
def closeOpenOrders(self, short_symbol, btcOrBnb="BTC"):
openOrderIds = self.checkOpenOrders(short_symbol=short_symbol, btcOrBnb=btcOrBnb)
if len(openOrderIds) > 0:
for orderId in openOrderIds:
self.cancelOrder(orderId=orderId, short_symbol=short_symbol, btcOrBnb=btcOrBnb)
def calcStopLossVal(self, totalcoin):
self.stop_loss = float(totalcoin) / ((100 - self.stop_loss_percentage) / 100)
# this method is used the wallet bnb or btc and buy and sell the subcoin
# that is depicted in short_symbol, with the help of profitPercentage
# before run range mode close all open orders
def runRangeMode(self, short_symbol, profitPercentage=1, btcOrBnb="BTC", walletPercentage=100):
openOrderList = self.checkOpenOrders(short_symbol=short_symbol, btcOrBnb=btcOrBnb)
if len(openOrderList) > 0:
return
hisData = self.getHistoricDatafromCoin(short_symbol=short_symbol, btcOrBnb=btcOrBnb)
# historic data that has min max and open price of the coin
currentPrice = self.getAskPrice(short_symbol=short_symbol, btcOrBnb=btcOrBnb)
totalCoin = self.getTotalCoin(btcOrBnb=btcOrBnb, walletPercantage=walletPercentage)
sellingPrice = 0
openPrice = hisData[OPEN_PRICE]
lowPrice = hisData[LOW_PRICE]
highPrice = hisData[HIGH_PRICE]
volume = hisData[VOLUME]
buyingMaxPrice = self.calcPriceWithPercentage(lowPrice, 10)
if self.stop_loss > 0 and (totalCoin >= self.stop_loss):
sellOrderAction = threading.Thread(
target=self.sellOrder(short_symbol=short_symbol, quantity=self.quantity, price=currentPrice,
btcOrBnb=btcOrBnb))
sellOrderAction.start()
self.quantity = 0
return
if (float(buyingMaxPrice) >= float(currentPrice) and self.quantity == 0):
quantity = self.calculateQuantity(totalCoin, currentPrice)
# quantity = int(quantity)
self.quantity = quantity
self.buying_price = currentPrice
buyOrderAction = threading.Thread(
target=self.buyOrder(short_symbol=short_symbol, quantity=quantity, price=currentPrice,
btcOrBnb=btcOrBnb))
buyOrderAction.start()
sellingPrice = self.calcPriceWithPercentage(price=currentPrice, percentage=profitPercentage)
self.selling_price = sellingPrice
self.calcStopLossVal(totalcoin=totalCoin)
print("openPrice :", openPrice, "lowPrice :", lowPrice, "highPrice :", highPrice, "volume :", volume,
"currentPrice :", currentPrice, "sellingPrice : " + str(self.selling_price),
"quantity :" + str(self.quantity), "stop_loss :" + str(self.stop_loss))
return
if self.quantity > 0 and self.selling_price > 0 and float(currentPrice) >= float(self.selling_price):
self.quantity = self.getAvailableQuantity(short_symbol)
sellOrderAction = threading.Thread(
target=self.sellOrder(short_symbol=short_symbol, quantity=self.quantity, price=currentPrice,
btcOrBnb=btcOrBnb))
sellOrderAction.start()
self.quantity = 0
return
def sellOrderTest(self, short_symbol, quantity, price, btcOrBnb="BTC"):
resp = ()
symbol = self.getSymbol(short_symbol, btcOrBnb)
param = {}
param["symbol"] = symbol
param["quantity"] = quantity
param["price"] = str(price)
try:
print("Sell Order is given the price : " + str(price) + " quantity : " + str(
quantity) + " symbol : " + symbol)
except BinanceAPIException as e:
print(e)
except BinanceWithdrawException as e:
print(e)
else:
print("Success")
def checkPrice(self):
symbol = self.getSymbol(self.short_symbol, self.btcOrBnb)
def buyOrderTest(self, short_symbol, quantity, price, btcOrBnb="BTC"):
resp = ()
symbol = self.getSymbol(short_symbol, btcOrBnb)
param = {}
param["symbol"] = symbol
param["quantity"] = quantity
param["price"] = str(price)
try:
print("Buy Order is given the price : " + str(price) + " quantity : " + str(
quantity) + " symbol : " + symbol)
except BinanceAPIException as e:
print(e)
except BinanceWithdrawException as e:
print(e)
else:
print("Success")
def calcBuyingMaxPrice(self, lowPrice, maxPrice):
dif = float(maxPrice) - float(lowPrice)
dif = dif / 3
return dif + float(lowPrice)
def runRangeModeTest(self, short_symbol, profitPercentage=1, btcOrBnb="BTC", walletPercentage=100):
hisData = self.getHistoricDatafromCoin(short_symbol=short_symbol, btcOrBnb=btcOrBnb)
# historic data that has min max and open price of the coin
currentPrice = self.getAskPrice(short_symbol=short_symbol, btcOrBnb=btcOrBnb)
totalCoin = self.test_total # btc that i have
sellingPrice = 0
openPrice = hisData[OPEN_PRICE]
lowPrice = hisData[LOW_PRICE]
highPrice = hisData[HIGH_PRICE]
volume = hisData[VOLUME]
buyingMaxPrice = self.calcBuyingMaxPrice(lowPrice, highPrice)
if self.stop_loss > 0 and (totalCoin >= self.stop_loss):
sellOrderAction = threading.Thread(
target=self.sellOrder(short_symbol=short_symbol, quantity=self.quantity, price=currentPrice,
btcOrBnb=btcOrBnb))
sellOrderAction.start()
self.quantity = 0
return
if (float(buyingMaxPrice) >= float(currentPrice) and self.quantity == 0):
quantity = float(totalCoin) / float(currentPrice)
print("self.quantity : " + str(quantity))
self.quantity = quantity
self.buying_price = currentPrice
buyOrderAction = threading.Thread(
target=self.buyOrderTest(short_symbol=short_symbol, quantity=quantity, price=currentPrice,
btcOrBnb=btcOrBnb))
buyOrderAction.start()
sellingPrice = self.calcPriceWithPercentage(price=currentPrice, percentage=profitPercentage)
self.selling_price = sellingPrice
self.calcStopLossVal(totalcoin=totalCoin)
print("openPrice :", openPrice, "lowPrice :", lowPrice, "highPrice :", highPrice, "volume :", volume,
"currentPrice :", currentPrice, "sellingPrice : " + str(self.selling_price),
"quantity :" + str(self.quantity), "stop_loss :" + str(self.stop_loss))
return
if self.quantity > 0 and self.selling_price > 0 and float(currentPrice) >= float(self.selling_price):
sellOrderAction = threading.Thread(
target=self.sellOrderTest(short_symbol=short_symbol, quantity=self.quantity, price=currentPrice,
btcOrBnb=btcOrBnb))
sellOrderAction.start()
self.test_total = self.quantity * float(currentPrice)
print("total price : " + str(self.test_total))
self.quantity = 0
return
def run(self):
actions = []
cnt = 0
while True:
startTime = time.time()
actionTrader = threading.Thread(
target=self.runRangeMode(short_symbol=self.short_symbol, profitPercentage=self.profitPercentage,
btcOrBnb=self.btcOrBnb, walletPercentage=self.walletPercentage))
actions.append(actionTrader)
actionTrader.start()
endTime = time.time()
if endTime - startTime < self.wait_time:
time.sleep(self.wait_time - (endTime - startTime))
cnt = cnt + 1
if cnt % 6 == 0:
print(datetime.utcnow())