def fill_depth(floor, upper, symbol, api_key, secret_key, digit=4, number=20, exchange=CgCoin()): """ :param floor: 挂单数量下限 :param upper: 挂单数量上限 :param symbol: 交易对 :param api_key: :param secret_key: :param number: 用于补单的触发条件 :param exchange: 交易所 :return: """ exchange.auth(api_key, secret_key) now = str(datetime.datetime.now()).split('.')[0] data = get_data_from_cg(symbol) bids = data['bids'] asks = data['asks'] print('当前时间:%s,交易对:%s,买单深度:%s档,卖单深度:%s档' % (now, symbol, bids[0], asks[0])) if bids[0] <= number: for _ in range(10): rand = round(random.uniform(0.98, 0.999), 4) price = round(bids[1] * rand, digit) amount = round(random.uniform(floor, upper), 6) order_buy = exchange.create_order(symbol=symbol, side=1, type=1, price=price, amount=amount, source=3) print('挂单价格:%s,挂单数量:%s, 买单:%s' % (price, amount, order_buy)) if asks[0] <= number: for _ in range(10): rand = round(random.uniform(1.001, 1.02), 4) price = round(asks[1] * rand, digit) amount = round(random.uniform(floor, upper), 6) order_sell = exchange.create_order(symbol=symbol, side=2, type=1, price=price, amount=amount, source=3) print('挂单价格:%s,挂单数量:%s, 卖单:%s' % (price, amount, order_sell)) print() time.sleep(0.5)
def repeal_bid(symbol, api_key, secret_key, num=25): """ :param num: 限制数量,表示还剩多少的挂单 :param symbol: 交易对 :param api_key: :param secret_key: :return: """ exchange = CgCoin() exchange.auth(api_key, secret_key) try: while True: # state : [int] 订单状态 0.正常 1.已完成 2.已撤销 3.部分成交 4.部分成交已撤销 5.撤销中 -1.获取历史成交 order_info = exchange.get_order_list(symbol=symbol, state=0, page=0, size=1000) # 可能会报签名错误 if order_info['status'] == 200: break order_df = pd.DataFrame( order_info['data'] ['list']) # 取order_info['data']['list']的信息,并将其转换成DataFrame格式 # "side" : 交易方向 1.买入 2.卖出 orders = order_df[order_df['side'] == 1] # 筛选买单深度列表 orders = orders.sort_values(by='price', ascending=False) # 对买单按价格排降序 orders = orders.drop_duplicates(subset='price', keep='first') # 价格去重 # counter = orders.count()['id'] # 数量 # print(counter) ids = orders['id'][num:] # 买单的所有ID if list(ids): print('需要撤销的买单ID:', '\n', ids) for id in ids: for _ in range(5): order = exchange.cancel(id) print('(%s)撤买单:%s - %s' % (symbol, id, order)) if order['status'] == 200: break time.sleep(0.5) else: print('暂无撤销的卖单!') except Exception as error: print('错误:', error)
def user_balance(user, symbol, api_key, secret_key, exchange=CgCoin()): for _ in range(30): exchange.auth(api_key, secret_key) msg = dict(exchange.get_user_balance()) # print(msg) if msg['status'] == 200: data = msg['data'] data = pd.DataFrame(data) # 转换成dataframe格式 data = pd.DataFrame(data, columns=['coin', 'amount', 'available']) # 对dataframe的行进行帅选、从新排序 data.rename(columns={ 'coin': '货币', 'amount': '总量', 'available': '可用量' }, inplace=True) print('%s(%s)的资产为:' % (user, symbol)) print(str(data) + '\n') break time.sleep(1)
def refresh_quantity(api_key, secret_key, price_low, price_high, symbol='cgt_eth', cg=CgCoin()): cg.auth(api_key, secret_key) differ = int(price_high * pow(10, 6) - price_low * pow(10, 6)) for _ in range(differ): now = str(datetime.datetime.now()).split('.')[0] amount = round(random.uniform(700, 1000), 4) price_low = round(price_low + 0.000001, 6) print('当前时刻:%s, 挂单价格:%s, 挂单数量:%s' % (now, price_low, amount)) buy_order = cg.create_order(symbol=symbol, side=1, type=1, price=price_low, amount=amount, source=3) # 买单 print(buy_order) print() time.sleep(2)
def refresh_quantity(api_key, secret_key, symbol='cgt_eth', cg=CgCoin()): cg.auth(api_key, secret_key) now = str(datetime.datetime.now()).split('.')[0] bid, ask = get_bid_and_ask_from_cg() print('当前时刻:%s, 买一价:%s, 卖一价:%s' % (now, bid, ask)) for _ in range(200): price = round(random.uniform(float(bid), float(ask)), 6) if float(bid) < price < float(ask): break if float(bid) < price < float(ask): amount = round(random.uniform(200000, 260000), 4) print('成交价:%s, 成交量:%s' % (price, amount)) sell_order = cg.create_order(symbol=symbol, side=2, type=1, price=price, amount=amount, source=3) # 卖单 print('卖单:', sell_order) if sell_order['status'] == 200: order_id = sell_order['data'] buy_order = cg.create_order(symbol=symbol, side=1, type=1, price=price, amount=amount, source=3) # 买单 print('买单:', buy_order) if buy_order['status'] != 200: for _ in range(3): cancel_order = cg.cancel(order_id) print('撤单:', cancel_order) if cancel_order['status'] == 200: break else: print('价差过小') print()
async def repeal_orders(side, symbol, api_key, secret_key, num=25): """ :param side: "side" : 控制交易方向 1.买入 2.卖出 :param symbol: 交易对 :param api_key: :param secret_key: :param num: 用于控制触发撤单的数目 :return: """ exchange = CgCoin() exchange.auth(api_key, secret_key) try: for _ in range(10): # state : [int] 订单状态 0.正常 1.已完成 2.已撤销 3.部分成交 4.部分成交已撤销 5.撤销中 -1.获取历史成交 info = exchange.get_order_list(symbol=symbol, state=0, page=0, size=30000) # 可能会报签名错误 if info['status'] == 200: break print(info) data = pd.DataFrame( info['data'] ['list']) # 取order_info['data']['list']的信息,并将其转换成DataFrame格式 await asyncio.sleep(1) # "side" : 交易方向 1.买入 2.卖出 if side == 1: msg = data[data['side'] == 1] # 筛选买单深度列表信息 orders = pd.DataFrame( msg, columns=['price', 'amount', 'id']) # 过滤信息,只看'price', 'amount', 'id'这三列 orders = orders.sort_values(by='price', ascending=False) # 对买单按价格排降序 orders_dup = orders.drop_duplicates(subset='price', keep='first') # 价格去重,重复元素保留第一个 counter_1 = orders_dup.count()['id'] # 数量 print('%s买单深度:%s档' % (symbol, counter_1)) if counter_1 >= num: all_orders = orders.append(orders_dup.head(num)) all_orders = all_orders.sort_values(by='price', ascending=False) # 排降序 _orders = all_orders.drop_duplicates( subset=['id'], keep=False) # _orders为orders与orders_dup.head(num)的差集 buy_ids = _orders['id'] # 卖单的所有ID print('需要撤销的买单ID:', '\n', buy_ids) for id in buy_ids: await asyncio.sleep(1) for _ in range(5): order = exchange.cancel(id) print('(%10s)撤买单:%s - %s' % (symbol, id, order)) if order['status'] == 200: break # time.sleep(1) else: print('%s暂无撤销的买单!' % symbol) elif side == 2: msg = data[data['side'] == 2] # 筛选买单深度列表信息 orders = pd.DataFrame( msg, columns=['price', 'amount', 'id']) # 过滤信息,只看'price', 'amount', 'id'这三列 orders = orders.sort_values(by='price') # 对卖单按价格排升序 orders_dup = orders.drop_duplicates(subset='price', keep='first') # 价格去重,重复元素保留第一个 counter_2 = orders_dup.count()['id'] # 数量 print('%s卖单深度:%s档' % (symbol, counter_2)) if counter_2 >= num: all_orders = orders.append(orders_dup.head(num)) all_orders = all_orders.sort_values(by='price') _orders = all_orders.drop_duplicates( subset=['id'], keep=False) # _orders为orders与orders_dup.head(num)的差集 sell_ids = _orders['id'] # 卖单的所有ID print('需要撤销的卖单ID:', '\n', sell_ids) for id in sell_ids: await asyncio.sleep(1) for _ in range(5): order = exchange.cancel(id) print('(%10s)撤卖单:%s - %s' % (symbol, id, order)) if order['status'] == 200: break # time.sleep(1) else: print('%s暂无撤销的卖单!' % symbol) else: print('传参错误, "side"控制交易方向: 1.买入 2.卖出') print( '*******************************************************************' ) except Exception as error: print('错误:', error)
time.sleep(0.5) if __name__ == '__main__': fill_depth(0.1, 0.3, 'xeth_usdt', '', '', digit=2) fill_depth(0.03, 0.08, 'xbtc_usdt', '', '', digit=2) fill_depth(3, 10, 'xeos_usdt', '', '', digit=4) fill_depth(6, 10, 'xxrp_usdt', '', '', digit=4) # https: // demo.i.cg.net fill_depth(0.1, 0.3, 'xeth_usdt', '', '', digit=2, exchange=CgCoin(base_url='https://demo.i.cg.net')) fill_depth(0.03, 0.08, 'xbtc_usdt', '', '', digit=2, exchange=CgCoin(base_url='https://demo.i.cg.net')) fill_depth(0.12, 0.15, 'eth_usdt', '', '') # fill_depth(1, 3, 'cgt_eth', '', '', digit=6) fill_depth(5, 20, 'xrp_usdt', '', '') fill_depth(0.002, 0.003, 'btc_usdt', '', '') fill_depth(0.002, 0.003, 'eth_btc', '', '', digit=8)