| | |
| | | import win32gui |
| | | import win32con |
| | | |
| | | import constant |
| | | from code_attribute import gpcode_manager |
| | | from db.redis_manager_delegate import RedisUtils |
| | | from ocr import ocr_util |
| | | from trade import l2_trade_util |
| | | from db import redis_manager |
| | | from log import * |
| | | from tool import async_call |
| | | from utils import win32_util |
| | | from db import redis_manager_delegate as redis_manager |
| | | from log_module.log import * |
| | | from utils.tool import async_call |
| | | from utils import win32_util, capture_util, tool |
| | | |
| | | |
| | | class THSGuiTrade(object): |
| | |
| | | cls.__instance.buy_lock = threading.RLock() |
| | | cls.__instance.buy_cancel_locks = {} |
| | | cls.__instance.buy_win_list = cls.get_buy_wins() |
| | | print("交易窗口", cls.__instance.buy_win_list) |
| | | # print("交易窗口", cls.__instance.buy_win_list) |
| | | cls.__instance.using_buy_wins = set() |
| | | cls.__instance.cancel_wins = cls.__instance.getCancelBuyWins() |
| | | return cls.__instance |
| | |
| | | # raise Exception(error) |
| | | |
| | | # TODO 暂时不验证涨停价 |
| | | if not constant.TEST: |
| | | if abs(float(limit_up_price_now) - float(limit_up_price)) >= 0.01: |
| | | error = "涨停价验证出错 {}-{}".format(limit_up_price, limit_up_price_now) |
| | | raise Exception(error) |
| | | # if not constant.TEST: |
| | | # if abs(float(limit_up_price_now) - float(limit_up_price)) >= 0.001: |
| | | # error = "涨停价验证出错 {}-{}".format(limit_up_price, limit_up_price_now) |
| | | # raise Exception(error) |
| | | |
| | | # 开始交易,买入按钮ID:0x000003EE |
| | | # buy_hwnd = win32gui.GetDlgItem(win, 0x000003EE) |
| | |
| | | self.buy_cancel_locks[w] = threading.RLock() |
| | | |
| | | |
| | | |
| | | |
| | | class THSGuiUtil: |
| | | @classmethod |
| | | def getText(cls, hwnd): |
| | |
| | | @classmethod |
| | | def __save_code_win(cls, code, win): |
| | | key = "buywin_distribute-{}".format(code) |
| | | cls.__get_redis().setex(key, tool.get_expire(), win) |
| | | RedisUtils.setex(cls.__get_redis(), key, tool.get_expire(), win) |
| | | |
| | | # 获取窗口分配的代码 |
| | | @classmethod |
| | | def __get_code_win(cls, code): |
| | | key = "buywin_distribute-{}".format(code) |
| | | win = cls.__get_redis().get(key) |
| | | win = RedisUtils.get(cls.__get_redis(), key) |
| | | if win is not None: |
| | | return int(win) |
| | | return None |
| | |
| | | @classmethod |
| | | def __del_code_win(cls, code): |
| | | key = "buywin_distribute-{}".format(code) |
| | | cls.__get_redis().delete(key) |
| | | RedisUtils.delete(cls.__get_redis(), key) |
| | | |
| | | # 获取所有已经分配窗口的代码 |
| | | @classmethod |
| | | def __get_distributed_win_codes(cls): |
| | | key = "buywin_distribute-*" |
| | | keys = cls.__get_redis().keys(key) |
| | | keys = RedisUtils.keys(cls.__get_redis(), key) |
| | | codes = [] |
| | | for k in keys: |
| | | codes.append(k.replace("buywin_distribute-", "")) |
| | |
| | | def __get_available_win(cls): |
| | | # 是否有可用的还未分配的窗口 |
| | | key = "buywin_distribute-*" |
| | | keys = cls.__get_redis().keys(key) |
| | | keys = RedisUtils.keys(cls.__get_redis(), key) |
| | | win_list = cls.get_buy_wins() |
| | | if len(win_list) < 1: |
| | | raise Exception("必须要有一个买入窗口") |
| | | win_set = set(win_list) |
| | | for k in keys: |
| | | win = int(cls.__get_redis().get(k)) |
| | | win = int(RedisUtils.get(cls.__get_redis(),k)) |
| | | if win in win_set: |
| | | win_set.remove(win) |
| | | if len(win_set) > 0: |
| | | return win_set.pop() |
| | | win_list = list(win_set) |
| | | random.shuffle(win_list) |
| | | return win_list[0] |
| | | |
| | | # 没有剩余的窗口,新增加窗口 |
| | | raise Exception("没有剩余窗口") |
| | |
| | | THSGuiUtil.set_buy_window_code(cls.get_trade_win(win), code) |
| | | time.sleep(0.5) |
| | | code_name_win = cls.__get_code_name(win) |
| | | if code_name == code_name_win: |
| | | is_success = True |
| | | break |
| | | if code_name == code_name_win or code_name_win.find(code_name) > -1: |
| | | if cls.__is_buy_limit_up_price(win): |
| | | is_success = True |
| | | break |
| | | else: |
| | | cls.__del_code_win(code) |
| | | THSGuiUtil.set_buy_window_code(cls.get_trade_win(win), "") |
| | | raise Exception("不是买涨停价") |
| | | if is_success: |
| | | logger_buy_win_distibute.info(f"新分配窗口成功:{code}-{win}") |
| | | else: |
| | |
| | | @classmethod |
| | | def get_distributed_code_wins(cls): |
| | | key = "buywin_distribute-*" |
| | | keys = cls.__get_redis().keys(key) |
| | | keys = RedisUtils.keys(cls.__get_redis(), key) |
| | | results = [] |
| | | for k in keys: |
| | | code = k.split("-")[-1] |
| | | win = cls.__get_redis().get(k) |
| | | win = RedisUtils.get(cls.__get_redis(), k) |
| | | results.append((code, win)) |
| | | return results |
| | | |
| | |
| | | if name is not None: |
| | | name = name.replace(" ", "") |
| | | return tool.strQ2B(name) |
| | | |
| | | # 是否是涨停价 |
| | | @classmethod |
| | | def __is_buy_limit_up_price(cls, win): |
| | | trade_win = cls.get_trade_win(win) |
| | | if trade_win is None: |
| | | return None |
| | | price_win = win32gui.GetDlgItem(trade_win, 0x00000409) |
| | | ocr_result = ocr_util.OcrUtil.ocr_with_key(capture_util.window_capture(price_win), "涨停价|张停价") |
| | | if ocr_result: |
| | | return True |
| | | return False |
| | | |
| | | @classmethod |
| | | def fill_codes(cls, codes): |
| | |
| | | new_delete_codes.append(code) |
| | | |
| | | cancel_wins = THSGuiTrade.getCancelBuyWins() |
| | | add_codes_num = len(cancel_wins)*10 |
| | | add_codes_num = len(cancel_wins) * 10 |
| | | add_codes = new_codes[0:add_codes_num] |
| | | del_codes = new_codes[add_codes_num:] |
| | | del_codes.extend(new_delete_codes) |
| | |
| | | print("分配的窗口:", win, THSGuiUtil.is_win_exist(win)) |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | |
| | | |
| | | # 根据涨幅高低分配交易窗口 |
| | | def re_distribute_buy_win(codes): |
| | | THSBuyWinManagerNew.fill_codes(codes) |
| | | |
| | | |
| | | class GUITest: |
| | |
| | | |
| | | |
| | | if __name__ == '__main__': |
| | | pass |
| | | # try: |
| | | # THSGuiTrade().cancel_buy_again("000637") |
| | | # except Exception as e: |
| | | # print(e) |
| | | print(THSGuiTrade().cancel_buy("000582")) |
| | | print(ocr_util.OcrUtil.ocr_with_key(capture_util.window_capture(0x000314EA), "涨停价|张停价")) |
| | | |
| | | # THSGuiTrade().buy("600613", 10.29) |