Administrator
2024-01-24 6ed8f7145cd2dc0a8e7f94727c77f3fc8873fce7
更改L后快速成交比例
5个文件已修改
1个文件已添加
224 ■■■■■ 已修改文件
constant.py 2 ●●● 补丁 | 查看 | 原始文档 | blame | 历史
l2/l2_data_manager_new.py 5 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
test/test_sell.py 7 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/huaxin_sell_util.py 41 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/huaxin/huaxin_trade_server.py 6 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
trade/sell/sell_manager.py 163 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
constant.py
@@ -125,7 +125,7 @@
# 小金额
L_CANCEL_MIN_MONEY = 50
# L后监听快速成交撤单比例
L_CANCEL_FAST_DEAL_RATE = 0.5
L_CANCEL_FAST_DEAL_RATE = 0.59
# L后监听快速成交金额
L_CANCEL_FAST_DEAL_MIN_MONEY = 99 * 10000
l2/l2_data_manager_new.py
@@ -1622,6 +1622,11 @@
        # 最大间隔时间ms
        max_space_time_ms = cls.__l2PlaceOrderParamsManagerDict[code].get_time_range() * 1000
        # 如果大单含有率大于50%,则时间囊括范围提高到3s
        if max_num_set and origin_count:
            if len(max_num_set)/origin_count > 0.5:
                max_space_time_ms = 3*1000
        # 最大买量
        max_buy_num = 0
        max_buy_num_set = set(max_num_set)
test/test_sell.py
@@ -1,4 +1,7 @@
from trade.huaxin import huaxin_trade_server
from trade.sell import sell_manager
if __name__ == '__main__':
    huaxin_trade_server.TradeServerProcessor.test_sell()
    code = "000333"
    __HumanSellManager = sell_manager.HumanSellManager()
    f = __HumanSellManager.distribute_sell_volume(code, 2000)
    print(f)
trade/huaxin/huaxin_sell_util.py
New file
@@ -0,0 +1,41 @@
# 撤卖单
import time
from log_module import async_log_util
from log_module.log import logger_trade
from trade.huaxin import huaxin_trade_order_processor, huaxin_trade_api
from utils import huaxin_util, sell_util, tool
import concurrent.futures
__cancel_sell_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=8)
def __cancel_sell_order(code, order_ref):
    for i in range(0, 10):
        time.sleep(0.2)
        order_entity = huaxin_trade_order_processor.TradeResultProcessor.get_huaxin_order_by_order_ref(order_ref)
        if order_entity:
            if order_entity.orderStatus == huaxin_util.TORA_TSTP_OST_AllTraded:
                # 成交的就不需要撤单了
                return
    try:
        result = huaxin_trade_api.cancel_order(2, code, None, orderRef=order_ref)
    except Exception as e:
        logger_trade.exception(e)
# 开始撤单
def start_sell(code, volume, price_type, limit_up_price, limit_down_price, current_price, blocking=False,
               request_id=None):
    price = sell_util.get_sell_price(price_type, limit_up_price, limit_down_price, current_price)
    if not price:
        raise Exception("价格获取出错")
    async_log_util.info(logger_trade, f"API卖:  单价-{price}")
    order_ref = huaxin_util.create_order_ref()
    result = huaxin_trade_api.order(2, code, volume, price, order_ref=order_ref,
                                    blocking=blocking, request_id=request_id)
    # 如果是在正常交易时间提交的2s之内还未成交的需要撤单
    if int("092958") <= int(tool.get_now_time_str().replace(":", "")) <= int("145655"):
        __cancel_sell_thread_pool.submit(lambda: __cancel_sell_order(code, order_ref))
    return result
trade/huaxin/huaxin_trade_server.py
@@ -55,7 +55,7 @@
from trade.deal_big_money_manager import DealOrderNoManager
from trade.huaxin import huaxin_trade_api as trade_api, huaxin_trade_api, huaxin_trade_data_update, \
    huaxin_trade_record_manager, huaxin_trade_order_processor
    huaxin_trade_record_manager, huaxin_trade_order_processor, huaxin_sell_util
from trade.huaxin.huaxin_trade_record_manager import PositionManager, DealRecordManager, DelegateRecordManager
from trade.l2_trade_factor import L2PlaceOrderParamsManager
from trade.sell import sell_manager
@@ -331,7 +331,7 @@
                                    # 提交卖
                                    limit_down_price = gpcode_manager.get_limit_down_price(code)
                                    limit_up_price = gpcode_manager.get_limit_up_price(code)
                                    sell_manager.start_sell(code, r.sell_volume, r.sell_price_type, limit_up_price,
                                    huaxin_sell_util.start_sell(code, r.sell_volume, r.sell_price_type, limit_up_price,
                                                            limit_down_price,
                                                            buy1_price)
                                    SellRuleManager().excute_sell(r.id_)
@@ -555,7 +555,7 @@
                    current_price = TradeServerProcessor.get_l1_current_price(code)
                    limit_down_price = gpcode_manager.get_limit_down_price(code)
                    limit_up_price = gpcode_manager.get_limit_up_price(code)
                    result = sell_manager.start_sell(code, volume, price_type, limit_up_price, limit_down_price,
                    result = huaxin_sell_util.start_sell(code, volume, price_type, limit_up_price, limit_down_price,
                                                     current_price, blocking=True, request_id=request_id)
                    async_log_util.info(logger_trade, f"API卖结果: {result}")
                    self.send_response(result, client_id, request_id)
trade/sell/sell_manager.py
@@ -2,76 +2,113 @@
卖票管理器
"""
import concurrent.futures
import time
from log_module import async_log_util
from log_module.log import logger_trade, logger_trade_position_api_request
from trade.huaxin import huaxin_trade_order_processor, huaxin_trade_api
from utils import huaxin_util, sell_util, tool
from trade.huaxin.huaxin_trade_record_manager import DealRecordManager, DelegateRecordManager
__cancel_sell_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=8)
# 撤卖单
def __cancel_sell_order(code, order_ref):
    for i in range(0, 10):
        time.sleep(0.2)
        order_entity = huaxin_trade_order_processor.TradeResultProcessor.get_huaxin_order_by_order_ref(order_ref)
        if order_entity:
            if order_entity.orderStatus == huaxin_util.TORA_TSTP_OST_AllTraded:
                # 成交的就不需要撤单了
                return
    try:
        result = huaxin_trade_api.cancel_order(2, code, None, orderRef=order_ref)
    except Exception as e:
        logger_trade.exception(e)
# 人工卖票管理器
class HumanSellManager:
    __code_sell_way_dict = {}
    __DealRecordManager = DealRecordManager()
    def set_code_sell_way(self, data):
        fdata = {}
        code = data["code"]
        mode = data["mode"]
        if mode == 1:
            mode1_rate_index = data["mode1_rate_index"]
            fdata["mode1_rate_index"] = mode1_rate_index
        elif mode == 2:
            # 第一笔卖的索引
            mode2_first_index = data["mode2_first_index"]
            # 剩下几笔卖的索引
            mode2_left_index = data["mode2_left_index"]
            fdata["mode2_first_index"] = mode2_first_index
            fdata["mode2_left_index"] = mode2_left_index
        fdata["code"] = code
        fdata["mode"] = mode
        self.__code_sell_way_dict[code] = fdata
        # 保存卖的方式
        async_log_util.info(logger_trade_position_api_request, f"设置卖出方式:{fdata}")
    def __get_deal_nums(self, code):
        deal_order_system_id_infos = {}
        # 获取已经卖的单数
        deal_list = self.__DealRecordManager.list_sell_by_code_cache(code)
        if deal_list:
            for d in deal_list:
                if d["orderSysID"] not in deal_order_system_id_infos:
                    deal_order_system_id_infos[d["orderSysID"]] = [d["volume"], d["tradeTime"]]
                else:
                    deal_order_system_id_infos[d["orderSysID"]][0] += d["volume"]
        # 获取9:30之前的卖委托
        current_delegates = DelegateRecordManager().list_current_delegates(code)
        if current_delegates:
            for d in current_delegates:
                if d["orderSysID"] not in deal_order_system_id_infos:
                    deal_order_system_id_infos[d["orderSysID"]] = [d["volume"], d["insertTime"]]
        deal_list = [deal_order_system_id_infos[k] for k in deal_order_system_id_infos]
        deal_list.sort(key=lambda x: x[1])
        sell_order_nums = [k[0] for k in deal_list]
        return sell_order_nums
    def distribute_sell_volume(self, code, total_volume):
        def average_volume(_total_volume, count):
            total_volume_100 = _total_volume // 100
            _base_100 = total_volume_100 // count
            _left_100 = total_volume_100 % count
            temp_list = []
            for i in range(count):
                if i + 1 <= _left_100:
                    temp_list.append((_base_100 + 1) * 100)
                else:
                    temp_list.append(_base_100 * 100)
            return temp_list
        if code not in self.__code_sell_way_dict:
            raise Exception("还没设置卖出方式")
        sell_way = self.__code_sell_way_dict.get(code)
        volumeList = []
        if sell_way["mode"] == 1:
            # 平均卖
            mode1_rate_index = sell_way["mode1_rate_index"]
            volumeList = average_volume(total_volume, mode1_rate_index)
        elif sell_way["mode"] == 2:
            # 按百分比卖
            mode2_first_index = sell_way["mode2_first_index"]
            # 剩下几笔卖的索引
            mode2_left_index = sell_way["mode2_left_index"]
            first_percent = (mode2_first_index + 1) * 10
            second_count = mode2_left_index + 1
            first = first_percent * total_volume // 100
            if first_percent * total_volume % 100 != 0:
                first += 100
            volumeList.append(first)
            total_left = total_volume - first
            left_volumes = average_volume(total_left, second_count)
            volumeList.extend(left_volumes)
        return volumeList
    # 计算卖出量
    def compute_sell_volume(self, code, total_volume):
        volumeList = self.distribute_sell_volume(code, total_volume)
        # 获取已卖和已挂单的数据
        sell_order_nums = self.__get_deal_nums(code)
        if len(sell_order_nums) >= len(volumeList):
            raise Exception(f"挂单已超过设置数量:{sell_order_nums}/{volumeList}")
        return volumeList[len(sell_order_nums)]
# 开始撤单
def start_sell(code, volume, price_type, limit_up_price, limit_down_price, current_price, blocking=False,
               request_id=None):
    price = sell_util.get_sell_price(price_type, limit_up_price, limit_down_price, current_price)
    if not price:
        raise Exception("价格获取出错")
    async_log_util.info(logger_trade, f"API卖:  单价-{price}")
    order_ref = huaxin_util.create_order_ref()
    result = huaxin_trade_api.order(2, code, volume, price, order_ref=order_ref,
                                    blocking=blocking, request_id=request_id)
    # 如果是在正常交易时间提交的2s之内还未成交的需要撤单
    if int("092958") <= int(tool.get_now_time_str().replace(":", "")) <= int("145655"):
        __cancel_sell_thread_pool.submit(lambda: __cancel_sell_order(code, order_ref))
    return result
if __name__ == '__main__':
    code = "000333"
    __HumanSellManager = HumanSellManager()
    __HumanSellManager.set_code_sell_way({"code": code, "mode": 1, "mode1_rate_index": 3})
    # __HumanSellManager.set_code_sell_way({"code": code, "mode": 2, "mode2_first_index": 1, "mode2_left_index": 4})
__code_sell_way_dict = {}
def set_code_sell_way(data):
    fdata = {}
    code = data["code"]
    mode = data["mode"]
    if mode == 1:
        mode1_rate_index = data["mode1_rate_index"]
        fdata["mode1_rate_index"] = mode1_rate_index
    elif mode == 2:
        # 第一笔卖的索引
        mode2_first_index = data["mode2_first_index"]
        # 剩下几笔卖的索引
        mode2_left_index = data["mode2_left_index"]
        fdata["mode2_first_index"] = mode2_first_index
        fdata["mode2_left_index"] = mode2_left_index
    fdata["code"] = code
    fdata["mode"] = mode
    __code_sell_way_dict[code] = fdata
    # 保存卖的方式
    async_log_util.info(logger_trade_position_api_request, f"设置卖出方式:{fdata}")
# 计算卖出量
def compute_sell_volume(code):
    if code not in __code_sell_way_dict:
        raise Exception("还没设置卖出方式")
    sell_way = __code_sell_way_dict.get(code)
    # TODO
    return 100
    f = __HumanSellManager.distribute_sell_volume(code, 2000)
    print(f)