在这里找到一个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, '无需换仓')