| | |
| | | price = CodePrePriceManager.get_price_pre_cache(code) |
| | | if price is None: |
| | | return None |
| | | limit_up_price = tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal("1.1")) |
| | | limit_up_price = tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal(f"{tool.get_limit_up_rate(code)}")) |
| | | __limit_up_price_dict[code] = limit_up_price |
| | | return limit_up_price |
| | | |
| | | |
| | | def get_price_pre_cache(code): |
| | | price = CodePrePriceManager.get_price_pre_cache(code) |
| | |
| | | return None |
| | | |
| | | |
| | | def get_limit_up_price_by_preprice(price): |
| | | if price is None: |
| | | return None |
| | | return tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal("1.1")) |
| | | |
| | | |
| | | def get_limit_down_price_by_preprice(price): |
| | | if price is None: |
| | | return None |
| | | return tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal("0.9")) |
| | | |
| | | |
| | | # 获取跌停价 |
| | | def get_limit_down_price(code): |
| | | price = CodePrePriceManager.get_price_pre_cache(code) |
| | | if price is None: |
| | | return None |
| | | return tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal("0.9")) |
| | | return tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal(f"{tool.get_limit_down_rate(code)}")) |
| | |
| | | filter=lambda record: record["extra"].get("name") == "hx_l2_transaction_sell_order", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | |
| | | logger.add(self.get_hx_path("l2", "transaction_big_buy"), |
| | | filter=lambda record: record["extra"].get("name") == "hx_l2_transaction_big_buy", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | | |
| | | logger.add(self.get_hx_path("l2", "transaction_desc"), |
| | | filter=lambda record: record["extra"].get("name") == "hx_l2_transaction_desc", |
| | | rotation="00:00", compression="zip", enqueue=True) |
| | |
| | | hx_logger_trade_callback = __mylogger.get_logger("hx_trade_callback") |
| | | hx_logger_trade_debug = __mylogger.get_logger("hx_trade_debug") |
| | | hx_logger_trade_loop = __mylogger.get_logger("hx_trade_loop") |
| | | hx_logger_l2_transaction_big_buy_order = __mylogger.get_logger("hx_l2_transaction_big_buy") |
| | | |
| | | # -------------------------------华鑫本地日志--------------------------------- |
| | | logger_local_huaxin_l2_transaction = __mylogger.get_logger("local_huaxin_transaction") |
| | |
| | | from huaxin_client.client_network import SendResponseSkManager |
| | | from log_module import async_log_util, log_export |
| | | from records import huaxin_trade_record_manager |
| | | from third_data.kpl_data_manager import PullTask |
| | | from third_data import kpl_data_manager, kpl_util |
| | | from third_data.kpl_data_manager import PullTask, KPLCodeJXBlockManager, KPLLimitUpDataRecordManager |
| | | from trade import huaxin_trade_api, huaxin_trade_data_update, huaxin_sell_util, backtest_trade |
| | | from trade.buy_strategy import BuyStrategyDataManager |
| | | from utils import middle_api_protocol, outside_api_command_manager, constant, tool, huaxin_util, socket_util, sell_util, \ |
| | | output_util, l2_huaxin_util |
| | | output_util, l2_huaxin_util, output_data_util |
| | | |
| | | middle_api_protocol.SERVER_PORT = 10008 |
| | | middle_api_protocol.SERVER_HOST = "43.138.167.68" |
| | |
| | | "buy1Money": output_util.money_desc( |
| | | underlying_market.buy1_price * underlying_market.buy1_volume), |
| | | "preClosePrice": underlying_market.pre_close_price} |
| | | |
| | | r["underlyingDetailInfo"] = output_data_util.load_code_detail_info(underlying_market.code) |
| | | r["underlyingDetailInfo"]["price"] = underlying_market.price |
| | | |
| | | # 获取买点与卖点 |
| | | if "buy_list" not in r: |
| | | buys = huaxin_trade_record_manager.DealRecordManager().list_buy_by_code_cache(cb_code) |
| | |
| | | try: |
| | | result = queue_market.get() |
| | | if result: |
| | | # 代码, 最近的价格, 涨幅, 买1价, 买1量, 成交总量 |
| | | # (代码, 最近的价格, 涨幅, 买1价, 买1量, 成交总量, 买入量, 卖出量, 昨日收盘价, 时间戳) |
| | | code_market_manager.set_market_info(result) |
| | | code = result[0] |
| | | # 正股需要加载板块 |
| | | if code.find("11")!=0 and code.find("12")!=0: |
| | | limit_up_price = tool.get_limit_up_price(code, result[8]) |
| | | |
| | | KPLCodeJXBlockManager().load_jx_blocks(result[0], result[3], |
| | | float(limit_up_price), |
| | | KPLLimitUpDataRecordManager.get_current_reasons()) |
| | | except Exception as e: |
| | | logger_debug.exception(e) |
| | | time.sleep(1) |
| | |
| | | logger_debug.exception(e) |
| | | |
| | | |
| | | def __kpl_limit_up_callback(results): |
| | | if results: |
| | | result_list = kpl_util.parseLimitUpData(results) |
| | | kpl_data_manager.KPLLimitUpDataRecordManager.save_record(tool.get_now_date_str(), result_list) |
| | | |
| | | |
| | | if __name__ == '__main__': |
| | | # ===========初始化数据========== |
| | | try: |
| | |
| | | huaxin_trade_data_update.run() |
| | | |
| | | # 定时拉取开盘啦涨停数据 |
| | | PullTask.run_limit_up_task() |
| | | PullTask.run_limit_up_task(__kpl_limit_up_callback) |
| | | |
| | | # 仿真交易不运行交易客户端 |
| | | # # ===========运行交易外部API========== |
| | |
| | | import json |
| | | |
| | | from code_attribute import history_k_data_util |
| | | from third_data import kpl_api, kpl_data_manager, kpl_block_util, kpl_util |
| | | from third_data.kpl_data_manager import KPLCodeJXBlockManager |
| | | from trade import huaxin_trade_api |
| | | from utils import output_data_util, tool |
| | | |
| | | if __name__ == "__main__": |
| | | # print(huaxin_trade_api.get_money()) |
| | | code = "113549" |
| | | price_results = history_k_data_util.HistoryKDatasUtils.get_gp_current_info([code]) |
| | | current_price = price_results[0]["price"] |
| | | results = kpl_api.getLimitUpInfoNew() |
| | | datas = kpl_util.parseLimitUpData(results) |
| | | kpl_data_manager.KPLLimitUpDataRecordManager.save_record(tool.get_now_date(), datas) |
| | | print(output_data_util.load_code_detail_info("300398")) |
| | |
| | | import datetime |
| | | import time |
| | | |
| | | import constant |
| | | from utils import tool |
| | | from utils import tool, constant |
| | | |
| | | |
| | | # 是否主板开1 |
| | |
| | | pre_close_price = round(float(limit_up_price) / tool.get_limit_up_rate(code), 2) |
| | | # 如果涨幅大于7%就读取板块 |
| | | price_rate = (buy_1_price - pre_close_price) / pre_close_price |
| | | if price_rate > 0.07: |
| | | if price_rate > 0.07 * (tool.get_limit_up_rate(code)-1) * 10: |
| | | jx_blocks_info = self.get_jx_blocks_cache(code) |
| | | if not jx_blocks_info: |
| | | start_time = time.time() |
| | |
| | | async_log_util.info(logger_kpl_block_can_buy, |
| | | f"{code}:获取到精选板块(更新)-{blocks} 耗时:{int(time.time() - start_time)}s") |
| | | self.save_jx_blocks(code, blocks, current_limit_up_blocks) |
| | | elif price_rate > 0.03: |
| | | elif price_rate > 0.03 * (tool.get_limit_up_rate(code)-1) * 10: |
| | | # 添加备用板块 |
| | | if not self.get_jx_blocks_cache(code, by=True): |
| | | start_time = time.time() |
| | |
| | | threading.Thread(target=cls.run_limit_up_task, daemon=True).start() |
| | | |
| | | @classmethod |
| | | def run_limit_up_task(cls): |
| | | def run_limit_up_task(cls, limit_up_result_callback): |
| | | # 关闭log |
| | | log.close_print() |
| | | while True: |
| | | try: |
| | | if (tool.is_trade_time() and int(tool.get_now_time_str().replace(':', '')) > int("092530")): |
| | | if (tool.is_trade_time() and int(tool.get_now_time_str().replace(':', '')) > int("092530")) or True: |
| | | results = kpl_api.getLimitUpInfoNew() |
| | | result = json.loads(results) |
| | | start_time = time.time() |
| | | cls.__upload_data("limit_up", result) |
| | | limit_up_result_callback(result) |
| | | except Exception as e: |
| | | try: |
| | | logging.exception(e) |
| | |
| | | """ |
| | | import decimal |
| | | |
| | | from trade.l2_transaction_data_manager import HuaXinBuyOrderManager |
| | | from utils import tool |
| | | |
| | | |
| | |
| | | self.__latest_market_info_dict = {} |
| | | |
| | | def set_pre_close_price(self, code, price): |
| | | if code.find("00") == 0 or code.find("60") == 0: |
| | | limit_up_price = tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal("1.1")) |
| | | else: |
| | | limit_up_price = tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal("1.2")) |
| | | limit_up_price = tool.to_price(decimal.Decimal(str(price)) * decimal.Decimal(f"{tool.get_limit_up_rate(code)}")) |
| | | self.__limit_up_price_dict[code] = round(float(limit_up_price), 2) |
| | | |
| | | def add_market_info(self, info): |
| | |
| | | self.set_pre_close_price(code, info[8]) |
| | | self.__latest_market_info_dict[info[0]] = info |
| | | |
| | | def add_transaction_info(self, info): |
| | | def add_transaction_info(self, info, backtest = False): |
| | | |
| | | """ |
| | | 添加成交信息 |
| | | :param backtest: 是否是回测 |
| | | :param info: 示例-{'SecurityID': '002842', 'TradePrice': 6.66, 'TradeVolume': 1100, 'OrderTime': 95436390, |
| | | 'MainSeq': 2011, 'SubSeq': 8162612, 'BuyNo': 8162610, 'SellNo': 8159424, 'ExecType': '1'} |
| | | :return: 返回是否下单 |
| | | """ |
| | | if not backtest: |
| | | HuaXinBuyOrderManager.statistic_big_buy_data(info['SecurityID'], |
| | | [(info['SecurityID'], info['TradePrice'], info['TradeVolume'], |
| | | info['OrderTime'], info['MainSeq'], info['SubSeq'], |
| | | info['BuyNo'], |
| | | info['SellNo'], info['ExecType'])]) |
| | | code = info["SecurityID"] |
| | | try: |
| | | if info["TradePrice"] == self.__limit_up_price_dict.get(code): |
New file |
| | |
| | | # 成交数据统计 |
| | | from log_module import async_log_util |
| | | from log_module.log import hx_logger_l2_transaction_big_buy_order |
| | | from utils import l2_huaxin_util |
| | | |
| | | |
| | | class HuaXinBuyOrderManager: |
| | | __instance = None |
| | | |
| | | # 正在成交的订单 |
| | | __dealing_order_info_dict = {} |
| | | # 最近成交的订单{"code":(订单号,是否成交完成)} |
| | | __latest_deal_order_info_dict = {} |
| | | |
| | | __total_big_buy_orders_dict={} |
| | | |
| | | def __new__(cls, *args, **kwargs): |
| | | if not cls.__instance: |
| | | cls.__instance = super(HuaXinBuyOrderManager, cls).__new__(cls, *args, **kwargs) |
| | | cls.__load_datas() |
| | | return cls.__instance |
| | | |
| | | @classmethod |
| | | def __load_datas(cls): |
| | | pass |
| | | |
| | | @classmethod |
| | | def get_big_buy_orders(cls, code): |
| | | """ |
| | | 获取正在成交的订单信息 |
| | | :param code: |
| | | :return:[(订单号,总股数,成交金额,成交开始时间,成交结束时间)] |
| | | """ |
| | | return cls.__total_big_buy_orders_dict.get(code) |
| | | |
| | | @classmethod |
| | | def get_dealing_order_info(cls, code): |
| | | """ |
| | | 获取当前正在成交的数据 |
| | | @param code: |
| | | @return: [订单号,总股数,成交金额,成交开始时间,成交结束时间] |
| | | """ |
| | | return cls.__dealing_order_info_dict.get(code) |
| | | |
| | | @classmethod |
| | | def statistic_big_buy_data(cls, code, datas): |
| | | """ |
| | | 统计大单买 |
| | | @param code: |
| | | @param datas: |
| | | @return: 返回数据里面(成交的大单,50w以上的单) |
| | | """ |
| | | big_buy_datas = [] |
| | | normal_buy_datas = [] |
| | | # 大单阈值 |
| | | threshold_big_money = l2_huaxin_util.get_big_money_val(datas[-1][1]) |
| | | for data in datas: |
| | | # q.append((data['SecurityID'], data['TradePrice'], data['TradeVolume'], |
| | | # data['OrderTime'], data['MainSeq'], data['SubSeq'], data['BuyNo'], |
| | | # data['SellNo'], data['ExecType'])) |
| | | |
| | | if code not in cls.__dealing_order_info_dict: |
| | | # 数据格式[订单号,总股数,成交金额,成交开始时间,成交结束时间] |
| | | cls.__dealing_order_info_dict[code] = [data[6], data[2], data[2] * data[1], data[3], data[3]] |
| | | if cls.__dealing_order_info_dict[code][0] == data[6]: |
| | | # 成交同一个订单号 |
| | | cls.__dealing_order_info_dict[code][1] += data[2] |
| | | cls.__dealing_order_info_dict[code][2] += data[2] * data[1] |
| | | cls.__dealing_order_info_dict[code][4] = data[3] |
| | | else: |
| | | # 设置最近成交完成的一条数据 |
| | | deal_info = cls.__dealing_order_info_dict[code] |
| | | cls.__latest_deal_order_info_dict[code] = deal_info |
| | | # 是否为大买单 |
| | | if deal_info[2] >= threshold_big_money: |
| | | big_buy_datas.append(deal_info) |
| | | async_log_util.info(hx_logger_l2_transaction_big_buy_order,f"{code}#{deal_info}") |
| | | if deal_info[2] >= 500000: |
| | | normal_buy_datas.append(deal_info) |
| | | # 初始化本条数据 |
| | | cls.__dealing_order_info_dict[code] = [data[6], data[2], data[2] * data[1], data[3], data[3]] |
| | | if big_buy_datas: |
| | | if code not in cls.__total_big_buy_orders_dict: |
| | | cls.__total_big_buy_orders_dict[code] = [] |
| | | cls.__total_big_buy_orders_dict[code].extend(big_buy_datas) |
| | | |
| | | return big_buy_datas, normal_buy_datas |
| | |
| | | return fdatas |
| | | |
| | | |
| | | # 获取大资金的金额 |
| | | def get_big_money_val(limit_up_price): |
| | | if limit_up_price > 3.0: |
| | | return min(299 * 10000, round(limit_up_price * 7999 * 100)) |
| | | else: |
| | | max_money = limit_up_price * 10000 * 100 |
| | | return int(max_money * 0.95) |
| | | |
| | | if __name__ == "__main__": |
| | | ds = ["('605167', 10.08, 68500, '1', '0', 9303108, 2, 439438, 436472, 'D', 1695864632451)", |
| | | "('603439', 17.97, 27800, '1', '0', 9304966, 6, 435127, 407524, 'D', 1695864649883)", |
New file |
| | |
| | | from third_data import kpl_data_manager, kpl_block_util |
| | | from trade.l2_transaction_data_manager import HuaXinBuyOrderManager |
| | | from utils import tool, output_util |
| | | |
| | | |
| | | def load_code_detail_info(code): |
| | | """ |
| | | 加载代码详情信息 |
| | | :param code: |
| | | :return: |
| | | """ |
| | | # 获取板块 |
| | | fresults = {} |
| | | blocks = kpl_data_manager.KPLCodeJXBlockManager().get_jx_blocks_cache(code) |
| | | if not blocks: |
| | | blocks = kpl_data_manager.KPLCodeJXBlockManager().get_jx_blocks_cache(code, True) |
| | | if blocks is None: |
| | | blocks = [] |
| | | current_blocks = kpl_data_manager.KPLLimitUpDataRecordManager.get_current_blocks(code) |
| | | if current_blocks: |
| | | blocks = current_blocks |
| | | |
| | | # 历史涨停数据 |
| | | limit_up_records_data = kpl_data_manager.KPLLimitUpDataRecordManager.total_datas |
| | | code_limit_up_reasons_dict = {} |
| | | |
| | | for r in limit_up_records_data: |
| | | blocks_ = {r[2]} |
| | | blocks_ |= set(r[6].split("、")) |
| | | code_limit_up_reasons_dict[r[3]] = blocks_ |
| | | if r[3] == code: |
| | | if not current_blocks: |
| | | blocks = blocks_ |
| | | fresults["limit_up_time"] = tool.to_time_str(int(r[5])) |
| | | fresults["highDesc"] = r[12] |
| | | fresults["zyltgb"] = output_util.money_desc(r[13]) |
| | | # 获取板块详细信息 |
| | | blockInfos = [] |
| | | latest_datas = kpl_data_manager.KPLLimitUpDataRecordManager.latest_origin_datas |
| | | for b in blocks: |
| | | current_codes = kpl_data_manager.KPLLimitUpDataRecordManager.get_current_codes_by_block(b) |
| | | current_codes_count = 0 if not current_codes else len(current_codes) |
| | | temp_records = [] |
| | | for r in limit_up_records_data: |
| | | temp_blocks = {r[2]} |
| | | temp_blocks |= set(r[6].split("、")) |
| | | if b in temp_blocks: |
| | | temp_records.append(r) |
| | | rank = kpl_block_util.get_code_current_rank(code, b, latest_datas, code_limit_up_reasons_dict, [], [], [], 0) |
| | | |
| | | blockInfos.append({"name": b, "totalLimitUpCount": len(temp_records), |
| | | "openLimitUpCount": len(temp_records) - current_codes_count, "rank": rank[0] + 1}) |
| | | |
| | | # 统计板块的 |
| | | fresults["blockInfos"] = blockInfos |
| | | fresults["blocks"] = list(blocks) |
| | | big_orders = HuaXinBuyOrderManager.get_big_buy_orders(code) |
| | | if not big_orders: |
| | | big_orders = [] |
| | | fresults["bigOrderDealCount"] = len(big_orders) |
| | | fresults["bigOrderDealMoney"] = sum([x[1] for x in big_orders]) |
| | | return fresults |
| | |
| | | return 0.8 |
| | | |
| | | |
| | | def is_can_buy_code(code): |
| | | return code.find("00") == 0 or code.find("60") == 0 or code.find("30") == 0 |
| | | |
| | | |
| | | def get_limit_up_price(code, pre_close_price): |
| | | limit_up_price = to_price(decimal.Decimal(str(pre_close_price)) * decimal.Decimal(f"{get_limit_up_rate(code)}")) |
| | | return limit_up_price |
| | | |
| | | # 将时间戳s格式化 |
| | | def to_time_str(timestamp_second, format_="%H:%M:%S"): |
| | | return datetime.datetime.fromtimestamp(timestamp_second).strftime(format_) |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | pass |