forked from enarjord/passivbot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbinance.py
409 lines (380 loc) · 19.5 KB
/
binance.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
import asyncio
import hashlib
import hmac
import json
from time import time
from urllib.parse import urlencode
import aiohttp
import numpy as np
from pure_funcs import ts_to_date, sort_dict_keys
from passivbot import Bot
from procedures import load_key_secret, print_
class BinanceBot(Bot):
def __init__(self, config: dict):
self.exchange = 'binance'
super().__init__(config)
self.max_pos_size_ito_usdt = 0.0
self.max_pos_size_ito_coin = 0.0
self.session = aiohttp.ClientSession()
self.base_endpoint = ''
self.key, self.secret = load_key_secret('binance', config['user'])
async def public_get(self, url: str, params: dict = {}) -> dict:
async with self.session.get(self.base_endpoint + url, params=params) as response:
result = await response.text()
return json.loads(result)
async def private_(self, type_: str, base_endpoint: str, url: str, params: dict = {}) -> dict:
timestamp = int(time() * 1000)
params.update({'timestamp': timestamp, 'recvWindow': 5000})
for k in params:
if type(params[k]) == bool:
params[k] = 'true' if params[k] else 'false'
elif type(params[k]) == float:
params[k] = str(params[k])
params = sort_dict_keys(params)
params['signature'] = hmac.new(self.secret.encode('utf-8'),
urlencode(params).encode('utf-8'),
hashlib.sha256).hexdigest()
headers = {'X-MBX-APIKEY': self.key}
async with getattr(self.session, type_)(base_endpoint + url, params=params,
headers=headers) as response:
result = await response.text()
return json.loads(result)
async def private_get(self, url: str, params: dict = {}, base_endpoint: str = None) -> dict:
if base_endpoint is not None:
return await self.private_('get', base_endpoint, url, params)
else:
return await self.private_('get', self.base_endpoint, url, params)
async def private_post(self, base_endpoint: str, url: str, params: dict = {}) -> dict:
return await self.private_('post', base_endpoint, url, params)
async def private_delete(self, url: str, params: dict = {}) -> dict:
return await self.private_('delete', self.base_endpoint, url, params)
async def init_market_type(self):
fapi_endpoint = 'https://fapi.binance.com'
dapi_endpoint = 'https://dapi.binance.com'
fapi_info = await self.private_get('/fapi/v1/exchangeInfo', base_endpoint=fapi_endpoint)
if self.symbol in {e['symbol'] for e in fapi_info['symbols']}:
print('linear perpetual')
self.market_type = 'linear_perpetual'
self.inverse = self.config['inverse'] = False
self.base_endpoint = fapi_endpoint
self.endpoints = {
'position': '/fapi/v2/positionRisk',
'balance': '/fapi/v2/balance',
'exchange_info': '/fapi/v1/exchangeInfo',
'leverage_bracket': '/fapi/v1/leverageBracket',
'open_orders': '/fapi/v1/openOrders',
'ticker': '/fapi/v1/ticker/bookTicker',
'fills': '/fapi/v1/userTrades',
'income': '/fapi/v1/income',
'create_order': '/fapi/v1/order',
'cancel_order': '/fapi/v1/order',
'ticks': '/fapi/v1/aggTrades',
'margin_type': '/fapi/v1/marginType',
'leverage': '/fapi/v1/leverage',
'position_side': '/fapi/v1/positionSide/dual',
'websocket': f"wss://fstream.binance.com/ws/{self.symbol.lower()}@aggTrade"
}
else:
dapi_info = await self.private_get('/dapi/v1/exchangeInfo', base_endpoint=dapi_endpoint)
if self.symbol in {e['symbol'] for e in dapi_info['symbols']}:
print('inverse coin margined')
self.base_endpoint = dapi_endpoint
self.market_type = 'inverse_coin_margined'
self.inverse = self.config['inverse'] = True
self.endpoints = {
'position': '/dapi/v1/positionRisk',
'balance': '/dapi/v1/balance',
'exchange_info': '/dapi/v1/exchangeInfo',
'leverage_bracket': '/dapi/v1/leverageBracket',
'open_orders': '/dapi/v1/openOrders',
'ticker': '/dapi/v1/ticker/bookTicker',
'fills': '/dapi/v1/userTrades',
'income': '/dapi/v1/income',
'create_order': '/dapi/v1/order',
'cancel_order': '/dapi/v1/order',
'ticks': '/dapi/v1/aggTrades',
'margin_type': '/dapi/v1/marginType',
'leverage': '/dapi/v1/leverage',
'position_side': '/dapi/v1/positionSide/dual',
'websocket': f"wss://dstream.binance.com/ws/{self.symbol.lower()}@aggTrade"
}
else:
raise Exception(f'unknown symbol {self.symbol}')
self.spot_base_endpoint = 'https://api.binance.com'
self.endpoints['transfer'] = '/sapi/v1/asset/transfer'
self.endpoints['account'] = '/api/v3/account'
async def _init(self):
await self.init_market_type()
exchange_info, leverage_bracket = await asyncio.gather(
self.public_get(self.endpoints['exchange_info']),
self.private_get(self.endpoints['leverage_bracket']),
)
for e in exchange_info['symbols']:
if e['symbol'] == self.symbol:
self.coin = e['baseAsset']
self.quot = e['quoteAsset']
self.margin_coin = e['marginAsset']
self.pair = e['pair']
if self.market_type == 'inverse_coin_margined':
self.c_mult = self.config['c_mult'] = \
float(e['contractSize'])
price_precision = e['pricePrecision']
qty_precision = e['quantityPrecision']
for q in e['filters']:
if q['filterType'] == 'LOT_SIZE':
self.min_qty = self.config['min_qty'] = float(q['minQty'])
elif q['filterType'] == 'MARKET_LOT_SIZE':
self.qty_step = self.config['qty_step'] = float(q['stepSize'])
elif q['filterType'] == 'PRICE_FILTER':
self.price_step = self.config['price_step'] = float(q['tickSize'])
elif q['filterType'] == 'MIN_NOTIONAL':
self.min_cost = self.config['min_cost'] = float(q['notional'])
try:
z = self.min_cost
except AttributeError:
self.min_cost = self.config['min_cost'] = 0.0
break
max_lev = 25 # lowest max lev for any binance futures symbol, as per 2021-06-12
for e in leverage_bracket:
if ('pair' in e and e['pair'] == self.pair) or \
('symbol' in e and e['symbol'] == self.symbol):
for br in e['brackets']:
max_lev = max(max_lev, int(br['initialLeverage']))
break
self.max_leverage = self.config['max_leverage'] = max_lev
await super()._init()
await self.init_order_book()
await self.update_position()
async def check_if_other_positions(self):
positions, open_orders = await asyncio.gather(
self.private_get(self.endpoints['position']),
self.private_get(self.endpoints['open_orders'])
)
do_abort = False
for e in positions:
if float(e['positionAmt']) != 0.0:
if e['symbol'] != self.symbol and self.margin_coin in e['symbol']:
print('\n\nWARNING\n\n')
print('account has position in other symbol:', e)
print('\n\n')
do_abort = True
for e in open_orders:
if e['symbol'] != self.symbol and self.margin_coin in e['symbol']:
print('\n\nWARNING\n\n')
print('account has open orders in other symbol:', e)
print('\n\n')
do_abort = True
if do_abort:
if not ('allow_sharing_wallet' in self.config and self.config['allow_sharing_wallet']):
print('please close other positions and cancel other open orders '
'or add "allow_sharing_wallet": True to config')
self.stop()
return True
else:
print('no positions or open orders in other symbols sharing margin wallet')
return False
async def execute_leverage_change(self):
lev = int(min(self.max_leverage, max(3.0, np.ceil(max(self.xk['pbr_limit']) * 2))))
return await self.private_post(self.base_endpoint,
self.endpoints['leverage'],
{'symbol': self.symbol, 'leverage': lev})
async def init_exchange_config(self):
try:
print(await self.private_post(self.base_endpoint,
self.endpoints['margin_type'],
{'symbol': self.symbol, 'marginType': 'CROSSED'}))
except Exception as e:
print(e)
try:
lev = await self.execute_leverage_change()
print_([lev])
if self.market_type == 'linear_perpetual':
self.max_pos_size_ito_usdt = float(lev['maxNotionalValue'])
print('max pos size in terms of usdt', self.max_pos_size_ito_usdt)
elif self.market_type == 'inverse_coin_margined':
self.max_pos_size_ito_coin = float(lev['maxQty'])
print('max pos size in terms of coin', self.max_pos_size_ito_coin)
except Exception as e:
print(e)
try:
res = await self.private_post(self.base_endpoint,
self.endpoints['position_side'],
{'dualSidePosition': 'true'})
print(res)
except Exception as e:
if '"code":-4059' not in e.args[0]:
print(e)
print('unable to set hedge mode, aborting')
raise Exception('failed to set hedge mode')
return await self.check_if_other_positions()
async def init_order_book(self):
ticker = await self.public_get(self.endpoints['ticker'], {'symbol': self.symbol})
if self.market_type == 'inverse_coin_margined':
ticker = ticker[0]
self.ob = [float(ticker['bidPrice']), float(ticker['askPrice'])]
self.price = np.random.choice(self.ob)
async def fetch_open_orders(self) -> [dict]:
return [
{'order_id': int(e['orderId']),
'symbol': e['symbol'],
'price': float(e['price']),
'qty': float(e['origQty']),
'type': e['type'].lower(),
'side': e['side'].lower(),
'position_side': e['positionSide'].lower().replace('short', 'shrt'),
'timestamp': int(e['time'])}
for e in await self.private_get(self.endpoints['open_orders'], {'symbol': self.symbol})
]
async def fetch_position(self) -> dict:
positions, balance = await asyncio.gather(
self.private_get(self.endpoints['position'], ({'symbol': self.symbol}
if self.market_type == 'linear_perpetual'
else {'pair': self.pair})),
self.private_get(self.endpoints['balance'], {})
)
positions = [e for e in positions if e['symbol'] == self.symbol]
position = {'long': {'size': 0.0, 'price': 0.0, 'liquidation_price': 0.0, 'upnl': 0.0, 'leverage': 0.0},
'shrt': {'size': 0.0, 'price': 0.0, 'liquidation_price': 0.0, 'upnl': 0.0, 'leverage': 0.0}}
if positions:
for p in positions:
if p['positionSide'] == 'LONG':
position['long'] = {'size': float(p['positionAmt']),
'price': float(p['entryPrice']),
'liquidation_price': float(p['liquidationPrice']),
'upnl': float(p['unRealizedProfit']),
'leverage': float(p['leverage'])}
elif p['positionSide'] == 'SHORT':
position['shrt'] = {'size': float(p['positionAmt']),
'price': float(p['entryPrice']),
'liquidation_price': float(p['liquidationPrice']),
'upnl': float(p['unRealizedProfit']),
'leverage': float(p['leverage'])}
for e in balance:
if e['asset'] == (self.quot if self.market_type == 'linear_perpetual' else self.coin):
position['wallet_balance'] = float(e['balance'])
position['equity'] = position['wallet_balance'] + float(e['crossUnPnl'])
break
return position
async def execute_order(self, order: dict) -> dict:
params = {'symbol': self.symbol,
'side': order['side'].upper(),
'positionSide': order['position_side'].replace('shrt', 'short').upper(),
'type': order['type'].upper(),
'quantity': str(order['qty'])}
if params['type'] == 'LIMIT':
params['timeInForce'] = 'GTX'
params['price'] = str(order['price'])
if 'custom_id' in order:
params['newClientOrderId'] = \
f"{order['custom_id']}_{str(int(time() * 1000))[8:]}_{int(np.random.random() * 1000)}"
o = await self.private_post(self.base_endpoint, self.endpoints['create_order'], params)
if 'side' in o:
return {'symbol': self.symbol,
'side': o['side'].lower(),
'position_side': o['positionSide'].lower().replace('short', 'shrt'),
'type': o['type'].lower(),
'qty': float(o['origQty']),
'price': float(o['price'])}
else:
return o
async def execute_cancellation(self, order: dict) -> [dict]:
cancellation = await self.private_delete(self.endpoints['cancel_order'],
{'symbol': self.symbol, 'orderId': order['order_id']})
if 'side' in cancellation:
return {'symbol': self.symbol, 'side': cancellation['side'].lower(),
'position_side': cancellation['positionSide'].lower().replace('short', 'shrt'),
'qty': float(cancellation['origQty']), 'price': float(cancellation['price'])}
else:
return cancellation
async def fetch_fills(self, limit: int = 1000, from_id: int = None, start_time: int = None, end_time: int = None):
params = {'symbol': self.symbol, 'limit': min(100, limit) if self.inverse else limit}
if from_id is not None:
params['fromId'] = max(0, from_id)
if start_time is not None:
params['startTime'] = start_time
if end_time is not None:
params['endTime'] = end_time
try:
fetched = await self.private_get(self.endpoints['fills'], params)
fills = [{'symbol': x['symbol'],
'order_id': int(x['orderId']),
'side': x['side'].lower(),
'price': float(x['price']),
'qty': float(x['qty']),
'realized_pnl': float(x['realizedPnl']),
'cost': float(x['baseQty']) if self.inverse else float(x['quoteQty']),
'fee_paid': float(x['commission']),
'fee_token': x['commissionAsset'],
'timestamp': int(x['time']),
'position_side': x['positionSide'].lower().replace('short', 'shrt'),
'is_maker': x['maker']} for x in fetched]
except Exception as e:
print('error fetching fills a', e)
return []
return fills
async def fetch_income(self, limit: int = 1000, start_time: int = None, end_time: int = None):
params = {'symbol': self.symbol, 'limit': limit}
if start_time is not None:
params['startTime'] = start_time
if end_time is not None:
params['endTime'] = end_time
try:
fetched = await self.private_get(self.endpoints['income'], params)
income = [{'symbol': x['symbol'],
'incomeType': x['incomeType'],
'income': float(x['income']),
'asset': x['asset'],
'info': x['info'],
'timestamp': int(x['time']),
'tranId': x['tranId'],
'tradeId': x['tradeId']} for x in fetched]
except Exception as e:
print('error fetching incoming: ', e)
return []
return income
async def fetch_account(self):
try:
return await self.private_get(base_endpoint=self.spot_base_endpoint, url=self.endpoints['account'])
except Exception as e:
print('error fetching account: ', e)
return {'balances': []}
async def fetch_ticks(self, from_id: int = None, start_time: int = None, end_time: int = None,
do_print: bool = True):
params = {'symbol': self.symbol, 'limit': 1000}
if from_id is not None:
params['fromId'] = max(0, from_id)
if start_time is not None:
params['startTime'] = start_time
if end_time is not None:
params['endTime'] = end_time
try:
fetched = await self.private_get(self.endpoints['ticks'], params)
except Exception as e:
print('error fetching ticks a', e)
return []
try:
ticks = [{'trade_id': int(t['a']), 'price': float(t['p']), 'qty': float(t['q']),
'timestamp': int(t['T']), 'is_buyer_maker': t['m']}
for t in fetched]
if do_print:
print_(['fetched ticks', self.symbol, ticks[0]['trade_id'],
ts_to_date(float(ticks[0]['timestamp']) / 1000)])
except Exception as e:
print('error fetching ticks b', e, fetched)
ticks = []
if do_print:
print_(['fetched no new ticks', self.symbol])
return ticks
async def fetch_ticks_time(self, start_time: int, end_time: int = None, do_print: bool = True):
return await self.fetch_ticks(start_time=start_time, end_time=end_time, do_print=do_print)
async def transfer(self, type_: str, amount: float, asset: str = 'USDT'):
params = {'type': type_.upper(), 'amount': amount, 'asset': asset}
return await self.private_post(self.spot_base_endpoint, self.endpoints['transfer'], params)
def standardize_websocket_ticks(self, data: dict) -> [dict]:
try:
return [{'price': float(data['p']), 'qty': float(data['q']), 'is_buyer_maker': data['m']}]
except Exception as e:
print('error in websocket tick', e)
return []
async def subscribe_ws(self, ws):
pass