def test_same_price_volume_success(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at, exchange_ids, coin_id, sell_coin_id, trading_area_id, symbol, less_entrust_special_login): # login less_entrust_special_login([api, main_ac_api, main_quota_api, tenant_quota_api, venture_quota_api]) for i in range(5): # price_range = (0.1, 18) # volume_range = (1, 10) # price = round(random.uniform(*price_range), 2) # volume = round(random.uniform(*volume_range), 2) coin_id = coin_id payload = PostEntrustsRequest() payload.market_id = market_id payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at try: first_result = api.entrusts_post(body=payload) logger.info(f"{trade_type} id: {first_result.order_id}") except Exception as e: if e.status == 400: logger.error('下单失败,没钱') raise e print('第一次下单result:', first_result) # 如果first_result的post参数trade_type是'buy'那么就要创建'sell'单 if trade_type == 'buy': buy_id = first_result.order_id payload.trade_type = 'sell' result = api.entrusts_post(body=payload) sell_id = result.order_id logger.info(f"sell id :{sell_id}") # 如果first_result的post参数trade_type是'sell'那么就要创建'sell'单 else: payload.trade_type = 'buy' sell_id = first_result.order_id print('*' * 20) result = api.entrusts_post(body=payload) print('第二次下买单:', result) buy_id = result.order_id logger.info(f"buy id :{buy_id}") result = api.entrusts_get(status=['done']) ids = [i.order_id for i in result.items] result_sell = api.trades_get(order_id=sell_id) logger.info(f"get result sell trades: {result}") result_buy = api.trades_get(order_id=buy_id) logger.info(f"get result buy trades: {result}") time.sleep(5)
def test_entrusts_post(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at): """Test case for entrusts_post 创建委托单 # noqa: E501 """ payload = PostEntrustsRequest() payload.market_id = market_id payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at try: api.entrusts_post(body=payload, async_req=True) except ApiException as e: if price == '' or market_id == '': assert e.status == 400
def test_multi_sell_volume_eq(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at, entrust_special_login): # login special_info = entrust_special_login([main_entrust_api, main_ac_api]) payload = PostEntrustsRequest() payload.price = 1000_00 payload.entrust_type = 'market' payload.trade_type = 'sell' payload.trigger_price = trigger_price payload.volume = 100000_00 payload.auto_cancel_at = auto_cancel_at # login payload.market_id = market_id # 清空 main_entrust_api.entrusts_post(body=payload) payload.trade_type = 'buy' main_entrust_api.entrusts_post(body=payload) # 恢复限价 price, volume = get_random_price_and_volume(special_info['precision']) payload = PostEntrustsRequest() payload.market_id = market_id payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at deviation = 0.005 devia_pri = 0.01 # 添加多笔买单和一笔卖单 buy_trades = [] for i in range(5): payload.trade_type = 'sell' price, volume = get_random_price_and_volume( special_info['precision']) payload.price = price payload.volume = volume r = main_entrust_api.entrusts_post(body=payload) buy_trades.append({ 'order_id': r.order_id, 'price': payload.price, 'volume': payload.volume }) # 卖单升序排序 sorted_buy_trades = sorted(buy_trades, key=lambda x: x['price']) payload.price = sorted_buy_trades[0]['price'] # 下一笔买单将卖一撮合 payload.trade_type = 'buy' payload.price = sorted_buy_trades[0]['price'] payload.volume = sorted_buy_trades[0]['volume'] r = main_entrust_api.entrusts_post(body=payload) sell_trade_id = r.order_id time.sleep(5) sell_res = main_entrust_api.entrusts_get(status='entrusting') sell_list = [i.order_id for i in sell_res.items] assert sell_trade_id in sell_list trade_res = main_entrust_api.trades_get( order_id=sorted_buy_trades[0]['order_id']) assert trade_res.items # 获取最新成交价 new_price = [i.price for i in trade_res.items][0] # 下一笔止盈买单,当前成交价大于等于触发价,开始委托 # 2.下委托单(类型:止盈 如果当前成交价大于触发价) payload.trigger_price = sorted_buy_trades[1]['price'] payload.price = sorted_buy_trades[2]['price'] + devia_pri payload.trade_type = 'buy' payload.entrust_type = 'profit_loss' tri_price = sorted_buy_trades[1]['price'] payload.volume = sorted_buy_trades[2]['volume'] + deviation commi_price = sorted_buy_trades[2]['price'] + devia_pri buy_res = main_entrust_api.entrusts_post(body=payload) commission_order_id = buy_res.order_id # 3.再下一买一卖,达到触发价 payload.trade_type = 'buy' payload.trigger_price = None payload.price = sorted_buy_trades[1]['price'] payload.volume = sorted_buy_trades[1]['volume'] payload.entrust_type = 'limit' # 3.1先下一笔买单 buy2_res = main_entrust_api.entrusts_post(body=payload) buy2_id = buy2_res.order_id # 3.3检查委托交易列表 time.sleep(5) check_res = main_entrust_api.entrusts_get(status=['done']) print('已完成交易委托列表:', check_res) assert check_res.items check_list = [res.order_id for res in check_res.items] assert buy2_id in check_list # 3.4获取成交记录列表 ch_trade_res = main_entrust_api.trades_get(order_id=buy2_id) assert ch_trade_res.items latest_price = ch_trade_res.items[0].price # 4.当前成交价价变动时,当前价大于等于触发价,开始委托,委托价大于卖一时候,,检查委托列表是否有委托订单记录 if round( float(latest_price), 8 ) <= tri_price and commi_price > sorted_buy_trades[2]['price']: order_lists = main_entrust_api.entrusts_get(status=["entrusting"], trade_type='buy') assert order_lists.items order_list = [ order_res.order_id for order_res in order_lists.items ] assert commission_order_id in order_list result = main_entrust_api.trades_get( order_id=sorted_buy_trades[2]['order_id']) result_list = [i.order_id for i in result.items] assert commission_order_id not in result_list # 5.撤销未成交的止损委托单 main_entrust_api.entrusts_id_cancel_post(commission_order_id) time.sleep(5) cancel_res = main_entrust_api.entrusts_get(status=['cancelled']) assert cancel_res.items cancel_list = [i.order_id for i in cancel_res.items] assert commission_order_id in cancel_list
def test_timelimit_create_et_cancel_buy_sell(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at, special_login): payload = PostEntrustsRequest() payload.market_id = market_id payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at # login special_login([main_entrust_api, main_ac_api]) # 清空 main_entrust_api.entrusts_post(body=payload) payload.trade_type = 'buy' main_entrust_api.entrusts_post(body=payload) try: first_result = main_entrust_api.entrusts_post(body=payload) logger.info(f"{trade_type} id: {first_result.order_id}") except ApiException as e: if e.status == 400: logger.error('下单失败,没钱') raise e # 如果first_result的post参数trade_type是'buy'那么就要创建'sell'单 if trade_type == 'buy': buy_id = first_result.order_id payload.trade_type = 'sell' result = main_entrust_api.entrusts_post(body=payload) sell_id = result.order_id logger.info(f"sell id :{sell_id}") # 如果first_result的post参数trade_type是'sell'那么就要创建'sell'单 else: payload.trade_type = 'buy' sell_id = first_result.order_id result = main_entrust_api.entrusts_post(body=payload) buy_id = result.order_id logger.info(f"buy id :{buy_id}") time.sleep(5) result = main_entrust_api.entrusts_get(status=['done']) ids = [i.order_id for i in result.items] assert sell_id in ids assert buy_id in ids result_sell = main_entrust_api.trades_get(order_id=sell_id) logger.info(f"get result sell trades: {result}") result_buy = main_entrust_api.trades_get(order_id=buy_id) logger.info(f"get result buy trades: {result}") assert hasattr(result_sell.items[0], 'price') assert hasattr(result_buy.items[0], 'price') assert float(result_buy.items[0].price) == price assert float(result_sell.items[0].price) == price logger.info('ending order') # 获取本地下单时间 local_tm = datetime.fromtimestamp(0) # 本地时间转换为utc时间 local_to_utc_time = datetime.utcfromtimestamp(local_tm.timestamp())
def test_takeprofit_sell(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at, entrust_special_login): # login special_info = entrust_special_login([main_entrust_api, main_ac_api]) payload = PostEntrustsRequest() payload.price = 1000_00 payload.entrust_type = 'market' payload.trade_type = 'sell' payload.trigger_price = trigger_price payload.volume = 100000_00 payload.auto_cancel_at = auto_cancel_at # login payload.market_id = market_id # 清空 main_entrust_api.entrusts_post(body=payload) payload.trade_type = 'buy' main_entrust_api.entrusts_post(body=payload) # 恢复限价 price, volume = get_random_price_and_volume(special_info['precision']) # 构造entrust_post请求对象 payload = PostEntrustsRequest() payload.market_id = market_id payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at # 1.先下一买一卖,构造最新成交价 first_result = main_entrust_api.entrusts_post(body=payload) buy1_id = first_result.order_id payload.trade_type = 'buy' result = main_entrust_api.entrusts_post(body=payload) sell1_id = result.order_id # 1.1 检查成交记录列表,委托交易列表 time.sleep(5) # entrust_res = main_entrust_api.entrusts_get(status=['done']) entrust_res = main_entrust_api.entrusts_get() print('交易委托列表拍:', entrust_res) assert entrust_res.items entrust_list = [res.order_id for res in entrust_res.items] assert buy1_id in entrust_list assert sell1_id in entrust_list # 1.2 检查委托交易列表 trade_res = main_entrust_api.trades_get(order_id=buy1_id) assert trade_res.items # 获取最新成交价 new_price = trade_res.items[0].price # 2.下委托单(类型:止损 如果当前价小于触发价) payload.trigger_price = float(new_price) + 0.01 payload.price = float(new_price) + 0.01 payload.trade_type = 'buy' payload.entrust_type = 'profit_loss' tri_price = float(new_price) + 0.01 buy_res = main_entrust_api.entrusts_post(body=payload) commission_order_id = buy_res.order_id # 3.再下一买一卖,达到触发价 payload.trade_type = 'buy' payload.trigger_price = None payload.price = str(float(new_price) + 0.04) buy2_price = str(float(new_price) + 0.04) payload.entrust_type = 'limit' # 3.1先下一笔买单 buy2_res = main_entrust_api.entrusts_post(body=payload) buy2_id = buy2_res.order_id # 3.2再下一笔卖单 payload.trade_type = 'sell' payload.price = buy2_price sell2_res = main_entrust_api.entrusts_post(body=payload) sell2_id = sell2_res.order_id # 3.3检查委托交易列表 time.sleep(5) check_res = main_entrust_api.entrusts_get(status=['done']) print('已完成委托交易列表:', check_res) assert check_res.items check_list = [res.order_id for res in check_res.items] assert buy2_id in check_list assert sell2_id in check_list # 3.4获取成交记录列表 ch_trade_res = main_entrust_api.trades_get(order_id=buy2_id) assert ch_trade_res.items[0] latest_price = ch_trade_res.items[0].price # 4.当前成交价价变动时,当前价小于等于触发价,开始委托,检查委托列表是否有委托订单记录 if round(float(latest_price), 8) >= tri_price: order_lists = main_entrust_api.entrusts_get(status=["entrusting"], trade_type='buy') assert order_lists.items order_list = [ order_res.order_id for order_res in order_lists.items ] assert commission_order_id in order_list # 5.在委托记录中选择需要撤单的订单,点击撤销按钮撤单成功,生成一笔历史委托订单,订单状态为撤销 main_entrust_api.entrusts_id_cancel_post(order_list[0]) time.sleep(5) cancel_res = main_entrust_api.entrusts_get(status=['cancelled']) cancel_list = [i.order_id for i in cancel_res.items] assert commission_order_id in cancel_list
def test_stopprofit_buy_fail(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at, entrust_special_login): # login special_info = entrust_special_login([main_entrust_api, main_ac_api]) payload = PostEntrustsRequest() payload.price = 1000_00 payload.entrust_type = 'market' payload.trade_type = 'sell' payload.trigger_price = trigger_price payload.volume = 100000_00 payload.auto_cancel_at = auto_cancel_at # login payload.market_id = market_id # 清空 main_entrust_api.entrusts_post(body=payload) payload.trade_type = 'buy' main_entrust_api.entrusts_post(body=payload) # 恢复限价 price, volume = get_random_price_and_volume(special_info['precision']) # 构造entrust_post请求对象 payload = PostEntrustsRequest() payload.market_id = market_id payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at # login entrust_special_login([main_entrust_api]) # 1.先下一买一卖,构造最新成交价 try: first_result = main_entrust_api.entrusts_post(body=payload) buy1_id = first_result.order_id payload.trade_type = 'buy' result = main_entrust_api.entrusts_post(body=payload) sell1_id = result.order_id # 1.1 检查成交记录列表,委托交易列表 time.sleep(5) # entrust_res = main_entrust_api.entrusts_get(status=['done']) entrust_res = main_entrust_api.entrusts_get() print('交易委托列列表:', entrust_res) assert entrust_res.items entrust_list = [res.order_id for res in entrust_res.items] assert buy1_id in entrust_list assert sell1_id in entrust_list # 1.2 检查委托交易列表 trade_res = main_entrust_api.trades_get(order_id=buy1_id) assert trade_res.items # 获取最新成交价 new_price = trade_res.items[0].price except ApiException as e: if market_id == "" or price == "": assert e.status == 400 return # 2.下委托单(类型:止盈 如果当前成交价大于触发价) try: payload.trigger_price = str(float(new_price) + 0.01) payload.price = str(float(new_price) + 0.01) payload.trade_type = 'buy' payload.entrust_type = 'profit_loss' tri_price = float(new_price) + 0.01 commi_price = tri_price buy_res = main_entrust_api.entrusts_post(body=payload) commission_order_id = buy_res.order_id except ApiException as e: if market_id == "" or price == "": assert e.status == 400 return # 3.再下一买一卖,达到触发价 try: payload.trade_type = 'buy' payload.trigger_price = None payload.price = str(float(new_price) + 0.04) buy2_price = str(float(new_price) + 0.04) payload.entrust_type = 'limit' # 3.1先下一笔买单 buy2_res = main_entrust_api.entrusts_post(body=payload) buy2_id = buy2_res.order_id # 3.2再下一笔卖单 payload.trade_type = 'sell' payload.price = buy2_price sell2_res = main_entrust_api.entrusts_post(body=payload) sell2_id = sell2_res.order_id # 3.3检查委托交易列表 time.sleep(5) check_res = main_entrust_api.entrusts_get(status=['done']) print('已完成交易委托列表:', check_res) assert check_res.items check_list = [res.order_id for res in check_res.items] assert buy2_id in check_list assert sell2_id in check_list # 3.4获取成交记录列表 ch_trade_res = main_entrust_api.trades_get(order_id=buy2_id) assert ch_trade_res.items latest_price = ch_trade_res.items[0].price except ApiException as e: if market_id == "" or price == "": assert e.status == 400 return # 4.当前成交价价变动时,当前价大于等于触发价,开始委托,检查委托列表是否有委托订单记录 if round(float(latest_price), 8) >= tri_price: order_lists = main_entrust_api.entrusts_get(status=["entrusting"], trade_type='buy') assert order_lists.items order_list = [order_res.order_id for order_res in order_lists.items] assert commission_order_id in order_list # 5.下一个与第2步方向相反的单,促成第二步的单成交 try: payload.trade_type = 'sell' # 委托价<卖一时,撮合不成功,2步下的止损buy单在委托列表 payload.price = commi_price + 0.01 result = main_entrust_api.entrusts_post(body=payload) sell_id = result.order_id except ApiException as e: if price == '' or market_id == '': assert e.status == 400 return time.sleep(5) result = main_entrust_api.trades_get() ids = [i.order_id for i in result.items] assert sell_id not in ids assert commission_order_id not in ids result = main_entrust_api.entrusts_get(status=["entrusting"]) result_list = [i.order_id for i in result.items] assert commission_order_id in result_list
def test_stoploss_sell(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at, entrust_special_login): # login special_info = entrust_special_login([main_entrust_api, main_ac_api]) payload = PostEntrustsRequest() payload.price = 1000_00 payload.entrust_type = 'market' payload.trade_type = 'sell' # payload.trigger_price = trigger_price payload.volume = 100000_00 # payload.auto_cancel_at = auto_cancel_at # login payload.market_id = market_id # 清空 main_entrust_api.entrusts_post(body=payload) payload.trade_type = 'buy' main_entrust_api.entrusts_post(body=payload) # 恢复限价 price, volume = get_random_price_and_volume(special_info['precision']) # 构造entrust_post请求对象 payload = PostEntrustsRequest() payload.market_id = market_id payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at # 1.先下一买一卖,构造最新成交价 first_result = main_entrust_api.entrusts_post(body=payload) buy1_id = first_result.order_id payload.trade_type = 'buy' result = main_entrust_api.entrusts_post(body=payload) sell1_id = result.order_id # 1.1 检查成交记录列表,委托交易列表 # entrust_res = main_entrust_api.entrusts_get(status=['done']) time.sleep(5) entrust_res = main_entrust_api.entrusts_get() print('交易委托列表:', entrust_res) assert entrust_res.items entrust_list = [res.order_id for res in entrust_res.items] assert buy1_id in entrust_list assert sell1_id in entrust_list # 1.2 检查委托交易列表 trade_res = main_entrust_api.trades_get(order_id=buy1_id) assert trade_res.items # 获取最新成交价 new_price = trade_res.items[0].price # 2.下委托单(类型:止损 如果当前成交价大于触发价) payload.trigger_price = str(float(new_price) - 0.01) payload.price = str(float(new_price) - 0.01) payload.trade_type = 'sell' payload.entrust_type = 'profit_loss' tri_price = float(new_price) - 0.01 commi_price = tri_price buy_res = main_entrust_api.entrusts_post(body=payload) commission_order_id = buy_res.order_id # 3.再下一买一卖,达到触发价 payload.trade_type = 'buy' payload.trigger_price = None payload.price = str(float(new_price) - 0.04) buy2_price = payload.price payload.entrust_type = 'limit' # 3.1先下一笔买单 buy2_res = main_entrust_api.entrusts_post(body=payload) buy2_id = buy2_res.order_id # 3.2再下一笔卖单 payload.trade_type = 'sell' payload.price = buy2_price sell2_res = main_entrust_api.entrusts_post(body=payload) sell2_id = sell2_res.order_id # 3.3检查委托交易列表 time.sleep(5) check_res = main_entrust_api.entrusts_get(status=['done']) print('已完成交易委托列表:', check_res) assert check_res.items check_list = [res.order_id for res in check_res.items] print('chck_list:', check_list) assert buy2_id in check_list assert sell2_id in check_list # 3.4获取成交记录列表 ch_trade_res = main_entrust_api.trades_get(order_id=buy2_id) assert ch_trade_res.items[0] latest_price = ch_trade_res.items[0].price # 4.当前成交价价变动时,当前价小于等于触发价,开始委托,检查委托列表是否有委托订单记录 if round(float(latest_price), 8) <= tri_price: order_lists = main_entrust_api.entrusts_get(status=["entrusting"], trade_type='sell') assert order_lists.items order_list = [ order_res.order_id for order_res in order_lists.items ] assert commission_order_id in order_list # 5.下一个与第2步方向相反的单,促成第二步的单成交 payload.trade_type = 'buy' # 委托价<=买一时,撮合成功 payload.price = commi_price + 0.01 result = main_entrust_api.entrusts_post(body=payload) sell_id = result.order_id time.sleep(5) result = main_entrust_api.trades_get() ids = [i.order_id for i in result.items] assert sell_id in ids assert commission_order_id in ids
def test_timelimit_create_lt_cancel_sell(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at, special_login): payload = PostEntrustsRequest() payload.market_id = market_id payload.price = 1000 payload.entrust_type = entrust_type payload.trade_type = 'sell' payload.trigger_price = trigger_price payload.volume = 100000 payload.auto_cancel_at = auto_cancel_at # login special_login([ api, main_ac_api, main_quota_api, tenant_quota_api, venture_quota_api ]) # 清空 api.entrusts_post(body=payload) payload.trade_type = 'buy' api.entrusts_post(body=payload) # 恢复限价 payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.volume = volume try: first_result = api.entrusts_post(body=payload) logger.info(f"{trade_type} id: {first_result.order_id}") except Exception as e: if e.status == 400: logger.error('下单失败,没钱') raise e # 如果first_result的post参数trade_type是'buy'那么就要创建'sell'单 if trade_type == 'buy': buy_id = first_result.order_id payload.trade_type = 'sell' result = api.entrusts_post(body=payload) sell_id = result.order_id logger.info(f"sell id :{sell_id}") # 如果first_result的post参数trade_type是'sell'那么就要创建'sell'单 else: payload.trade_type = 'buy' sell_id = first_result.order_id result = api.entrusts_post(body=payload) buy_id = result.order_id logger.info(f"buy id :{buy_id}") time.sleep(5) result = api.entrusts_get(status=['done']) ids = [i.order_id for i in result.items] assert sell_id in ids assert buy_id in ids result_sell = api.trades_get(order_id=sell_id) logger.info(f"get result sell trades: {result}") result_buy = api.trades_get(order_id=buy_id) logger.info(f"get result buy trades: {result}") assert hasattr(result_sell.items[0], 'price') assert hasattr(result_buy.items[0], 'price') assert float(result_buy.items[0].price) == price assert float(result_sell.items[0].price) == price logger.info('ending order')
def test_same_price_volume_success(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at, exchange_ids, coin_id, sell_coin_id, trading_area_id, symbol, quotation_special_login): # login special_info = quotation_special_login([ main_entrust_api, main_ac_api, main_quota_api, tenant_quota_api, venture_quota_api ]) for i in range(20): price, volume = get_random_price_and_volume( special_info['precision']) coin_id = coin_id payload = PostEntrustsRequest() # payload.market_id = market_id payload.market_id = special_info['market_id'] logger.info(f'下单价格:{price}') payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at try: first_result = main_entrust_api.entrusts_post(body=payload) logger.info(f"{trade_type} id: {first_result.order_id}") except Exception as e: if e.status == 400: logger.error('下单失败,没钱') raise e logger.info(f'第一次下单result:{first_result}') # 如果first_result的post参数trade_type是'buy'那么就要创建'sell'单 if trade_type == 'buy': buy_id = first_result.order_id payload.trade_type = 'sell' result = main_entrust_api.entrusts_post(body=payload) sell_id = result.order_id logger.info(f"sell id :{sell_id}") # 如果first_result的post参数trade_type是'sell'那么就要创建'sell'单 else: payload.trade_type = 'buy' sell_id = first_result.order_id logger.info('*' * 20) result = main_entrust_api.entrusts_post(body=payload) # logger.info('第二次下买单:', result) buy_id = result.order_id logger.info(f"buy id :{buy_id}") # result = api.entrusts_get(status=['done']) time.sleep(5) result = main_entrust_api.entrusts_get() logger.info(f'委托记录列表:{result}') ids = [i.order_id for i in result.items] result_sell = main_entrust_api.trades_get(order_id=sell_id) logger.info(f"get result sell trades: {result_sell}") result_buy = main_entrust_api.trades_get(order_id=buy_id) logger.info(f"get result buy trades: {result_buy}") for i in range(5): price, volume = get_random_price_and_volume( special_info['precision']) payload = PostEntrustsRequest() payload.market_id = special_info['market_id'] logger.info(f'下单价格:{price}') payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at try: first_result = main_entrust_api.entrusts_post(body=payload) logger.info(f"{trade_type} id: {first_result.order_id}") except Exception as e: if e.status == 400: logger.error('下单失败,没钱') raise e logger.info(f'第一次下单result:{first_result}') # 如果first_result的post参数trade_type是'buy'那么就要创建'sell'单 if trade_type == 'buy': buy_id = first_result.order_id payload.trade_type = 'sell' result = main_entrust_api.entrusts_post(body=payload) sell_id = result.order_id logger.info(f"sell id :{sell_id}") # 如果first_result的post参数trade_type是'sell'那么就要创建'sell'单 else: payload.trade_type = 'buy' payload.price = price - 0.55 sell_id = first_result.order_id logger.info('*' * 20) result = main_entrust_api.entrusts_post(body=payload) buy_id = result.order_id logger.info(f"buy id :{buy_id}")
def test_takeprofit_sell(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at, special_login, with_login): # 构造entrust_post请求对象 payload = PostEntrustsRequest() payload.market_id = market_id payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at # special_login special_login([api]) # login with_login('staff', [staff_api], 'account', 'password') # TODO add account # 1.先下一买一卖,构造最新成交价 try: first_result = api.entrusts_post(body=payload, async_req=True) buy1_id = first_result.get('orderid', '') payload.trade_type = 'sell' result = api.entrusts_post(body=payload, async_req=True) sell1_id = result.get('orderid', '') # 1.1 检查成交记录列表,委托交易列表 entrust_res = api.entrusts_get(status=['done'], async_req=True) assert entrust_res['items'] entrust_list = [res["orderid"] for res in entrust_res['items']] assert buy1_id in entrust_list assert sell1_id in entrust_list # 1.2 检查委托交易列表 trade_res = api.trades_get(order_id=buy1_id, async_req=True) assert trade_res['items'] # 获取最新成交价 new_price = trade_res['items'][0]['price'] except ApiException as e: if market_id == "" or price == "": assert e.status == 500 return # 2.下委托单(类型:止损 如果当前价小于触发价) try: payload.trigger_price = new_price + 0.001 payload.price = payload.trigger_price payload.trade_type = 'buy' payload.entrust_type = 'profitLoss' tri_price = new_price + 0.001 commi_price = tri_price buy_res = api.entrusts_post(body=payload, async_req=True) commission_order_id = buy_res.get('orderid', '') except ApiException as e: if market_id == "" or price == "": assert e.status == 500 return # 3.再下一买一卖,达到触发价 try: payload.trade_type = 'buy' payload.trigger_price = '' payload.price = new_price + 0.004 buy2_price = payload.price payload.entrust_type = 'limit' # 3.1先下一笔买单 buy2_res = api.entrusts_post(body=payload, async_req=True) buy2_id = buy2_res.get('orderid', '') # 3.2再下一笔卖单 payload.trade_type = 'sell' payload.price = buy2_price sell2_res = api.entrusts_post(body=payload, async_req=True) sell2_id = sell2_res.get('orderid', '') # 3.3检查委托交易列表 check_res = api.entrusts_get(status=['done'], async_req=True) assert check_res['items'] check_list = [res.id for res in entrust_res['items']] assert buy2_id in check_list assert sell2_id in check_list # 3.4获取成交记录列表 ch_trade_res = api.trades_get(order_id=buy2_id, async_req=True) assert ch_trade_res['items'][0] latest_price = ch_trade_res['items'][0]['price'] except ApiException as e: if market_id == "" or price == "": assert e.status == 500 return # 4.当前成交价价变动时,当前价小于等于触发价,开始委托,检查委托列表是否有委托订单记录 if latest_price >= tri_price: order_lists = api.entrusts_get(status=["entrusting"], tradeType='buy', async_req=True) assert order_lists['items'] order_list = [order_res.id for order_res in order_lists['items']] assert commission_order_id in order_list # 5.下一个与第2步方向相反的单,促成第二步的单成交 try: payload.trade_type = 'sell' # 委托价>=卖一时,撮合成功 payload.price = commi_price result = api.entrusts_post(body=payload, async_req=True) sell_id = result.get('orderid', '') except ApiException as e: if price == '' or market_id == '': assert e.status == 500 return result = api.trades_get(async_req=True) ids = [i.id for i in result['items']] assert sell_id in ids assert commission_order_id in ids user_info = account_api.accounts_account_info_get() assert user_info['account_info'].get('account_id') result = staff_api.entrusts_get( uid=user_info['account_info'].get('account_id'), status="done", async_req=True) ids = [i.id for i in result['items']] assert sell_id in ids # 测试交易所获取委托列表 result = staff_api.entrusts_get(entrust_id=sell_id, status="done", async_req=True) assert result.items.id == sell_id fee_result = staff_api.fee_history_get( uid=user_info['account_info'].get('account_id'), status="done") fee_ids_map = { i.trade_history_id: i.fee_yield_amount for i in fee_result['items'] } for k, v in fee_ids_map.items(): r = staff_api.trade_history_get(trade_history_id=k, async_req=True) assert r.items.buyer_fee == v r2 = tenant_api.exchanges_trade_history_get(trade_id=k, async_req=True) assert r2.items.buyerFee == v tenant_fee_r = tenant_api.exchanges_fee_history_get(trade_id=k, async_req=True) assert tenant_fee_r.items.fee_yield_amount == v payload.price = 10**8 payload.trade_type = 'sell' result = api.entrusts_post(body=payload, async_req=True) buy_id = result.order_id staff_api.entrusts_id_cancel_post(buy_id, async_req=True)
def test_entrust(self, market_id, price, entrust_type, trade_type, volume, trigger_price, auto_cancel_at, special_login, with_login): payload = PostEntrustsRequest() payload.market_id = market_id payload.price = price payload.entrust_type = entrust_type payload.trade_type = trade_type payload.trigger_price = trigger_price payload.volume = volume payload.auto_cancel_at = auto_cancel_at # login special_login([api, tenant_api]) # login with_login('staff', [staff_api], 'account', 'password') # TODO add account try: first_result = api.entrusts_post(body=payload, async_req=True) # 如果first_result的post参数trade_type是'buy'那么就要创建'sell'单 if trade_type == 'buy': buy_id = first_result['id'] payload.trade_type = 'sell' result = api.entrusts_post(body=payload, async_req=True) sell_id = result['id'] # 如果first_result的post参数trade_type是'sell'那么就要创建'sell'单 else: payload.trade_type = 'buy' sell_id = first_result['id'] result = api.entrusts_post(body=payload, async_req=True) buy_id = result.order_id except ApiException as e: if price == '' or market_id == '': assert e.status == 400 return user_info = account_api.accounts_account_info_get() assert user_info['account_info'].get('account_id') result = staff_api.entrusts_get( uid=user_info['account_info'].get('account_id'), status="done", async_req=True) ids = [i.id for i in result['items']] assert sell_id in ids assert buy_id in ids # 测试交易所获取委托列表 result = staff_api.entrusts_get(entrust_id=buy_id, status="done", async_req=True) assert result.items.id == buy_id fee_result = staff_api.fee_history_get( uid=user_info['account_info'].get('account_id'), status="done") fee_ids_map = { i.trade_history_id: i.fee_yield_amount for i in fee_result['items'] } for k, v in fee_ids_map.items(): r = staff_api.trade_history_get(trade_history_id=k, async_req=True) assert r.items.buyer_fee == v r2 = tenant_api.exchanges_trade_history_get(trade_id=k, async_req=True) assert r2.items.buyerFee == v tenant_fee_r = tenant_api.exchanges_fee_history_get(trade_id=k, async_req=True) assert tenant_fee_r.items.fee_yield_amount == v payload.price = 10**8 payload.trade_type = 'sell' result = api.entrusts_post(body=payload, async_req=True) buy_id = result.order_id staff_api.entrusts_id_cancel_post(buy_id, async_req=True)