| | |
| | | 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 |
| | |
| | | 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() |
| | |
| | | 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): |
| | |
| | | |
| | | # 撤买 |
| | | 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 |
| | |
| | | # 输入框控件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 |
| | |
| | | |
| | | # 撤单快捷键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)) |
| | |
| | | |
| | | 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) |