Administrator
2022-08-25 34491829675033e41715648b1e92f339bf2f35d1
trade_gui.py
@@ -6,7 +6,13 @@
import win32api
import win32con
from log import *
from threading import Thread
def async_call(fn):
    def wrapper(*args, **kwargs):
        Thread(target=fn, args=args, kwargs=kwargs).start()
    return wrapper
class THSGuiTrade(object):
    __instance = None
@@ -29,6 +35,12 @@
    def refresh_hwnds(self):
        self.cancel_win = self.__instance.getCancelBuyWin()
        self.buy_win_list = self.get_buy_wins();
    # 打开交易环境
    def open_trade_env(self):
        # 打开交易界面(F12)
        pass
    def get_available_buy_win(self):
        self.buy_lock.acquire()
@@ -234,24 +246,43 @@
            win32gui.PostMessage(win, win32con.WM_KEYDOWN, 66, 0);
            logger_trade_gui.info("执行买入结束:code-{} 耗时:{}".format(code, int(round(time.time() * 1000)) - start))
            self.close_delegate_success_dialog()
            # 循环读取下单结果,最多等待10s
            for i in range(0, 50):
                hwnd = self.getTradeResultWin()
                if hwnd > 0:
                    time.sleep(0.2)
                    code_str = self.getTradeSuccessCode(hwnd)
                    t = time.time()
                    print(t)
                    end = int(round(t * 1000))
                    print("买入耗时:", end - start)
                    logger_trade_gui.info("获取委托单号:code-{} 单号-{} 整体耗时:{}".format(code, code_str, end - start))
                    # 关闭交易结果弹框
                    self.closeTradeResultDialog(hwnd)
                    return code, code_str
                time.sleep(0.02)
            raise Exception("获取交易结果出错")
            # for i in range(0, 50):
            #     hwnd = self.getTradeResultWin()
            #     if hwnd > 0:
            #         time.sleep(0.2)
            #         code_str = self.getTradeSuccessCode(hwnd)
            #         t = time.time()
            #         print(t)
            #         end = int(round(t * 1000))
            #         print("买入耗时:", end - start)
            #         logger_trade_gui.info("获取委托单号:code-{} 单号-{} 整体耗时:{}".format(code, code_str, end - start))
            #         # 关闭交易结果弹框
            #         self.closeTradeResultDialog(hwnd)
            #         return code, code_str
            #     time.sleep(0.02)
            return code,""
            # raise Exception("获取交易结果出错")
        finally:
            self.using_buy_wins.discard(win)
    @async_call
    def close_delegate_success_dialog(self):
        for i in range(0, 50):
            hwnd = self.getTradeResultWin()
            if hwnd > 0:
                time.sleep(0.2)
                code_str = self.getTradeSuccessCode(hwnd)
                t = time.time()
                print(t)
                end = int(round(t * 1000))
                # logger_trade_gui.info("获取委托单号:code-{} 单号-{} 整体耗时:{}".format(code, code_str, end - start))
                # 关闭交易结果弹框
                self.closeTradeResultDialog(hwnd)
                break
                # return code, code_str
            time.sleep(0.02)
    # 撤销确认框
    def getCancelBuySureWin(self):
@@ -275,8 +306,8 @@
    # 撤买
    def cancel_buy(self, code):
        self.buy_cancel_lock.acquire()
        global code_input
        try:
            logger_trade_gui.info("开始撤单:code-{}".format(code))
            win = self.cancel_win
@@ -289,6 +320,11 @@
            # 输入框控件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)
            code_result = "-"
            retry_count = 0
@@ -310,7 +346,10 @@
            # 撤单快捷键X
            time.sleep(0.01)
            win32gui.PostMessage(win, win32con.WM_KEYDOWN, 0x00000058, 0);
            win32gui.PostMessage(win, win32con.WM_KEYDOWN, 0x00000058, 0x002D001);
            win32gui.PostMessage(win, win32con.WM_CHAR, 0x00000078, 0x002D001);
            win32gui.PostMessage(win, win32con.WM_KEYUP, 0x00000058, 0x002D001);
            #win32gui.PostMessage(win, win32con.WM_KEYUP, 0x00000058, 0);
            t = time.time()
            print(t)
            end = int(round(t * 1000))
@@ -325,16 +364,16 @@
if __name__ == '__main__':
    try:
        THSGuiTrade.checkEnv();
        print("环境正常")
        # THSGuiTrade.checkEnv();
        # print("环境正常")
        trade = THSGuiTrade();
        print(id(trade))
        # win = trade.get_available_buy_win()
        # if win < 1:
        #     raise Exception("无可用的交易窗口")
        result = trade.buy("002564", "7.26")
        # result = trade.buy("002564", "7.26")
        # # print("交易成功")
        # time.sleep(0.2)
        trade.cancel_buy("002564")
        trade.cancel_buy("000716")
    except Exception as e:
        print(e)