| | |
| | | from log_module.async_log_util import huaxin_l2_log |
| | | from log_module.log import logger_local_huaxin_l2_subscript, logger_system, logger_l2_codes_subscript, \ |
| | | logger_local_huaxin_l2_transaction, logger_local_huaxin_l2_upload, logger_local_huaxin_l2_error, printlog, \ |
| | | logger_trade, logger_debug |
| | | logger_trade, logger_debug, logger_local_huaxin_trade_debug |
| | | from trade.buy_strategy import BuyStrategyDataManager |
| | | from utils import tool, l2_huaxin_util |
| | | |
| | |
| | | results = self.buyStrategyDataManager.add_transaction_info(item) |
| | | for result in results: |
| | | if result[0]: |
| | | huaxin_l2_log.info(logger_local_huaxin_trade_debug, f"达到买入条件:{pTick['SecurityID']} - {results} - {item}") |
| | | l2_transaction_price_queue.put_nowait( |
| | | (pTick['SecurityID'], pTick['Price'], pTick['TickTime'], results)) |
| | | break |
| | |
| | | from records import huaxin_trade_record_manager |
| | | 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, buy_strategy |
| | | from trade import huaxin_trade_api, huaxin_trade_data_update, huaxin_sell_util, backtest_trade, buy_strategy, \ |
| | | sell_strategy |
| | | from trade.buy_strategy import BuyStrategyDataManager, StrategyBuyOrderRefManager |
| | | from trade.trade_manager import CodeTradeStateManager |
| | | from trade.trade_settings import WantBuyCodesManager, TradeStateManager |
| | |
| | | code = result[0] |
| | | # 正股需要加载板块 |
| | | if code.find("11") != 0 and code.find("12") != 0: |
| | | # 正股代码 |
| | | limit_up_price = tool.get_limit_up_price(code, result[8]) |
| | | # 涨幅大于5%才开始获取板块 |
| | | if result[2] > 0.05: |
| | | KPLCodeJXBlockManager().load_jx_blocks(result[0], result[3], |
| | | float(limit_up_price), |
| | | KPLLimitUpDataRecordManager.get_current_reasons()) |
| | | else: |
| | | # 可转债代码,处理 |
| | | sell_strategy.process_market_info(result, __BuyStrategyDataManager) |
| | | except Exception as e: |
| | | logger_debug.exception(e) |
| | | time.sleep(1) |
| | |
| | | self.set_pre_close_price(code, info[8]) |
| | | self.__latest_market_info_dict[info[0]] = info |
| | | |
| | | def get_latest_market_info(self, code): |
| | | return self.__latest_market_info_dict.get(code) |
| | | |
| | | def add_transaction_info(self, info, backtest=False): |
| | | |
| | | """ |
| | |
| | | # 删除第一个元素 |
| | | del self.__latest_transaction_price_dict[code][0] |
| | | return self.__latest_transaction_price_dict[code][-1][0] - self.__latest_transaction_price_dict[code][0][0] |
| | | |
| | | def process(self, underlying_code, underlying_transaction_info, underlying_market_info, cb_code, cb_market_info): |
| | | """ |
| | | 处理数据 |
| | | :param underlying_code: 正股代码 |
| | | :param underlying_transaction_info:正股成交信息 |
| | | :param underlying_market_info: 正股行情 |
| | | :param cb_code: 可转债代码 |
| | | :param cb_market_info: 可转债行情 |
| | | :return: 是否可以买, 买入信息 |
| | | """ |
| | | pass |
| | | # 是否开1 |
| | | limit_up_datas = KPLLimitUpDataRecordManager.latest_origin_datas |
| | | for item in limit_up_datas: |
| | | pass |
| | | |
| | | |
| | | class StrategyBuyOrderRefManager: |
New file |
| | |
| | | """ |
| | | 卖出策略 |
| | | """ |
| | | from code_attribute import target_codes_manager |
| | | from log_module import async_log_util |
| | | from log_module.log import logger_trade, logger_debug |
| | | from records import huaxin_trade_record_manager |
| | | from trade import huaxin_sell_util |
| | | from trade.buy_strategy import BuyStrategyDataManager |
| | | from utils import tool |
| | | |
| | | |
| | | def process_market_info(market, buyStrategyDataManager: BuyStrategyDataManager): |
| | | try: |
| | | """ |
| | | 处理市场行情 |
| | | :param market: (代码, 最近的价格, 涨幅, 买1价, 买1量, 成交总量, 买入量, 卖出量, 昨日收盘价, 时间戳) |
| | | :return: |
| | | """ |
| | | cb_code = market[0] |
| | | buys = huaxin_trade_record_manager.DealRecordManager().list_buy_by_code_cache(cb_code) |
| | | if not buys: |
| | | return |
| | | sells = huaxin_trade_record_manager.DealRecordManager().list_sell_by_code_cache(cb_code) |
| | | if sells is None: |
| | | sells = [] |
| | | left_volume = sum([int(x["volume"]) for x in buys]) - sum([int(x["volume"]) for x in sells]) |
| | | if left_volume <= 0: |
| | | # 无剩余持仓 |
| | | return |
| | | # 计算买入金额与卖出金额的差值 |
| | | total_buy_money = sum([float(x["price"]) * int(x["volume"]) for x in buys]) |
| | | total_sell_money = sum([float(x["price"]) * int(x["volume"]) for x in sells]) |
| | | left_money = total_buy_money - total_sell_money |
| | | cos_price = round(left_money / left_volume, 3) |
| | | # 如果现价低于现在的2%就需要全部卖出 |
| | | down_rate = (cos_price - market[1]) / cos_price |
| | | threshold_rate = 0.02 |
| | | if down_rate < threshold_rate: |
| | | return |
| | | underlying_code = target_codes_manager.get_underlying_code(cb_code) |
| | | if not underlying_code: |
| | | # 获取最近的行情 |
| | | underlying_market = buyStrategyDataManager.get_latest_market_info(underlying_code) |
| | | if underlying_market: |
| | | underlying_limit_up_price = float(tool.get_limit_up_price(underlying_code, underlying_market[8])) |
| | | if abs(underlying_limit_up_price - underlying_market[1]) < 0.0001: |
| | | # 正股是涨停的 |
| | | threshold_rate = 0.03 |
| | | if down_rate <= threshold_rate: |
| | | return |
| | | # 降幅触发强平机制,已跌停价卖出 |
| | | limit_down_price = round( float(tool.get_limit_down_price(cb_code, market[8])),3) |
| | | result = huaxin_sell_util.start_sell(cb_code, left_volume, 1, None, |
| | | limit_down_price, |
| | | market[1], blocking=False) |
| | | async_log_util.info(logger_trade, f"平仓API卖结果: {result}") |
| | | except Exception as e: |
| | | logger_debug.exception(e) |
| | |
| | | |
| | | |
| | | 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)}")) |
| | | limit_up_price = to_price(decimal.Decimal(str(pre_close_price)) * decimal.Decimal(f"{get_limit_up_rate(code)}"), "0.00" if is_stock(code) else "0.000") |
| | | return limit_up_price |
| | | |
| | | |
| | | def get_limit_down_price(code, pre_close_price): |
| | | limit_down_price = to_price(decimal.Decimal(str(pre_close_price)) * decimal.Decimal(f"{get_limit_down_rate(code)}"), "0.00" if is_stock(code) else "0.000") |
| | | return limit_down_price |
| | | |
| | | |
| | | # 将时间戳s格式化 |
| | | def to_time_str(timestamp_second, format_="%H:%M:%S"): |
| | | return datetime.datetime.fromtimestamp(timestamp_second).strftime(format_) |