def result(self): if self.refresh_flag: # may add a thread of order info for thr in self.__thread_cancel: thr.join() self.__thread_order.join() # confirm success of 'cancel' tempc = 0 temp_order_id = [] all_order_id = list(self.active_order_id) for idt in self.active_order_id: err_flag = False res = self.__thread_cancel[tempc].get_result() if 'errcode' in res.keys(): err_flag = True if not err_flag: temp_order_id.append(idt) tempc += 1 self.active_order_id = [] # add active order id to list res = self.__thread_order.get_result() if res and str(res) not in ['0', 'None', '500', '405']: self.active_order_id.append(self.__thread_order.get_result()) # self.order_id_list.append(self.__thread_order.get_result()) # reset thread self.__thread_cancel = [] self.__thread_order = MyThread(plog) self.refresh_flag = False return temp_order_id, all_order_id else: return [], []
def get_all_balance(): while True: try: th_b = [] for ex in ex_bxx: th_b.append(MyThread(ex.api.get_account, args=())) for th in th_b: th.start() time.sleep(0.5) # [th.start() for th in th_b] [th.join() for th in th_b] ac_list = [th.get_result() for th in th_b] sum_sym = 0 sum_sym_base = 0 for acl in ac_list: for dic in acl: if dic['coinName'] == sym: sum_sym += dic['carryingAmount'] elif dic['coinName'] == sym_base: sum_sym_base += dic['carryingAmount'] except Exception as e: print(e) else: break return sum_sym, sum_sym_base
def refresh(self, standard_price, my_bid, my_ask): if self.side == 'buy': low_price = standard_price * (1 - self.__low) high_price = standard_price * (1 - self.__high) dtag = (low_price < self.fake_price) else: low_price = standard_price * (1 + self.__low) high_price = standard_price * (1 + self.__high) dtag = (low_price > self.fake_price) if not self.active_order_id: dtag = True if random.uniform(0, 1) < self.__refresh_rate and time.time( ) - self.__latest_refresh_time > self.delay: dtag = True if self.side == 'buy' and low_price >= my_ask: dtag = False if self.side == 'sell' and low_price <= my_bid: dtag = False if dtag: self.refresh_flag = True self.__latest_refresh_time = time.time() self.delay = 0 for idt in self.active_order_id: self.__thread_cancel.append( MyThread(self.__api.cancel, args=(idt, ))) self.fake_price = round(random.uniform(low_price, high_price), self.__price_decimal) fake_qty = round( random.uniform(0.5 * self.__base_quantity, 1.5 * self.__base_quantity), self.__quant_decimal) if random.randint(0, 1) == 1: fake_qty = round(fake_qty, symbol_quant_int_decimal) # print(self.__symbol, self.side, self.fake_price, fake_qty) fake_qty = max(fake_qty, min_quant) self.__thread_order = MyThread(self.__api.order, args=( self.__symbol, self.side, fake_qty, self.fake_price, )) # may add a thread of order info for thr in self.__thread_cancel: thr.start() self.__thread_order.start()
def cancle(orderid): print('开始撤销订单') ex_huobi = [Exchange('huobi', key) for key in huobi_account] th_h = [] for ex in ex_huobi: th_h.append(MyThread(ex.api.cancel, args=(orderid, ))) # for ex in ex_bxx[ex_name][symbol[1]][symbol[0]]: # th_b.append(MyThread(ex.api.get_account, args=())) [th.start() for th in th_h] [th.join() for th in th_h] order_result = [th.get_result() for th in th_h] print('订单撤销完毕') print(order_result) return order_result
def __init__(self, order_type, api_id, symbol, side, low, high, base_quantity, refresh_rate, decimal, range_scale): self.order_type = order_type # self.order_id_list = [] self.active_order_id = [] self.__thread_cancel = [] self.__thread_order = MyThread(plog) self.__thread_query = [] self.api_id = api_id self.__api = ex_bxx[api_id].api self.__symbol = symbol self.sym = symbol[0] self.sym_base = symbol[1] self.side = side self.__low = low * range_scale self.__high = high * range_scale self.fake_price = 0 self.__base_quantity = base_quantity self.refresh_flag = False self.__refresh_rate = refresh_rate self.__price_decimal = decimal[0] self.__quant_decimal = decimal[1] self.__latest_refresh_time = time.time() self.delay = 0
def get_huobi_balance(): th_h = [] ex_huobi = [Exchange('huobi', key) for key in huobi_account] for ex in ex_huobi: th_h.append(MyThread(ex.api.get_account, args=())) # for ex in ex_bxx[ex_name][symbol[1]][symbol[0]]: # th_b.append(MyThread(ex.api.get_account, args=())) [th.start() for th in th_h] [th.join() for th in th_h] ac_list = [th.get_result() for th in th_h] blance_huobi = dict() for acl in ac_list[0]: if acl['type'] != 'frozen': blance_huobi[acl['currency']] = acl['balance'] print(blance_huobi) return blance_huobi
def get_all_balance(ex_name, symbol): th_b = [] for ex in ex_bxx[ex_name][symbol[1]][symbol[0]]: th_b.append(MyThread(ex.api.get_account, args=())) [th.start() for th in th_b] [th.join() for th in th_b] ac_list = [th.get_result() for th in th_b] sum_sym = 0 sum_sym_base = 0 for acl in ac_list: for dic in acl: if dic['coinName'] == symbol[0]: sum_sym += dic['carryingAmount'] elif dic['coinName'] == symbol[1]: sum_sym_base += dic['carryingAmount'] balance_bxx[ex_name][symbol[1]][symbol[0]] = [sum_sym, sum_sym_base]
def get_all_balance(): th_b = [] for ex in ex_bxx: th_b.append(MyThread(ex.api.get_account, args=())) [th.start() for th in th_b] [th.join() for th in th_b] ac_list = [th.get_result() for th in th_b] sum_sym = 0 sum_sym_base = 0 for acl in ac_list: for dic in acl: if dic['coinName'] == sym: sum_sym += dic['carryingAmount'] elif dic['coinName'] == sym_base: sum_sym_base += dic['carryingAmount'] return sum_sym, sum_sym_base
def get_price(sym_base, sym, type): print('开始获取市场深度') symbol = [sym, sym_base] th_h = [] ex_huobi = [Exchange('huobi', key) for key in huobi_account] for ex in ex_huobi: th_h.append(MyThread(ex.api.get_depth, args=(symbol, ))) # for ex in ex_bxx[ex_name][symbol[1]][symbol[0]]: # th_b.append(MyThread(ex.api.get_account, args=())) [th.start() for th in th_h] [th.join() for th in th_h] price_dict = [th.get_result() for th in th_h] # 卖 if type == 'asks': print(price_dict) price = price_dict[0][type][4][0] print(price) return price # 买 elif type == 'bids': print(price_dict) price = price_dict[0][type][4][0] print(price) return price
# *_*coding:utf-8 *_* import sys import os import time from ex_api.exchange import Exchange, MyThread start_time = time.time() account_info = dict() account_info['TTEX'] = { 'BTC': { 'POWR': [], }, } for i in range(5000, 7000): account_info['TTEX']['BTC']['POWR'].append( ['TTEX', str(12012340001 + i), '1234Rty77899x']), symbol = ['POWR', "BTC"] bxx_key = account_info["TTEX"]["BTC"]['POWR'] ex_bxx = [Exchange('bxx', key) for key in bxx_key] th_cancel_all = [] for i in range(len(ex_bxx), 0, -1): th_cancel_all.append( MyThread(ex_bxx[i - 1].cancel_all_bxx, args=(symbol, ))) # ex_bxx[i - 1].cancel_all_bxx(symbol) [th.start() for th in th_cancel_all] [th.join() for th in th_cancel_all] end_time = time.time() print('&&&&&&&&&&&&&&&&&&&&&&') print('共用%s' % (end_time - start_time))
# if balance1 > balance2: # ex_buy = ex_bxx2 # ex_sell = ex_bxx1 # else: # ex_buy = ex_bxx1 # ex_sell = ex_bxx2 num += 1 print('No', num, 'time:', round(time.time() - test_quantity.otime, 3)) print(time.asctime()) try: test_quantity.refresh_volume() except Exception as e: print('refresh volume failed') if num % 10 == 0: thp_cancel = list() thp_cancel.append(MyThread(ex_bxx1.cancel_all_bxx, args=(symbol, ))) # thp_cancel.append(MyThread(ex_bxx2.cancel_all_bxx, args=(symbol,))) [th.start() for th in thp_cancel] # ex_bxx1.cancel_all_bxx(symbol) # ex_bxx2.cancel_all_bxx(symbol) try: dep = ex_bxx1.api.get_depth(symbol) bid_1 = 0 bid_1_q = 0 if len(dep['bids']) > 0: bid_1 = dep['bids'][0][0] bid_1_q = dep['bids'][0][1] ask_1 = dep['asks'][0][0] ask_1_q = dep['asks'][0][1] bid_1 = max(ask_1 * 0.7, bid_1) # depth = tempcurl(url_symbol)
def start_confirm(self): for idt in self.active_order_id: self.__thread_query.append( MyThread(self.__api.get_query, args=(idt, ))) [thr.start() for thr in self.__thread_query]
class fake_order: def __init__(self, order_type, api_id, symbol, side, low, high, base_quantity, refresh_rate, decimal, range_scale): self.order_type = order_type # self.order_id_list = [] self.active_order_id = [] self.__thread_cancel = [] self.__thread_order = MyThread(plog) self.__thread_query = [] self.api_id = api_id self.__api = ex_bxx[api_id].api self.__symbol = symbol self.sym = symbol[0] self.sym_base = symbol[1] self.side = side self.__low = low * range_scale self.__high = high * range_scale self.fake_price = 0 self.__base_quantity = base_quantity self.refresh_flag = False self.__refresh_rate = refresh_rate self.__price_decimal = decimal[0] self.__quant_decimal = decimal[1] self.__latest_refresh_time = time.time() self.delay = 0 def print_info(self): print('symbol:', self.__symbol) print('side:', self.side) def set_quantity(self, new_quantity): self.__base_quantity = new_quantity def set_api_id(self, new_api_id): self.api_id = new_api_id self.__api = ex_bxx[new_api_id].api def reset_params_midnight(self): self.__low += 0.05 self.__high += 0.05 self.__base_quantity /= 5 def reset_params_normal(self): self.__low -= 0.05 self.__high -= 0.05 self.__base_quantity *= 5 def start_confirm(self): for idt in self.active_order_id: self.__thread_query.append( MyThread(self.__api.get_query, args=(idt, ))) [thr.start() for thr in self.__thread_query] def end_confirm(self): cond = 0 num_id = 0 for thr in self.__thread_query: thr.join() ret = thr.get_result() try: if ret and float(ret['deal']) > 0: cond += 1 # with open('./' + self.__symbol[0] + '_' + self.__symbol[1] + '_hedge/id_list.txt', 'a') as f: # f.writelines(self.active_order_id[num_id] + ' ') except Exception: print(ret) num_id += 1 if cond > 0: self.__latest_refresh_time = time.time() if self.order_type == 'thin': self.delay = 30 self.__thread_query = [] return cond def refresh(self, standard_price, my_bid, my_ask): if self.side == 'buy': low_price = standard_price * (1 - self.__low) high_price = standard_price * (1 - self.__high) dtag = (low_price < self.fake_price) else: low_price = standard_price * (1 + self.__low) high_price = standard_price * (1 + self.__high) dtag = (low_price > self.fake_price) if not self.active_order_id: dtag = True if random.uniform(0, 1) < self.__refresh_rate and time.time( ) - self.__latest_refresh_time > self.delay: dtag = True if self.side == 'buy' and low_price >= my_ask: dtag = False if self.side == 'sell' and low_price <= my_bid: dtag = False if dtag: self.refresh_flag = True self.__latest_refresh_time = time.time() self.delay = 0 for idt in self.active_order_id: self.__thread_cancel.append( MyThread(self.__api.cancel, args=(idt, ))) self.fake_price = round(random.uniform(low_price, high_price), self.__price_decimal) fake_qty = round( random.uniform(0.5 * self.__base_quantity, 1.5 * self.__base_quantity), self.__quant_decimal) if random.randint(0, 1) == 1: fake_qty = round(fake_qty, symbol_quant_int_decimal) # print(self.__symbol, self.side, self.fake_price, fake_qty) fake_qty = max(fake_qty, min_quant) self.__thread_order = MyThread(self.__api.order, args=( self.__symbol, self.side, fake_qty, self.fake_price, )) # may add a thread of order info for thr in self.__thread_cancel: thr.start() self.__thread_order.start() def result(self): if self.refresh_flag: # may add a thread of order info for thr in self.__thread_cancel: thr.join() self.__thread_order.join() # confirm success of 'cancel' tempc = 0 temp_order_id = [] all_order_id = list(self.active_order_id) for idt in self.active_order_id: err_flag = False res = self.__thread_cancel[tempc].get_result() if 'errcode' in res.keys(): err_flag = True if not err_flag: temp_order_id.append(idt) tempc += 1 self.active_order_id = [] # add active order id to list res = self.__thread_order.get_result() if res and str(res) not in ['0', 'None', '500', '405']: self.active_order_id.append(self.__thread_order.get_result()) # self.order_id_list.append(self.__thread_order.get_result()) # reset thread self.__thread_cancel = [] self.__thread_order = MyThread(plog) self.refresh_flag = False return temp_order_id, all_order_id else: return [], []
0.1 * base_order_quantity_scale * quantity_scale_sell, 0.2, sdecimal, base_order_range_scale)) for i in range(12): thin_order_sell[i].set_api_id(2 * (11 - i)) for i in range(12): thin_order_buy[i].set_api_id((11 - i) * 2 + 1) for i in range(12): far_order[i].set_api_id(i + 24) th_cancel_all = [] for i in range(len(ex_bxx), 0, -1): th_cancel_all.append( MyThread(ex_bxx[i - 1].cancel_all_bxx, args=(symbol, ))) # ex_bxx[i - 1].cancel_all_bxx(symbol) [th.start() for th in th_cancel_all] [th.join() for th in th_cancel_all] cur_time = time.time() id_buy = '' id_sell = '' num = 0 my_bid_1 = 0 my_ask_1 = 1000000 flag_midnight = False if sym in fake_coin:
def refresh_order(self): self.__thread_order = MyThread(self.api.order, args=(self.symbol, self.side, self.quantity, self.price,)) return self.__thread_order
th_order.append(test_pressure(api, stand_price, 'buy', symbol, quantity_buy, '0.00002074').refresh_order()) # api = sell_order_msg[api_id] quantity_sell = round(random.random() + 1, 8) th_order.append(test_pressure(api, stand_price, 'sell', symbol, quantity_sell, '0.00002074').refresh_order()) [th.start() for th in th_order] # result = [th.get_result() for th in th_order] # print('############################################## ' + str(time.time())) # print('%s个账户在同时交易' % (i + 1) * 10) # print(str(result)) time.sleep(1) count += 1 print('&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&') print('程序跑完了') # end_time = time.time() # print('共需要%s时间' % (end_time - start_time)) if count == 3600: start_time = time.time() symbol = ['POWR', "BTC"] bxx_key = account_info["TTEX"]["BTC"]['POWR'] ex_bxx = [Exchange('bxx', key) for key in bxx_key] th_cancel_all = [] for i in range(len(ex_bxx), 0, -1): th_cancel_all.append(MyThread(ex_bxx[i - 1].cancel_all_bxx, args=(symbol,))) # ex_bxx[i - 1].cancel_all_bxx(symbol) [th.start() for th in th_cancel_all] [th.join() for th in th_cancel_all] end_time = time.time() print('&&&&&&&&&&&&&&&&&&&&&&') print('共用%s' % (end_time - start_time)) break