Administrator
2 天以前 6bbfbbb16d792f7737ec86cabdba5c0e98dcf4b4
trade/trade_manager.py
@@ -11,7 +11,9 @@
import concurrent.futures
from cancel_strategy.s_l_h_cancel_strategy import CancelRateHumanSettingManager
from code_attribute import gpcode_manager
from code_attribute.gpcode_manager import CodesContinueBuyMoneyManager
from db import mysql_data_delegate as mysql_data, redis_manager_delegate as redis_manager
from db.mysql_data_delegate import Mysqldb
from db.redis_manager_delegate import RedisUtils
@@ -24,23 +26,22 @@
from l2 import l2_data_manager, l2_data_log
from log_module.log import *
from trade.buy_money_count_setting import BuyMoneyUtil
from trade.huaxin.huaxin_trade_record_manager import TradeOrderIdManager
from trade.order_statistic import DealAndDelegateWithBuyModeDataManager
from trade.trade_data_manager import AccountMoneyManager, RadicalBuyDealCodesManager
from utils import import_util, tool, huaxin_util
trade_gui = import_util.import_lib("trade.trade_gui")
try:
    trade_gui = import_util.import_lib("trade.trade_gui")
except:
    pass
__db = 2
__redis_manager = redis_manager.RedisManager(2)
guiTrade = None  # trade_gui.THSGuiTrade() if trade_gui is not None else None
latest_trade_delegate_data = []
# 成交的代码
deal_codes = set()
# 委托代码
delegate_codes = set()
# 关闭购买入口
@@ -94,6 +95,8 @@
# 交易目标票模式
class TradeTargetCodeModeManager:
    # 只买辨识度
    MODE_ONLY_BUY_SPECIAL_CODES = 2
    # 只买想买单
    MODE_ONLY_BUY_WANT_CODES = 1
    # 买所有
@@ -120,7 +123,7 @@
        self.__trade_buy_mode_cache = self.get_mode()
    def set_mode(self, mode):
        if mode != self.MODE_ONLY_BUY_WANT_CODES and mode != self.MODE_BUY_ALL:
        if mode != self.MODE_ONLY_BUY_WANT_CODES and mode != self.MODE_BUY_ALL and mode != self.MODE_ONLY_BUY_SPECIAL_CODES:
            raise Exception("mode参数值错误")
        self.__trade_buy_mode_cache = mode
        RedisUtils.setex(self.__get_redis(), "trade_buy_mode", tool.get_expire(), mode)
@@ -131,7 +134,7 @@
        # 默认设置为可交易
        val = RedisUtils.get(cls.__get_redis(), "trade_buy_mode")
        if val is None:
            return cls.MODE_BUY_ALL
            return cls.MODE_ONLY_BUY_WANT_CODES
        return int(val)
    def get_mode_cache(self):
@@ -179,6 +182,7 @@
    def get_mode_cache(self):
        return self.__auto_cancel_sell_mode
# 代码的交易状态管理
class CodesTradeStateManager:
@@ -408,6 +412,18 @@
        async_log_util.info(logger_trade, "{} trade.manager.start_buy 结束".format(code))
def test_order(code, last_data, exec_index):
    """
    TODO 测试下单
    @param code:
    @param last_data:
    @param exec_index:
    @return:
    """
    price = gpcode_manager.get_limit_up_price(code)
    __buy(code, price, trade_constant.TRADE_STATE_NOT_TRADE, 0, last_data, last_data["index"], 0, exec_index=exec_index)
# 中断买入
def break_buy(code, reason):
    trade_data_manager.TradeBuyDataManager().remove_buy_position_info(code)
@@ -419,16 +435,34 @@
    async_log_util.info(logger_trade, "{} trade_manager.__buy 开始".format(code))
    try:
        if constant.API_TRADE_ENABLE:
            count = tool.get_buy_volume(price)
            can_buy, money, msg = BuyMoneyUtil.get_buy_data(tool.get_now_time_str(), mode,
                                                            DealAndDelegateWithBuyModeDataManager().get_deal_codes_info(
                                                                mode),
                                                            DealAndDelegateWithBuyModeDataManager().get_delegates_codes_info(
                                                                mode))
            if not can_buy:
                raise Exception(msg)
            continue_buy_money = CodesContinueBuyMoneyManager().get_continue_buy_money(code)
            if continue_buy_money:
                # 设置了续买金额的
                money = continue_buy_money
            count = tool.get_buy_volume_by_money(price, money)
            if count < 4 and float(price) < 200:
                count = 4
            # if mode == OrderBeginPosInfo.MODE_RADICAL:
            #     # 激进买入金额为1手
            #     count = 100
            if constant.TRADE_WAY == constant.TRADE_WAY_JUEJIN:
                trade_juejin.order_volume(code, price, count)
            elif constant.TRADE_WAY == constant.TRADE_WAY_HUAXIN:
                order_ref = huaxin_util.create_order_ref()
                TradeOrderIdManager().add_order_ref(code, order_ref)
                trade_huaxin.order_volume(code, price, count, last_data, order_ref=order_ref, exec_index=exec_index)
                if constant.IS_NEW_VERSION_PLACE_ORDER:
                    trade_huaxin.order_volume_new(code, price, count, last_data, exec_index=exec_index)
                else:
                    order_ref = huaxin_util.create_order_ref()
                    TradeOrderIdManager().add_order_ref(code, order_ref)
                    trade_huaxin.order_volume(code, price, count, last_data, order_ref=order_ref, exec_index=exec_index)
        else:
            guiTrade.buy(code, price)
        __place_order_success(code, capture_timestamp, last_data, last_data_index)
@@ -470,6 +504,12 @@
# 开始取消买入
def start_cancel_buy(code, force=False):
    """
    开始撤单
    @param code:
    @param force:
    @return:
    """
    async_log_util.info(logger_trade, "{} trade_manager.start_cancel_buy 开始".format(code))
    trade_state = __CodesTradeStateManager.get_trade_state_cache(code)
    try:
@@ -548,23 +588,25 @@
            continue
        # 买入成功
        if code is not None and int(data["type"]) == 0:
            l2_trade_util.forbidden_trade(code, msg="交易成功", force=True)
            if not CodesContinueBuyMoneyManager().get_continue_buy_money(code):
                l2_trade_util.forbidden_trade(code, msg="交易成功", force=True)
            state = CodesTradeStateManager().get_trade_state_cache(code)
            if state != trade_constant.TRADE_STATE_BUY_SUCCESS:
                CodesTradeStateManager().set_trade_state(code, trade_constant.TRADE_STATE_BUY_SUCCESS)
                # 删除买撤记录的临时信息
                kp_client_msg_manager.add_msg(code, "买入成交")
                l2_data_manager.TradePointManager().delete_buy_point(code)
                # 移除交易窗口分配
                if trade_gui is not None:
                    trade_gui.THSBuyWinManagerNew.cancel_distribute_win_for_code(code)
                if not CodesContinueBuyMoneyManager().get_continue_buy_money(code):
                    # 没设置续买
                    CodesTradeStateManager().set_trade_state(code, trade_constant.TRADE_STATE_BUY_SUCCESS)
                    # 删除买撤记录的临时信息
                    kp_client_msg_manager.add_msg(code, "买入成交")
                    l2_data_manager.TradePointManager().delete_buy_point(code)
            # 交易成功时间过去3s之后,且当前委托列表里面还有该代码数据就再次执行撤单
            if tool.trade_time_sub(tool.get_now_time_str(), _time) > 3:
                # 获取到当前是否委托
                for dd in latest_trade_delegate_data:
                    if dd["code"] == code:
                        logger_trade.info("{}交易成功触发,重复下单撤单".format(code))
                        start_cancel_buy(code, True)
            # 新版下单不处理
            if not constant.IS_NEW_VERSION_PLACE_ORDER:
                if tool.trade_time_sub(tool.get_now_time_str(), _time) > 3:
                    # 获取到当前是否委托
                    for dd in latest_trade_delegate_data:
                        if dd["code"] == code:
                            logger_trade.info("{}交易成功触发,重复下单撤单".format(code))
                            start_cancel_buy(code, True)
# 处理委托成功数据
@@ -575,7 +617,6 @@
    latest_trade_delegate_data.extend(datas)
    codes = []
    for data in datas:
        code = data["code"]
        if code is not None:
            codes.append(code)
@@ -634,16 +675,25 @@
# 买入成功
def buy_success(code):
    # 加入黑名单
    # 移除L后比例
    CancelRateHumanSettingManager().remove_l_down(code)
    if not l2_trade_util.is_in_forbidden_trade_codes(code):
        l2_trade_util.forbidden_trade(code, "buy success", force=True)
        if TradePointManager().get_latest_place_order_mode(code) == OrderBeginPosInfo.MODE_RADICAL:
        if not CodesContinueBuyMoneyManager().get_continue_buy_money(code):
            l2_trade_util.forbidden_trade(code, "buy success", force=True)
        mode = TradePointManager().get_latest_place_order_mode(code)
        if mode is None:
            mode = OrderBeginPosInfo.MODE_NORMAL
        DealAndDelegateWithBuyModeDataManager().add_deal_code(code, tool.get_now_time_str(), mode)
        if mode == OrderBeginPosInfo.MODE_RADICAL:
            RadicalBuyDealCodesManager().add_deal_code(code)
    # 取s消所有的挂单
    if constant.API_TRADE_ENABLE:
        if constant.TRADE_WAY == constant.TRADE_WAY_JUEJIN:
            trade_juejin.cancel_order(code)
        elif constant.TRADE_WAY == constant.TRADE_WAY_HUAXIN:
            trade_huaxin.cancel_order(code)
        if not constant.IS_NEW_VERSION_PLACE_ORDER:
            # 新版本拆单下单不撤单
            if constant.TRADE_WAY == constant.TRADE_WAY_JUEJIN:
                trade_juejin.cancel_order(code)
            elif constant.TRADE_WAY == constant.TRADE_WAY_HUAXIN:
                trade_huaxin.cancel_order(code)
    else:
        guiTrade.cancel_buy(code)