QMT的运行机制有什么区别?逐k、事件以及定时!
发布时间:6小时前阅读:18
QMT 系统主要提供三种运行机制,分别是逐k运行、事件驱动以及定时任务。
如需咨询了解或办理,欢迎点击头像与我联系,新开账户享受免费qmt和ptrade量化软件使用,支持快速交易通道,资金达标可申请股票万1及以下,ETF万0.5,可转债万0.5,逆回购1折等优惠佣金!还有更多福利活动,欢迎右上角与我们专属经理联系。
一、逐 K 线驱动:handlebar
handlebar是主图历史 k 线+盘中订阅推送。运行开始时,所选周期历史 k 线从左向右每根触发一次handlebar函数调用。盘中时,主图品种每个新分笔数据到达,触发一次handlebar函数调用,盘中分笔驱动,但是逐 K 线生效。
逐 K 线驱动(handlebar)示例:
因此,结合不同场景需求(回测或实盘),针对不同的机制(定时任务或事件驱动),我们分别给出回测与实盘的完整示例,复制到策略编辑器中即可使用。
在编写策略前,需要注意在编写一个策略时,首先需要在代码的最前一行写上: #coding:gbk 统一脚本的编码格式是GBK,缩进需要统一,全部统一为····或者->
实盘示例-基于 handlebar
双均线实盘策略代码:
#coding:gbk
# 导入包
import pandas as pd
import numpy as np
import datetime
"""示例说明:双均线实盘策略,通过计算快慢双均线,在金叉时买入,死叉时做卖出"""
class a():
pass
A = a() #创建空的类的实例 用来保存委托状态
def init(C):
A.stock= C.stockcode + '.' + C.market #品种为模型交易界面选择品种
A.acct= account #账号为模型交易界面选择账号
A.acct_type= accountType #账号类型为模型交易界面选择账号
A.amount = 10000 #单笔买入金额 触发买入信号后买入指定金额
A.line1=17 #快线周期
A.line2=27 #慢线周期
A.waiting_list = [] #未查到委托列表 存在未查到委托情况暂停后续报单 防止超单
A.buy_code = 23 if A.acct_type == 'STOCK' else 33 #买卖代码 区分股票 与 两融账号
A.sell_code = 24 if A.acct_type == 'STOCK' else 34
print(f'双均线实盘示例{A.stock} {A.acct} {A.acct_type} 单笔买入金额{A.amount}')
def handlebar(C):
#跳过历史k线
if not C.is_last_bar():
return
now = datetime.datetime.now()
now_time = now.strftime('%H%M%S')
# 跳过非交易时间
if now_time < '093000' or now_time > "150000":
return
account = get_trade_detail_data(A.acct, A.acct_type, 'account')
if len(account)==0:
print(f'账号{A.acct} 未登录 请检查')
return
account = account[0]
available_cash = int(account.m_dAvailable)
#如果有未查到成交 查询成交
if A.waiting_list:
found_list = []
deals = get_trade_detail_data(A.acct, A.acct_type, 'deal')
for deal in deals:
if deal.m_strRemark in A.waiting_list:
found_list.append(deal.m_strRemark)
A.waiting_list = [i for i in A.waiting_list if i not in found_list]
if A.waiting_list:
print(f"当前有未查到委托 {A.waiting_list} 暂停后续报单")
return
holdings = get_trade_detail_data(A.acct, A.acct_type, 'position')
holdings = {i.m_strInstrumentID + '.' + i.m_strExchangeID : i.m_nCanUseVolume for i in holdings}
#获取行情数据
data = C.get_market_data_ex(["close"],[A.stock],period = '1d',count = max(A.line1, A.line2)+1)
close_list = data[A.stock].values
if len(close_list) < max(A.line1, A.line2)+1:
print('行情长度不足(新上市或最近有停牌) 跳过运行')
return
pre_line1 = np.mean(close_list[-A.line1-1: -1])
pre_line2 = np.mean(close_list[-A.line2-1: -1])
current_line1 = np.mean(close_list[-A.line1:])
current_line2 = np.mean(close_list[-A.line2:])
#如果快线穿过慢线,则买入委托 当前无持仓 买入
vol = int(A.amount / close_list[-1] / 100) * 100 #买入数量 向下取整到100的整数倍
if A.amount < available_cash and vol >= 100 and A.stock not in holdings and pre_line1 < pre_line2 and current_line1 > current_line2:
#下单开仓 ,参数说明可搜索PY交易函数 passorder
msg = f"双均线实盘 {A.stock} 上穿均线 买入 {vol}股"
passorder(A.buy_code, 1101, A.acct, A.stock, 14, -1, vol, '双均线实盘', 2 , msg, C)
print(msg)
A.waiting_list.append(msg)
#如果快线下穿慢线,则卖出委托
if A.stock in holdings and holdings[A.stock] > 0 and pre_line1 > pre_line2 and current_line1 < current_line2:
msg = f"双均线实盘 {A.stock} 下穿均线 卖出 {holdings[A.stock]}股"
passorder(A.sell_code, 1101, A.acct, A.stock, 14, -1, holdings[A.stock], '双均线实盘', 2 , msg, C)
print(msg)
A.waiting_list.append(msg)
二、事件驱动 :subscribe 订阅推送
盘中订阅指定品种的分笔数据,新分笔到达时,触发指定的回调函数。
实盘示例-基于 subscribe
#coding:gbk
class a():
pass
A = a()
A.bought_list = []
account = 'testaccount'
def init(C):
#下单函数的参数需要 ContextInfo对象 在init中定义行情回调函数 可以用到init函数的入参 不用手动传入
def callback_func(data):
#print(data)
for stock in data:
current_price = data[stock]['close']
pre_price = data[stock]['preClose']
ratio = current_price / pre_price - 1
print(stock, C.get_stock_name(stock), '当前涨幅', ratio)
if ratio > 0 and stock not in A.bought_list:
msg = f"当前涨幅 {ratio} 大于0 买入100股"
print(msg)
#下单函数passorder 安全起见处于注释状态 需要实际测试下单交易时再放开
#passorder(23, 1101, account, stock, 5, -1, 100, '订阅下单示例', 2, msg, C)
A.bought_list.append(stock)
stock_list = ['600000.SH', '000001.SZ']
for stock in stock_list:
C.subscribe_quote(stock, period = '1d', callback = callback_func)
三、定时任务 :run_time 定时运行
指定固定的时间间隔,持续触发指定的回调函数。
实盘示例-基于 run_time
1.#coding:gbk
2.import time, datetime
3.class a():
4.pass
5.A = a()
6.def init(C):
7.A.hsa = C.get_stock_list_in_sector('沪深A股')
8.A.vol_dict = {}
9.for stock in A.hsa:
10.A.vol_dict[stock] = C.get_last_volume(stock)
11.A.bought_list = []
12.C.run_time("f", "1nSecond", "2019-10-14 13:20:00")
13.def f(C):
14.t0 = time.time()
15.now = datetime.datetime.now()
16.full_tick = C.get_full_tick(A.hsa)
17.total_market_value = 0
18.total_ratio = 0
19.count = 0
20.for stock in A.hsa:
21.ratio = full_tick[stock]['lastPrice'] / full_tick[stock]['lastClose'] - 1
22.if ratio > 0.09 and stock not in A.bought_list:
23.msg = f"{now} {stock} {C.get_stock_name(stock)} 当前涨幅 {ratio} 大于5% 买入100股"
24.#下单示例 安全起见处于注释状态 需要实际测试下单时可以放开
25.#passorder(23, 1101, account, stock, 5, -1, 100, '示例策略', 2, msg, C)
26.A.bought_list.append(stock)
27.market_value = full_tick[stock]['lastPrice'] * A.vol_dict[stock]
28.total_ratio += ratio * market_value
29.total_market_value += market_value
30.count += 1
31.total_ratio /= total_market_value
32.total_ratio *= 100
33.print(f'{now} 当前A股加权涨幅 {round(total_ratio, 2)}% 函数运行耗时{round(time.time()- t0, 5)}秒')
股票开户优惠费率及开通量化权限,专属经理一对一服务,咨询联系可添加微信或电话联系!
温馨提示:投资有风险,选择需谨慎。
下一篇资讯:
暂无下一篇
-
买股总怕买贵?【PB低估值】工具:帮你精准揪出“打折股”
2026-03-09 15:29
-
炒股不懂K线?用这个 AI 工具,小白选股不盲目
2026-03-09 15:29
-
2025年业绩涨28%之后,2026年的科创板还能投吗?(附开通条件)
2026-03-09 15:29


问一问

+微信
分享该文章
