//------------------------------------------------------------------------
// 名称: 量大入场,量小出场-正向开仓策略(TBQuant3版)
//------------------------------------------------------------------------
Params
Integer volLen(20); // 成交量均线周期
Numeric highVolMult(2.0); // 高量阈值倍数(入场)
Numeric lowVolMult(0.5); // 低量阈值倍数(平仓)
Integer atrLen(14); // ATR周期
Numeric slMult(1.0); // 止损倍数
Numeric tpMult(2.5); // 止盈倍数
Numeric qty(1); // 手数
Vars
Numeric volAvg, atrValue;
Numeric longStop, longTP, shortStop, shortTP;
Bool highVolCondition, lowVolCondition;
Bool signalLong, signalShort;
//-------------------------------
// 1. 成交量计算
//-------------------------------
volAvg = Average(Volume, volLen);
highVolCondition = Volume >= volAvg * highVolMult;
lowVolCondition = Volume <= volAvg * lowVolMult;
//-------------------------------
// 2. ATR止盈止损计算
//-------------------------------
atrValue = AvgTrueRange(atrLen);
longStop = Close - atrValue * slMult;
longTP = Close + atrValue * tpMult;
shortStop = Close + atrValue * slMult;
shortTP = Close - atrValue * tpMult;
//-------------------------------
// 3. 信号定义
//-------------------------------
signalLong = Close > Average(Close, volLen) and highVolCondition;
signalShort = Close < Average(Close, volLen) and highVolCondition;
//-------------------------------
// 4. 平仓逻辑
//-------------------------------
if lowVolCondition then
begin
if MarketPosition = 1 then Sell("ExitLong") Next Bar at Market;
if MarketPosition = -1 then BuyToCover("ExitShort") Next Bar at Market;
end;
//-------------------------------
// 5. 开仓逻辑(禁止双向)
//-------------------------------
if MarketPosition <= 0 and signalLong then
begin
if MarketPosition = -1 then BuyToCover("CloseShort") Next Bar at Market;
Buy("OpenLong") qty shares Next Bar at Market;
end;
if MarketPosition >= 0 and signalShort then
begin
if MarketPosition = 1 then Sell("CloseLong") Next Bar at Market;
SellShort("OpenShort") qty shares Next Bar at Market;
end;
//-------------------------------
// 6. 止盈止损(动态判断)
//-------------------------------
if MarketPosition = 1 then
begin
if Low <= longStop then Sell("LongStop") Next Bar at Market;
if High >= longTP then Sell("LongTP") Next Bar at Market;
end;
if MarketPosition = -1 then
begin
if High >= shortStop then BuyToCover("ShortStop") Next Bar at Market;
if Low <= shortTP then BuyToCover("ShortTP") Next Bar at Market;
end;
| 模块 | 逻辑说明 |
|---|---|
| 成交量条件 | 当前成交量 ≥ 均量 × 高倍数 → 触发入场; ≤ 均量 × 低倍数 → 触发出场 |
| 趋势方向 | close > 均线 → 多头; close < 均线 → 空头 |
| 止盈止损 | 使用 ATR 动态止盈止损,分别为 2.5×ATR、1×ATR |
| 仓位控制 | 禁止双向开仓,开仓前自动平掉反向仓 |
| 开多条件 | 成交量高 + 收盘价 > 均线 |
| 开空条件 | 成交量高 + 收盘价 < 均线 |
| 平仓条件 | 成交量低(多空一致) |
| 参数 | 默认值 | 作用 |
|---|---|---|
volLen | 20 | 成交量均线周期 |
highVolMult | 2.0 | 高成交量阈值倍数 |
lowVolMult | 0.5 | 低成交量阈值倍数 |
atrLen | 14 | ATR周期 |
slMult | 1.0 | 止损倍数 |
tpMult | 2.5 | 止盈倍数 |
//------------------------------------------------------------------------
// 简称: TB_Vol
// 名称: 量大入场,量小出场-正向开仓策略
// 类别: 策略应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
Integer volLen(20); // 成交量均线周期
Numeric highVolMult(2.0); // 高量阈值倍数(入场)
Numeric lowVolMult(0.5); // 低量阈值倍数(平仓)
Integer atrLen(14); // ATR周期
Numeric slMult(1.0); // 止损倍数
Numeric tpMult(2.5); // 止盈倍数
Numeric qty(1); // 手数
Vars
Numeric volAvg;
Numeric atrValue;
Numeric longStop;
Numeric longTP;
Numeric shortStop;
Numeric shortTP;
Bool highVolCondition;
Bool lowVolCondition;
Bool signalLong;
Bool signalShort;
Defs
//此处添加公式函数
Events
//此处实现事件函数
OnInit()
//设置周期
{
}
//Bar更新事件函数,参数indexs表示变化的数据源图层ID数组
OnBar(ArrayRef<Integer>indexs)
{
//-------------------------------
// 1. 成交量计算
//-------------------------------
volAvg = Average(Vol, volLen);
highVolCondition = Vol >= volAvg * highVolMult;
lowVolCondition = Vol <= volAvg * lowVolMult;
//-------------------------------
// 2. ATR止盈止损计算
//-------------------------------
atrValue = AvgTrueRange(atrLen);
longStop = Close - atrValue * slMult;
longTP = Close + atrValue * tpMult;
shortStop = Close + atrValue * slMult;
shortTP = Close - atrValue * tpMult;
//-------------------------------
// 3. 信号定义
//-------------------------------
signalLong = Close > Average(Close, volLen) and highVolCondition;
signalShort = Close < Average(Close, volLen) and highVolCondition;
//-------------------------------
// 4. 平仓逻辑
//-------------------------------
If(lowVolCondition)
{
if(MarketPosition == 1 )
{
Sell(0, Open);
}
Else
if (MarketPosition == -1 )
{
BuyToCover(0, Open) ;
}
}
//-------------------------------
// 5. 开仓逻辑(禁止双向)
//-------------------------------
if (MarketPosition <= 0 and signalLong )
{
Buy(qty, Open);
}
Else
if (MarketPosition >= 0 and signalShort )
{
SellShort(qty, Open);
}
//-------------------------------
// 6. 止盈止损(动态判断)
//-------------------------------
if (MarketPosition == 1 And ((Low <= longStop)OR(High>=longTP)))
{
Sell(0, Open);
}
if (MarketPosition == -1 And ((High >= shortStop)OR (Low <= shortTP)))
{
BuyToCover(0,Open);
}
}
//------------------------------------------------------------------------
// 编译版本 2025/10/17 1396/1762/277
// 版权所有 jswxyfx
// 更改声明 TradeBlazer Software保留对TradeBlazer平台
// 每一版本的TradeBlazer策略修改和重写的权利
//------------------------------------------------------------------------
编译通过了,不知道是不是你所要的结果。
AI代码不考虑修改
王老师好,如可删除自己的贴子和评论