| | |
| | | from http.server import BaseHTTPRequestHandler |
| | | import urllib.parse as urlparse |
| | | |
| | | from log_module.log import hx_logger_l2_transaction |
| | | from log_module.log import hx_logger_l2_transaction, logger_debug |
| | | from strategy import data_cache |
| | | from trade import huaxin_trade_api, huaxin_trade_data_update |
| | | from trade.huaxin_trade_record_manager import DelegateRecordManager, DealRecordManager, MoneyManager, PositionManager |
| | |
| | | fdatas = [] |
| | | for code in codes: |
| | | data = data_cache.latest_code_market_info_dict.get(code) |
| | | logger_debug.info(f"获取L1行情接口:{code}-{data}") |
| | | if data: |
| | | fdatas.append(data) |
| | | response_data = json.dumps({"code": 0, "data": fdatas}) |
| | |
| | | price = tool.get_buy_max_price(current_price) |
| | | price = min(price, tool.get_limit_up_price(code,pre_price)) |
| | | else: |
| | | price = round(params.get("price"), 2) # 价格 |
| | | price = round(float(params.get("price")), 2) # 价格 |
| | | result = huaxin_trade_api.order(1, code, volume, price, blocking=True) |
| | | result_str = json.dumps(result) |
| | | elif url.path == "/sell": |
| | |
| | | 订阅股票指数行情 |
| | | """ |
| | | # 沪深300 |
| | | self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_SSE, "000300") # 沪深300 |
| | | self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_COMM, "000300") # 沪深300 |
| | | self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_SZSE, "000300") |
| | | self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_SSE, "000001") # 上证 |
| | | self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_SZSE, "399006") # 创业板指数 |
| | | self.m_api.SubscribeStockIndexData(qcvalueaddproapi.QCVD_EXD_SZSE, "399001") # 深圳成指 |
| | |
| | | # 指数数据 |
| | | try: |
| | | data = { |
| | | "PreClosePrice":pStockIndexData.PreClosePrice, |
| | | "LastPrice": pStockIndexData.LastPrice, |
| | | "SecurityID": pStockIndexData.SecurityID, |
| | | "UpdateTime": pStockIndexData.UpdateTime, |
| | |
| | | data_cache.DataCache() |
| | | # 初始化A股所有目标票标的信息 |
| | | data_cache.all_stocks = utils.juejin_api.JueJinApi.get_target_codes() |
| | | # 获取目标票标的K线 |
| | | # 获取目标标的K线---初始化 |
| | | all_K_line.k_line_history.init(data_cache.DataCache().today_date, data_cache.DataCache().next_trading_day, |
| | | data_cache.DataCache().filtered_stocks) |
| | | |
| | | # 直接调用指标K线写入本地文件 |
| | | # 直接调用目标标的指标K线写入本地文件 |
| | | # all_K_line.all_stocks_all_k_line_dict_write() |
| | | |
| | | # 先使用json.load()直接从文件中读取【已经存储在本地的K线指标属性字典】并解析JSON数据 |
| | |
| | | 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() |
| | | |
| | | |
| | | # 第一步:初始化context函数,并开启获取实时数据的线程 |
| | | def init(): |
| | |
| | | |
| | | # 实例化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线列表 |
| | |
| | | f.write(json_data) |
| | | except Exception as error: |
| | | print(f"An error occurred while converting the data to JSON: {error}") |
| | | logger.info(f"历史k线写完了!{tool.get_now_time_str()}") |
| | | logger.info(f"标的个股历史k线写完了!{tool.get_now_time_str()}") |
| | | |
| | | |
| | | # 写入主要指数的90天K线 |
| | | def main_index_k_line_dict_write(): |
| | | main_index_base_K_line_dict = main_index_k_line_history.k_line_history_90day() |
| | | # 初始化所有个股的指标K线列表 |
| | | main_index_k_line_property_dict = {} |
| | | for i in data_cache.DataCache().main_index_stocks: |
| | | # print(f"i==========={i}") |
| | | i_k_line = main_index_base_K_line_dict[i] # 获取i的K线 |
| | | i_k_line_copy = copy.deepcopy(i_k_line) # 深拷贝i的K线 |
| | | # it_K_line_reversed = list(reversed(i_k_line_copy)) # 开盘啦获取的数据需要反转i的K线 |
| | | it_K_line_reversed = list(i_k_line_copy) # 小辉端的数据不需要反转i的K线 |
| | | if not it_K_line_reversed: |
| | | continue |
| | | k_line_history.get_property_limit_mark(it_K_line_reversed, i) # 给标的的K线更新指标属性 把股票代码同时传给要调用的函数 |
| | | index_k_line_property_dict = {i: it_K_line_reversed} # 添加 更新极限指标属性的K线 字典 |
| | | # print(f"index_k_line_property_dict===={index_k_line_property_dict}") |
| | | main_index_base_K_line_dict.update(index_k_line_property_dict) |
| | | |
| | | # 构造时间格式datetime转化为字符串,以便将K线属性指标转化为json格式写入本地文件 |
| | | def convert_datetime(obj): |
| | | if isinstance(obj, datetime.datetime): |
| | | return obj.strftime('%Y-%m-%d %H:%M:%S') # 转换为字符串 |
| | | elif isinstance(obj, dict): |
| | | return {k: convert_datetime(v) for k, v in obj.items()} # 递归处理字典 |
| | | elif isinstance(obj, list): |
| | | return [convert_datetime(element) for element in obj] # 递归处理列表 |
| | | # 可以添加其他类型的处理逻辑 |
| | | else: |
| | | # 对于未知类型,你可以选择保留原样、跳过或引发异常 |
| | | # 这里我们选择保留原样 |
| | | return obj |
| | | |
| | | try: |
| | | json_data = json.dumps(convert_datetime(main_index_k_line_property_dict), ensure_ascii=False, indent=4) |
| | | # 将转换后的JSON字符串写入文件 |
| | | with open(constant.K_BARS_PATH, 'w', encoding='utf-8') as f: |
| | | f.write(json_data) |
| | | except Exception as error: |
| | | print(f"An error occurred while converting the data to JSON: {error}") |
| | | logger.info(f"主要指数的历史k线写完了!{tool.get_now_time_str()}") |
| | | |
| | | |
| | | |
| | | # 用开盘啦数据检测昨日的K线中涨停属性是否有误(盘尾 集合竞价 炸开一个卖一档,但涨幅未变的) |
| | |
| | | # print(f"当前-时间:{formatted_time}") |
| | | time.sleep(1) |
| | | |
| | | |
| | | # 历史K线累计涨停天数函数 |
| | | def count_limit_up_day(k_line_data): |
| | | limit_up_day = 0 # 初始化涨停天数 |
| | |
| | | 'SZSE.00'))] |
| | | # self.filtered_stocks = self.filtered_stocks[:10] |
| | | print(f"过滤后上证A股和深证A股数量filtered_stocks:{len(self.filtered_stocks)}") |
| | | # 声明一下需要拉取K线的列表 |
| | | self.main_index_stocks = ['SHSE.000001', 'SZSE.399001', 'SZSE.399006', 'SHSE.000300'] |
| | | # 获取上证A股和深证A股 基本信息 |
| | | instruments = [stock for stock in self.all_stocks if stock['symbol'] in self.filtered_stocks] |
| | | # 将获取到的上证A股和深证A股 基本信息 汇编为一个字典 |
| | |
| | | # {"code":(代码,昨日收盘价,最新价,总成交量,总成交额,买五档(价格,成交额),卖五档(价格,成交额),更新时间)} |
| | | latest_code_market_info_dict = {} |
| | | |
| | | # 股票指数字典 例如:{"000001":(指数, 量, 额)} |
| | | # 股票指数字典 例如:{"000001":(指数, 量, 额, 昨日收盘价)} |
| | | stock_index_dict = {} |
| | | |
| | | logging.info(f"全局初始化数据 完成《《《 - {os.getpid()}") |
| | |
| | | def instant_trend_strategy(current_info): |
| | | len_current_info = len(current_info) |
| | | if current_info is not None and len_current_info > 0: |
| | | print(f"current_info------------{current_info}") |
| | | # print(f"current_info------------{current_info}") |
| | | # 上证指数数据 |
| | | Shanghai_index_data = current_info.get('000001') |
| | | Shanghai_index = Shanghai_index_data[0] # 上证指数 |
| | | Shanghai_index_volume = Shanghai_index_data[1] # 上证指数 当日当时成交量 |
| | | Shanghai_index_turnover = Shanghai_index_data[2] # 上证指数 当日当时成交额度 |
| | | print( |
| | | f"上证 指数------------{Shanghai_index}" |
| | | f"上指 成交量------------{Shanghai_index_volume}" |
| | | f"上指 成交额------------{Shanghai_index_turnover}" |
| | | ) |
| | | Shanghai_index_volume = round(Shanghai_index_data[1]/100000000, 2) # 上证指数 当日当时成交量 |
| | | Shanghai_index_turnover = round(Shanghai_index_data[2]/100000000, 2) # 上证指数 当日当时成交额度 |
| | | # print( |
| | | # f"上证 指数------------{Shanghai_index} 成交量------------{Shanghai_index_volume}亿 手 成交额------------{Shanghai_index_turnover}亿 元" |
| | | # ) |
| | | |
| | | # 深证指数数据 |
| | | Shenzhen_index_data = current_info.get('399001') |
| | | Shenzhen_index = Shenzhen_index_data[0] # 深证指数 |
| | | Shenzhen_index_volume = Shenzhen_index_data[1] # 深证指数 当日当时成交量 |
| | | Shenzhen_index_turnover = Shenzhen_index_data[2] # 深证指数 当日当时成交额度 |
| | | print( |
| | | f"深证 指数------------{Shenzhen_index}" |
| | | f"深证 成交量------------{Shenzhen_index_volume}" |
| | | f"深证 成交额------------{Shenzhen_index_turnover}" |
| | | ) |
| | | Shenzhen_index_volume = round(Shenzhen_index_data[1]/100000000, 2) # 深证指数 当日当时成交量 |
| | | Shenzhen_index_turnover = round(Shenzhen_index_data[2]/100000000, 2) # 深证指数 当日当时成交额度 |
| | | # print( |
| | | # f"深证 指数------------{Shenzhen_index} 成交量------------{Shenzhen_index_volume}亿 手 成交额------------{Shenzhen_index_turnover}亿 元" |
| | | # ) |
| | | |
| | | # 创业板指数数据 |
| | | TSXV_index_data = current_info.get('399006') |
| | | TSXV_index = TSXV_index_data[0] # 创业板指 |
| | | TSXV_index_volume = TSXV_index_data[1] # 创业板指 当日当时成交量 |
| | | TSXV_index_turnover = TSXV_index_data[2] # 创业板指 当日当时成交额度 |
| | | print( |
| | | f"创业板 指数------------{TSXV_index}" |
| | | f"创业板指 成交量------------{TSXV_index_volume}" |
| | | f"创业板指 成交额------------{TSXV_index_turnover}" |
| | | ) |
| | | # 调用涨幅公式计算对应的股票tick瞬时涨幅 |
| | | Shanghai_tick_growth = basic_methods.calculate_growth('000001', Shanghai_index) |
| | | print(f"Shanghai_tick_growth ==== {Shanghai_tick_growth}") |
| | | # 调用涨幅公式计算对应的股票tick瞬时涨幅 |
| | | Shenzhen_tick_growth = basic_methods.calculate_growth('399001', Shanghai_index) |
| | | print(f"Shenzhen_tick_growth ==== {Shenzhen_tick_growth}") |
| | | # 调用涨幅公式计算对应的股票tick瞬时涨幅 |
| | | TSXV_tick_growth = basic_methods.calculate_growth('399006', Shanghai_index) |
| | | print(f"TSXV_tick_growth ==== {TSXV_tick_growth}") |
| | | TSXV_index_volume = round(TSXV_index_data[1]/100000000, 2) # 创业板指 当日当时成交量 |
| | | TSXV_index_turnover = round(TSXV_index_data[2]/100000000, 2) # 创业板指 当日当时成交额度 |
| | | # print( |
| | | # f"创业板 指数------------{TSXV_index} 成交量------------{TSXV_index_volume}亿 手 成交额------------{TSXV_index_turnover}亿 元" |
| | | # ) |
| | | # # 调用涨幅公式计算对应的股票tick瞬时涨幅 |
| | | # Shanghai_tick_growth = basic_methods.calculate_growth('000001', Shanghai_index) |
| | | # print(f"Shanghai_tick_growth ==== {round(Shanghai_tick_growth, 4)}") |
| | | # # 调用涨幅公式计算对应的股票tick瞬时涨幅 |
| | | # Shenzhen_tick_growth = basic_methods.calculate_growth('399001', Shanghai_index) |
| | | # print(f"Shenzhen_tick_growth ==== {round(Shenzhen_tick_growth, 4)}") |
| | | # # 调用涨幅公式计算对应的股票tick瞬时涨幅 |
| | | # TSXV_tick_growth = basic_methods.calculate_growth('399006', Shanghai_index) |
| | | # print(f"TSXV_tick_growth ==== {round(TSXV_tick_growth, 4)}") |
| | |
| | | 创建一个函数来对主要指数的实时行情作处理 |
| | | ''' |
| | | |
| | | |
| | | # 获取实时指数行情函数 |
| | | def index_market_current(): |
| | | logging.info(f"index_market_trend进入") |
| | |
| | | """ |
| | | 值的格式为: |
| | | { |
| | | "PreClosePrice":pStockIndexData, |
| | | "LastPrice": pStockIndexData.LastPrice, |
| | | "SecurityID": pStockIndexData.SecurityID, |
| | | "UpdateTime": pStockIndexData.UpdateTime, |
| | |
| | | } |
| | | """ |
| | | d = data[k] |
| | | data_cache.stock_index_dict[k] = (round(d["LastPrice"], 2), d["Volume"], d["Turnover"]) |
| | | data_cache.stock_index_dict[k] = (round(d["LastPrice"], 2), d["Volume"], d["Turnover"], d["PreClosePrice"]) |
| | | except: |
| | | pass |
| | | |