"""新股自动申购,提供交易日校验、券商对账和本地幂等保护。""" from __future__ import annotations import json import logging from datetime import datetime, time from pathlib import Path from typing import Any import config from sdk import Client from libs.calc import trading_time from libs.lockfile import is_lock,write_lockfile IPO_STRATEGY_NAME = "IPO_SUBSCRIBE" IPO_REMARKS = {IPO_STRATEGY_NAME, "新股申购"} IPO_SESSIONS = ((time(9, 30), time(11, 30)), (time(13, 0), time(15, 0))) TRADING_CALENDAR_SYMBOL = "000001.SH" def AutoBuyIpo(now: datetime | None = None) -> int: """安全执行一次新股申购,返回成功提交的证券数量。""" if not config.account_config.enable_auto_ipo: logging.info("[IPO] 自动申购未启用") return 0 if not trading_time(): logging.info("[IPO] 非交易时间") return 0 client = Client( config.global_config.qmt_base_url, config.global_config.qmt_token, config.HTTP_TIMEOUT, ) result = client.ipo_data("STOCK") for stock in result: lp = Path(config.global_config.qmt_data_dir/f"{stock}.lock") if is_lock(lp): ipo_price = result[stock]['issuePrice'] # 发行价 maxPurchaseNum = result[stock]['maxPurchaseNum'] # 可申购额度 client.passorder( op_type=23, stock=stock, volume=maxPurchaseNum, pr_type=11, price=ipo_price, strategy_name="新股申购", ) write_lockfile(lp)