-
Notifications
You must be signed in to change notification settings - Fork 22
/
BinanceArb.py
275 lines (218 loc) · 11.6 KB
/
BinanceArb.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
'''
@Description: Binance Arbitrage Bot
@Author: Yang Boyu
@Email: [email protected]
'''
import ccxt
import time
import traceback
from Logger import get_logger
class BinanceArbBot:
def __init__(self, exchange: ccxt.binance, coin: str, future_date: str, coin_precision: float,
slippage: float, spot_fee_rate: float, contract_fee_rate: float, multiplier: dict,
amount: float, num_maximum: int, threshold: float, max_trial: int):
self.exchange = exchange
self.coin = coin
self.future_date = future_date
self.coin_precision = coin_precision
self.slippage = slippage
self.spot_fee_rate = spot_fee_rate
self.contract_fee_rate = contract_fee_rate
self.multipler = multiplier
self.amount = amount
self.num_maximum = num_maximum
self.threshold = threshold
self.max_trial = max_trial
self.logger = get_logger("Basis-Trading Starts")
self.spot_symbol = {'type1': coin + 'USDT', 'type2': coin + '/USDT'}
self.future_symbol = {'type1': coin + 'USD_' + future_date}
def retry_wrapper(self, func, params=dict(), act_name='', sleep_seconds=1, is_exit=True):
for _ in range(self.max_trial):
try:
# NOTE: reset the local timestamp when requesting again,otherwise requests may be declined
if isinstance(params, dict) and 'timestamp' in list(params.keys()):
params['timestamp'] = int(time.time()) * 1000
result = func(**params)
return result
except Exception as e:
print('Function parameters:', params)
self.logger.warning(f"{act_name} ({self.exchange.id}) FAIL | Retry after {sleep_seconds} seconds...")
self.logger.warning(traceback.format_exc())
time.sleep(sleep_seconds)
else:
self.logger.critical(f"{act_name} FAIL too many times... Arbitrage STOPs!")
if is_exit: exit()
def binance_spot_place_order(self, symbol: str, direction: str, price: float, amount: float):
if direction == 'long':
order_info = self.exchange.create_limit_buy_order(symbol, amount, price)
elif direction == 'short':
order_info = self.exchange.create_limit_sell_order(symbol, amount, price)
else:
raise ValueError('Parameter `direction` supports `long` or `short` only')
self.logger.debug(f'spot orders ({self.exchange.id}) SUCCESS: {direction} > {symbol} > {amount} > {price}')
self.logger.debug(f'Order info: {str(order_info)}')
return order_info
def binance_future_place_order(self, symbol: str, direction: str, price: float, amount: int):
if direction == 'open_short':
side = 'SELL'
elif direction == 'close_short':
side = 'BUY'
else:
raise NotImplemented('Parameter `direction` only supports `open_short` and `close_short` currently')
params = {
'side': side,
'positionSide': 'SHORT',
'symbol': symbol,
'type': 'LIMIT',
'price': price,
'quantity': amount,
'timeInForce': 'GTC',
}
params['timestamp'] = int(time.time() * 1000)
order_info = self.exchange.dapiPrivatePostOrder(params)
self.logger.debug(f'({self.exchange.id}) Future orders SUCCESS: {direction} > {symbol} > {amount} > {price}')
self.logger.debug(f'Order info: {str(order_info)}')
return order_info
def binance_account_transfer(self, currency: str, amount, from_account='spot', to_account='coin-margin'):
"""
POST /sapi/v1/asset/transfer (HMAC SHA256)
"""
if from_account == 'spot' and to_account == 'coin-margin':
transfer_type = 'MAIN_CMFUTURE'
elif from_account == 'coin-margin' and to_account == 'spot':
transfer_type = 'CMFUTURE_MAIN'
else:
raise ValueError('Cannot recognize parameters for User Universal Transfer')
params = {
'type': transfer_type,
'asset': currency,
'amount': amount,
}
params['timestamp'] = int(time.time() * 1000)
transfer_info = self.exchange.sapiPostAssetTransfer(params=params)
self.logger.debug(f"({self.exchange.id}) Transfer SUCCESS: {from_account} --> {to_account} > amount: {amount}")
self.logger.debug(f'Transfer info: {str(transfer_info)}')
return transfer_info
def open_position(self):
execute_num = 0
while True:
spot_ask1 = self.exchange.publicGetTickerBookTicker(params={'symbol': self.spot_symbol['type1']})['askPrice']
coin_bid1 = self.exchange.dapiPublicGetTickerBookTicker(params={'symbol': self.future_symbol['type1']})[0]['bidPrice']
r = float(coin_bid1) / float(spot_ask1) - 1
operator = '>' if spot_ask1 > coin_bid1 else '<'
self.logger.info('Spot %.4f %s COIN-M %.4f -> Price Difference: %.4f%%' % (float(spot_ask1), operator, float(coin_bid1), r * 100))
if r < self.threshold:
self.logger.info('Price difference SMALLER than threshold >>> Retrying...')
else:
self.logger.debug('Price difference LARGER than threshold >>> Starting arbitrage...')
contract_num = int(self.amount / self.multipler[self.coin])
contract_coin_num = contract_num * self.multipler[self.coin] / float(coin_bid1)
contract_fee = contract_coin_num * self.contract_fee_rate
spot_amount = contract_coin_num / (1 - self.spot_fee_rate) + contract_fee
self.logger.debug(f'Arbitrage starts >>> future cotract num {contract_num} > coin-margin num {contract_coin_num} > fee {contract_fee} > spot amount {spot_amount}')
price = float(spot_ask1) * (1 + self.slippage)
params = {
'symbol': self.spot_symbol['type2'],
'direction': 'long',
'price': price,
'amount': spot_amount,
}
spot_order_info = self.retry_wrapper(func=self.binance_spot_place_order, params=params, act_name='Long spot orders')
price = float(coin_bid1) * (1 - self.slippage)
price = round(price, self.coin_precision)
params = {
'symbol': self.future_symbol['type1'],
'direction': 'open_short',
'price': price,
'amount': contract_num,
}
future_order_info = self.retry_wrapper(func=self.binance_future_place_order, params=params, act_name='Short coin-margin orders')
time.sleep(2)
balance = self.exchange.fetch_balance()
num = balance[self.coin]['free']
self.logger.debug(f'Amount to be transfered > {num}')
params = {
'currency': self.coin,
'amount': num,
'from_account': 'spot',
'to_account': 'coin-margin',
}
self.retry_wrapper(func=self.binance_account_transfer, params=params, act_name='Transfer (SPOT --> COIN-M)')
execute_num += 1
self.logger.info(f"Number of opening executions: {execute_num}")
print(spot_order_info['average'])
print(future_order_info)
time.sleep(2)
if execute_num >= self.num_maximum:
self.logger.info('Maximum execution number reached >>> Position opening stops.')
break
def close_position_utils(self):
"""close positions for basis trading"""
balance = self.exchange.fetch_balance()
num = balance['USDT']['free']
self.logger.info(f'Amount of USDT in spot account:{num}')
balance = self.exchange.fetch_balance()
num = balance[self.coin]['free']
self.logger.info(f'Amount of {self.coin} in coin-margin account:{num}')
if num < self.amount:
self.logger.error('Please ensure the coin-margin remaining balance is enough!')
now_execute_num = 0
while True:
spot_ask1 = self.exchange.publicGetTickerBookTicker(params={'symbol': self.spot_symbol['type1']})['bidPrice']
spot_ask1 = float(spot_ask1)
coin_bid1 = self.exchange.dapiPublicGetTickerBookTicker(params={'symbol': self.future_symbol['type1']})[0]['askPrice']
coin_bid1 = float(coin_bid1)
r = coin_bid1 / spot_ask1 - 1
operator = '>' if spot_ask1 > coin_bid1 else '<'
self.logger.info('Spot %.4f %s COIN-M %.4f -> Price Difference: %.4f%%' % (float(spot_ask1), operator, float(coin_bid1), r * 100))
if r > self.threshold:
self.logger.info('Price difference LARGER than threshold >>> Retrying...')
else:
self.logger.debug('Price difference SMALLER than threshold >>> Stopping arbitrage...')
contract_num = int(coin_bid1 * self.amount / self.multipler[self.coin])
contract_coin_num = contract_num * self.multipler[self.coin] / coin_bid1
contract_fee = contract_coin_num * self.contract_fee_rate
spot_amount = contract_coin_num - contract_fee
self.logger.debug(f'Closing contract num {contract_num} > equivalent coin num {contract_coin_num} > contract fee {contract_fee} > spot selling amount {spot_amount}')
price = coin_bid1 * (1 + self.slippage)
price = round(price, self.coin_precision)
params = {
'symbol': self.future_symbol['type1'],
'direction': 'close_short',
'price': price,
'amount': contract_num,
}
future_order_info = self.retry_wrapper(func=self.binance_future_place_order, params=params, act_name='Close short coin-margin orders')
price = spot_ask1 * (1 - self.slippage)
params = {
'symbol': self.spot_symbol['type2'],
'direction': 'short',
'price': price,
'amount': spot_amount,
}
spot_order_info = self.retry_wrapper(func=self.binance_spot_place_order, params=params, act_name='Long spot orders')
time.sleep(2)
params = {
'currency': self.coin,
# 'amount': self.amount,
'amount': num,
'from_account': 'coin-margin',
'to_account': 'spot',
}
self.retry_wrapper(func=self.binance_account_transfer, params=params, act_name='Transfer (COIN-M --> SPOT)')
now_execute_num = now_execute_num + 1
print(spot_order_info['average'])
print(future_order_info)
self.logger.info(f"Number of closing executions: {now_execute_num}")
time.sleep(2)
if now_execute_num >= self.num_maximum:
self.logger.info('Maximum execution number reached >>> Position closing stops.')
exit()
def close_position(self):
while True:
try:
self.close_position_utils()
except Exception as e:
self.logger.critical(f'Closing positions FAILED >>> Retrying...')
self.logger.warning(traceback.format_exc())
time.sleep(2)