-
Notifications
You must be signed in to change notification settings - Fork 6
/
exchanges.py
325 lines (257 loc) · 10.6 KB
/
exchanges.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
from abc import ABCMeta, abstractmethod
import hashlib
import hmac
import logging
import os
import requests
import time
from liquidpy.api import *
import python_bitbankcc
logging.basicConfig(level=logging.INFO, format='%(levelname)s %(asctime)s %(message)s')
logger = logging.getLogger()
SYMBOL_SEPARATOR = '/'
class Rebalancer(metaclass=ABCMeta):
def __init__(self, symbol: str):
if SYMBOL_SEPARATOR not in symbol:
raise ValueError('')
self.asset1, self.asset2 = symbol.split(SYMBOL_SEPARATOR)
@abstractmethod
def get_balance(self) -> dict[str, float]:
pass
@abstractmethod
def cancel_all_orders(self):
pass
@abstractmethod
def get_ltp(self) -> float:
pass
@abstractmethod
def get_min_order_size(self) -> float:
pass
@abstractmethod
def create_order(self, side: str, quantity: float, price: float) -> str:
pass
@property
def trade_coin(self) -> str:
return self.asset1
@property
def base_coin(self) -> str:
return self.asset2
@abstractmethod
def get_min_order_unit(self) -> float:
pass
@abstractmethod
def get_best_ask_price(self) -> float:
pass
@abstractmethod
def get_best_bid_price(self) -> float:
pass
@abstractmethod
def get_price_prec(self) -> float:
pass
class LiquidRebalancer(Rebalancer):
def __init__(self, symbol: str):
super().__init__(symbol)
self.client = Liquid()
if symbol == 'BTC/JPY':
self.product_id = PRODUCT_ID_BTCJPY
elif symbol == 'ETH/JPY':
self.product_id = PRODUCT_ID_ETHJPY
elif symbol == 'XRP/JPY':
self.product_id = PRODUCT_ID_XRPJPY
elif symbol == 'BCH/JPY':
self.product_id = PRODUCT_ID_BCHJPY
elif symbol == 'QASH/JPY':
self.product_id = PRODUCT_ID_QASHJPY
elif symbol == 'SOL/JPY':
self.product_id = PRODUCT_ID_SOLJPY
elif symbol == 'FTT/JPY':
self.product_id = PRODUCT_ID_FTTJPY
def get_balance(self) -> dict[str, float]:
balance = self.client.get_accounts_balance()
asset1 = sum([float(b['balance']) for b in balance if b['currency'] == self.asset1])
asset2 = sum([float(b['balance']) for b in balance if b['currency'] == self.asset2])
if not asset1 and not asset2:
raise SystemError(f"Neither coin has a balance. [{self.asset1}, {self.asset2}]")
return {self.asset1: asset1, self.asset2: asset2}
def cancel_all_orders(self):
logger.info(f"Cancel all orders.")
self.client.cancel_all_orders()
def get_ltp(self) -> float:
return float(self.client.get_products(product_id=self.product_id)['last_traded_price'])
def get_min_order_size(self) -> float:
return MIN_ORDER_QUANTITY[self.product_id]
def create_order(self, side: str, quantity: float, price: float) -> str:
return self.client.create_order(self.product_id, side, quantity, price)['id']
def get_min_order_unit(self) -> float:
raise NotImplementedError
def get_best_ask_price(self) -> float:
raise NotImplementedError
def get_best_bid_price(self) -> float:
raise NotImplementedError
class BitbankRebalancer(Rebalancer):
config = {
'btc': {
'min_order_size': 0.0001,
'min_order_unit': 0.0001,
'order_price_prec': 0,
},
'eth': {
'min_order_size': 0.0001,
'min_order_unit': 0.0001,
'order_price_prec': 0,
},
'xrp': {
'min_order_size': 0.0001,
'min_order_unit': 0.0001,
'order_price_prec': 3,
},
}
def __init__(self, symbol: str):
super().__init__(symbol)
coins = symbol.split(SYMBOL_SEPARATOR)
self.asset1 = coins[0].lower()
self.asset2 = coins[1].lower()
self.pair = f'{self.asset1}_{self.asset2}'
self.pub = python_bitbankcc.public()
self.prv = python_bitbankcc.private(os.getenv('BITBANK_API_KEY'), os.getenv('BITBANK_API_SECRET'))
def get_balance(self) -> dict[str, float]:
try:
assets = self.prv.get_asset()['assets']
except Exception as e:
raise e
asset1 = float([a for a in assets if a['asset'] == self.asset1][0]['onhand_amount'])
asset2 = float([a for a in assets if a['asset'] == self.asset2][0]['onhand_amount'])
if not asset1 and not asset2:
raise SystemError(f"Neither coin has a balance. [{self.asset1}, {self.asset2}]")
return {self.asset1: asset1, self.asset2: asset2}
def cancel_all_orders(self):
orders = self.prv.get_active_orders(self.pair)
for o in orders['orders']:
logger.info(f"Cancel order. [order_id: {o['order_id']}]")
self.prv.cancel_order(self.pair, o['order_id'])
def get_ltp(self) -> float:
return float(self.pub.get_ticker(self.pair)['last'])
def get_min_order_size(self) -> float:
return __class__.config[self.asset1]['min_order_size']
def create_order(self, side: str, quantity: float, price: float) -> str:
return self.prv.order(self.pair, price, quantity, side, 'limit', True)['order_id']
def get_min_order_unit(self) -> float:
return __class__.config[self.asset1]['min_order_unit']
def get_best_ask_price(self) -> float:
return float(self.pub.get_ticker(self.pair)['sell'])
def get_best_bid_price(self) -> float:
return float(self.pub.get_ticker(self.pair)['buy'])
def get_price_prec(self) -> int:
return __class__.config[self.asset1]['order_price_prec']
class GmoRebalancer(Rebalancer):
pub_url: str = 'https://api.coin.z.com/public'
prv_url: str = 'https://api.coin.z.com/private'
config = {
'BTC': {
'min_order_size': 0.0001,
'min_order_unit': 0.0001,
'order_price_prec': 0,
},
'ETH': {
'min_order_size': 0.01,
'min_order_unit': 0.0001,
'order_price_prec': 0,
},
'XRP': {
'min_order_size': 1,
'min_order_unit': 1,
'order_price_prec': 3,
},
}
def __init__(self, symbol: str):
super().__init__(symbol)
self.api_key = os.getenv('GMO_API_KEY')
self.api_secret = os.getenv('GMO_API_SECRET')
coins = symbol.split(SYMBOL_SEPARATOR)
self.asset1 = coins[0].upper()
self.asset2 = coins[1].upper()
if self.asset1 not in __class__.config.keys():
raise ValueError(f"Asset is not supported. [{self.asset1}]")
def __create_auth_header(self, method: str, path: str, data: str = '') -> dict:
timestamp = '{0}000'.format(int(time.mktime(datetime.now().timetuple())))
text = timestamp + method + path + data
sign = hmac.new(
bytes(self.api_secret.encode('ascii')),
bytes(text.encode('ascii')),
hashlib.sha256
).hexdigest()
return {
'API-KEY': self.api_key,
'API-TIMESTAMP': timestamp,
'API-SIGN': sign,
}
def get_balance(self) -> dict[str, float]:
path = '/v1/account/assets'
headers = self.__create_auth_header('GET', path)
res = requests.get(f"{__class__.prv_url}{path}", headers=headers)
balance = json.loads(res.text)
# check if error occurred
self.__raise_err_if_fail(balance)
for b in balance['data']:
if b['symbol'] == self.asset1:
asset1 = float(b['amount'])
elif b['symbol'] == self.asset2:
asset2 = float(b['amount'])
if not asset1 and not asset2:
raise SystemError(f"Neither coin has a balance. [{self.asset1}, {self.asset2}]")
return {self.asset1: asset1, self.asset2: asset2}
def cancel_all_orders(self):
logger.info(f"Cancel all orders.")
path = '/v1/cancelBulkOrder'
params = {'symbols': [self.asset1]}
headers = self.__create_auth_header('POST', path, json.dumps(params))
res = requests.post(f"{__class__.prv_url}{path}", headers=headers, data=json.dumps(params))
body = json.loads(res.text)
# check if error occurred
self.__raise_err_if_fail(body)
def get_ltp(self) -> float:
res = requests.get(f"{__class__.pub_url}/v1/ticker?symbol={self.asset1}")
ticker = json.loads(res.text)
return float(ticker['data'][0]['last'])
def get_min_order_size(self) -> float:
return __class__.config[self.asset1]['min_order_size']
def create_order(self, side: str, quantity: float, price: float) -> str:
# adjust price
prec = __class__.config[self.asset1]['order_price_prec']
price_s = str(price if prec > 0 else int(price))
path = '/v1/order'
params = {
'symbol': f'{self.asset1}',
'side': side.upper(),
'executionType': 'LIMIT',
'price': price_s,
'size': str(quantity),
'timeInForce': 'SOK',
}
headers = self.__create_auth_header('POST', path, json.dumps(params))
res = requests.post(f"{__class__.prv_url}{path}", headers=headers, data=json.dumps(params))
body = json.loads(res.text)
# check if error occurred
self.__raise_err_if_fail(body)
return str(body['data'])
def get_min_order_unit(self) -> float:
return __class__.config[self.asset1]['min_order_unit']
def get_best_ask_price(self) -> float:
res = requests.get(f"{__class__.pub_url}/v1/ticker?symbol={self.asset1}")
ticker = json.loads(res.text)
return float(ticker['data'][0]['ask'])
def get_best_bid_price(self) -> float:
res = requests.get(f"{__class__.pub_url}/v1/ticker?symbol={self.asset1}")
ticker = json.loads(res.text)
return float(ticker['data'][0]['bid'])
def get_price_prec(self) -> int:
return __class__.config[self.asset1]['order_price_prec']
def __raise_err_if_fail(self, body) -> str:
if body['status'] != 0:
err_msg = ''
msg = body['messages']
for i in range(len(msg)):
err_msg += msg[i]['message_code'] + ':' + msg[i]['message_string']
if i < len(msg) - 1:
err_msg += ', '
raise SystemError(f"Failed to create order: {err_msg}")