返回列表 发布新帖

为什么这个示例程序没有真正运行?

1017 2
发表于 2024-2-8 11:43:15 | 显示全部楼层 阅读模式

在这里找到一个QMT的示例,运行之后发现进入trade之后就退出了http://www.xding.info/article/2023/11/19/25.html

这部分对应的代码如下,我加了一个print(now),发现每天的输出都是000000,说明根本没法进入下边的时间判断。这是为什么呢?

def trade(ContextInfo):
    realtime = ContextInfo.get_bar_timetag(ContextInfo.barpos)
    now = timetag_to_datetime(realtime,'%H%M%S')
    nowDate = timetag_to_datetime(realtime,'%Y%m%d %H:%M:%S')
    account = get_trade_detail_data(A.acct, A.acct_type, 'account')
    if len(account)==0:
        print(f'账号{A.acct} 未登录 请检查')
        return
    print(now)
    if '141000' >= now >= '135000':
        holdings = get_trade_detail_data(A.acct, A.acct_type, 'position')
        A.holdings = {i.m_strInstrumentID + '.' + i.m_strExchangeID : i.m_nCanUseVolume for i in holdings}
        #ContextInfo.stock_pool = ContextInfo.get_stock_list_in_sector('沪深300')
        ContextInfo.stock_pool = ContextInfo.get_stock_list_in_sector('沪深A股')
        A.stock_list = prepare_stock_list(ContextInfo)

        stocks_to_sell = [s for s in A.holdings.keys() if s not in A.stock_list]
        num_stocks_to_buy = ContextInfo.buy_stock_count - len(stocks_to_sell)
        stocks_to_buy = []
        for s in A.stock_list:
            if s not in A.holdings.keys():
                stocks_to_buy.append(s)
            if len(stocks_to_buy) >= num_stocks_to_buy:
                break
        A.stocks_to_buy = stocks_to_buy
        for s in stocks_to_sell:
            msg = f"小市值 {s} 卖出 {A.holdings[s]/100} 手"
            print(nowDate, msg)
            if not ContextInfo.is_suspended_stock(s):
                #order_lots(s, -A.holdings[s]/100, ContextInfo, A.acct)
                passorder(A.sell_code, 1101, A.acct, s, 14, -1, A.holdings[s], '小市值策略', 2, msg, ContextInfo)
    if '144000' >= now >= '142000':
        # 获取可用资金
        print('可用资金', get_total_value(A.acct,'STOCK'))
        if len(A.stocks_to_buy) > 0:
            value = get_total_value(A.acct,'STOCK') / len(A.stocks_to_buy)
            for s in A.stocks_to_buy: # 立即以最新价格下单
                latest_price = ContextInfo.get_market_data_ex(['close'], \
                                            [s],period='1m',count = 1)[s]['close'][0]
                vol = value // (latest_price *100) 
                msg = f"小市值 {s} 买入 {vol}手"
                print(nowDate, msg)
                #order_lots(s, vol, ContextInfo, A.acct)
                #order_lots(s, 10, ContextInfo, '55010416')
                passorder(A.buy_code, 1101, A.acct, s, 14, -1, vol*100, '小市值策略', 2, msg, ContextInfo)
        else:
            print(nowDate, '无需换仓')

评论2

向朝华
发表于 2024-2-8 17:51:27 | 显示全部楼层
想交朋友,我也用qmt,来北京玩吗?包食宿我微信13381257591
*******2195楼主
发表于 2024-2-9 06:25:57 | 显示全部楼层
我知道原因了,这个策略指定了某些操作的执行时间,必须用分时数据去驱动,用日线方式无法执行。

回复

您需要登录后才可以回帖 登录 | 立即注册

客服专线

400-080-8112

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