3 分钟快速生成代码
输入想法,AI 即刻生成可运行代码
在日内交易中,单纯依赖价格突破容易产生假突破信号。结合成交量分布(Volume Profile / Auction Market Theory),可以精准识别市场筹码分布的核心区域——价值区(Value Area, VA)、高筹码集中区(POC, Point of Control) 以及 低筹码区(LVN, Low Volume Node)。
当价格放量突破价值区上界(VAH)时,代表多头力量主导且获得了成交量的确认,是极佳的日内顺势买入信号。
以下策略基于国信 QMT 原生 Python API 接口编写:
#encoding:gbk
import numpy as np
import pandas as pd
def init(ContextInfo):
# 设定标的与基础参数
ContextInfo.stock = ContextInfo.stockcode + '.' + ContextInfo.market
ContextInfo.set_universe([ContextInfo.stock])
ContextInfo.account = '6000000248' # 替换为您的资金账号
ContextInfo.set_account(ContextInfo.account)
# 策略参数
ContextInfo.trade_vol = 1000 # 单笔交易股数
ContextInfo.va_ratio = 0.70 # 价值区覆盖成交量比例 (70%)
ContextInfo.has_traded = False # 当日交易标记
def handlebar(ContextInfo):
if not ContextInfo.is_last_bar():
return
# 获取当前 K 线时间
timetag = ContextInfo.get_bar_timetag(ContextInfo.barpos)
# 使用 QMT 转换时间格式
time_str = timetag_to_datetime(timetag, '%H%M%S')
int_time = int(time_str)
# 每天开盘重置交易标记
if int_time == 93100:
ContextInfo.has_traded = False
# 1. 获取当日 1 分钟历史 K 线数据,用于计算 Volume Profile
data = ContextInfo.get_market_data_ex(
fields=['close', 'volume'],
stock_code=[ContextInfo.stock],
period='1m',
count=120,
subscribe=False
)
if ContextInfo.stock not in data or data[ContextInfo.stock].empty:
return
df = data[ContextInfo.stock]
if len(df) < 30:
return
closes = df['close'].values
volumes = df['volume'].values
current_price = closes[-1]
# 2. 构建成交量分布 (Volume Profile)
bins = 20 # 将价格区间划分为 20 个 Bin
counts, bin_edges = np.histogram(closes, bins=bins, weights=volumes)
# 找到 POC ( Point of Control )
max_idx = np.argmax(counts)
poc_price = (bin_edges[max_idx] + bin_edges[max_idx+1]) / 2.0
# 计算价值区 VAH 与 VAL (覆盖 70% 成交量)
total_vol = np.sum(counts)
target_vol = total_vol * ContextInfo.va_ratio
sorted_indices = np.argsort(counts)[::-1]
cum_vol = 0
va_bins = []
for idx in sorted_indices:
cum_vol += counts[idx]
va_bins.append(idx)
if cum_vol >= target_vol:
break
min_bin_idx = min(va_bins)
max_bin_idx = max(va_bins)
val = bin_edges[min_bin_idx]
vah = bin_edges[max_bin_idx + 1]
# 3. 交易逻辑判断
# 日内 09:45 至 14:50 期间寻求突破机会
if 94500 <= int_time < 14500 and not ContextInfo.has_traded:
# 向上突破 VAH 且价格高于 POC
if current_price > vah and current_price > poc_price:
print(f"[突破信号] 当前价: {current_price} 突破 VAH: {vah:.2f},买入多头")
passorder(23, 1101, ContextInfo.account, ContextInfo.stock, 5, -1, ContextInfo.trade_vol, 'VolumeProfileBreak', 1, 'VP_BUY', ContextInfo)
ContextInfo.has_traded = True
# 4. 日内风控与尾盘平仓 (14:55 强平防护)
if int_time >= 145500:
pos_list = get_trade_detail_data(ContextInfo.account, 'STOCK', 'POSITION')
for pos in pos_list:
if pos.m_strInstrumentID == ContextInfo.stockcode and pos.m_nCanUseVolume > 0:
print(f"[尾盘平仓] 时间: {int_time},卖出平仓")
passorder(24, 1101, ContextInfo.account, ContextInfo.stock, 5, -1, pos.m_nCanUseVolume, 'VP_Close', 1, 'VP_SELL', ContextInfo)
ContextInfo.get_market_data_ex):get_market_data_ex 能高效获取多维度的历史分钟数据,并提取价格与成交量数组,用于构建直方图。numpy.histogram 以成交量为权重计算指定价格区间内的累积成交量,精准定位筹码密集区 POC 及价值区(VAH/VAL)。passorder):passorder 进行智能委托,quickTrade 参数设置为 1 可在实时行情触发信号时立即下达指令。