迅投QMT社区 门户 查看主题

策略研究--qmt实现新股自动申购卖出策略

发布者: 落花忆流年 | 发布时间: 2026-7-29 17:23| 查看数: 19| 评论数: 1|帖子模式

风险提示:本内容为量化技术及平台操作的纯知识分享,不构成任何投资建议,不推荐任何具体证券或交易时机。部分内容可能理解包含错误,请自行核实。市场有风险,投资需谨慎

优化了一下以前的完整新股新购,晚上了一下功能,检查账户持股,新股上市第一天卖出,其他写了一个ptrade教程策略研究--新股新债自动申购上市当天卖出策略,

今天研究一下怎么样在ptrade上实现这个功能,新股的申购卖出,第一步设置定时函数,启动策略

    # 设置定时器1:每天09:42执行申购
    c.run_time("run_subscribe", "1nDay", "2024-07-25 14:30:00")
    print("定时器1已设置,每天14:30自动申购")
        
    c.run_time("check_and_sell_new_stock", "1nDay", "2024-07-25 14:50:00")
    print("定时器2已设置,每天14:50检查新股上市并卖出\n")

读取ipo数据申购,注意北京的交易所要看证券公司支持不

8595a42e60fecdadb4df87157e05b4a4.png

def run_subscribe(c):
    now = datetime.datetime.now()
    print(f"\n执行申购 - {now.strftime('%Y-%m-%d %H:%M:%S')}")
        
    all_ipo = get_ipo_data()
    print(f"获取到 {len(all_ipo) if all_ipo else 0} 个申购标的")
    
    if not all_ipo:
        print("今日无新股/新债可申购")
        return
        
    for code, info in all_ipo.items():
        issue_price = info.get('issuePrice', 0)
        max_purchase = info.get('maxPurchaseNum', 0)
        
        if issue_price <= 0 or max_purchase <= 0:
            print(f"  {code}: 发行价或申购数量无效,跳过")
            continue
        
        try:
            passorder(
                23,              # opType: 买入/申购
                1101,            # orderType: 普通委托
                c.account,       # 资金账号
                code,            # 证券代码(新股或新债)
                11,              # prType: 指定价
                issue_price,     # 发行价
                max_purchase,    # 最大申购数量
                '打新申购',       # 策略名
                1,               # 立即生效
                '打新',           # 备注
                c                # ContextInfo
            )
            print(f"已提交申购: {code}, 价格:{issue_price}, 数量:{max_purchase}")
        except Exception as e:
            print(f"申购失败 {code}: {e}")
        
    print("申购检查完成\n")

读取新股的上市时间,上市时间等于今天的卖出

e588a88b1a334451049a8a5833f67861.png

76137382f15a3c51612069b638268ff2.png

简单的代码整理

9ee1e1602edbe089d7896947affa7234.png

def check_and_sell_new_stock(c):
    """
    检查账户持仓,卖出上市当天的新股
    逻辑:查找持仓中上市日期等于今天(上市天数=0)的股票,全部卖出
    """
    now = datetime.datetime.now()
    today_str = now.strftime('%Y-%m-%d')
    today_int = int(now.strftime('%Y%m%d'))  # 转为整数,如 20260729
    print(f"\n执行新股上市检查卖出 - {today_str} {now.strftime('%H:%M:%S')}")
    
    # ---------- 1. 获取账户持仓 ----------
    positions = get_trade_detail_data(c.account, 'stock', 'position')
    
    if not positions:
        print("账户无持仓,无需检查")
        return
    
    print(f"持仓数量: {len(positions)}")
    
    # ---------- 2. 检查每只持仓股票的上市日期 ----------
    new_stocks_to_sell = []
    
    for pos in positions:
        stock_code = pos.m_strInstrumentID
        market = pos.m_strExchangeID
        full_code = f"{stock_code}.{market}"
        hold_volume = pos.m_nVolume
        avail_volume = pos.m_nCanUseVolume
        
        if hold_volume <= 0 or avail_volume <= 0:
            continue
        
        try:
            inst_detail = c.get_instrumentdetail(full_code)
            
            # 修复1:OpenDate 是整数格式 20221222,需要转为字符串再比较
            open_date = inst_detail.get('OpenDate', 0)
            
            if open_date and open_date > 0:
                # 将整数日期转为字符串格式
                open_date_str = str(open_date)
                # 判断是否为今日上市(OpenDate 等于今日日期)
                if open_date == today_int:
                    print(f"  {full_code}({pos.m_strInstrumentName}): 今日上市,准备卖出")
                    new_stocks_to_sell.append({
                        'code': full_code,
                        'volume': avail_volume,
                        'name': pos.m_strInstrumentName
                    })
                else:
                    print(f"  {full_code}: 上市日期{open_date_str},非今日上市,跳过")
            else:
                print(f"  {full_code}: 无法获取上市日期,跳过")
                
        except Exception as e:
            print(f"  {full_code}: 获取上市日期失败 - {e}")
        
    # ---------- 3. 执行卖出 ----------
    if not new_stocks_to_sell:
        print("今日无新股上市,无需卖出")
        return
        
    print(f"\n发现 {len(new_stocks_to_sell)} 只今日上市新股,开始执行卖出...")
    
    for stock in new_stocks_to_sell:
        try:
            passorder(
                24,              # opType: 24 = 卖出
                1101,            # orderType: 普通委托
                c.account,       # 资金账号
                stock['code'],   # 证券代码
                5,               # prType: 5 = 最新价(市价卖出)
                0,               # price: 市价时填0
                stock['volume'], # 可用数量全部卖出
                '新股上市卖出',   # 策略名
                1,               # 立即生效
                '新股卖出',       # 备注
                c                # ContextInfo
            )
            print(f"已提交卖出: {stock['code']}({stock['name']}), 数量:{stock['volume']}")
        except Exception as e:
            print(f"卖出失败 {stock['code']}: {e}")
        print("新股上市检查卖出完成\n")

策略没有问题挂模型交易就可以

962336d50a2669f5fd945aca7d82cc1b.png

下单的结果

c0a629146a5cfd56efff1400cf96061f.png

不懂的问我就可以

457f33d5efb86284790adb53ca036b4c.jpg

最新评论

落花忆流年发表于  3 小时前
不懂的问我就可以作者微信xg_quant
客服专线

400-080-8112

用思考的速度交易,用真诚的态度合作,我们是认真的!
  • 关注公众号
  • 添加微信客服
Copyright © 2001-2026 迅投QMT社区 版权所有 All Rights Reserved. 京ICP备2025122616号-3
关灯 快速发帖
扫一扫添加微信客服
QQ客服返回顶部
快速回复 返回顶部 返回列表