| | |
| | | async_log_util.info(logger_trade, f"正股涨停,准备买入可转债:{result}") |
| | | # 获取可以买的代码 |
| | | code, trade_time = result[0], result[1] |
| | | buy_info = result[3] |
| | | # 策略类型 |
| | | strategy_type = buy_info[1] |
| | | |
| | | # 获取股票代码的可转债代码 |
| | | cb_code = target_codes_manager.get_cb_code(code) |
| | | if CodeTradeStateManager().get_trade_state(cb_code, strategy_type) == CodeTradeStateManager.TRADE_STATE_ALREADY_BUY: |
| | | async_log_util.info(logger_trade, f"已经下单过:{result}") |
| | | continue |
| | | # 获取可转债的涨停价 |
| | | market_info = code_market_manager.get_market_info(cb_code) |
| | | limit_up_price = target_codes_manager.get_limit_up_price(cb_code) |
| | | if market_info: |
| | | if market_info.rate > 0.139: |
| | | async_log_util.info(logger_trade, f"可转债涨幅过高::{cb_code}-{market_info.rate}") |
| | | buy_infos = result[3] |
| | | for buy_info in buy_infos: |
| | | # 策略类型 |
| | | strategy_type = buy_info[1] |
| | | # 获取股票代码的可转债代码 |
| | | cb_code = target_codes_manager.get_cb_code(code) |
| | | if CodeTradeStateManager().get_trade_state(cb_code, strategy_type) == CodeTradeStateManager.TRADE_STATE_ALREADY_BUY: |
| | | async_log_util.info(logger_trade, f"已经下单过:{result}") |
| | | continue |
| | | # 获取可转债的涨停价 |
| | | market_info = code_market_manager.get_market_info(cb_code) |
| | | limit_up_price = target_codes_manager.get_limit_up_price(cb_code) |
| | | if market_info: |
| | | if market_info.rate > 0.139: |
| | | async_log_util.info(logger_trade, f"可转债涨幅过高::{cb_code}-{market_info.rate}") |
| | | continue |
| | | |
| | | volume = int(__get_buy_money() / float(limit_up_price)) |
| | | volume = (volume // 10) * 10 |
| | | buy_price = round(min(float(market_info.price * 1.02), float(limit_up_price)), 3) |
| | | async_log_util.info(logger_trade, f"准备下单:{cb_code}-{buy_price}") |
| | | # 买入20股 |
| | | result = huaxin_trade_api.order(1, cb_code, volume, buy_price, blocking=True) |
| | | CodeTradeStateManager().set_trade_state(cb_code, strategy_type, |
| | | CodeTradeStateManager.TRADE_STATE_ALREADY_BUY) |
| | | async_log_util.info(logger_trade, f"可转债下单结果:{result}") |
| | | huaxin_trade_data_update.add_position_list() |
| | | huaxin_trade_data_update.add_money_list() |
| | | huaxin_trade_data_update.add_deal_list() |
| | | volume = int(__get_buy_money() / float(limit_up_price)) |
| | | volume = (volume // 10) * 10 |
| | | buy_price = round(min(float(market_info.price * 1.02), float(limit_up_price)), 3) |
| | | async_log_util.info(logger_trade, f"准备下单:{cb_code}-{buy_price}") |
| | | # 买入20股 |
| | | result = huaxin_trade_api.order(1, cb_code, volume, buy_price, blocking=True) |
| | | CodeTradeStateManager().set_trade_state(cb_code, strategy_type, |
| | | CodeTradeStateManager.TRADE_STATE_ALREADY_BUY) |
| | | async_log_util.info(logger_trade, f"可转债下单结果:{result}") |
| | | huaxin_trade_data_update.add_position_list() |
| | | huaxin_trade_data_update.add_money_list() |
| | | huaxin_trade_data_update.add_deal_list() |
| | | except Exception as e: |
| | | logger_debug.exception(e) |
| | | time.sleep(1) |
New file |
| | |
| | | from third_data import kpl_api, kpl_util |
| | | from third_data.kpl_data_manager import KPLLimitUpDataRecordManager, CodePlateKeyBuyManager, KPLCodeJXBlockManager |
| | | from utils import tool |
| | | |
| | | if __name__ == "__main__": |
| | | code = "001208" |
| | | datas = kpl_util.parseLimitUpData(kpl_api.getLimitUpInfoNew()) |
| | | KPLLimitUpDataRecordManager.save_record(tool.get_now_date_str(), datas) |
| | | KPLCodeJXBlockManager().load_jx_blocks(code, 8.05, |
| | | 8.05, |
| | | KPLLimitUpDataRecordManager.get_current_reasons()) |
| | | results = CodePlateKeyBuyManager.statistic_block_infos(code, KPLLimitUpDataRecordManager.latest_origin_datas) |
| | | print(results) |
| | |
| | | @param code_limit_up_reasons_dict: 代码的涨停原因 |
| | | @param current_limit_up_block_codes_dict: 今日涨停板块对应的代码 |
| | | @param yesterday_current_limit_up_codes: 昨日涨停代码 |
| | | @return: 返回(前排开1数量,高位板数量,创业板数量,身位) |
| | | @return: 返回(前排开1代码集合,高位板集合,创业板集合,身位) |
| | | """ |
| | | block_codes = current_limit_up_block_codes_dict.get(block) |
| | | if block_codes is None: |
| | |
| | | len( |
| | | current_open_limit_up_codes), |
| | | shsz=True) |
| | | return len(current_open_limit_up_codes), len(high_codes), len(gem_codes), current_shsz_rank |
| | | return current_open_limit_up_codes, high_codes, gem_codes, current_shsz_rank |
| | | |
| | | @classmethod |
| | | def get_plate_keys(cls, code): |
| | |
| | | if d[6]: |
| | | blocks |= set(d[6].split('、')) |
| | | blocks = blocks - constant.KPL_INVALID_BLOCKS |
| | | code_limit_up_reasons_dict[d[3]] = blocks |
| | | code_limit_up_reasons_dict[d[0]] = blocks |
| | | return code_limit_up_reasons_dict |
| | | |
| | | if current_limit_up_datas is None: |
| | |
| | | |
| | | for block in keys: |
| | | # 前排开1数量, 高位板数量, 创业板数量, 身位 |
| | | open_limit_up_count, high_count, gem_count, rank = cls.__is_block_can_buy_new( |
| | | open_limit_up_codes, high_codes, gem_codes, rank = cls.__is_block_can_buy_new( |
| | | code, block, current_limit_up_datas, code_limit_up_reasons_dict, |
| | | current_limit_up_block_codes_dict, yesterday_current_limit_up_codes) |
| | | fresults.append((block, open_limit_up_count, high_count, gem_count, rank)) |
| | | fresults.append((block, open_limit_up_codes, high_codes, gem_codes, rank)) |
| | | return fresults, keys |
| | | |
| | | # 返回:(可以买的板块列表, 是否是独苗, 消息简介,可买的强势主线, 激进买入板块列表) |
| | |
| | | if market_info: |
| | | __BuyStrategyDataManager.add_market_info(market_info) |
| | | |
| | | need_buy, strategy_type, need_buy_msg = __BuyStrategyDataManager.add_transaction_info(t, True) |
| | | if need_buy and code not in position_dict: |
| | | # 持仓结果保存 |
| | | position_dict[code] = t |
| | | logger_debug.info(f"回测下单({need_buy_msg}):{t}") |
| | | results = __BuyStrategyDataManager.add_transaction_info(t, True) |
| | | for need_buy, strategy_type, need_buy_msg in results: |
| | | if need_buy and code not in position_dict: |
| | | # 持仓结果保存 |
| | | position_dict[code] = t |
| | | logger_debug.info(f"回测下单({need_buy_msg}):{t}") |
| | | except Exception as e: |
| | | logging.exception(e) |
| | | logger_debug.exception(e) |
| | |
| | | |
| | | from log_module import async_log_util |
| | | from log_module.log import logger_trade |
| | | from third_data.kpl_data_manager import KPLLimitUpDataRecordManager |
| | | from third_data.kpl_data_manager import KPLLimitUpDataRecordManager, CodePlateKeyBuyManager |
| | | from trade.l2_transaction_data_manager import HuaXinBuyOrderManager |
| | | from utils import tool, l2_huaxin_util |
| | | |
| | |
| | | |
| | | code = info["SecurityID"] |
| | | try: |
| | | can_buy, stategy_type, msg = self.__can_place_order(code, info, backtest) |
| | | return can_buy, stategy_type, msg |
| | | results = self.__can_place_order(code, info, backtest) |
| | | return results |
| | | finally: |
| | | self.__latest_trade_price_dict[code] = info["TradePrice"] |
| | | |
| | | def __can_place_order(self, code, info, backtest): |
| | | """ |
| | | 是否可以下单 |
| | | :param code: 代码 |
| | | :param transaction_info: 成交信息 |
| | | :param backtest: 是否是回测模式 |
| | | :return: 返回是否可以下单 |
| | | 是否可以下单 |
| | | :param code: 代码 |
| | | :param transaction_info: 成交信息 |
| | | :param backtest: 是否是回测模式 |
| | | :return: 返回是否每种策略是否可以下单 |
| | | """ |
| | | if backtest: |
| | | |
| | | def can_buy_for_stategy_1(): |
| | | # 获取2秒内吃的档数 |
| | | rised_price = self.__get_rised_price(code, info) |
| | | if rised_price < 0.1: |
| | |
| | | pre_close_price = self.__pre_close_price_dict.get(code) |
| | | if pre_close_price is None: |
| | | return False, STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS, "没有获取到收盘价" |
| | | |
| | | if (info["TradePrice"] - pre_close_price) / pre_close_price < 0.07: |
| | | rate = (info["TradePrice"] - pre_close_price) / pre_close_price |
| | | if rate < 0.07: |
| | | return False, STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS, "涨幅小于7%" |
| | | |
| | | return True, STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS, f"2S内上升{int(rised_price * 100)}档" |
| | | else: |
| | | # 判断是否涨停 |
| | | is_limit_up = info["TradePrice"] == self.__limit_up_price_dict.get(code) |
| | | limit_up_price = self.__limit_up_price_dict.get(code) |
| | | if not is_limit_up and code.find("30") == 0: |
| | | if rate >= 0.09: |
| | | return True, STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS, f"2S内上升{int(rised_price * 100)}档 涨幅({rate})>9%" |
| | | |
| | | # (板块, 前排开1数量, 高位板数量, 创业板数量, 身位) |
| | | block_infos = CodePlateKeyBuyManager.statistic_block_infos(code, |
| | | KPLLimitUpDataRecordManager.latest_origin_datas) |
| | | |
| | | if rate >= 0.08: |
| | | for b in block_infos: |
| | | codes = b[1] | b[2] | b[3] |
| | | if len(codes) >= 1: |
| | | return True, STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS, f"2S内上升{int(rised_price * 100)}档,板块信息:{b}" |
| | | |
| | | if rate >= 0.07: |
| | | for b in block_infos: |
| | | codes = b[1] | b[2] | b[3] |
| | | if len(codes) >= 2: |
| | | return True, STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS, f"2S内上升{int(rised_price * 100)}档,板块信息:{b}" |
| | | return False, STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS, f"不满足买入条件" |
| | | |
| | | def can_buy_for_stategy_2(): |
| | | if code.find("30") == 0: |
| | | # 30开始的9.8以上的算涨停 |
| | | markekt = self.__latest_market_info_dict.get(info["SecurityID"]) |
| | | if markekt and markekt[2] >= 0.098 and self.__latest_trade_price_dict.get(code): |
| | | # 上一次涨幅小于0.098 |
| | | if (self.__latest_trade_price_dict.get(code) - markekt[8]) / markekt[8] < 0.098: |
| | | return True, STRATEGY_TYPE_RISE_HIGH, f"创业板涨幅超过9.8" |
| | | if is_limit_up: |
| | | # 当前为涨停价 |
| | | if code not in self.__latest_trade_price_dict: |
| | | # 开1 |
| | | # 查询买1金额 |
| | | markekt = self.__latest_market_info_dict.get(info["SecurityID"]) |
| | | if markekt and markekt[3] * markekt[4] >= 1e8: |
| | | async_log_util.info(logger_trade, f"{code}:买1({markekt[3] * markekt[4]})超过1亿") |
| | | return True, STRATEGY_TYPE_LIMIT_UP, "买1超过1亿,开1可买" |
| | | return False, STRATEGY_TYPE_RISE_HIGH, "不满足买入条件" |
| | | |
| | | def can_buy_for_stategy_3(): |
| | | is_limit_up = info["TradePrice"] == self.__limit_up_price_dict.get(code) |
| | | try: |
| | | limit_up_price = self.__limit_up_price_dict.get(code) |
| | | if is_limit_up: |
| | | # 当前为涨停价 |
| | | if code not in self.__latest_trade_price_dict: |
| | | # 开1 |
| | | # 查询买1金额 |
| | | markekt = self.__latest_market_info_dict.get(info["SecurityID"]) |
| | | if markekt and markekt[3] * markekt[4] >= 1e8: |
| | | async_log_util.info(logger_trade, f"{code}:买1({markekt[3] * markekt[4]})超过1亿") |
| | | return True, STRATEGY_TYPE_LIMIT_UP, "买1超过1亿,开1可买" |
| | | else: |
| | | return False, STRATEGY_TYPE_LIMIT_UP, "开1" |
| | | else: |
| | | return False, STRATEGY_TYPE_LIMIT_UP, "开1" |
| | | else: |
| | | if self.__latest_trade_price_dict.get(code) != limit_up_price and code not in self.__limit_up_record_codes: |
| | | # 初次涨停之前那一次不是涨停价 |
| | | return True, STRATEGY_TYPE_LIMIT_UP, "涨停" |
| | | if self.__latest_trade_price_dict.get( |
| | | code) != limit_up_price and code not in self.__limit_up_record_codes: |
| | | # 初次涨停之前那一次不是涨停价 |
| | | return True, STRATEGY_TYPE_LIMIT_UP, "涨停" |
| | | finally: |
| | | if is_limit_up: |
| | | # 加入涨停代码列表 |
| | | self.__limit_up_record_codes.add(code) |
| | | return False, STRATEGY_TYPE_LIMIT_UP, "不满足买入条件" |
| | | return [can_buy_for_stategy_1(),can_buy_for_stategy_2(), can_buy_for_stategy_3()] |
| | | |
| | | if is_limit_up: |
| | | # 加入涨停代码列表 |
| | | self.__limit_up_record_codes.add(code) |
| | | |
| | | return False, STRATEGY_TYPE_LIMIT_UP, "" |
| | | |
| | | def __get_rised_price(self, code, transaction_info, time_space=2000): |
| | | """ |