From bc1913abfdc364d97facb3edcd2d54130c8e1ce2 Mon Sep 17 00:00:00 2001 From: admin <admin@example.com> Date: 星期一, 23 六月 2025 10:30:59 +0800 Subject: [PATCH] bug修复 --- main.py | 172 +++++++++++++++++++++++++++++++++++++++++++++----------- 1 files changed, 137 insertions(+), 35 deletions(-) diff --git a/main.py b/main.py index 154134c..c3b0ceb 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,7 @@ # coding=utf-8 from __future__ import print_function, absolute_import, unicode_literals import logging -import json -import os.path -# from log import logger +import multiprocessing import threading import time @@ -11,7 +9,8 @@ # 寮曞叆鎺橀噾妗ユAPI import utils.juejin_api from db.redis_manager_delegate import RedisUtils -from log_module.log import logger_common, logger_kpl_jingxuan_in, logger_system +from huaxin_client.l2_data_transform_protocol import L2DataCallBack +from log_module.log import logger_common, logger_kpl_jingxuan_in, logger_system, logger_debug # 寮曞叆寮�鐩樺暒API妯″潡 # 寮曞叆鍏ㄥ眬鍙橀噺妯″潡 # 寮曞叆瀹氭椂鍣ㄦā鍧� @@ -19,11 +18,13 @@ # 寮曞叆鐬椂鍒嗘椂琛屾儏妯″潡 # 寮曞叆璐︽埛绠$悊妯″潡銆愯繘琛岃祫閲戝拰浠撲綅绠$悊銆� from strategy import kpl_api, data_cache, check_timer, all_K_line, instant_time_market, account_management, \ - order_methods, local_data_management -from huaxin_client import l2_market_client -from log_module import async_log_util -from trade import huaxin_trade_data_update -from utils import hx_qc_value_util + order_methods, local_data_management, kpl_data_manager, market_sentiment_analysis, plate_strength_analysis, \ + selling_strategy +from huaxin_client import l2_market_client, l2_client, trade_client +from log_module import async_log_util, log +from strategy.order_methods import TodayBuyCodeManager +from trade import huaxin_trade_data_update, huaxin_trade_api +from utils import hx_qc_value_util, huaxin_util, juejin_api, tool # 寮曞叆琛屾儏璁㈤槄妯″潡 # import subscribe_market @@ -67,19 +68,9 @@ local_data_management.read_local_K_line_data() # 璇诲彇鏈湴涓偂鎵�灞炴澘鍧楁暟鎹� 骞舵洿鏂板埌data_cache local_data_management.read_local_all_stocks_plate_data() + # 鍒濆鍖栨媺鍙栧綋鏃ヤ拱鍏ヤ唬鐮� + TodayBuyCodeManager() - # todo 2025-03-25 娴嬭瘯鏃犺鍗冲彲鍒犻櫎涓嬮儴娉ㄩ噴 - # # 鍏堜娇鐢╦son.load()鐩存帴浠庢枃浠朵腑璇诲彇銆愬凡缁忓瓨鍌ㄥ湪鏈湴鐨凨绾挎寚鏍囧睘鎬у瓧鍏搞�戝苟瑙f瀽JSON鏁版嵁 - # if os.path.exists(constant.K_BARS_PATH): - # with open(constant.K_BARS_PATH, 'r', encoding='utf-8') as f: - # data_cache.all_stocks_all_K_line_property_dict = json.load(f) - # print( - # f"data_cache.all_stocks_all_K_line_property_dict鐨勪釜鏁�==={len(data_cache.all_stocks_all_K_line_property_dict)}") - - # # 鑾峰彇鐩爣鏍囩殑K绾�---鍒濆鍖� - # all_K_line.main_index_k_line_history.init(data_cache.DataCache().today_date, data_cache.DataCache().next_trading_day, data_cache.DataCache().main_index_stocks) - # # 鐩存帴璋冪敤涓昏鎸囨暟K绾垮啓鍏ユ湰鍦版枃浠� - # all_K_line.main_index_k_line_dict_write() # 绗竴姝ワ細鍒濆鍖朿ontext鍑芥暟锛屽苟寮�鍚幏鍙栧疄鏃舵暟鎹殑绾跨▼ @@ -89,35 +80,48 @@ # 瀹炴椂杩愯瀹氭椂鍣ㄧ嚎绋嬨�愬畾鏃跺櫒鍑芥暟鐩墠 鍙鐞� 15:00 鍚庤繍琛屼竴娆� 鏁寸悊褰撴棩娑ㄥ仠淇℃伅 鍜� 鑾峰彇鎵�鏈変釜鑲$殑鏉垮潡姒傚康銆� threading.Thread(target=lambda: check_timer.check_time(), daemon=True).start() # 鑾峰彇瀹炴椂澶х洏琛屾儏鎯呯华缁煎悎寮哄害 [鍒嗘暟] 绾跨▼ - threading.Thread(target=lambda: kpl_api.get_real_time_market_strong(), daemon=True).start() + threading.Thread(target=lambda: market_sentiment_analysis.set_plan_position_quantity(), daemon=True).start() # 瀹炴椂妫�娴嬫槸鍚︽媺鍙朘绾跨嚎绋� threading.Thread(target=lambda: all_K_line.check_time_and_data_date(), daemon=True).start() # print(f"all_stocks_all_K_line_property_dict== {type(data_cache.all_stocks_all_K_line_property_dict)}") # 鑾峰彇瀹炴椂澶х洏鎸囨暟琛屾儏绾跨▼ threading.Thread(target=lambda: instant_time_market.index_market_current(), daemon=True).start() + # instant_time_market.index_market_trend() # 寮�鐩樺暒鐨勬定鍋滄蹇电殑鍥炶皟鍑芥暟 def kpl_limit_up_process(datas): # print(f"鍥炶皟鎴愬姛==={datas}") - data_cache.limit_up_block_names = datas + now_time = tool.get_now_time_str() + if datas is not None and len(datas) > 0: + data_cache.limit_up_block_names = datas + if data_cache.CLOSING_TIME < now_time < data_cache.AFTER_CLOSING_TIME: + logger_common.info(f"鏀剁洏娑ㄥ仠姒傚康鍒楄〃鏇存柊==={now_time}=={datas}") + else: + data_cache.limit_up_block_names = [] # # 璁$畻褰撳墠绾跨▼鏁伴噺 # get_current_thread_count() # 寮�鍚紑鐩樺暒 娑ㄥ仠鍒楄〃 鍜� 鍏ㄧ洏涓偂姒傚康鏉垮潡 鎺ュ彛绾跨▼ # 娑ㄥ仠姒傚康绾跨▼ - # threading.Thread(target=kpl_api.kpl_limit_up_process, daemon=True).start() #璇ヨ浠g爜涓哄彧杩愯鍗曚竴绾跨▼涓嶅洖璋冩暟鎹殑鏂瑰紡 - threading.Thread(target=kpl_api.kpl_limit_up_process, args=(kpl_limit_up_process,), daemon=True).start() + # threading.Thread(target=plate_strength_analysis.kpl_limit_up_process, daemon=True).start() #璇ヨ浠g爜涓哄彧杩愯鍗曚竴绾跨▼涓嶅洖璋冩暟鎹殑鏂瑰紡 + threading.Thread(target=plate_strength_analysis.kpl_limit_up_process, args=(kpl_limit_up_process,), + daemon=True).start() # # 寮�鐩樺暒鐨勬澘鍧楀己搴︿笅鐨勪釜鑲″己搴﹀洖璋冨嚱鏁� - def get_market_sift_plate_its_stock_power_process(market_sift_plate_stock_dict): + def get_market_sift_plate_its_stock_power_process(market_sift_plate_info): # print(f"鍥炶皟鎴愬姛===绮鹃�夋澘鍧楄偂绁ㄥ己搴︽暟鎹洿鏂�==={market_sift_plate_stock_dict}") # logger_kpl_jingxuan_in.info(f"{market_sift_plate_stock_dict}") - data_cache.market_sift_plate_stock_dict = market_sift_plate_stock_dict + if market_sift_plate_info is None: + return + market_sift_plate_stock_dict, market_sift_plates = market_sift_plate_info[0], market_sift_plate_info[1] + if market_sift_plate_stock_dict: + data_cache.market_sift_plate_stock_dict = market_sift_plate_stock_dict + data_cache.market_sift_plates = market_sift_plates # 鏉垮潡寮哄害涓嬩釜鑲″己搴︾嚎绋� - threading.Thread(target=kpl_api.get_market_sift_plate_its_stock_power_process, + threading.Thread(target=plate_strength_analysis.get_market_sift_plate_its_stock_power_process, args=(get_market_sift_plate_its_stock_power_process,), daemon=True).start() # 鍒濆鍖杇et_current_data鏂规硶鍑芥暟锛屼笅鍗曚拱閫昏緫鎵嶄細杩愯涓�傘�傘�傘�愭牳蹇冧富绾跨▼锛岄殢鏃惰�冭檻鍏跺惎鍔ㄩ『搴忋��>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> @@ -125,13 +129,13 @@ try: # 璁$畻寮�鐩樺暒鏄ㄦ棩鎷夊彇鐨勬蹇垫暟鎹腑涓虹┖鐨勮偂绁ㄦ暟閲� - kpl_api.get_have_no_plate_num() + plate_strength_analysis.get_have_no_plate_num() except Exception as e: logger_system.exception(e) # 鑾峰彇鍘嗗彶娑ㄥ仠淇℃伅鏁版嵁骞舵暣鐞� try: - kpl_api.get_handling_limit_up_info() + plate_strength_analysis.get_handling_limit_up_info() except Exception as e: logger_system.exception(e) @@ -141,12 +145,97 @@ except Exception as e: logger_system.exception(e) - # # 鑾峰彇鎵�鏈変釜鑲$殑鏉垮潡姒傚康骞跺啓鍏ユ枃浠躲�愯�楁椂杈冮暱搴旇鏀惧湪 鏍稿績涓荤嚎绋� 鍜� 浠撲綅绠$悊 鍚庨潰銆� - # kpl_api.get_all_stocks_plate_dict(data_cache.min_stocks) + +# 鎸佷粨浠g爜鐨凩2鏁版嵁鍥炶皟 +class MyPositionsL2DataCallback(L2DataCallBack): + __last_price_dict = {} + __pre_close_price_dict = {} # 鏄ㄦ棩鏀剁洏浠� + + def OnL2Transaction(self, code, datas): + """ + 鏄ㄦ棩鎸佷粨L2閫愮瑪鎴愪氦鍥炶皟 + :param code: + :param datas: + :return: + """ + if datas: + start_time = time.time() + # logger_debug.debug(f"{code} - L2閫愮瑪鎴愪氦涓婃姤锛歿len(datas)}") + price, time_str = datas[-1][1], huaxin_util.convert_time(datas[-1][3]) + try: + # 鑾峰彇鏈�杩戠殑鎴愪氦浠� + if code not in self.__pre_close_price_dict: + # 鑾峰彇鏀剁洏浠锋牸 + results = juejin_api.JueJinApi.history_n(tool.get_symbol(code), "1d", 1, 1, "close") + if results: + self.__pre_close_price_dict[code] = results[0]["close"] + logger_debug.debug(f"{code} - 鑾峰彇鍒版槰鏃ユ敹鐩樹环锛歿results[0]['close']}") + if self.__last_price_dict.get(code) == price: + return + limit_up_price = tool.get_limit_up_price(code, self.__pre_close_price_dict[code]) + if code in self.__last_price_dict: + if abs(limit_up_price - self.__last_price_dict[code]) < 0.0001 < abs(limit_up_price - price): + # 澶勭悊鐐告澘閫昏緫 + # 鐩戝惉浜嗙偢鏉夸簡瑕佸仛鐨勫嚱鏁� + try: + selling_strategy.explosion_strategy(code) + # 鐐告澘s + finally: + logger_debug.info(f"鐐告澘:{code}-({price},{time_str})") + except Exception as e: + logger_debug.exception(e) + finally: + self.__last_price_dict[code] = price + data_cache.latest_deal_price_dict[code] = price + use_time = time.time() - start_time + if use_time > 0.1: + logger_debug.warning(f"L2閫愮瑪鎴愪氦澶勭悊鑰楁椂锛歿use_time} 鏈�鍚庝竴鏉℃暟鎹細{datas[-1]}") + + + def OnMarketData(self, code, datas): + # logger_debug.info(f"鏀跺埌L2Market鏁版嵁锛歿datas}") + for d in datas: + code = d["securityID"] + buy1 = d["buy"][0] + + # 瀹炴椂L2涔�1鎴愪氦閲� + def OnRealTimeBuy1Info(self, code, buy1_info): + pass + # buy1_info: [涔�1鏃堕棿,涔�1浠锋牸, 鍘熷涔�1閲�, 瀹炴椂涔�1閲廬 + # 鏈�缁堢殑涔�1涓猴細 鍘熷涔�1閲�+瀹炴椂涔�1閲� + # async_log_util.info(logger_debug, f"OnRealTimeBuy1Info锛歿code}-{buy1_info}") + # L1DataProcessor.excute_sell_rule(code, buy1_info[3], buy1_info[1], "l2-real") + + +l2_data_callbacks = [] + + +# 璁㈤槄鎸佷粨L2鏁版嵁 +def __subscript_position_l2(): + """ + 璁㈤槄鎸佷粨L2鏁版嵁 + :return: + """ + position_result = huaxin_trade_api.get_position_list(blocking=True) + logger_debug.info(f"鑾峰彇鎸佷粨缁撴灉锛歿position_result}") + if not position_result or position_result["code"] != 0 or not position_result["data"]: + return + positions = position_result["data"] + subscript_codes = set() + for p in positions: + if p["prePosition"] > 0: + subscript_codes.add(p["securityID"]) + if not subscript_codes: + return + + for i in range(len(subscript_codes)): + l2_data_callbacks.append(MyPositionsL2DataCallback()) + l2_client.run(subscript_codes, l2_data_callbacks) # 绗笁姝ワ細鎵ц绛栫暐鐨勫垵濮嬭缃� if __name__ == '__main__': + log.close_print() class MyMarketDataCallback(l2_market_client.L2MarketDataCallback): def on_markets(self, datas): """ @@ -155,13 +244,15 @@ :return: """ data_cache.latest_code_market_info_dict = {x[0]: x for x in datas} - if datas: - print(datas[0]) if constant.is_windows(): instant_time_market.get_current_info() else: instant_time_market.set_current_info(datas) + + # 鍔犺浇寮�鐩樺暒鏉垮潡鏃ュ織鏁版嵁 + kpl_data_manager.KPLMarketsSiftPlateLogManager() + kpl_data_manager.KPLMarketStockHeatLogManager() # 鍚姩寮傛鏃ュ織 threading.Thread(target=async_log_util.run_sync, daemon=True).start() @@ -169,8 +260,16 @@ # redis 鏁版嵁鍚屾 threading.Thread(target=RedisUtils.run_loop, daemon=True).start() + # 绛栫暐涓庝氦鏄撻�氫俊闃熷垪 + queue_strategy_r_trade_w, queue_strategy_w_trade_r, queue_strategy_w_trade_for_query_r = multiprocessing.Queue(), multiprocessing.Queue(), multiprocessing.Queue() + + # 涓嶆槸妯℃嫙鐩樼殑鏃跺�欏惎鍔ㄤ氦鏄� + if not constant.IS_SIMULATED_TRADE: + multiprocessing.Process(target=trade_client.run, args=( + queue_strategy_r_trade_w, queue_strategy_w_trade_r, queue_strategy_w_trade_for_query_r,)).start() + # 鍚姩浜ゆ槗 - order_methods.run() + order_methods.run(queue_strategy_r_trade_w, queue_strategy_w_trade_r, queue_strategy_w_trade_for_query_r) # 杩愯鍗庨懌澧炲�兼湇鍔¤繘绋�,鐢ㄤ簬鑾峰彇K绾夸笌浜ゆ槗鏃ュ巻 threading.Thread(target=hx_qc_value_util.run, daemon=True).start() @@ -181,6 +280,9 @@ # 绛夊緟5s锛岀瓑鍏朵粬绾跨▼/杩涚▼鍚姩瀹屾瘯 time.sleep(15) + # 璁㈤槄鎸佷粨绁� + threading.Thread(target=__subscript_position_l2, daemon=True).start() + try: # 鍒濆鍖栨暟鎹� init() -- Gitblit v1.8.0