| | |
| | | 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 |
| | | from trade.buy_strategy import BuyStrategyDataManager |
| | | from utils import tool, l2_huaxin_util |
| | | |
| | | ###B类### |
| | |
| | | # 代码的上次成交的订单唯一索引 |
| | | __last_transaction_keys_dict = {} |
| | | |
| | | limit_up_price_dict = {} |
| | | __is_limit_up_dict = {} |
| | | buyStrategyDataManager = BuyStrategyDataManager() |
| | | |
| | | # 高涨幅的代码 |
| | | __high_rate_codes = set() |
| | |
| | | "SubSeq": pTransaction['SubSeq'], "BuyNo": pTransaction['BuyNo'], |
| | | "SellNo": pTransaction['SellNo'], |
| | | "ExecType": pTransaction['ExecType'].decode()} |
| | | |
| | | logger_local_huaxin_l2_transaction.info(f"{item}") |
| | | |
| | | huaxin_l2_log.info(logger_local_huaxin_l2_transaction, f"{item}") |
| | | |
| | | if pTransaction['TradePrice'] == self.limit_up_price_dict.get(pTransaction['SecurityID']): |
| | | # TODO 成交价是涨停价才输出 |
| | | #huaxin_l2_log.info(logger_local_huaxin_l2_transaction, f"{item}") |
| | | if not self.__is_limit_up_dict.get(pTransaction['SecurityID']): |
| | | huaxin_l2_log.info(logger_local_huaxin_l2_upload, |
| | | f"{pTransaction['TradeTime']} {pTransaction['SecurityID']} 下单") |
| | | l2_transaction_price_queue.put_nowait( |
| | | (pTransaction['SecurityID'], pTransaction['TradePrice'], pTransaction['TradeTime'])) |
| | | |
| | | self.__is_limit_up_dict[pTransaction['SecurityID']] = True |
| | | else: |
| | | self.__is_limit_up_dict[pTransaction['SecurityID']] = False |
| | | if self.buyStrategyDataManager.add_transaction_info(item): |
| | | l2_transaction_price_queue.put_nowait( |
| | | (pTransaction['SecurityID'], pTransaction['TradePrice'], pTransaction['TradeTime'])) |
| | | except Exception as e: |
| | | logger_local_huaxin_l2_error.exception(e) |
| | | |
| | |
| | | logger_system.info(f'昨日收盘价数量:{len(market_codes)}') |
| | | if pre_price_dict: |
| | | for k in pre_price_dict: |
| | | if k.find("00") == 0 or k.find("60") == 0: |
| | | limit_up_price = tool.to_price(decimal.Decimal(str(pre_price_dict[k])) * decimal.Decimal("1.1")) |
| | | else: |
| | | limit_up_price = tool.to_price(decimal.Decimal(str(pre_price_dict[k])) * decimal.Decimal("1.2")) |
| | | Lev2MdSpi.limit_up_price_dict[k] = round(float(limit_up_price), 2) |
| | | Lev2MdSpi.buyStrategyDataManager.set_pre_close_price(k, pre_price_dict[k]) |
| | | return market_codes |
| | | |
| | | |
| | |
| | | def load_latest_market_info(date=tool.get_now_date_str()): |
| | | path = f"{constant.get_path_prefix()}/{constant.LOG_DIR}/huaxin_local/l2/market.{date}.log" |
| | | fdatas = {} |
| | | |
| | | MAX_LINE = 5000 |
| | | with open(path, 'r') as file: |
| | | file.seek(0, 2) |
| | |
| | | return fdatas |
| | | |
| | | |
| | | def load_transactions(date=tool.get_now_date_str()): |
| | | path = f"{constant.get_path_prefix()}/{constant.LOG_DIR}/huaxin_local/l2/transaction.{date}.log" |
| | | fdatas = [] |
| | | with open(path, 'r') as file: |
| | | lines = file.readlines() |
| | | for line in lines: |
| | | start_index = line.find("]") |
| | | if start_index<0: |
| | | continue |
| | | line = line[start_index + 1:].strip() |
| | | data = eval(line) |
| | | fdatas.append(data) |
| | | return fdatas |
| | | |
| | | |
| | | |
| | | |
| | | if __name__ == "__main__": |
| | | fdatas = load_latest_market_info() |
| | | fdatas = load_latest_market_info("2024-05-29") |
| | | print(fdatas) |
| | | |
| | | pass |
| | |
| | | from huaxin_client.client_network import SendResponseSkManager |
| | | from log_module import async_log_util, log_export |
| | | from records import huaxin_trade_record_manager |
| | | from trade import huaxin_trade_api, huaxin_trade_data_update, huaxin_sell_util |
| | | 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 |
| | | output_util, l2_huaxin_util |
| | | |
| | | middle_api_protocol.SERVER_PORT = 10008 |
| | | middle_api_protocol.SERVER_HOST = "43.138.167.68" |
| | |
| | | import concurrent.futures |
| | | |
| | | __cancel_sell_thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=8) |
| | | __BuyStrategyDataManager = BuyStrategyDataManager() |
| | | |
| | | |
| | | def __send_response(data_bytes): |
| | |
| | | results = huaxin_trade_record_manager.PositionManager().list_by_day(tool.get_now_date_str("%Y%m%d"), code) |
| | | else: |
| | | results = huaxin_trade_record_manager.PositionManager().get_from_cache() |
| | | if constant.backtest_mode_info: |
| | | results.clear() |
| | | position_dict = backtest_trade.position_dict |
| | | for p in position_dict: |
| | | translation = position_dict[p] |
| | | underlying_code = p["SecurityID"] |
| | | p["buy_list"] = [{"price": str(translation['TradePrice']), |
| | | "tradeTime": l2_huaxin_util.convert_time(translation['OrderTime']), "volume": 10}] |
| | | cb_code = target_codes_manager.get_cb_code(underlying_code) |
| | | underlying_market = code_market_manager.get_market_info(underlying_code) |
| | | results.append( |
| | | {"securityID": cb_code, "securityName": gpcode_manager.CodesNameManager().get_code_name(cb_code), |
| | | "buy_list": p["buy_list"], "sell_list": [], "currentPosition": 10, "id": cb_code}) |
| | | |
| | | for r in results: |
| | | cb_code = r["securityID"] |
| | | underlying_code = target_codes_manager.get_underlying_code(cb_code) |
| | |
| | | underlying_market.buy1_price * underlying_market.buy1_volume), |
| | | "preClosePrice": underlying_market.pre_close_price} |
| | | # 获取买点与卖点 |
| | | buys = huaxin_trade_record_manager.DealRecordManager().list_buy_by_code_cache(cb_code) |
| | | sells = huaxin_trade_record_manager.DealRecordManager().list_sell_by_code_cache(cb_code) |
| | | r["buy_list"] = [{"price": str(x["price"]), "tradeTime": x["tradeTime"], "volume": x["volume"]} for x in |
| | | buys] |
| | | r["sell_list"] = [{"price": str(x["price"]), "tradeTime": x["tradeTime"], "volume": x["volume"]} for x in |
| | | sells] |
| | | if "buy_list" not in r: |
| | | buys = huaxin_trade_record_manager.DealRecordManager().list_buy_by_code_cache(cb_code) |
| | | r["buy_list"] = [{"price": str(x["price"]), "tradeTime": x["tradeTime"], "volume": x["volume"]} for x in |
| | | buys] |
| | | if "sell_list" not in r: |
| | | sells = huaxin_trade_record_manager.DealRecordManager().list_sell_by_code_cache(cb_code) |
| | | r["sell_list"] = [{"price": str(x["price"]), "tradeTime": x["tradeTime"], "volume": x["volume"]} for x |
| | | in |
| | | sells] |
| | | send_response({"code": 0, "data": results}, client_id, request_id) |
| | | elif type_ == "refresh_trade_data": |
| | | # 刷新交易数据 |
| | |
| | | # 获取账户资金 |
| | | result = huaxin_trade_record_manager.MoneyManager.get_data() |
| | | send_response({"code": 0, "data": result}, client_id, request_id) |
| | | elif ctype == 'set_backtest_mode': |
| | | try: |
| | | # 设置回测模式 |
| | | date = data.get("date") |
| | | mode = data.get("mode") |
| | | if not date: |
| | | date = tool.get_now_date_str() |
| | | if mode: |
| | | # 开始回撤 |
| | | constant.backtest_mode_info = (True, date) |
| | | threading.Thread(target=lambda: backtest_trade.start_backtest(date), daemon=True).start() |
| | | else: |
| | | # 结束回撤 |
| | | constant.backtest_mode_info = None |
| | | send_response({"code": 0, "data": {}}, client_id, request_id) |
| | | except Exception as e: |
| | | send_response({"code": 0, "data": {}, "msg": str(e)}, client_id, request_id) |
| | | |
| | | |
| | | def test(): |
New file |
| | |
| | | """ |
| | | 回撤交易 |
| | | """ |
| | | # 持仓字典 |
| | | from log_module import log_export |
| | | from trade.buy_strategy import BuyStrategyDataManager |
| | | |
| | | position_dict = {} |
| | | |
| | | |
| | | def start_backtest(date): |
| | | __BuyStrategyDataManager = BuyStrategyDataManager() |
| | | # 回撤 |
| | | markets_dict = log_export.load_latest_market_info(date) |
| | | for code in markets_dict: |
| | | __BuyStrategyDataManager.add_market_info(markets_dict[code]) |
| | | transactions = log_export.load_transactions(date) |
| | | for t in transactions: |
| | | code = t["SecurityID"] |
| | | need_buy = __BuyStrategyDataManager.add_transaction_info(t) |
| | | if need_buy and code not in position_dict: |
| | | # 持仓结果保存 |
| | | position_dict[code] = t |
New file |
| | |
| | | """ |
| | | 买入策略数据管理器 |
| | | """ |
| | | import decimal |
| | | |
| | | from utils import tool |
| | | |
| | | |
| | | class BuyStrategyDataManager: |
| | | """ |
| | | 买入策略管理 |
| | | """ |
| | | |
| | | def __init__(self): |
| | | # 涨停价管理 |
| | | self.__limit_up_price_dict = {} |
| | | self.__latest_trade_price_dict = {} |
| | | 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")) |
| | | self.__limit_up_price_dict[code] = round(float(limit_up_price), 2) |
| | | |
| | | def add_market_info(self, info): |
| | | """ |
| | | 添加市场信息 |
| | | :param info: (代码, 最近的价格, 涨幅, 买1价, 买1量, 成交总量, 委托买入总量, 委托卖出总量, 昨日收盘价) |
| | | :return: |
| | | """ |
| | | code = info[0] |
| | | # 设置昨日收盘价格 |
| | | if code not in self.__limit_up_price_dict: |
| | | self.set_pre_close_price(code, info[8]) |
| | | self.__latest_market_info_dict[info[0]] = info |
| | | |
| | | def add_transaction_info(self, info): |
| | | """ |
| | | 添加成交信息 |
| | | :param info: 示例-{'SecurityID': '002842', 'TradePrice': 6.66, 'TradeVolume': 1100, 'OrderTime': 95436390, |
| | | 'MainSeq': 2011, 'SubSeq': 8162612, 'BuyNo': 8162610, 'SellNo': 8159424, 'ExecType': '1'} |
| | | :return: 返回是否下单 |
| | | """ |
| | | code = info["SecurityID"] |
| | | try: |
| | | if self.__latest_trade_price_dict.get(code) == self.__limit_up_price_dict.get(code): |
| | | # 当前为涨停价 |
| | | if self.__latest_trade_price_dict.get(code) != self.__limit_up_price_dict.get(code): |
| | | return True |
| | | finally: |
| | | self.__latest_trade_price_dict[code] = info["TradePrice"] |
| | | return False |
| | |
| | | |
| | | TEST = False |
| | | |
| | | |
| | | JUEJIN_LOCAL_API = False |
| | | |
| | | |
| | | def is_windows(): |
| | | system = platform.system() |
| | |
| | | return True |
| | | return False |
| | | |
| | | REDIS_DB = 10 |
| | | |
| | | REDIS_DB = 10 |
| | | |
| | | # redis设置 |
| | | REDIS_CONFIG = { |
| | |
| | | |
| | | LOG_DIR = "logs_cb" |
| | | |
| | | |
| | | # 获取根路径 |
| | | def get_path_prefix(): |
| | | return 'D:' if is_windows() else '/home/userzjj' |
| | | return 'D:' if is_windows() else '/home/userzjj' |
| | | |
| | | |
| | | # 回撤模式信息 |
| | | backtest_mode_info = None |