| | |
| | | from __future__ import print_function, absolute_import, unicode_literals |
| | | |
| | | import copy |
| | | import logging |
| | | # import logging |
| | | import os |
| | | import time |
| | | import datetime |
| | |
| | | import json |
| | | |
| | | import constant |
| | | # 引入掘金API |
| | | import utils.juejin_api |
| | | # import kpl_api |
| | | from log_module import async_log_util |
| | | from log_module.log import logger_common, logger_k_bars |
| | | from strategy import data_cache |
| | | |
| | | # 引入基础算法模块 |
| | | from strategy import basic_methods |
| | | # 引入本地日志模块 |
| | | from strategy.logging_config import get_logger |
| | | from utils import hx_qc_value_util, tool |
| | | |
| | | # 获取logger实例 |
| | | logger = get_logger() |
| | | logger = logger_common |
| | | |
| | | start_time = time.time() |
| | | print(f"all_K_line开始运行--{start_time}") |
| | |
| | | results = [] |
| | | dates_list = [] |
| | | # 查询交易标的基本信息并赋值 |
| | | sec_name = data_cache.DataCache().code_name_dict.get(symbol) |
| | | sec_name = data_cache.DataCache().code_name_dict.get(symbol, "default_name") |
| | | # print(f"K_line_sec_name=={sec_name}") |
| | | # TODO 查询代码名称 |
| | | # instruments = get_instruments(symbols=symbol, exchanges=None, sec_types=None, names=None, fields="sec_name", df=False)[0] |
| | | # 将获取到的公司名称直接 生成字典放到 K线的第一个序列中 |
| | | it_K_line[0]['sec_name'] = sec_name |
| | | for i in range(0, len(it_K_line) - 1): |
| | |
| | | # 最高位据昨日较远 且 昨收价距历史最高价差距较小(小于5%,涨幅5%是左右是最容易把低吸骗进去的日内涨幅) 且 整体涨跌幅度较大 【当前二度突破近前高风险】 |
| | | # 【双成药业 2024-11-08 这样的二次近前高,实际数值为1.4646...,为安全起见还是设置为1.5】 |
| | | if (2 < historical_high_price_index < 15 and len(it_K_line) >= 90) and ( |
| | | historical_high_price / round(it_K_line[0]['close'], 2) < 1.5) and ( |
| | | historical_high_price > historical_low_price * 1.8): |
| | | historical_high_price / round(it_K_line[0]['close'], 2) < 1.2) and ( |
| | | historical_high_price > historical_low_price * 2): |
| | | it_K_line[0]['risk_position'] = 'recent_second_break_near_high_position_risk' |
| | | # 昨日就是最高位 且 整体涨跌幅度较大 【当前高位风险】 |
| | | if (historical_high_price_index < 2 and len(it_K_line) >= 90) and ( |
| | |
| | | current_count = 0 |
| | | start = None |
| | | # 确保不会出现除以零的报错 |
| | | if current_yesterday_volume == 0: |
| | | current_yesterday_volume = 1 |
| | | if current_yesterday_volume != 0: |
| | | if round(current_today_volume / current_yesterday_volume, 2) > 1.1: |
| | | # print(f"i=={i} {it_K_line[i]['bob']} {round(current_today_volume/current_yesterday_volume,2)} 【放量】") |
| | | if current_today_growth > 0: |
| | |
| | | else: |
| | | it_K_line[i]['today_volume_shape'] = 'decreases_balance' |
| | | # print(f"i=={i} {it_K_line[i]['bob']} 【缩量平收】") |
| | | else: |
| | | # print(f"i=={i} {it_K_line[i]['bob']} {round(current_today_volume/current_yesterday_volume,2)} 【平量】") |
| | | if current_today_growth > 0: |
| | | it_K_line[i]['today_volume_shape'] = 'remained_up' |
| | | # print(f"i=={i} {it_K_line[i]['bob']} 【平量上涨】") |
| | | elif current_today_growth < 0: |
| | | it_K_line[i]['today_volume_shape'] = 'remained_down' |
| | | # print(f"i=={i} {it_K_line[i]['bob']} 【平量下跌】") |
| | | else: |
| | | it_K_line[i]['today_volume_shape'] = 'remained_balance' |
| | | # print(f"i=={i} {it_K_line[i]['bob']} 【平量平收】") |
| | | # print(f"i=={i} {it_K_line[i]['bob']} {round(current_today_volume/current_yesterday_volume,2)} 【平量】") |
| | | if current_today_growth > 0: |
| | | it_K_line[i]['today_volume_shape'] = 'remained_up' |
| | | # print(f"i=={i} {it_K_line[i]['bob']} 【平量上涨】") |
| | | elif current_today_growth < 0: |
| | | it_K_line[i]['today_volume_shape'] = 'remained_down' |
| | | # print(f"i=={i} {it_K_line[i]['bob']} 【平量下跌】") |
| | | else: |
| | | it_K_line[i]['today_volume_shape'] = 'remained_balance' |
| | | # print(f"i=={i} {it_K_line[i]['bob']} 【平量平收】") |
| | | else: |
| | | logger.info(f"{symbol} 的 昨日成交量 为 0,报错!!") |
| | | |
| | | if current_open - previous_close > 0: |
| | | # print(f"i=={i} {it_K_line[i]['bob']} 成交总量:{today_volume},,,成交总金额:{today_amount}") |
| | |
| | | |
| | | # 实例化K线对象 |
| | | k_line_history = KLineHistory() |
| | | # 实例化指数K线对象 |
| | | main_index_k_line_history = KLineHistory() |
| | | |
| | | |
| | | # 在main.py中初始化函数里面就实例化上证A股和深证A股的历史K线方法【在本文件中调用且不写入本地文件时才需要在本文件内实例化】 |
| | | # k_line_history.k_line_history_90day() |
| | | |
| | | # 写入全股票90天K线 |
| | | # 写入全目标标的股票90天K线 |
| | | def all_stocks_all_k_line_dict_write(): |
| | | all_stocks_base_K_line_dict = k_line_history.k_line_history_90day() |
| | | # 初始化所有个股的指标K线列表 |
| | |
| | | try: |
| | | json_data = json.dumps(convert_datetime(all_stocks_all_K_line_property_dict), ensure_ascii=False, indent=4) |
| | | # 将转换后的JSON字符串写入文件 |
| | | with open('local_storage_data/all_stocks_all_K_line_property_dict.json', 'w', encoding='utf-8') as f: |
| | | with open(constant.K_BARS_PATH, 'w', encoding='utf-8') as f: |
| | | f.write(json_data) |
| | | async_log_util.info(logger_k_bars, json_data) |
| | | except Exception as error: |
| | | print(f"An error occurred while converting the data to JSON: {error}") |
| | | print(f"历史k线写完了!{tool.get_now_time_str()}") |
| | | logger.info(f"标的个股历史k线写完了!{tool.get_now_time_str()}") |
| | | |
| | | |
| | | # 用开盘啦数据检测昨日的K线中涨停属性是否有误(盘尾 集合竞价 炸开一个卖一档,但涨幅未变的) |
| | |
| | | for key, values in data_cache.all_stocks_all_K_line_property_dict.items(): |
| | | # print(f"data_cache.limit_up_code_list==={data_cache.limit_up_code_list}") |
| | | if 'attribute' in values[0]: |
| | | if key not in data_cache.yesterday_limit_up_code_list and values[0][ |
| | | 'attribute'] in data_cache.limit_up_type: |
| | | if key not in data_cache.yesterday_limit_up_code_list and values[0]['attribute'] in data_cache.limit_up_type: |
| | | logger.info(f"在K线中昨日涨停,但是在开盘啦中没有,key==={key} 公司名称==={values[0]['sec_name']} ") |
| | | data_cache.yesterday_frying_plate_last_minute_list.append(key) |
| | | else: |
| | |
| | | while True: |
| | | try: |
| | | # 声明赋值实时时间 |
| | | now_time = datetime.datetime.now().strftime("%H:%M:%S") |
| | | now_time = tool.get_now_time_str() |
| | | |
| | | # print(f"now_time==={now_time}") |
| | | # if now_time > data_cache.after_closing_time and data_cache.execution is False: |
| | | # # if now_time > data_cache.after_closing_time: |
| | | # if now_time > data_cache.AFTER_CLOSING_TIME and data_cache.execution is False: |
| | | # # if now_time > data_cache.AFTER_CLOSING_TIME: |
| | | # data_cache.execution = True |
| | | # # 整理当日涨停信息并写入本地管理好本地数据 |
| | | # kpl_api.get_arrange_limit_up_info() |
| | | # plate_strength_analysis.get_arrange_limit_up_info() |
| | | # logger.info(f"整理当日涨停信息 已经运行完成") |
| | | # # # 获取所有个股的板块概念并写入文件【耗时较长应该放在 核心主线程 和 仓位管理 后面】 |
| | | # kpl_api.get_all_stocks_plate_dict(data_cache.min_stocks) |
| | | # plate_strength_analysis.get_all_stocks_plate_dict(data_cache.min_stocks) |
| | | # # logger.info(f"获取所有个股的板块概念 已经运行完成") |
| | | |
| | | # 构造一个循环检测K线子带你中所有个股代码下的日期是不是和某日一致的,如果不一致则返回False |
| | |
| | | # data_date_and_target_date = False |
| | | # return data_date_and_target_date |
| | | |
| | | # 盘中不进行时间与数据的判断,一方面判断应该在开盘前完成,另一方面收盘后离最新拉取时间都还有很长时间 |
| | | # if (hour < 9 or (hour == 9 and minute < 30)) or hour >= 17: |
| | | if data_cache.opening_time > now_time or now_time > data_cache.closing_time: |
| | | # 盘中不进行时间与数据的判断(也不能早于服务器重启时间,因为次日凌晨拉取的K线会有错误),一方面判断应该在开盘前完成,另一方面收盘后离最新拉取时间都还有很长时间 |
| | | # 在这个时间段内运行 9:00--9:30 或 15:00--23:00 |
| | | if data_cache.SERVER_RESTART_TIME < now_time < data_cache.OPENING_TIME or data_cache.CLOSING_TIME < now_time < data_cache.PROGRAM_SLEEP_TIME: |
| | | # 然后判断一下,当K线字典中的任意一只个股的的第一个K线表中的第一个日期 等于 上一个交易日日期 或 今日日期 且 运行时时间未到 18:30 那么不需要新拉取直接读取已有的就行, |
| | | # 否者还是得调用K线对象方法拉取,并重新读取赋值全局化 |
| | | # if hour < 18 or (hour == 18 and minute < 31): |
| | | if now_time < data_cache.update_data_time: |
| | | if now_time < data_cache.UPDATE_DATA_TIME: |
| | | # if now_time < data_cache.closing_time: |
| | | check_pre_trading_day = check_data_date(data_cache.DataCache().pre_trading_day) |
| | | if check_pre_trading_day is True: |
| | | # if hour >= 17: |
| | | if now_time > data_cache.checking_data_time: |
| | | if now_time > data_cache.CHECKING_DATA_TIME: |
| | | print( |
| | | f"未到【18:31】 但 既有数据 【是】 上个交易日数据 【不执行拉取K线】 ε=ε=ε=ε=ε=ε=ε=ε=ε=ε=ε=ε=ε(/’-‘)/") |
| | | else: |
| | |
| | | # 调用指标K线写入本地文件 |
| | | all_stocks_all_k_line_dict_write() |
| | | # 读取已经获取到并存储在本地的所有票的指标K线字典,并赋值给data_cache全局缓存 |
| | | with open('local_storage_data/all_stocks_all_K_line_property_dict.json', '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)}") |
| | | 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)}") |
| | | |
| | | else: |
| | | check_today_date = check_data_date(data_cache.DataCache.today_date) |
| | | check_today_date = check_data_date(data_cache.DataCache().today_date) |
| | | if check_today_date is True: |
| | | pass |
| | | print( |
| | | f"到了【18:31】 但 既有数据 【是】 本交易日数据 【不执行拉取K线】 ε=ε=ε=ε=ε=ε=ε=ε=ε=ε=ε=ε=ε(/’-‘)/") |
| | | print(f"到了【18:31】 但 既有数据 【是】 本交易日数据 【不执行拉取K线】 ε=ε=ε=ε=ε=ε=ε=ε=ε=ε=ε=ε=ε(/’-‘)/") |
| | | else: |
| | | print( |
| | | f"到了【18:31】 且 既有数据 【不是】 本交易日数据 执行拉取K线 【既有数据日期:{data_cache.all_stocks_all_K_line_property_dict['SZSE.000034'][0]['bob'][:10]} 本交易日:{data_cache.DataCache.today_date}】") |
| | | f"到了【18:31】 且 既有数据 【不是】 本交易日数据 执行拉取K线 【既有数据日期:{data_cache.all_stocks_all_K_line_property_dict['SZSE.000034'][0]['bob'][:10]} 本交易日:{data_cache.DataCache().today_date}】") |
| | | # 调用指标K线写入本地文件 |
| | | all_stocks_all_k_line_dict_write() |
| | | # 读取已经获取到并存储在本地的所有票的指标K线字典,并赋值给data_cache全局缓存 |
| | | with open('local_storage_data/all_stocks_all_K_line_property_dict.json', '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)}") |
| | | 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)}") |
| | | except Exception as error: |
| | | logging.exception(error) |
| | | logger.exception(error) |
| | | print(f"实时检测是否拉取K线线程报错An error occurred: {error}") |
| | | finally: |
| | | current_time = datetime.datetime.now() |
| | |
| | | # 计算90天内涨停天数 |
| | | for i in k_line_data: |
| | | if 'attribute' in i and ( |
| | | i['attribute'] == 'one_line_limit_up' or i['attribute'] == 'limit_down_then_limit_up' or i[ |
| | | 'attribute'] == 'limit_up_then_limit_down_then_limit_up' or i['attribute'] == 'limit_up'): |
| | | i['attribute'] == 'one_line_limit_up' or i['attribute'] == 'limit_down_then_limit_up' or i['attribute'] == 'limit_up_then_limit_down_then_limit_up' or i['attribute'] == 'limit_up'): |
| | | limit_up_day += 1 |
| | | # print(f"涨停提日期:{i['bob']}") |
| | | # print(f"涨停天数----limit_up_day======{limit_up_day}") |