forked from sean-mw/crypto-arbitrage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinance_api.py
70 lines (51 loc) · 2.01 KB
/
binance_api.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
from decimal import Decimal
from binance.spot import Spot as Client
from binance.error import ClientError
from symbol_info_util import get_symbol_info
from graph import get_price
from config import config
client = Client(config['key'], config['secret'], base_url='https://api.binance.com')
def client_get_price(symbol):
return client.book_ticker(symbol)
def round_step_size(quantity, step_size):
quantity = Decimal(str(quantity))
return float(quantity - quantity % Decimal(str(step_size)))
def format_quantity(qty, symbol):
symbol_info = get_symbol_info(symbol)
precision = int(symbol_info['base_precision'])
step_size = float(symbol_info['step_size'])
qty = float('{:0.0{}f}'.format(qty, precision))
return round_step_size(qty, step_size)
def buy(symbol, qty):
fills = buy_helper(symbol, qty)
balance = sum([float(i['qty']) for i in fills])
avg_executed_price = 1 / sum([(float(i['qty']) / balance) * float(i['price']) for i in fills])
return balance, avg_executed_price
def sell(symbol, qty):
fills = sell_helper(symbol, qty)
balance = sum([float(i['qty']) * float(i['price']) for i in fills])
avg_executed_price = sum([((float(i['qty']) * float(i['price'])) / balance) * float(i['price']) for i in fills])
return balance, avg_executed_price
def buy_helper(symbol, qty):
symbol_info = get_symbol_info(symbol)
price = get_price(symbol_info['target'], symbol_info['base'])
order_qty = qty * price
order_qty = format_quantity(order_qty, symbol)
try:
order = place_order(symbol, order_qty, 'BUY')
except ClientError as e:
print(e)
return buy_helper(symbol, qty)
return order['fills']
def sell_helper(symbol, qty):
order_qty = format_quantity(qty, symbol)
order = place_order(symbol, order_qty, 'SELL')
return order['fills']
def place_order(symbol, qty, side):
params = {
'symbol': symbol,
'side': side,
'type': 'MARKET',
'quantity': qty,
}
return client.new_order(**params)