def __init__(self, api_key='', secret='', entry_point='', auth=True, rest_entry_point=''): threading.Thread.__init__(self) self.factories = {} self._conns = {} self._connected_event = threading.Event() self._api_key = api_key self._secret = secret self._entry_point = entry_point self._user_timer = None self._listen_key = None if auth: self._client = BrokerClient(api_key=self._api_key, secret=self._secret, entry_point=rest_entry_point) if api_key and secret else None if not entry_point.endswith('/'): entry_point = entry_point + '/' self._entry_point = entry_point
def __init__(self, symbol, pm): self.symbol = symbol self.pm = pm self.api = BrokerClient(entry_point=self.pm.get( 'entry_point', 'https://api.bhex.com/openapi/'), api_key=self.pm['api_key'], secret=self.pm['secret']) # self.api = BhexClient(api_key=self.pm['api_key'], secret=self.pm['secret'], entry_point=self.pm.get('entry_point', 'https://api.bhex.com/openapi/')) self.imitate_symbol = self.pm.get('imitate_symbol', '') # load order config file self.tgtStructBid = pd.DataFrame.from_dict(self.pm['tgtStructBid']) self.tgtStructAsk = pd.DataFrame.from_dict(self.pm['tgtStructAsk']) # print(self.tgtStructBid.loc[str(1), 'fromBestBid'], self.tgtStructAsk) self.orderThread = None self.klineThread = None
class BrokerSocketManager(threading.Thread): def __init__(self, api_key='', secret='', entry_point='', auth=True, rest_entry_point=''): threading.Thread.__init__(self) self.factories = {} self._conns = {} self._connected_event = threading.Event() self._api_key = api_key self._secret = secret self._entry_point = entry_point self._user_timer = None self._listen_key = None if auth: self._client = BrokerClient(api_key=self._api_key, secret=self._secret, entry_point=rest_entry_point) if api_key and secret else None if not entry_point.endswith('/'): entry_point = entry_point + '/' self._entry_point = entry_point def _start_socket(self, id_, path, payload, callback): if id_ in self._conns: return False factory_url = self._entry_point + path print(factory_url) factory = BrokerClientFactory(factory_url, useragent=user_agent, payload=payload) factory.base_client = self factory.protocol = BrokerClientProtocol factory.callback = callback factory.reconnect = True self.factories[id_] = factory reactor.callFromThread(self.add_connection, id_) def _start_quote_socket(self, id_, payload, callback): self._start_socket(id_, 'quote/ws/v1', payload, callback) def _start_auth_socket(self, id_, payload, callback): listen_key = self._client.stream_get_listen_key() if not listen_key: raise BrokerRequestException('Get listen key failure.') self._listen_key = listen_key.get('listenKey', '') path = 'ws/' + self._listen_key self._start_socket(id_, path, payload, callback) self._start_user_timer() def add_connection(self, id_): factory = self.factories[id_] context_factory = ssl.ClientContextFactory() self._conns[id_] = connectWS(factory, context_factory) def _start_user_timer(self): self._user_timer = threading.Timer(1800, self._keep_alive_user_socket) self._user_timer.setDaemon(True) self._user_timer.start() def _keep_alive_user_socket(self): self._client.stream_keepalive(self._listen_key) self._start_user_timer() def stop_socket(self, conn_key): if conn_key not in self._conns: return self._conns[conn_key].factory = WebSocketClientFactory(self._entry_point) self._conns[conn_key].disconnect() del self._conns[conn_key] def run(self): try: reactor.run(installSignalHandlers=False) except ReactorAlreadyRunning: # Ignore error about reactor already runing pass def close(self): keys = set(self._conns.keys()) for key in keys: self.stop_socket(key) self._conns = {}
from broker.client import BrokerClient if __name__ == '__main__': from broker import broker_log broker_log.setLevel(logging.DEBUG) broker_log.addHandler(logging.StreamHandler()) proxies = { "http": "", "https": "", } entry_point = '' # like: https://api.xxx.yyy/openapi/ where xxx.yyy is your base domain # like: https://api.alteumx.com/openapi/ where alteumx.com is your base domain b = BrokerClient(entry_point, api_key='', secret='', proxies=proxies) print(b.time()) print(int(time.time() * 1000)) print(b.broker_info()) print(b.depth('BTCUSDT')) print(b.trades('BTCUSDT')) print(b.klines('BTCUSDT')) print(b.ticker_24hr('BTCUSDT'))
if __name__ == '__main__': from broker import broker_log broker_log.setLevel(logging.DEBUG) broker_log.addHandler(logging.StreamHandler()) proxies = { "http": "", "https": "", } api_key = '26lgbEfY4eeVXFX9U81GbLQIWrPoej10PKe80r7UQSeKANtZDsFn2h03P81nbnfW' #"fONXv5tzp4sguTNbons2YcwmE8sZCHoMKuDCnZbRekdB3REMqRZ0f8Nj93uYpZlZ" api_secret = 'E8dL7PtMUY8yfPsa0UqrPfqkYrmv3UhzjdiOn3EFH2SDqHzSDF1E0IKKZhPZn3IX' #"CIbyLoeBRyAVeJkqDT4sAGCkioKszVuZedOlIn8USegGuJkNQbie1H7FMkdx8pXj" entry_point = 'https://api.zg.com' # like: https://api.xxx.yyy/openapi/ where xxx.yyy is your base domain b = BrokerClient(entry_point, api_key=api_key, secret=api_secret, proxies=proxies) # print(b.time()) # print(int(time.time() * 1000)) # print(b.broker_info()) # print(b.depth('BTCUSDT')) # print(b.trades('BTCUSDT')) # print(b.klines('BTCUSDT')) # print(b.ticker_24hr('BTCUSDT'))
class icoMarketMaking(object): def __init__(self, symbol, pm): self.symbol = symbol self.pm = pm self.api = BrokerClient(entry_point=self.pm.get( 'entry_point', 'https://api.bhex.com/openapi/'), api_key=self.pm['api_key'], secret=self.pm['secret']) # self.api = BhexClient(api_key=self.pm['api_key'], secret=self.pm['secret'], entry_point=self.pm.get('entry_point', 'https://api.bhex.com/openapi/')) self.imitate_symbol = self.pm.get('imitate_symbol', '') # load order config file self.tgtStructBid = pd.DataFrame.from_dict(self.pm['tgtStructBid']) self.tgtStructAsk = pd.DataFrame.from_dict(self.pm['tgtStructAsk']) # print(self.tgtStructBid.loc[str(1), 'fromBestBid'], self.tgtStructAsk) self.orderThread = None self.klineThread = None def start(self): orders = self.api.open_orders(symbol=self.symbol, limit=1000) self.tgtStructBid['cid'] = [''] * len(self.tgtStructBid) self.tgtStructAsk['cid'] = [''] * len(self.tgtStructAsk) orders = sorted(orders, key=lambda x: float(x['price']), reverse=True) i = 0 for o in orders: if o['side'] == 'BUY' and o['clientOrderId'][:len( self.pm['orderPrefix'])] == self.pm['orderPrefix']: if i < self.pm['orderNum']: self.tgtStructBid.loc[str(i), 'cid'] = o['clientOrderId'] i += 1 else: self.api.order_cancel(client_order_id=o['clientOrderId']) i = 0 for o in reversed(orders): if o['side'] == 'SELL' and o['clientOrderId'][:len( self.pm['orderPrefix'])] == self.pm['orderPrefix']: if i < self.pm['orderNum']: self.tgtStructAsk.loc[str(i), 'cid'] = o['clientOrderId'] i += 1 else: self.api.order_cancel(client_order_id=o['clientOrderId']) self.orderThread = Thread(target=self.orderWorker) self.orderThread.start() self.klineThread = Thread(target=self.drawKline) self.klineThread.start() # to calculate the fair bid/ask price def getFairPrice(self): try: depth = self.api.depth(symbol=self.symbol, limit=100) print(str(depth['bids'])) bids = depth['bids'] pqsum = qsum = 0 for b in bids: qsum += float(b[1]) pqsum += float(b[0]) * float(b[1]) if qsum >= self.pm['fairBidPriceQty']: break fair_bid_price = pqsum / qsum asks = depth['asks'] pqsum = qsum = 0 for a in asks: qsum += float(a[1]) pqsum += float(a[0]) * float(a[1]) if qsum >= self.pm['fairAskPriceQty']: break fair_ask_price = pqsum / qsum if self.imitate_symbol: tgt = self.api.klines(symbol=self.imitate_symbol, limit=1)[0] chg = float(tgt[4]) / float(tgt[1]) fair_bid_price *= chg fair_ask_price *= chg return fair_bid_price, fair_ask_price except: traceback.print_exc() # order placing fn, will check one pair of bid and ask at one time def orderWorker(self): while True: for i in reversed(range(self.pm['orderNum'])): print(i) try: fair_bid_price, fair_ask_price = self.getFairPrice() # print(fair_bid_price, fair_ask_price) fair_mid_price = 0.5 * (fair_bid_price + fair_ask_price) best_bid = fair_mid_price * (1 - self.pm['bid_spread']) best_ask = fair_mid_price * (1 + self.pm['ask_spread']) send_flag = True price = best_bid * ( 1 - self.tgtStructBid.loc[str(i), 'fromBestBid']) order = self.api.order_get( orig_client_order_id=self.tgtStructBid.loc[str(i), 'cid']) print(str(order)) try: if order['status'] == 'FILLED': None elif abs(float(order['price']) - price) > 0.0005 * price: self.api.order_cancel( client_order_id=self.tgtStructBid.loc[str(i), 'cid']) else: send_flag = False except: traceback.print_exc() print(order) if send_flag: params = { 'symbol': self.symbol, 'side': 'BUY', 'type': 'LIMIT_MAKER', 'quantity': self.pm['orderQty'] * self.tgtStructBid.loc[str(i), 'qtyMultiplier'] * (1 + 0.6 * (rd.random() - 0.5)), 'price': price * (1 + 0.0005 * (rd.random() - 0.5)), 'newClientOrderId': self.pm['orderPrefix'] + str(int(time.time() * 1000)) + 'b' + str(i) } self.prepareOrder(params) self.tgtStructBid.loc[ str(i), 'cid'] = params['newClientOrderId'] send_flag = True price = best_ask * ( 1 + self.tgtStructAsk.loc[str(i), 'fromBestAsk']) order = self.api.order_get( orig_client_order_id=self.tgtStructAsk.loc[str(i), 'cid']) try: if order['status'] == 'FILLED': None elif abs(float(order['price']) - price) > 0.0005 * price: self.api.order_cancel( client_order_id=self.tgtStructAsk.loc[str(i), 'cid']) else: send_flag = False except: traceback.print_exc() print(order) if send_flag: params = { 'symbol': self.symbol, 'side': 'SELL', 'type': 'LIMIT_MAKER', 'quantity': self.pm['orderQty'] * self.tgtStructAsk.loc[str(i), 'qtyMultiplier'] * (1 + 0.6 * (rd.random() - 0.5)), 'price': price * (1 + 0.0005 * (rd.random() - 0.5)), 'newClientOrderId': self.pm['orderPrefix'] + str(int(time.time() * 1000)) + 'a' + str(i) } self.prepareOrder(params) self.tgtStructAsk.loc[ str(i), 'cid'] = params['newClientOrderId'] except: traceback.print_exc() time.sleep(self.pm.get('orderRefreshInterval', 5)) # draw k line fn def drawKline(self): while True: try: res = self.api.ticker_24hr(symbol=self.symbol) # print(res) best_bid = float(res['bestBidPrice']) best_ask = float(res['bestAskPrice']) cid = str(int(time.time())) order = { 'symbol': self.symbol, 'quantity': self.pm['orderQty'] * (0.2 + 1.6 * rd.random()), 'price': round(best_bid + (best_ask - best_bid) * rd.random(), self.pm['priceDecimalLength']), 'newClientOrderId': cid } qty = order['quantity'] if best_bid + 0.5 * (best_ask - best_bid) < order[ 'price'] < best_ask or order['price'] == best_ask: order['side'] = 'SELL' order['type'] = 'LIMIT_MAKER' self.prepareOrder(order) # time.sleep(3 + 7 * rd.random()) order.pop('newClientOrderId') order['side'] = 'BUY' order['type'] = 'LIMIT' order['timeInForce'] = 'IOC' order['quantity'] = qty * (0.1 + 0.8 * rd.random()) self.prepareOrder(order) # time.sleep(2 + 5 * rd.random()) if rd.random() < 0.2: order['quantity'] = qty - order['quantity'] self.prepareOrder(order) self.api.order_cancel(client_order_id=cid) elif best_bid < order['price'] < best_bid + 0.5 * ( best_ask - best_bid) or order['price'] == best_bid: order['side'] = 'BUY' order['type'] = 'LIMIT_MAKER' self.prepareOrder(order) # time.sleep(3 + 7 * rd.random()) order.pop('newClientOrderId') order['side'] = 'SELL' order['type'] = 'LIMIT' order['timeInForce'] = 'IOC' order['quantity'] = qty * (0.1 + 0.8 * rd.random()) self.prepareOrder(order) # time.sleep(2 + 5 * rd.random()) if rd.random() < 0.2: order['quantity'] = qty - order['quantity'] self.prepareOrder(order) self.api.order_cancel(client_order_id=cid) except: traceback.print_exc() print(res) time.sleep(10 + 30 * rd.random()) # random cancel order fn def randomCancel(self): try: orders = self.api.open_orders(symbol=self.symbol, limit=1000) i = int(rd.random() * len(orders)) self.api.order_cancel(client_order_id=orders[i]['clientOrderId']) print('randomCancel', orders[i]) except: traceback.print_exc() # send order fn def prepareOrder(self, params): params['price'] = round(params['price'], self.pm['priceDecimalLength']) params['quantity'] = round(params['quantity'], self.pm['qtyDecimalLength']) res = self.api.order_new(**params) if res.get('msg', '') == 'Balance insufficient ': print(res, params) self.randomCancel() print(self.api.account()) elif 'msg' in res: print(res, params)