Administrator
2023-01-13 48dcb788e821d40be37fa05c9789b751a6e2a69b
trade_gui.py
@@ -326,38 +326,42 @@
                        pass
        return 0
    def __get_code_input(self):
        win = self.cancel_win
        if win <= 0 or not win32gui.IsWindowVisible(win):
            self.cancel_win = self.getCancelBuyWin()
            win = self.cancel_win
            if win <= 0:
                raise Exception("无法找到取消委托窗口")
        t = time.time()
        print(t)
        start = int(round(t * 1000))
        print(win)
        # 输入框控件ID 0x000003E9
        code_input = win32gui.GetDlgItem(win, 0x00000996)
        code_input = win32gui.FindWindowEx(code_input, 0, "Edit", None)
        # 刷新句柄
        if code_input <= 0:
            self.refresh_hwnds()
            code_input = win32gui.GetDlgItem(win, 0x00000996)
            code_input = win32gui.FindWindowEx(code_input, 0, "Edit", None)
        return code_input, win
    # 撤买
    def cancel_buy(self, code):
        if constant.TEST:
            return
        self.buy_cancel_lock.acquire()
        code_input = 0
        logger_trade_gui.info("开始获取撤单控件:code-{}".format(code))
        code_input, win = self.__get_code_input()
        try:
            logger_trade_gui.info("开始撤单:code-{}".format(code))
            win = self.cancel_win
            if win <= 0 or not win32gui.IsWindowVisible(win):
                self.cancel_win = self.getCancelBuyWin()
                win = self.cancel_win
                if win <= 0:
                    raise Exception("无法找到取消委托窗口")
            t = time.time()
            print(t)
            start = int(round(t * 1000))
            print(win)
            # 输入框控件ID 0x000003E9
            code_input = win32gui.GetDlgItem(win, 0x00000996)
            code_input = win32gui.FindWindowEx(code_input, 0, "Edit", None)
            # 刷新句柄
            if code_input <= 0:
                self.refresh_hwnds()
                code_input = win32gui.GetDlgItem(win, 0x00000996)
                code_input = win32gui.FindWindowEx(code_input, 0, "Edit", None)
            start = int(round(time.time() * 1000))
            code_result = "-"
            retry_count = 0
            while code != code_result and retry_count < 5:
                if retry_count > 0:
                code_result = self.getText(code_input)
                if retry_count > 0 or len(code_result) > 0:
                    self.input_number(code_input, "")
                    time.sleep(0.01)
@@ -369,6 +373,7 @@
                code_result = code_result.split(",")[0]
                retry_count += 1
            if code != code_result:
                self.input_number(code_input, "")
                raise Exception("输入代码出错")
            # 撤单快捷键X
@@ -382,13 +387,28 @@
            end = int(round(t * 1000))
            print("耗时", end - start)
            logger_trade_gui.info("撤单成功:code-{} 耗时:{}".format(code, end - start))
            time.sleep(0.05)
            time.sleep(0.03)
        finally:
            self.buy_cancel_lock.release()
            # 清空代码框
            self.input_number(code_input, "")
            # 再次清除代码框
            self.input_number(code_input, "")
    # 交易盘口中的撤买
    def cancel_buy_again(self, code):
        if constant.TEST:
            return
        win = THSBuyWinManagerNew.get_distributed_code_win(code)
        if win is None or win <= 0:
            raise Exception("没找到分配的交易窗口")
        cancel_btn_hwnd = win32gui.FindWindowEx(win, 0, "Button", "撤单")
        if cancel_btn_hwnd <= 0:
            raise Exception("没有找到撤单按钮")
        if not win32gui.IsWindowVisible(cancel_btn_hwnd):
            raise Exception("撤单按钮不可见")
        THSGuiUtil.click(cancel_btn_hwnd)
    # 刷新交易窗口数据
    @async_call
@@ -777,7 +797,7 @@
        code_name_win = win32gui.GetDlgItem(trade_win, 0x000005C2)
        name = THSGuiUtil.getText(code_name_win)
        if name is not None:
            name=name.replace(" ","")
            name = name.replace(" ", "")
        return tool.strQ2B(name)
    @classmethod
@@ -816,7 +836,7 @@
                    cls.cancel_distribute_win_for_code(code)
                else:
                    code_name = cls.__get_code_name(win)
                    #'深振业A'
                    # '深振业A'
                    if name_codes.get(code_name) != code:
                        cls.cancel_distribute_win_for_code(code)
                continue
@@ -851,7 +871,10 @@
if __name__ == '__main__':
    THSGuiTrade().cancel_buy("000419")
    try:
        THSGuiTrade().cancel_buy_again("000637")
    except Exception as e:
        print(e)
    # GUITest().test_distribute()
    # try:
    #     THSGuiUtil.set_buy_window_code(0x000112D0, "000333")