def __init__(self, ex_name, sym, sym_base, price_decimal, start_price, refer_ex_name, refer_sym, refer_sym_base, scale, change_mode=False, change_sym=False, change_sym_base=False, adjust_by_market=False): Price.__init__(self, ex_name, sym, sym_base, price_decimal, start_price, adjust_by_market) if refer_ex_name.lower() == refer_ex_name: self.api_refer = Exchange(refer_ex_name, ['', '']).api else: self.api_refer = self.api_bxx self.refer_sym = refer_sym self.refer_sym_base = refer_sym_base self.refer_symbol = [self.refer_sym, self.refer_sym_base] self.scale = scale self.change_mode = change_mode self.change_symbol = [change_sym, change_sym_base]
def __init__(self, ex_name, sym, sym_base, price_decimal, start_price, adjust_by_market=False): # 初始化时需要指定交易对和起始价格 self.ex_name = ex_name self.sym = sym self.sym_base = sym_base self.symbol = [self.sym, self.sym_base] self.api_bxx = Exchange('bxx', [self.ex_name, '', '']).api self.price = start_price self.decimal = price_decimal self.flag_adjust = adjust_by_market
class FixScalePrice(Price): def __init__(self, ex_name, sym, sym_base, price_decimal, start_price, refer_ex_name, refer_sym, refer_sym_base, scale, change_mode=False, change_sym=False, change_sym_base=False, adjust_by_market=False): Price.__init__(self, ex_name, sym, sym_base, price_decimal, start_price, adjust_by_market) if refer_ex_name.lower() == refer_ex_name: self.api_refer = Exchange(refer_ex_name, ['', '']).api else: self.api_refer = self.api_bxx self.refer_sym = refer_sym self.refer_sym_base = refer_sym_base self.refer_symbol = [self.refer_sym, self.refer_sym_base] self.scale = scale self.change_mode = change_mode self.change_symbol = [change_sym, change_sym_base] def set_params(self, scale, adjust_by_market=False): self.scale = scale self.flag_adjust = adjust_by_market def refresh_price(self): try: depth = self.api_refer.get_depth(self.refer_symbol) # 传入不同的self.scale(比例) 可以的到不同的比例的价格 并让价格保留几位小数 new_price = round( (float(depth['asks'][0][0]) + float(depth['bids'][0][0])) / 2 * self.scale, self.decimal) if self.change_mode == 'm': # 获取比对交易对上一次的开高低收的价格的平均值作为基准 tic = eval(self.api_bxx.get_tick(self.change_symbol)) new_price = round(new_price * sum(tic[1:5]) / 4, self.decimal) if self.change_mode == 'd': tic = eval(self.api_bxx.get_tick(self.change_symbol)) new_price = round(new_price / sum(tic[1:5]) * 4, self.decimal) # 自己平台定价统一美元计算 若需要参照coinw(人民币)时需要用到汇率换算 if self.change_mode == 'CNY': USDT_CNY = get_USDT_CNY() new_price = round(new_price / USDT_CNY, self.decimal) self.price = new_price # 根据当前市场价格 调整定价 if self.flag_adjust: self.adjust_by_market() except Exception as e: print(e) return self.price
class Price: def __init__(self, ex_name, sym, sym_base, price_decimal, start_price, adjust_by_market=False): # 初始化时需要指定交易对和起始价格 self.ex_name = ex_name self.sym = sym self.sym_base = sym_base self.symbol = [self.sym, self.sym_base] self.api_bxx = Exchange('bxx', [self.ex_name, '', '']).api self.price = start_price self.decimal = price_decimal self.flag_adjust = adjust_by_market def set_params(self, *args): # 给定一种策略,重新设置参数 pass def refresh_price(self): # 给定一种策略,刷新并返回价格 try: if self.flag_adjust: self.adjust_by_market() except Exception as e: print(e) return self.price def adjust_by_market(self): # 根据当前市场价格调整定价 try: tic = eval(self.api_bxx.get_tick(self.symbol)) self.price = round((0.7 * self.price + 0.3 * sum(tic[1:5]) / 4), self.decimal) except Exception as e: print(e) def slight_wave(self, wave_range=0.001): return round( self.price * (1 + random.uniform(-wave_range, wave_range)), self.decimal) def save_price(self, out_dir, flag_new=False): # 将当前时间和价格存储至日志,或者存储至临时文件与其它进程交流 mode = 'a' if flag_new: mode = 'w' with open(out_dir, mode) as f: f.writelines(time.asctime() + '\n' + self.ex_name + ' ' + self.sym + ' ' + self.sym_base + '\n' + str(self.price) + '\n\n')
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 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
class FixScalePrice(Price): def __init__(self, ex_name, sym, sym_base, price_decimal, start_price, refer_ex_name, refer_sym, refer_sym_base, scale, change_mode=False, change_sym=False, change_sym_base=False, adjust_by_market=False): Price.__init__(self, ex_name, sym, sym_base, price_decimal, start_price, adjust_by_market) if refer_ex_name.lower() == refer_ex_name: self.api_refer = Exchange(refer_ex_name, ['', '']).api else: self.api_refer = self.api_bxx self.refer_sym = refer_sym self.refer_sym_base = refer_sym_base self.refer_symbol = [self.refer_sym, self.refer_sym_base] self.scale = scale self.change_mode = change_mode self.change_symbol = [change_sym, change_sym_base] def set_params(self, scale, adjust_by_market=False): self.scale = scale self.flag_adjust = adjust_by_market def refresh_price(self): try: depth = self.api_refer.get_depth(self.refer_symbol) new_price = round((float(depth['asks'][0][0]) + float(depth['bids'][0][0])) / 2 * self.scale, self.decimal) if self.change_mode == 'm': tic = eval(self.api_bxx.get_tick(self.change_symbol)) new_price = round(new_price * sum(tic[1:5]) / 4, self.decimal) if self.change_mode == 'd': tic = eval(self.api_bxx.get_tick(self.change_symbol)) new_price = round(new_price / sum(tic[1:5]) * 4, self.decimal) if self.change_mode == 'CNY': USDT_CNY = get_USDT_CNY() new_price = round(new_price / USDT_CNY, self.decimal) self.price = new_price if self.flag_adjust: self.adjust_by_market() except Exception as e: print(e) return self.price
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))
print('交易区') print(sym_base) balance_bxx[ex_name][sym_base] = dict() ex_bxx[ex_name][sym_base] = dict() balance_base[ex_name][sym_base] = dict() balance_diff[ex_name][sym_base] = dict() # balance_diff_all[sym_base] = dict() if sym_base not in balance_diff_all.keys(): balance_diff_all[sym_base] = dict() for sym in account_info[ex_name][sym_base].keys(): print(sym) balance_bxx[ex_name][sym_base][sym] = [0, 0] balance_base[ex_name][sym_base][sym] = 0 balance_diff[ex_name][sym_base][sym] = 0 ex_bxx[ex_name][sym_base][sym] = [ Exchange('bxx', key) for key in account_info[ex_name][sym_base][sym] ] # if sym not in balance_diff_all.keys(): # balance_diff_all[sym] = 0 balance_diff_all[sym_base][sym] = 0 # 获取某个交易所中某个交易对的操盘账户的总余额 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]
count = 0 balance = dict() balance_new_list = list() # while True: for i in parames: ex_name = i[0] sym = i[1][0] sym_base = i[1][1] if ex_name not in balance.keys(): balance[ex_name] = dict() if sym_base not in balance[ex_name].keys(): balance[ex_name][sym_base] = dict() symbol = [sym, sym_base] bxx_key = account_info[ex_name][symbol[1]][symbol[0]] # ex_bxx = [Exchange('bxx', key) for key in bxx_key] ex_bxx = [Exchange('bxx', key) for key in bxx_token] sum_balance = get_all_balance() balance[ex_name][sym_base][sym] = sum_balance[0] # balance[sym_base] = sum_balance[1] now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S') print(str(now_time) + sym + "," + sym_base + ' 结果获取结束') time.sleep(10) balance_new_list.append(balance) if count == 0: balance_old_list = balance_new_list balance = {} balance_new_list = [] print(balance_old_list) print(balance_new_list) # else: # now_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
fcoin_key = [ '948f765b4bd7427c9c02d09fc42e6c8d', '3bfa3d0fd9534a8fb0f55884d0460021' ] huobi_key = [ '23d16220-7b1883cc-dc468afc-6865d', 'fe31646f-78407147-ec119bc8-cbdab' ] coinw_key = [ 'a048a77f-b61e-49c5-b459-3ad171242959', 'ZTMF18573EILZLUIBB4RARLDOM3YCFOAYPXZ' ] # !!!initiate ex_bin = Exchange('binance', binance_key) # ex_zb = Exchange('zb', zb_key) ex_huobi = Exchange('huobi', huobi_key) ex_hadax = Exchange('hadax', huobi_key) ex_bxx = [Exchange('bxx', key) for key in bxx_key] ex_bxx1 = ex_bxx[-2] ex_bxx2 = ex_bxx[-1] if sym == 'HB': symbol = ['HB', 'USDT'] ex_bxx1 = ex_bxx[-1] sdecimal = [6, 4] if sym == 'DVC':