From 68464c679ae5e1ae35e7e67e3b339ba0f939cbd3 Mon Sep 17 00:00:00 2001 From: Administrator <admin@example.com> Date: 星期三, 15 三月 2023 14:46:03 +0800 Subject: [PATCH] 选股宝板块优化 --- l2/l2_data_manager_new.py | 235 +++++++++++++--------------------------------------------- 1 files changed, 52 insertions(+), 183 deletions(-) diff --git a/l2/l2_data_manager_new.py b/l2/l2_data_manager_new.py index 7ef1e69..85157d3 100644 --- a/l2/l2_data_manager_new.py +++ b/l2/l2_data_manager_new.py @@ -175,7 +175,7 @@ # 鍔犺浇鍘嗗彶鏁版嵁,杩斿洖鏁版嵁鏄惁姝e父 is_normal = l2.l2_data_util.load_l2_data(code) if not is_normal: - print("鍘嗗彶鏁版嵁寮傚父:",code) + print("鍘嗗彶鏁版嵁寮傚父:", code) # 鏁版嵁涓嶆甯搁渶瑕佺姝氦鏄� l2_trade_util.forbidden_trade(code) # 绾犳鏁版嵁 @@ -224,6 +224,8 @@ "l2鏁版嵁棰勫鐞嗘椂闂�") if len(add_datas) > 0: + # 鏄惁涓洪鏉夸唬鐮� + is_first_code = gpcode_manager.FirstCodeManager.is_in_first_record(code) latest_time = add_datas[len(add_datas) - 1]["val"]["time"] # 鏃堕棿宸笉鑳藉お澶ф墠鑳藉鐞� if l2.l2_data_util.L2DataUtil.is_same_time(now_time_str, @@ -235,10 +237,10 @@ end_index = len(total_datas) - 1 if state == trade_manager.TRADE_STATE_BUY_DELEGATED or state == trade_manager.TRADE_STATE_BUY_PLACE_ORDER or state == trade_manager.TRADE_STATE_BUY_SUCCESS: # 宸叉寕鍗� - cls.__process_order(code, start_index, end_index, capture_timestamp) + cls.__process_order(code, start_index, end_index, capture_timestamp, is_first_code) else: # 鏈寕鍗� - cls.__process_not_order(code, start_index, end_index, capture_timestamp) + cls.__process_not_order(code, start_index, end_index, capture_timestamp, is_first_code) logger_l2_process.info("code:{} 澶勭悊鏁版嵁鑼冨洿: {}-{} 澶勭悊鏃堕棿:{} 鎴浘鏃堕棿鎴筹細{}", code, add_datas[0]["index"], add_datas[-1]["index"], round(t.time() * 1000) - __start_time, @@ -248,7 +250,7 @@ # 澶勭悊鏈寕鍗� @classmethod - def __process_not_order(cls, code, start_index, end_index, capture_time): + def __process_not_order(cls, code, start_index, end_index, capture_time, is_first_code): __start_time = round(t.time() * 1000) # 鑾峰彇闃堝�� threshold_money, msg = cls.__get_threshmoney(code) @@ -256,16 +258,16 @@ __start_time = l2_data_log.l2_time(code, round(t.time() * 1000) - __start_time, "鑾峰彇m鍊兼暟鎹�楁椂") - cls.__start_compute_buy(code, start_index, end_index, threshold_money, capture_time) + cls.__start_compute_buy(code, start_index, end_index, threshold_money, capture_time, is_first_code) # 娴嬭瘯涓撶敤 @classmethod - def process_order(cls, code, start_index, end_index, capture_time, new_add=True): - cls.__process_order(code, start_index, end_index, capture_time, new_add) + def process_order(cls, code, start_index, end_index, capture_time, is_first_code, new_add=True): + cls.__process_order(code, start_index, end_index, capture_time, is_first_code, new_add) # 澶勭悊宸叉寕鍗� @classmethod - def __process_order(cls, code, start_index, end_index, capture_time, new_add=True): + def __process_order(cls, code, start_index, end_index, capture_time, is_first_code, new_add=True): # 璁$畻瀹夊叏绗旀暟 @dask.delayed def compute_safe_count(): @@ -312,7 +314,8 @@ try: b_need_cancel, b_cancel_data = SecondCancelBigNumComputer.need_cancel(code, buy_single_index, buy_exec_index, start_index, - end_index, total_data) + end_index, total_data, + is_first_code) if b_need_cancel: return b_cancel_data, "S澶у崟鎾ら攢姣斾緥瑙﹀彂闃堝��" except Exception as e: @@ -330,7 +333,7 @@ b_need_cancel, b_cancel_data = HourCancelBigNumComputer.need_cancel(code, buy_exec_index, start_index, end_index, total_data, local_today_num_operate_map.get( - code)) + code), is_first_code) if b_need_cancel and b_cancel_data: return b_cancel_data, "H鎾ら攢姣斾緥瑙﹀彂闃堝��" except Exception as e: @@ -398,7 +401,7 @@ _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "宸蹭笅鍗�-鎾ゅ崟 鑰楁椂") # 鎾ゅ崟鎴愬姛锛岀户缁绠椾笅鍗� - cls.__process_not_order(code, cancel_data["index"] + 1, end_index, capture_time) + cls.__process_not_order(code, cancel_data["index"] + 1, end_index, capture_time, is_first_code) _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "澶勭悊鍓╀綑鏁版嵁 鑰楁椂") else: @@ -412,14 +415,14 @@ # unreal_buy_info 鐨勫唴瀹规牸寮忎负锛�(瑙︽硶涔版搷浣滀笅鏍�,鎴浘鏃堕棿) # 鐪熷疄涓嬪崟 cls.__buy(code, unreal_buy_info[1], local_today_datas[code][unreal_buy_info[0]], - unreal_buy_info[0]) + unreal_buy_info[0], is_first_code) _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "宸茶櫄鎷熶笅鍗�-鎵ц鐪熷疄涓嬪崟 澶栭儴鑰楁椂") @classmethod - def __buy(cls, code, capture_timestamp, last_data, last_data_index): + def __buy(cls, code, capture_timestamp, last_data, last_data_index, is_first_code): __start_time = tool.get_now_timestamp() - can, need_clear_data, reason = cls.__can_buy(code) + can, need_clear_data, reason = cls.__can_buy(code, is_first_code) __start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - __start_time, "鏈�鍚庡垽鏂槸鍚﹁兘涓嬪崟", force=True) # 鍒犻櫎铏氭嫙涓嬪崟 if code in cls.unreal_buy_dict: @@ -428,8 +431,10 @@ if not can: l2_log.debug(code, "涓嶅彲浠ヤ笅鍗曪紝鍘熷洜锛歿}", reason) if need_clear_data: - # 涓柇涔板叆 - trade_manager.break_buy(code, reason) + buy_single_index, buy_exec_index, buy_compute_index, num, count, max_num_set = cls.__get_order_begin_pos( + code) + trade_result_manager.real_cancel_success(code, buy_single_index, buy_exec_index, + local_today_datas.get(code)) return else: l2_log.debug(code, "鍙互涓嬪崟锛屽師鍥狅細{}", reason) @@ -451,6 +456,9 @@ def __can_cancel(cls, code): if constant.TEST: return True, "" + if l2_trade_util.WhiteListCodeManager.is_in(code): + return False, "浠g爜鍦ㄧ櫧鍚嶅崟涓�" + # 鏆傛椂娉ㄩ噴鎺� # 14鐐瑰悗濡傛灉鏄澘鍧楄�佸ぇ灏变笉闇�瑕佸彇娑堜簡 # now_time_str = tool.get_now_time_str() @@ -480,14 +488,18 @@ # 鏄惁鍙互涔� # 杩斿洖鏄惁鍙互涔�,鏄惁闇�瑕佹竻闄や箣鍓嶇殑涔板叆淇℃伅锛屽師鍥� @classmethod - def __can_buy(cls, code): + def __can_buy(cls, code, is_first_code): __start_time = t.time() # 鍒ゆ柇鏄惁涓洪鏉夸唬鐮� - is_first = gpcode_manager.FirstCodeManager.is_in_first_record(code) - if is_first: + if is_first_code: # 棣栨澘浠g爜涓斿皻鏈定鍋滆繃鐨勪笉鑳戒笅鍗� is_limited_up = gpcode_manager.FirstCodeManager.is_limited_up(code) if not is_limited_up: + gpcode_manager.FirstCodeManager.add_limited_up_record([code]) + place_order_count = trade_data_manager.placeordercountmanager.get_place_order_count( + code) + if place_order_count == 0: + trade_data_manager.placeordercountmanager.place_order(code) return False, True, "棣栨澘浠g爜锛屼笖灏氭湭娑ㄥ仠杩�" try: @@ -504,7 +516,6 @@ total_datas = local_today_datas[code] if total_datas[-1]["index"] + 1 > len(total_datas): return False, True, "L2鏁版嵁閿欒" - try: sell1_time, sell1_price, sell1_volumn = cls.__ths_l2_trade_queue_manager.get_sell1_info(code) @@ -524,30 +535,34 @@ elif L2DataUtil.is_limit_up_price_buy_cancel(_val): buy_nums -= _val["num"] * total_datas[i]["re"] if buy_nums < sell1_volumn * 0.49: - return False, True, "绾拱閲�({})灏忎簬鍗�1閲忕殑49%{} 鍗�1鏃堕棿锛歿}".format(buy_nums, sell1_volumn, sell1_time) + return False, False, "绾拱閲�({})灏忎簬鍗�1閲忕殑49%{} 鍗�1鏃堕棿锛歿}".format(buy_nums, sell1_volumn, sell1_time) except Exception as e: logging.exception(e) # 閲忔瘮瓒呰繃1.3鐨勪笉鑳戒拱 volumn_rate = l2_trade_factor.L2TradeFactorUtil.get_volumn_rate_by_code(code) if volumn_rate >= 1.3: - return False, True, "鏈�澶ч噺姣旇秴杩�1.3涓嶈兘涔�" + return False, False, "鏈�澶ч噺姣旇秴杩�1.3涓嶈兘涔�" limit_up_time = limit_up_time_manager.get_limit_up_time(code) - if limit_up_time is not None and l2.l2_data_util.L2DataUtil.get_time_as_second( - limit_up_time) >= l2.l2_data_util.L2DataUtil.get_time_as_second( - "14:30:00"): - return False, True, "14:55鍚庢定鍋滅殑涓嶈兘涔帮紝娑ㄥ仠鏃堕棿涓簕}".format(limit_up_time) + if limit_up_time is not None: + limit_up_time_seconds = l2.l2_data_util.L2DataUtil.get_time_as_second( + limit_up_time) + if not is_first_code and limit_up_time_seconds >= l2.l2_data_util.L2DataUtil.get_time_as_second( + "13:00:00"): + return False, False, "浜屾澘涓嬪崍娑ㄥ仠鐨勪笉鑳戒拱锛屾定鍋滄椂闂翠负{}".format(limit_up_time) + if limit_up_time_seconds >= l2.l2_data_util.L2DataUtil.get_time_as_second("14:55:00"): + return False, False, "14:55鍚庢定鍋滅殑涓嶈兘涔帮紝娑ㄥ仠鏃堕棿涓簕}".format(limit_up_time) # 鍚屼竴鏉垮潡涓�佷簩鍚庨潰鐨勪笉鑳戒拱 industry, codes = ths_industry_util.get_same_industry_codes(code, gpcode_manager.get_gp_list()) if industry is None: - return True, True, "娌℃湁鑾峰彇鍒拌涓�" + return True, False, "娌℃湁鑾峰彇鍒拌涓�" codes_index = industry_codes_sort.sort_codes(codes, code) if codes_index is not None and codes_index.get(code) is not None and codes_index.get(code) > 1: # 褰撹�佸ぇ鑰佷簩褰撳墠娌℃定鍋� - return False, True, "鍚屼竴鏉垮潡涓�佷笁,鑰佸洓,...涓嶈兘涔�" + return False, False, "鍚屼竴鏉垮潡涓�佷笁,鑰佸洓,...涓嶈兘涔�" if cls.__codeActualPriceProcessor.is_under_water(code, total_datas[-1]["val"]["time"]): # 姘翠笅鎹炰笖鏉垮潡涓殑绁ㄥ皬浜�16涓嶈兘涔� @@ -559,7 +574,7 @@ # 鑾峰彇鑰佸ぇ鐨勫競鍊� for c in codes_index: if codes_index.get(c) == 0 and global_util.zyltgb_map.get(code) > global_util.zyltgb_map.get(c): - return False, True, "姘翠笅鎹烇紝涓嶆槸鑰佸ぇ锛屼笖鑷敱娴侀�氬競鍊煎ぇ浜庤�佸ぇ" + return False, False, "姘翠笅鎹烇紝涓嶆槸鑰佸ぇ锛屼笖鑷敱娴侀�氬競鍊煎ぇ浜庤�佸ぇ" # 13:30鍚庢定鍋滐紝鏈澘鍧椾腑娑ㄥ仠绁ㄦ暟<29涓嶈兘涔� # if limit_up_time is not None: @@ -649,6 +664,7 @@ @classmethod def __start_compute_buy(cls, code, compute_start_index, compute_end_index, threshold_money, capture_time, + is_first_code, new_add=True): if compute_end_index < compute_start_index: return @@ -721,7 +737,8 @@ # 涔板叆淇″彿浣嶄笌璁$畻浣嶇疆闂撮殧2s鍙婁互涓婁簡 if rebegin_buy_pos is not None: # 闇�瑕侀噸鏂拌绠楃函涔伴 - cls.__start_compute_buy(code, rebegin_buy_pos, compute_end_index, threshold_money, capture_time, False) + cls.__start_compute_buy(code, rebegin_buy_pos, compute_end_index, threshold_money, capture_time, + is_first_code, False) return if compute_index is not None: @@ -763,7 +780,7 @@ need_cancel, cancel_data = SecondCancelBigNumComputer.need_cancel(code, buy_single_index, compute_index, buy_single_index, compute_index, - total_datas, + total_datas, is_first_code, True) _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "S绾уぇ鍗曞鐞嗚�楁椂", force=True) @@ -774,17 +791,17 @@ # 鎵ц鎾ゅ崟鎴愬姛 pass else: - cls.__buy(code, capture_time, total_datas[compute_index], compute_index) + cls.__buy(code, capture_time, total_datas[compute_index], compute_index, is_first_code) else: SecondCancelBigNumComputer.need_cancel(code, buy_single_index, compute_index, buy_single_index, - compute_index, total_datas, False) + compute_index, total_datas, is_first_code, False) _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, "S绾уぇ鍗曞鐞嗚�楁椂", force=True) # 鏁版嵁灏氭湭澶勭悊瀹屾瘯锛岃繘琛屼笅涓�姝ュ鐞� l2_log.debug(code, "鏁版嵁灏氭湭澶勭悊瀹屾瘯锛岃繘琛屼笅涓�姝ュ鐞嗭紝澶勭悊杩涘害锛歿}", compute_index) # 澶勭悊鎾ゅ崟姝ラ - cls.__process_order(code, compute_index + 1, compute_end_index, capture_time, False) + cls.__process_order(code, compute_index + 1, compute_end_index, capture_time, is_first_code, False) _start_time = l2_data_log.l2_time(code, tool.get_now_timestamp() - _start_time, f"澶勭悊鎾ゅ崟姝ラ鑰楁椂锛岃寖鍥达細{compute_index + 1}-{compute_end_index}", force=True) @@ -892,7 +909,7 @@ place_order_count = trade_data_manager.placeordercountmanager.get_place_order_count(code) # 鐩爣璁㈠崟鏁伴噺 - threshold_count = cls.__buyL2SafeCountManager.get_safe_count(code,is_first_code, place_order_count) + threshold_count = cls.__buyL2SafeCountManager.get_safe_count(code, is_first_code, place_order_count) buy_single_time_seconds = L2DataUtil.get_time_as_second(total_datas[buy_single_index]["val"]["time"]) @@ -988,154 +1005,6 @@ threshold_num, buy_count, get_threshold_count(), len(max_buy_num_set), big_num_count) return None, buy_nums, buy_count, None, max_buy_num_set - - @classmethod - def test(cls): - code = "002556" - l2_trade_test.clear_trade_data(code) - load_l2_data(code, True) - - _start = t.time() - if True: - state = trade_manager.get_trade_state(code) - cls.random_key[code] = random.randint(0, 100000) - capture_timestamp = 1999988888 - try: - if state == trade_manager.TRADE_STATE_BUY_DELEGATED or state == trade_manager.TRADE_STATE_BUY_PLACE_ORDER: - # 宸叉寕鍗� - cls.__process_order(code, 1552, 1641, capture_timestamp) - else: - # 鏈寕鍗� - cls.__process_not_order(code, 1552, 1641, capture_timestamp) - except Exception as e: - logging.exception(e) - print("澶勭悊鏃堕棿", round((t.time() - _start) * 1000)) - return - - # 鎸塻鎵归噺鍖栨暟鎹� - total_datas = local_today_datas.get(code) - start_time = total_datas[0]["val"]["time"] - start_index = 0 - for i in range(0, len(total_datas)): - if total_datas[i]["val"]["time"] != start_time: - cls.random_key[code] = random.randint(0, 100000) - # 澶勭悊鏁版嵁 - start = start_index - # if start != 201: - # continue - end = i - 1 - print("澶勭悊杩涘害锛歿},{}".format(start, end)) - capture_timestamp = 1999999999 - state = trade_manager.get_trade_state(code) - try: - if state == trade_manager.TRADE_STATE_BUY_DELEGATED or state == trade_manager.TRADE_STATE_BUY_PLACE_ORDER: - # 宸叉寕鍗� - cls.__process_order(code, start, end, capture_timestamp) - else: - # 鏈寕鍗� - cls.__process_not_order(code, start, end, capture_timestamp) - except Exception as e: - logging.exception(e) - # t.sleep(1) - start_index = i - start_time = total_datas[i]["val"]["time"] - - print("鏃堕棿鑺辫垂:", round((t.time() - _start) * 1000)) - - @classmethod - def test1(cls): - code = "002556" - l2_trade_test.clear_trade_data(code) - local_latest_datas[code] = [] - load_l2_data(code, True) - _start = t.time() - capture_timestamp = 1999999999 - cls.process(code, local_today_datas[code][1552:1641], capture_timestamp) - print("鏃堕棿鑺辫垂:", round((t.time() - _start) * 1000)) - pass - - @classmethod - def test2(cls): - code = "002864" - load_l2_data(code) - limit_up_time_manager.load_limit_up_time() - limit_up_time = limit_up_time_manager.get_limit_up_time(code) - if limit_up_time is not None and l2.l2_data_util.L2DataUtil.get_time_as_second( - limit_up_time) >= l2.l2_data_util.L2DataUtil.get_time_as_second( - "14:55:00"): - return False, "14:55鍚庢定鍋滅殑涓嶈兘涔帮紝娑ㄥ仠鏃堕棿涓簕}".format(limit_up_time) - - # 鍚屼竴鏉垮潡涓�佷簩鍚庨潰鐨勪笉鑳戒拱 - industry, codes = ths_industry_util.get_same_industry_codes(code, gpcode_manager.get_gp_list()) - if industry is None: - return True, "娌℃湁鑾峰彇鍒拌涓�" - codes_index = industry_codes_sort.sort_codes(codes, code) - if codes_index is not None and codes_index.get(code) is not None and codes_index.get(code) > 1: - return False, "鍚屼竴鏉垮潡涓�佷笁,鑰佸洓,...涓嶈兘涔�" - - if cls.__codeActualPriceProcessor.is_under_water(code): - # 姘翠笅鎹炰笖鏉垮潡涓殑绁ㄥ皬浜�21涓嶈兘涔� - if global_util.industry_hot_num.get(industry) is not None and global_util.industry_hot_num.get( - industry) <= 16: - return False, "姘翠笅鎹�,鏉垮潡涓殑绁ㄥ皬浜�2鍙�,涓簕}".format(global_util.industry_hot_num.get(industry)) - - if codes_index.get(code) != 0: - return False, "姘翠笅鎹�,涓嶆槸鑰佸ぇ锛屾槸鑰亄}".format(codes_index.get(code)) - - # 13:30鍚庢定鍋滐紝鏈澘鍧椾腑娑ㄥ仠绁ㄦ暟<29涓嶈兘涔� - limit_up_time = limit_up_time_manager.get_limit_up_time(code) - if limit_up_time is not None: - if int(limit_up_time.replace(":", "")) >= 133000 and global_util.industry_hot_num.get(industry) is not None: - if global_util.industry_hot_num.get(industry) < 16: - return False, "13:30鍚庢定鍋滐紝鏈澘鍧椾腑娑ㄥ仠绁ㄦ暟<16涓嶈兘涔�" - - if codes_index.get(code) is not None and codes_index.get(code) == 1: - # ----姝ゆ潯娉ㄩ噴----- - # 濡傛灉鑰佸ぇ宸茬粡涔版垚鍔熶簡锛岃�佷簩灏变笉闇�瑕佷拱浜� - # first_codes = [] - # for key in codes_index: - # if codes_index.get(key) == 0: - # first_codes.append(key) - # - # for key in first_codes: - # state = trade_manager.get_trade_state(key) - # if state == trade_manager.TRADE_STATE_BUY_SUCCESS: - # # 鑰佸ぇ宸茬粡涔版垚鍔熶簡 - # return False, "鑰佸ぇ{}宸茬粡涔版垚鍔燂紝鑰佷簩鏃犻渶璐拱".format(key) - # ----姝ゆ潯娉ㄩ噴----- - - # ----姝ゆ潯娉ㄩ噴----- - # 鏈�9鐐瑰崐娑ㄥ仠鐨勮�佸ぇ鎵嶈兘涔拌�佷簩锛屼笉鐒朵笉鑳戒拱 - # 鑾峰彇鑰佸ぇ鐨勬定鍋滄椂闂� - # for key in first_codes: - # # 鎵惧埌浜嗚�佸ぇ - # time_ = limit_up_time_manager.get_limit_up_time(key) - # if time_ == "09:30:00": - # return True, "9:30娑ㄥ仠鐨勮�佸ぇ锛岃�佷簩鍙互涓嬪崟" - # return False, "鑰佸ぇ闈�9:30娑ㄥ仠锛岃�佷簩涓嶈兘涓嬪崟" - # ----姝ゆ潯娉ㄩ噴----- - - return True, "鑰佷簩鍙互涓嬪崟" - - @classmethod - def test3(cls): - code = "002094" - load_l2_data(code, True) - cls.random_key[code] = random.randint(0, 100000) - buy_single_begin_index, buy_exec_index = 426, 479 - - L2LimitUpMoneyStatisticUtil.process_data(code, 480, 519, - buy_single_begin_index, buy_exec_index, False) - L2LimitUpMoneyStatisticUtil.process_data(code, 480, 519, - buy_single_begin_index, buy_exec_index, False) - - @classmethod - def test_can_buy(cls): - code = "002923" - load_l2_data(code, True) - limit_up_time_manager.load_limit_up_time() - can, msg = cls.__can_buy(code) - print(can, msg) if __name__ == "__main__": -- Gitblit v1.8.0