| | |
| | | import constant |
| | | from db import redis_manager_delegate as redis_manager, mysql_data_delegate as mysql_data |
| | | from db.redis_manager_delegate import RedisUtils |
| | | from log_module import log_export |
| | | from log_module import log_export, async_log_util |
| | | from log_module.log import hx_logger_l2_transaction, logger_debug, logger_request_api |
| | | from strategy import data_cache |
| | | from strategy.kpl_data_manager import KPLStockOfMarketsPlateLogManager |
| | |
| | | raise Exception("没有获取到L1数据") |
| | | pre_price = data[1] |
| | | current_price = data[2] if data[2] else data[5][0][0] |
| | | # 获取最新成交价格 |
| | | latest_deal_price = data_cache.latest_deal_price_dict.get(code) |
| | | if latest_deal_price: |
| | | current_price = round(float(latest_deal_price), 2) |
| | | async_log_util.info(logger_debug, f"根据成交价卖出:{code}-{latest_deal_price}") |
| | | |
| | | price = tool.get_buy_min_price(current_price) |
| | | price = max(price, tool.get_limit_down_price(code, pre_price)) |
| | | else: |
| | |
| | | logger_debug.exception(e) |
| | | finally: |
| | | self.__last_price_dict[code] = price |
| | | data_cache.latest_deal_price_dict[code] = price |
| | | |
| | | def OnMarketData(self, code, datas): |
| | | # logger_debug.info(f"收到L2Market数据:{datas}") |
| | |
| | | elif data_cache.have_plate_buy_times >= 3: |
| | | logger_info(f"【不利】有概念买入已经 3 次了!不买了!!公司名称:{k_line_data[0]['sec_name']},") |
| | | elif len(data_cache.addition_position_symbols_set) >= 3: |
| | | L2_data_analysis.find_L2_big_order_of_code(symbol_code) |
| | | logger_info(f"【不利】当日已经买了3只票!不买了!!公司名称:{k_line_data[0]['sec_name']},") |
| | | L2_data_analysis.find_L2_big_order_of_code(symbol_code) |
| | | elif now_time < data_cache.OPENING_TIME or now_time > data_cache.NOON_MARKET_TIME: |
| | | logger_info(f"【不利】不在9:30-13:05时间内!不买!!") |
| | | else: |
| | |
| | | f"【不利】同概念只买一次,不买了,公司名称:{k_line_data[0]['sec_name']},重复相交概念==={intersection_plate}") |
| | | elif data_cache.have_plate_buy_times >= 1: |
| | | logger_info(f"【不利】有概念无强度买入已经1次了!不买了!!公司名称:{k_line_data[0]['sec_name']},") |
| | | L2_data_analysis.find_L2_big_order_of_code(symbol_code) |
| | | elif len(data_cache.addition_position_symbols_set) >= 4: |
| | | logger_info(f"【不利】当日已经买了3只票!不买了!!公司名称:{k_line_data[0]['sec_name']},") |
| | | L2_data_analysis.find_L2_big_order_of_code(symbol_code) |
| | | elif now_time < data_cache.OPENING_TIME or now_time > data_cache.NOON_MARKET_TIME: |
| | | logger_info(f"【不利】不在9:30-13:05时间内!不买!!") |
| | | else: |
| | |
| | | f"【不利】自由市值小于6亿!不买!! 公司名称:{k_line_data[0]['sec_name']},最新价: {current_price}") |
| | | elif data_cache.have_strength_buy_times >= 1: |
| | | logger_info(f"【不利】有强度买入 1 次了!不买了!!公司名称:{k_line_data[0]['sec_name']},") |
| | | L2_data_analysis.find_L2_big_order_of_code(symbol_code) |
| | | elif len(data_cache.addition_position_symbols_set) >= 3: |
| | | logger_info(f"【不利】当日已经买了3只票!不买了!!公司名称:{k_line_data[0]['sec_name']},") |
| | | L2_data_analysis.find_L2_big_order_of_code(symbol_code) |
| | | elif now_time < data_cache.OPENING_TIME or now_time > data_cache.NOON_MARKET_TIME: |
| | | logger_info(f"【不利】不在9:30-13:05时间内!不买!!") |
| | | else: |
| | |
| | | f"【不利】自由市值小于6亿!不买!! 公司名称:{k_line_data[0]['sec_name']},最新价: {current_price}") |
| | | elif data_cache.have_small_turn_large_buy_times >= 1: |
| | | logger_info(f"【不利】有小量换大涨幅买入已经 1 次了!不买了!!公司名称:{k_line_data[0]['sec_name']},") |
| | | L2_data_analysis.find_L2_big_order_of_code(symbol_code) |
| | | elif len(data_cache.addition_position_symbols_set) >= 4: |
| | | logger_info( |
| | | f"【不利】当日已经买了4只票!不买了!!公司名称:{k_line_data[0]['sec_name']},") |
| | | L2_data_analysis.find_L2_big_order_of_code(symbol_code) |
| | | elif ( |
| | | data_cache.MORN_MARKET_TIME < now_time < data_cache.NOON_MARKET_TIME) is False or free_market_value < 100 or ( |
| | | today_open_growth < 5 or today_growth < 5): |
| | |
| | | f"目前获取的精选板块股票强度数据 == {data_cache.market_sift_plate_stock_dict}") |
| | | # buying_ratio = basic_methods.maximum_buying_ratio(len(data_cache.addition_position_symbols_set)) |
| | | # 调用下单方法下单 |
| | | order_methods.buy_order_by_value(symbol, 1000, |
| | | k_line_data[0]['sec_name'], |
| | | current_price) |
| | | # order_methods.buy_order_by_value(symbol, 1000, |
| | | # k_line_data[0]['sec_name'], |
| | | # current_price) |
| | | # # 将有小量换大涨幅买入次数自加1 |
| | | # data_cache.have_small_turn_large_buy_times += 1 |
| | | # # 将买入个股的当时概念添加到全局变量中存储 |
| | |
| | | # 最新的L1数据: {代码: L1数据} |
| | | current_l1_dict = {} |
| | | |
| | | # 最新成交价格 |
| | | latest_deal_price_dict = {} |
| | | |
| | | logging.info(f"全局初始化数据 完成《《《 - {os.getpid()}") |
| | | |
| | | |
| | |
| | | |
| | | # 计算市场分布形态因子 函数 |
| | | # ====================== 输入数据 ====================== |
| | | data = {'-1': '2676', '-10': '4', '-2': '769', '-3': '181', '-4': '59', '-5': '43', '-6': '18', '-7': '11', '-8': '5', '-9': '4', '0': '219', '1': '714', '10': '6', '2': '214', '3': '69', '4': '45', '5': '23', '6': '9', '7': '9', '8': '7', '9': '6', 'DT': 17, 'SJDT': '8', 'SJZT': '17', 'STDT': '9', 'STZT': '4', 'SZJS': 1123, 'XDJS': 3787, 'ZSZDFB': '480,1639,112,123,351,26,76,202,15,17,32,1,15,30,5,78,204,18,', 'ZT': 21, 'sign': '市场人气一般', 'szln': 2591290, 'qscln': 6629612, 's_zrcs': 2604433, 'q_zrcs': 6660830, 's_zrtj': 38031347, 'q_zrtj': 91465597} |
| | | data = {'-1': '2676', '-10': '4', '-2': '769', '-3': '181', '-4': '59', '-5': '43', '-6': '18', '-7': '11', '-8': '5', |
| | | '-9': '4', '0': '219', '1': '714', '10': '6', '2': '214', '3': '69', '4': '45', '5': '23', '6': '9', '7': '9', |
| | | '8': '7', '9': '6', 'DT': 17, 'SJDT': '8', 'SJZT': '17', 'STDT': '9', 'STZT': '4', 'SZJS': 1123, 'XDJS': 3787, |
| | | 'ZSZDFB': '480,1639,112,123,351,26,76,202,15,17,32,1,15,30,5,78,204,18,', 'ZT': 21, 'sign': '市场人气一般', |
| | | 'szln': 2591290, 'qscln': 6629612, 's_zrcs': 2604433, 'q_zrcs': 6660830, 's_zrtj': 38031347, 'q_zrtj': 91465597} |
| | | |
| | | |
| | | # ====================== 总手涨跌分布数据预处理 ====================== |
| | |
| | | |
| | | # 2. 定义区间划分(假设从-9%到+9%,1%间隔) |
| | | bins = [ |
| | | "[+9%, +8%)", "[+8%, +7%)", "[+7%, +6%)", "[+6%, +5%)", "[+5%, +4%)", "[+4%, +3%)", "[+3%, +2%)", "[+2%, +1%)", "[+1%, 0%)", "[0%, -1%)", "[-1%, -2%)", "[-2%, -3%)", "[-3%, -4%)", "[-4%, -5%)", "[-5%, -6%)", "[-6%, -7%)", "[-7%, -8%)", "[ -8%, -9%)" |
| | | ] |
| | | "[+9%, +8%)", "[+8%, +7%)", "[+7%, +6%)", "[+6%, +5%)", "[+5%, +4%)", "[+4%, +3%)", "[+3%, +2%)", "[+2%, +1%)", |
| | | "[+1%, 0%)", "[0%, -1%)", "[-1%, -2%)", "[-2%, -3%)", "[-3%, -4%)", "[-4%, -5%)", "[-5%, -6%)", "[-6%, -7%)", |
| | | "[-7%, -8%)", "[ -8%, -9%)" |
| | | ] |
| | | |
| | | # 3. 验证数据长度 |
| | | if len(counts) != len(bins): |
| | |
| | | } |
| | | |
| | | return result |
| | | |
| | | |
| | | # ====================== 核心因子计算 ====================== |
| | | def calculate_factors(data): |
| | |
| | | signals.append("高情绪热度:涨停股多且人气向好") |
| | | |
| | | # 信号5:涨跌分布综合 |
| | | if factors['top_three']['top1']['value']+factors['top_three']['top2']['value']+factors['top_three']['top3']['value'] > 50: |
| | | value = factors['top_three']['top1']['value'] + factors['top_three']['top2']['value'] + factors['top_three']['top3']['value'] |
| | | if factors['top_three']['top1']['value'] + factors['top_three']['top2']['value'] + factors['top_three']['top3'][ |
| | | 'value'] > 50: |
| | | value = factors['top_three']['top1']['value'] + factors['top_three']['top2']['value'] + \ |
| | | factors['top_three']['top3']['value'] |
| | | signals.append(f"涨跌分布:长尾分布{value}") |
| | | if factors['top_three']['top1']['value']/factors['top_three']['top2']['value'] > 1.25: |
| | | signals.append(f"涨跌分布:强势聚集{factors['top_three']['top1']['key']}:{factors['top_three']['top1']['value']}%") |
| | | if factors['top_three']['top1']['value'] / factors['top_three']['top2']['value'] > 1.25: |
| | | signals.append( |
| | | f"涨跌分布:强势聚集{factors['top_three']['top1']['key']}:{factors['top_three']['top1']['value']}%") |
| | | else: |
| | | signals.append(f"涨跌分布:中度聚集{factors['top_three']['top1']['key']}:{factors['top_three']['top1']['value']}%") |
| | | signals.append( |
| | | f"涨跌分布:中度聚集{factors['top_three']['top1']['key']}:{factors['top_three']['top1']['value']}%") |
| | | else: |
| | | value = factors['top_three']['top1']['value'] + factors['top_three']['top2']['value'] + factors['top_three']['top3']['value'] |
| | | value = factors['top_three']['top1']['value'] + factors['top_three']['top2']['value'] + \ |
| | | factors['top_three']['top3']['value'] |
| | | signals.append(f"涨跌分布:均匀分布{value}") |
| | | |
| | | return signals if signals else ["无显著信号:维持当前策略"] |
| | |
| | | # data_cache.today_planned_order_amount = ((data_cache.real_time_market_strong + data_cache.index_trend_expectation_score) * 0.01) * ( |
| | | # usefulMoney * 0.9 * low_emotion_mood_ratio / Unfinished_opening_plan_number) |
| | | # 除以3应该是一个常量,如果以Unfinished_opening_plan_number,会出现float division by zero 错误 |
| | | data_cache.today_planned_order_amount = usefulMoney * 0.95 * ideal_trading_market_score / 3 |
| | | if not data_cache.today_planned_order_amount or data_cache.today_planned_order_amount <= 0: |
| | | data_cache.today_planned_order_amount = usefulMoney * 0.95 * ideal_trading_market_score / 3 |
| | | logger.info(f"第一次计划下单金额==={data_cache.today_planned_order_amount}元") |
| | | # 开仓计划运行时间段 |
| | | if data_cache.OPENING_TIME < now_time < data_cache.NOON_MARKET_TIME: |
| | | # # 如果一日三仓的计划还未完成 且 首次开仓金额为0 |
| | |
| | | # logger.info(f"采用开仓策略计算方式=》今日计划下单金额:{data_cache.today_planned_order_amount}") |
| | | if addition_position_number > 0: |
| | | # 下过一次单过后再计算动态下单金额 |
| | | data_cache.today_first_planned_order_amount = float(data_cache.today_planned_order_amount) |
| | | data_cache.today_first_planned_order_amount = float( |
| | | data_cache.today_planned_order_amount) |
| | | logger.info( |
| | | f"采用开仓策略计算方式--》今日(首个)新增持仓产生,将实时计算金额赋值给首笔持仓金额=》今日计划下单金额:{data_cache.today_planned_order_amount}") |
| | | |
| | |
| | | print(f"总股票数: {factors['total_stocks']}") |
| | | print(f"涨跌比(BDR): {factors['rise_vs_fall']['rise_vs_fall_ratio']:.2f}") |
| | | print(f"极端波动比例: {factors['sentiment']['extreme_ratio']:.2%}") |
| | | print(f"资金净流入(元): {round(factors['capital_flow']['net']/10000, 2)}万") |
| | | print(f"资金净流入(元): {round(factors['capital_flow']['net'] / 10000, 2)}万") |
| | | print(f"涨停股占比: {factors['sentiment']['zt_ratio']:.2%}") |
| | | print(f"聚集前三名情况字典=={factors['top_three']}") |
| | | print(f"上涨幅段最大:{factors['rise_vs_fall']['rise_max_key']}:{factors['rise_vs_fall']['percentages'].get(factors['rise_vs_fall']['rise_max_key'])}%") |
| | | print(f"下跌幅段最大:{factors['rise_vs_fall']['fall_max_key']}:{factors['rise_vs_fall']['percentages'].get(factors['rise_vs_fall']['fall_max_key'])}%") |
| | | print(f"聚集区域:{factors['rise_vs_fall']['gather_area']},聚集区域的比例值:{factors['rise_vs_fall']['percentages'].get(factors['rise_vs_fall']['gather_area'])}%") |
| | | print(f"零散区域:{factors['rise_vs_fall']['scattered_area']},聚集区域的比例值:{factors['rise_vs_fall']['percentages'].get(factors['rise_vs_fall']['scattered_area'])}%") |
| | | print( |
| | | f"上涨幅段最大:{factors['rise_vs_fall']['rise_max_key']}:{factors['rise_vs_fall']['percentages'].get(factors['rise_vs_fall']['rise_max_key'])}%") |
| | | print( |
| | | f"下跌幅段最大:{factors['rise_vs_fall']['fall_max_key']}:{factors['rise_vs_fall']['percentages'].get(factors['rise_vs_fall']['fall_max_key'])}%") |
| | | print( |
| | | f"聚集区域:{factors['rise_vs_fall']['gather_area']},聚集区域的比例值:{factors['rise_vs_fall']['percentages'].get(factors['rise_vs_fall']['gather_area'])}%") |
| | | print( |
| | | f"零散区域:{factors['rise_vs_fall']['scattered_area']},聚集区域的比例值:{factors['rise_vs_fall']['percentages'].get(factors['rise_vs_fall']['scattered_area'])}%") |
| | | print(f"市场情绪量化: {'积极' if factors['sentiment']['sign'] else '谨慎'}") |
| | | print(f"涨跌因子字典={factors['rise_vs_fall']}") |
| | | |
| | |
| | | for item in factors['rise_vs_fall']['parsed_data']["区间分布"]: |
| | | print(f"{item['区间']}: {item['成交量(手)']}手") |
| | | print(f"\n总成交量: {factors['rise_vs_fall']['parsed_data']['总成交量(手)']}手") |
| | | print(f"说明: {factors['rise_vs_fall']['parsed_data']['说明']}") |
| | | print(f"说明: {factors['rise_vs_fall']['parsed_data']['说明']}") |