| | |
| | | result = self.buyStrategyDataManager.add_transaction_info(item) |
| | | if result[0]: |
| | | l2_transaction_price_queue.put_nowait( |
| | | (pTransaction['SecurityID'], pTransaction['TradePrice'], pTransaction['TradeTime'])) |
| | | (pTransaction['SecurityID'], pTransaction['TradePrice'], pTransaction['TradeTime'], result)) |
| | | except Exception as e: |
| | | logger_local_huaxin_l2_error.exception(e) |
| | | |
| | |
| | | result = self.buyStrategyDataManager.add_transaction_info(item) |
| | | if result[0]: |
| | | l2_transaction_price_queue.put_nowait( |
| | | (pTick['SecurityID'], pTick['Price'], pTick['TickTime'])) |
| | | (pTick['SecurityID'], pTick['Price'], pTick['TickTime'], result)) |
| | | except Exception as e: |
| | | logger_local_huaxin_l2_error.exception(e) |
| | | |
| | |
| | | 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) == CodeTradeStateManager.TRADE_STATE_ALREADY_BUY: |
| | | if CodeTradeStateManager().get_trade_state(cb_code, strategy_type) == CodeTradeStateManager.TRADE_STATE_ALREADY_BUY: |
| | | async_log_util.info(logger_trade, f"已经下单过:{result}") |
| | | continue |
| | | # 获取可转债的涨停价 |
| | |
| | | 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, CodeTradeStateManager.TRADE_STATE_ALREADY_BUY) |
| | | 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() |
| | |
| | | from code_attribute import history_k_data_util, target_codes_manager |
| | | 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 trade import huaxin_trade_api, buy_strategy |
| | | from trade.trade_manager import CodeTradeStateManager |
| | | from utils import output_data_util, tool |
| | | |
| | |
| | | sse_other_codes.append(code.encode()) |
| | | return sse_stock_codes, sse_other_codes, szse_codes |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | underlying_codes = target_codes_manager.get_subscript_underlying_codes() |
| | | print(__split_codes(underlying_codes)) |
| | | code = "000333" |
| | | print(CodeTradeStateManager().get_trade_state(code,buy_strategy.STRATEGY_TYPE_LIMIT_UP)) |
| | | print(CodeTradeStateManager().get_trade_state(code, buy_strategy.STRATEGY_TYPE_RISE_HIGH)) |
| | | |
| | | CodeTradeStateManager().set_trade_state(code, buy_strategy.STRATEGY_TYPE_LIMIT_UP, |
| | | CodeTradeStateManager.TRADE_STATE_ALREADY_BUY) |
| | | print(CodeTradeStateManager().get_trade_state(code,buy_strategy.STRATEGY_TYPE_LIMIT_UP)) |
| | | print(CodeTradeStateManager().get_trade_state(code, buy_strategy.STRATEGY_TYPE_RISE_HIGH)) |
| | | |
| | | CodeTradeStateManager().set_trade_state(code, buy_strategy.STRATEGY_TYPE_RISE_HIGH, |
| | | CodeTradeStateManager.TRADE_STATE_ALREADY_BUY) |
| | | print(CodeTradeStateManager().get_trade_state(code,buy_strategy.STRATEGY_TYPE_LIMIT_UP)) |
| | | print(CodeTradeStateManager().get_trade_state(code, buy_strategy.STRATEGY_TYPE_RISE_HIGH)) |
| | | |
| | | |
| | | |
| | |
| | | if market_info: |
| | | __BuyStrategyDataManager.add_market_info(market_info) |
| | | |
| | | need_buy, need_buy_msg = __BuyStrategyDataManager.add_transaction_info(t, True) |
| | | 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 |
| | |
| | | from trade.l2_transaction_data_manager import HuaXinBuyOrderManager |
| | | from utils import tool, l2_huaxin_util |
| | | |
| | | # 涨停买入策略 |
| | | STRATEGY_TYPE_LIMIT_UP = 1 |
| | | # 涨幅高买入策略 |
| | | STRATEGY_TYPE_RISE_HIGH = 2 |
| | | # 涨幅高且有板块的买入策略 |
| | | STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS = 3 |
| | | |
| | | |
| | | class BuyStrategyDataManager: |
| | | __latest_transaction_price_dict = {} |
| | | # 涨停过的代码集合 |
| | | __limit_up_record_codes = set() |
| | | """ |
| | | 买入策略管理 |
| | | """ |
| | |
| | | info['BuyNo'], |
| | | info['SellNo'], info['ExecType'])]) |
| | | if int(l2_huaxin_util.convert_time(info['OrderTime']).replace(":", "")) < int("093000"): |
| | | return False, "09:30之前不下单" |
| | | return False, STRATEGY_TYPE_LIMIT_UP, "09:30之前不下单" |
| | | |
| | | code = info["SecurityID"] |
| | | try: |
| | | can_buy, msg = self.__can_place_order(code, info, backtest) |
| | | return can_buy, msg |
| | | can_buy, stategy_type, msg = self.__can_place_order(code, info, backtest) |
| | | return can_buy, stategy_type, msg |
| | | finally: |
| | | self.__latest_trade_price_dict[code] = info["TradePrice"] |
| | | |
| | |
| | | # 获取2秒内吃的档数 |
| | | rised_price = self.__get_rised_price(code, info) |
| | | if rised_price < 0.1: |
| | | return False, "2S内涨幅小于10档" |
| | | return False, STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS, "2S内涨幅小于10档" |
| | | pre_close_price = self.__pre_close_price_dict.get(code) |
| | | if pre_close_price is None: |
| | | return False, "没有获取到收盘价" |
| | | return False, STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS, "没有获取到收盘价" |
| | | |
| | | if (info["TradePrice"] - pre_close_price) / pre_close_price < 0.07: |
| | | return False, "涨幅小于7%" |
| | | return False, STRATEGY_TYPE_RISE_HIGH_WITH_BLOCKS, "涨幅小于7%" |
| | | |
| | | return True, f"2S内上升{int(rised_price * 100)}档" |
| | | 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: |
| | | # 30开始的9.8以上的算涨停 |
| | | markekt = self.__latest_market_info_dict.get(info[0]) |
| | | if markekt and markekt[2] >= 0.098: |
| | | is_limit_up = True |
| | | limit_up_price = info["TradePrice"] |
| | | |
| | | 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[0]) |
| | | 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, "买1超过1亿,开1可买" |
| | | return True, STRATEGY_TYPE_LIMIT_UP, "买1超过1亿,开1可买" |
| | | else: |
| | | return False, "开1" |
| | | return False, STRATEGY_TYPE_LIMIT_UP, "开1" |
| | | else: |
| | | if self.__latest_trade_price_dict.get(code) != limit_up_price: |
| | | # 之前那一次不是涨停价 |
| | | return True, "涨停" |
| | | return False, "" |
| | | 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 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): |
| | | """ |
| | |
| | | # 今日成交了的票就不下单了 |
| | | for d in datas: |
| | | if d["volume"] > 0 and int(d["direction"]) == 0: |
| | | CodeTradeStateManager().set_trade_state(d["securityID"], |
| | | CodeTradeStateManager().set_trade_state(d["securityID"], 0, |
| | | CodeTradeStateManager.TRADE_STATE_ALREADY_BUY) |
| | | huaxin_trade_record_manager.DealRecordManager.add(datas) |
| | | # 持仓股 |
| | |
| | | def __load_datas(cls): |
| | | keys = cls.__get_redis().keys("trade_state-*") |
| | | for k in keys: |
| | | code = k.split("-")[1] |
| | | val = cls.__get_redis().get(k) |
| | | val = int(val) |
| | | cls.__trade_state_cache[code] = val |
| | | try: |
| | | code = k.split("-")[1] |
| | | strategy_type = int(k.split("-")[2]) |
| | | val = cls.__get_redis().get(k) |
| | | val = int(val) |
| | | cls.__trade_state_cache[f"{code}-{strategy_type}"] = val |
| | | except: |
| | | pass |
| | | |
| | | @classmethod |
| | | def __get_redis(cls): |
| | | return cls.__redisManager.getRedis() |
| | | |
| | | # 设置交易状态 |
| | | def set_trade_state(self, code, state): |
| | | self.__trade_state_cache[code] = state |
| | | RedisUtils.setex_async(constant.REDIS_DB, f"trade_state-{code}", tool.get_expire(), state) |
| | | def set_trade_state(self, code, strategy_type, state): |
| | | k = f"{code}-{strategy_type}" |
| | | self.__trade_state_cache[k] = state |
| | | RedisUtils.setex_async(constant.REDIS_DB, f"trade_state-{k}", tool.get_expire(), state) |
| | | |
| | | # 获取交易状态 |
| | | def get_trade_state(self, code): |
| | | state = self.__trade_state_cache.get(code) |
| | | def get_trade_state(self, code, strategy_type): |
| | | k = f"{code}-{strategy_type}" |
| | | state = self.__trade_state_cache.get(k) |
| | | if state is None: |
| | | return self.TRADE_STATE_NOT_BUY |
| | | return state |