| | |
| | | name = gpcode_manager.get_code_name(code) |
| | | datas.append(f"{name}:{code}") |
| | | fresult = {"code": 0, "data": datas} |
| | | elif code_list_type == outside_api_command_manager.CODE_LIST_GREEN: |
| | | if operate == outside_api_command_manager.OPERRATE_SET: |
| | | gpcode_manager.GreenListCodeManager().add_code(code) |
| | | name = gpcode_manager.get_code_name(code) |
| | | if not name: |
| | | results = HistoryKDatasUtils.get_gp_codes_names([code]) |
| | | if results: |
| | | gpcode_manager.CodesNameManager.add_first_code_name(code, results[code]) |
| | | elif operate == outside_api_command_manager.OPERRATE_DELETE: |
| | | gpcode_manager.GreenListCodeManager().remove_code(code) |
| | | elif operate == outside_api_command_manager.OPERRATE_GET: |
| | | codes = gpcode_manager.GreenListCodeManager().list_code_cache() |
| | | datas = [] |
| | | for code in codes: |
| | | name = gpcode_manager.get_code_name(code) |
| | | datas.append(f"{name}:{code}") |
| | | fresult = {"code": 0, "data": datas} |
| | | |
| | | self.send_response(fresult, client_id, request_id) |
| | | except Exception as e: |
| | |
| | | RedisUtils.delete(self.__get_redis(), "forbidden-trade-codes") |
| | | |
| | | |
| | | class GreenListCodeManager: |
| | | """ |
| | | 绿名单:买入即加红,撤单不移红 |
| | | """ |
| | | __instance = None |
| | | __db = 2 |
| | | __redis_manager = redis_manager.RedisManager(2) |
| | | __codes_set = set() |
| | | |
| | | def __new__(cls, *args, **kwargs): |
| | | if not cls.__instance: |
| | | cls.__instance = super(GreenListCodeManager, cls).__new__(cls, *args, **kwargs) |
| | | # 初始化设置 |
| | | # 获取交易窗口的锁 |
| | | cls.__codes_set = RedisUtils.smembers(cls.__get_redis(), "green-trade-codes") |
| | | return cls.__instance |
| | | |
| | | @classmethod |
| | | def __get_redis(cls): |
| | | return cls.__redis_manager.getRedis() |
| | | |
| | | def add_code(self, code): |
| | | self.__codes_set.add(code) |
| | | RedisUtils.sadd_async(self.__db, "green-trade-codes", code) |
| | | RedisUtils.expire_async(self.__db, "green-trade-codes", tool.get_expire()) |
| | | |
| | | def sync(self): |
| | | data = RedisUtils.smembers(self.__get_redis(), |
| | | "green-trade-codes") |
| | | self.__codes_set.clear() |
| | | if data: |
| | | self.__codes_set |= data |
| | | |
| | | def remove_code(self, code): |
| | | self.__codes_set.discard(code) |
| | | RedisUtils.srem(self.__get_redis(), "green-trade-codes", code) |
| | | |
| | | def is_in(self, code): |
| | | return RedisUtils.sismember(self.__get_redis(), "green-trade-codes", code) |
| | | |
| | | def is_in_cache(self, code): |
| | | return code in self.__codes_set |
| | | |
| | | def list_codes(self): |
| | | codes = RedisUtils.smembers(self.__get_redis(), "green-trade-codes") |
| | | self.__codes_set = codes |
| | | return codes |
| | | |
| | | def list_codes_cache(self): |
| | | return self.__codes_set |
| | | |
| | | def clear(self): |
| | | self.__codes_set.clear() |
| | | RedisUtils.delete(self.__get_redis(), "green-trade-codes") |
| | | |
| | | |
| | | def __parse_codes_data(code_datas): |
| | | codes = [] |
| | | name_codes = {} |
| | |
| | | left_count, left_money = L2DataComputeUtil.compute_left_buy_order(code, trade_index, end_index, limit_up_price, |
| | | min_money=min_money) |
| | | if left_count < 1: |
| | | return True, f"范围:{real_order_index}-{end_index} 大单数量:{left_count}" |
| | | return True, f"范围:{trade_index}-{end_index} 大单数量:{left_count}" |
| | | return False, "大单数量够" |
| | | |
| | | |
| | |
| | | |
| | | from huaxin_client import constant as huaxin_client_constant |
| | | from l2 import l2_data_util, l2_data_source_util |
| | | from l2.huaxin import l2_huaxin_util |
| | | from l2.l2_data_util import L2DataUtil |
| | | from log_module import async_log_util |
| | | from log_module.log import hx_logger_trade_debug, logger_real_place_order_position, logger_debug |
| | |
| | | real_place_index = None |
| | | # 从中间向两头遍历 |
| | | # 从虚拟单位置向上找 |
| | | try: |
| | | exec_time_with_ms = L2DataUtil.get_time_with_ms(exec_data["val"]) |
| | | except: |
| | | pass |
| | | for i in range(shadow_place_order_index - 1, start_index - 1, -1): |
| | | d = total_datas[i] |
| | | if d["val"]["num"] != volume // 100: |
| | |
| | | # 必须是买入 |
| | | if not L2DataUtil.is_limit_up_price_buy(d["val"]): |
| | | continue |
| | | try: |
| | | if tool.is_sh_code(code) and tool.trade_time_sub_with_ms(L2DataUtil.get_time_with_ms(d["val"]), exec_time_with_ms) <= 30: |
| | | # 上证真实下单位置与执行位置要相差30ms以上才行 |
| | | continue |
| | | except: |
| | | pass |
| | | |
| | | real_place_index = i |
| | | break |
| | | if not real_place_index: |
| | |
| | | continue |
| | | real_place_index = i |
| | | break |
| | | |
| | | if not real_place_index: |
| | | real_place_index_info = shadow_place_order_index, RELIABILITY_TYPE_VIRTUAL |
| | | else: |
| | |
| | | # 获取大资金的金额 |
| | | def get_big_money_val(limit_up_price, is_ge=False): |
| | | if is_ge: |
| | | return min(299 * 10000, round(limit_up_price * 2999 * 100)) |
| | | return min(299 * 10000, round(limit_up_price * 2900 * 100)) |
| | | else: |
| | | if limit_up_price > 3.0: |
| | | return min(299 * 10000, round(limit_up_price * 7999 * 100)) |
| | |
| | | CODE_LIST_WANT = "want" |
| | | CODE_LIST_PAUSE_BUY = "pause_buy" |
| | | CODE_LIST_MUST_BUY = "must_buy" |
| | | CODE_LIST_GREEN = "green" |
| | | |
| | | # 类型 |
| | | API_TYPE_TRADE = "trade" # 交易 |
| | |
| | | from third_data import kpl_data_manager, kpl_util, block_info |
| | | from third_data import kpl_data_manager, kpl_util |
| | | from third_data.code_plate_key_manager import CodesHisReasonAndBlocksManager |
| | | from third_data.kpl_limit_up_data_manager import LatestLimitUpBlockManager, CodeLimitUpSequenceManager |
| | | from utils import tool |
| | | from utils.kpl_data_db_util import KPLLimitUpDataUtil |
| | | |
| | | |
| | | def block_run(): |
| | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | get_code_current_block() |
| | | code = "000546" |
| | | k3 = CodesHisReasonAndBlocksManager().get_history_blocks(code) |
| | | print(k3) |
| | | k3 = CodesHisReasonAndBlocksManager().get_history_blocks(code) |
| | | print(k3) |
| | | # print(KPLLimitUpDataUtil.get_latest_block_infos(code="000561")) |
| | | # print(code_plate_key_manager.ForbiddenBlockManager().get_blocks()) |
| | | # code_plate_key_manager.ForbiddenBlockManager().add("测试2") |
| | | # code_plate_key_manager.ForbiddenBlockManager().add("测试3") |
New file |
| | |
| | | from third_data import history_k_data_manager |
| | | from third_data.history_k_data_manager import HistoryKDataManager |
| | | from utils import tool, init_data_util |
| | | |
| | | if __name__ == '__main__': |
| | | code = "603960" |
| | | day = tool.get_now_date_str() |
| | | datas = init_data_util.get_volumns_by_code(code, 150) |
| | | HistoryKDataManager().save_history_bars(code, day, datas, force=True) |
| | | print(HistoryKDataManager().get_history_bars(code, day)) |
| | | print(HistoryKDataManager().get_pre_close(code, day)) |
| | | history_k_data_manager.re_set_price_pres([code]) |
New file |
| | |
| | | import json |
| | | import logging |
| | | |
| | | from log_module.log import logger_debug |
| | | from trade import trade_data_manager |
| | | from utils import tool |
| | | |
| | | if __name__ == "__main__": |
| | | try: |
| | | fdata = {"delegates": {}} |
| | | # 获取本月的手续费 |
| | | end_date = tool.get_now_date_str("%Y%m%d") |
| | | start_date = f"{end_date[:6]}01" |
| | | delegates_month = trade_data_manager.AccountMoneyManager().get_delegated_count_info(start_date, |
| | | end_date) |
| | | # 股票,上证可转债 , 深证可转债 |
| | | |
| | | deals_month = trade_data_manager.AccountMoneyManager().get_deal_count_info(start_date, end_date) |
| | | cost_month = sum([round(0.1 * x[1], 2) for x in delegates_month]) |
| | | make_month = 0 |
| | | make_month += 5 * deals_month[0][1] + 1 * deals_month[1][1] + 0 * deals_month[2][1] |
| | | fdata["month_commission"] = round(make_month - cost_month, 2) |
| | | # 计算当日手续费详情 |
| | | delegates = trade_data_manager.AccountMoneyManager().get_delegated_count_info() |
| | | delegates = [{"count": x[1], "price": 0.1, "money": round(0.1 * x[1], 2)} for x in delegates] |
| | | fdata["delegates"]["buy"] = delegates[0] |
| | | fdata["delegates"]["buy_cancel"] = delegates[1] |
| | | fdata["delegates"]["sell_cancel"] = delegates[2] |
| | | fdata["delegates"]["sell"] = delegates[3] |
| | | deals = trade_data_manager.AccountMoneyManager().get_deal_count_info() |
| | | fdata["deals"] = {} |
| | | fdata["deals"]["stock"] = {"count": deals[0][1], "price": 5, "money": round(5 * deals[0][1], 2)} |
| | | fdata["deals"]["sh_cb"] = {"count": deals[1][1], "price": 1, "money": round(1 * deals[1][1], 2)} |
| | | fdata["deals"]["sz_cb"] = {"count": deals[2][1], "price": 0, "money": round(0 * deals[2][1], 2)} |
| | | fdata["commission"] = trade_data_manager.AccountMoneyManager().get_commission_cache() |
| | | print(fdata) |
| | | response_data = json.dumps({"code": 0, "data": fdata}) |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | logger_debug.exception(e) |
New file |
| | |
| | | """ |
| | | 策略分析 |
| | | """ |
| | | import datetime |
| | | |
| | | from db.mysql_data_delegate import Mysqldb |
| | | from third_data import history_k_data_util |
| | | from utils import tool |
| | | |
| | | |
| | | def __load_limit_up_records(day): |
| | | mysql = Mysqldb() |
| | | datas = mysql.select_all(f"select * from kpl_limit_up_record where _day = '{day}'") |
| | | return datas |
| | | |
| | | |
| | | if __name__ == '__main__': |
| | | dates = history_k_data_util.JueJinApi.get_trading_dates("2024-05-01", "2024-08-19") |
| | | datas_dict = {} |
| | | for day in dates: |
| | | datas = __load_limit_up_records(day) |
| | | # 将数据根据涨停原因聚合 |
| | | temp_dict = {} |
| | | for d in datas: |
| | | if d[2] not in temp_dict: |
| | | temp_dict[d[2]] = [] |
| | | temp_dict[d[2]].append(d) |
| | | datas_dict[day] = temp_dict |
| | | # 统计前面7天的所有板块 |
| | | datas_list = [(x, datas_dict[x]) for x in datas_dict] |
| | | SD = 7 |
| | | for i in range(SD, len(datas_list)): |
| | | fblocks = set() |
| | | for j in range(1, SD + 1): |
| | | blocks = set(datas_list[i - j][1].keys()) |
| | | fblocks |= blocks |
| | | c_blocks = datas_list[i][1].keys() |
| | | first_limit_up_fblocks = c_blocks - fblocks |
| | | print(f"-------------[{datas_list[i][0]}]--------------") |
| | | for b in first_limit_up_fblocks: |
| | | codes_list = [(x[3], x[4], x[12], datetime.datetime.fromtimestamp(int(x[5])).strftime('%H:%M:%S')) for x in |
| | | datas_list[i][1][b]] |
| | | if len(codes_list) >= 2: |
| | | print(b, codes_list) |
| | | |
| | | print(len(datas_dict)) |
| | |
| | | from third_data.kpl_util import KPLPlatManager |
| | | from trade import trade_manager, l2_trade_util, trade_constant |
| | | |
| | | |
| | | # 代码精选板块管理 |
| | | from utils.kpl_data_db_util import KPLLimitUpDataUtil |
| | | |
| | | |
| | | class KPLCodeJXBlockManager: |
| | | __db = 3 |
| | | __redisManager = redis_manager.RedisManager(3) |
| | |
| | | blocks = set() |
| | | return reasons | blocks |
| | | |
| | | __history_blocks_dict_cache = {} |
| | | |
| | | def get_history_blocks(self, code): |
| | | """ |
| | | 获取180天的历史涨停原因 |
| | | @param code: |
| | | @return: |
| | | """ |
| | | if code in self.__history_blocks_dict_cache: |
| | | return self.__history_blocks_dict_cache.get(code) |
| | | try: |
| | | kpl_results = KPLLimitUpDataUtil.get_latest_block_infos(code=code) |
| | | keys = set() |
| | | if kpl_results: |
| | | keys |= set([x[2] for x in kpl_results]) |
| | | for r in kpl_results: |
| | | if r[3]: |
| | | keys |= set(r[3].split("、")) |
| | | self.__history_blocks_dict_cache[code] = keys |
| | | return keys |
| | | except: |
| | | pass |
| | | return set() |
| | | |
| | | |
| | | # 目标代码板块关键词管理 |
| | | class TargetCodePlateKeyManager: |
| | |
| | | # 返回key集合(排除无效板块),今日涨停原因,今日历史涨停原因,历史涨停原因,二级,精选板块 |
| | | def get_plate_keys(self, code): |
| | | """ |
| | | 获取代码的板块 |
| | | 获取代码的板块: (180天的涨停原因+推荐原因)+今日涨停原因+今日涨停推荐原因+今日推荐原因 |
| | | @param code: |
| | | @return: (板块关键词集合,今日涨停原因+涨停推荐原因,今日历史涨停原因,历史涨停原因,精选板块) |
| | | """ |
| | |
| | | k2 = self.__CodesPlateKeysManager.get_history_limit_up_reason_cache(code) |
| | | if k2 is None: |
| | | k2 = set() |
| | | k3 = set() |
| | | industry = global_util.code_industry_map.get(code) |
| | | if industry: |
| | | k3 = {industry} |
| | | |
| | | k3 = self.__CodesPlateKeysManager.get_history_blocks(code) |
| | | if k3: |
| | | keys |= k3 |
| | | # industry = global_util.code_industry_map.get(code) |
| | | # if industry: |
| | | # k3 = {industry} |
| | | |
| | | k4 = set() |
| | | jingxuan_block_info = self.__KPLCodeJXBlockManager.get_jx_blocks_cache(code) |
| | |
| | | if k1: |
| | | # 涨停过 |
| | | keys |= k1 |
| | | keys = keys - set(constant.KPL_INVALID_BLOCKS) |
| | | if not keys: |
| | | # 获取不到涨停原因 |
| | | keys |= k4 |
| | | keys = keys - set(constant.KPL_INVALID_BLOCKS) |
| | | |
| | | # 获取不到涨停原因 |
| | | keys |= k4 |
| | | keys = keys - set(constant.KPL_INVALID_BLOCKS) |
| | | return keys, k1, k11, k2, k3, k4 |
| | | |
| | | |
| | |
| | | if result: |
| | | if "List" in result: |
| | | names = [kpl_util.filter_block(x["CName"]) for x in result["List"]] |
| | | return names |
| | | return names if len(names) < 3 else names[:2] |
| | | return [] |
| | | |
| | | |
| | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | print(getCodeJingXuanBlocks("003043")) |
| | | print(__getConceptJXBK("000546")) |
| | | # __getConceptBK("300564") |
| | | # data = (getMarketJingXuanRealRankingInfo()) |
| | | # data=json.loads(data) |
| | |
| | | return results |
| | | |
| | | @classmethod |
| | | def get_latest_block_infos(cls, min_day=tool.date_sub(tool.get_now_date_str(), 180), code=None): |
| | | """ |
| | | |
| | | @param min_day: 默认获取180天之前的 |
| | | @param code: 代码 |
| | | @return: 最近的涨停板块信息 |
| | | """ |
| | | sql = f"SELECT r.`_code`, r.`_day`, r.`_hot_block_name`, r.`_blocks` FROM `kpl_limit_up_record` r WHERE r.`_day`>'{min_day}'" |
| | | if code: |
| | | sql += f" AND _code='{code}'" |
| | | sql += " order by _create_time" |
| | | mysqldb = mysql_data.Mysqldb() |
| | | results = mysqldb.select_all(sql) |
| | | return results |
| | | |
| | | @classmethod |
| | | def get_latest_blocks_set(cls, code): |
| | | results = cls.get_latest_infos(code, 2, False) |
| | | bs = set([b[0] for b in results]) |
| | |
| | | |
| | | block_count_list = [(k, block_count_dict[k]) for k in block_count_dict] |
| | | block_count_list.sort(key=lambda x: x[1], reverse=True) |
| | | block_count_list = block_count_list[:20] |
| | | block_count_list = block_count_list[:50] |
| | | # [(涨停原因,累计涨停次数,连续次数)] |
| | | fdatas = [] |
| | | today_records_code_dict = {d[3]: d for d in self.__history_limit_up_day_datas.get(now_day)} |
| | |
| | | from cancel_strategy.s_l_h_cancel_strategy import HourCancelBigNumComputer |
| | | from cancel_strategy.s_l_h_cancel_strategy import LCancelBigNumComputer |
| | | from cancel_strategy.s_l_h_cancel_strategy import SCancelBigNumComputer |
| | | from code_attribute.gpcode_manager import MustBuyCodesManager |
| | | from code_attribute.gpcode_manager import MustBuyCodesManager, GreenListCodeManager |
| | | from l2 import l2_data_manager, place_order_single_data_manager |
| | | from l2.cancel_buy_strategy import FCancelBigNumComputer, \ |
| | | NewGCancelBigNumComputer, JCancelBigNumComputer, NBCancelBigNumComputer |
| | |
| | | # 下单成功 |
| | | PlaceOrderCountManager().place_order(code) |
| | | # 下单成功之后移除红名单 |
| | | MustBuyCodesManager().remove_code(code) |
| | | if not GreenListCodeManager().is_in_cache(code): |
| | | MustBuyCodesManager().remove_code(code) |
| | | |
| | | # 如果是绿名单,下单之后就加红 |
| | | if GreenListCodeManager().is_in_cache(code): |
| | | MustBuyCodesManager().add_code(code) |
| | | |
| | | # 清除下单信号 |
| | | place_order_single_data_manager.L2TradeSingleDataManager.clear_data(code) |
| | | |
| | |
| | | __latest_cancel_l2_data_dict[code] = total_datas[-1] |
| | | |
| | | # 撤单成功需要移除红名单 |
| | | MustBuyCodesManager().remove_code(code) |
| | | if not GreenListCodeManager().is_in_cache(code): |
| | | MustBuyCodesManager().remove_code(code) |
| | | |
| | | |
| | | # 根据撤单时间判断是否可以下单 |
New file |
| | |
| | | from db.mysql_data_delegate import Mysqldb |
| | | from utils import tool |
| | | |
| | | |
| | | class KPLLimitUpDataUtil: |
| | | @classmethod |
| | | def get_latest_block_infos(cls, min_day=tool.date_sub(tool.get_now_date_str(), 180), code=None): |
| | | """ |
| | | @param min_day: 默认获取180天之前的 |
| | | @param code: 代码 |
| | | @return: 最近的涨停板块信息 |
| | | """ |
| | | sql = f"SELECT r.`_code`, r.`_day`, r.`_hot_block_name`, r.`_blocks` FROM `kpl_limit_up_record` r WHERE r.`_day`>'{min_day}'" |
| | | if code: |
| | | sql += f" AND _code='{code}'" |
| | | sql += " order by _create_time" |
| | | mysqldb = Mysqldb() |
| | | results = mysqldb.select_all(sql) |
| | | return results |