编写的交易策略,回测时总是成交点位显示在最高点或者最低点,麻烦大佬帮看看做修正。
//------------------------------------------------------------------------
// 简称: Intraday_Breakout_Filtered
// 名称: 日内动量突破策略
// 类别: 公式应用
// 类型: 内建应用
// 输出:
//------------------------------------------------------------------------
Params
// --- 周期参数 ---
Numeric BigPeriod(60); // 大周期(分钟),用于趋势判断,如60分钟
Numeric SmallPeriod(5); // 小周期(分钟),用于交易信号,如5分钟
Numeric LookBackDays(20); // 回溯天数,用于计算日内高低点
// --- 指标参数 ---
Numeric TrendEmaPeriod(20); // 趋势EMA周期
Numeric VolAvgPeriod(20); // 成交量均线周期
Numeric VolFilterRatio(1.5); // 成交量过滤倍数
// --- 风控参数 ---
Numeric StopLossATR(1.5); // 止损ATR倍数
Numeric TakeProfitATR(3.0); // 止盈ATR倍数
Numeric Lots(1); // 交易手数
Vars
// --- 核心数据 ---
Series<Numeric> bigHigh; // 大周期最高价(前一交易日)
Series<Numeric> bigLow; // 大周期最低价(前一交易日)
Series<Numeric> bigEma; // 大周期EMA(趋势方向)
// --- 指标 ---
Series<Numeric> atrVal; // ATR波动率
Series<Numeric> avgVol; // 平均成交量
Series<Numeric> currentHigh; // 当前日内最高
Series<Numeric> currentLow; // 当前日内最低
// --- 信号 ---
Bool trendUp; // 大趋势向上
Bool trendDown; // 大趋势向下
Bool volumeConfirm; // 成交量确认
Bool breakHigh; // 突破前高(收盘价确认)
Bool breakLow; // 跌破前低(收盘价确认)
// --- 风控 ---
Numeric stopPrice;
Numeric targetPrice;
Events
OnBar(ArrayRef<Integer> indexs)
{
// ============ 1. 计算关键价位 ============
// 获取前一交易日的日内高低点(避免未来函数)
If (Date != Date[1]) // 新交易日
{
// 记录昨日最高最低作为今日关键位
bigHigh = High[1];
bigLow = Low[1];
}
// 计算大周期EMA(使用小时线数据)
// 注意:TB中获取不同周期数据较复杂,这里简化处理
// 实际中可能需要引用外部数据或使用更高时间框架
bigEma = Average(Close, TrendEmaPeriod);
// 计算当前交易日内的实时高低点
currentHigh = Highest(High, CurrentBar);
currentLow = Lowest(Low, CurrentBar);
// ============ 2. 计算指标 ============
atrVal = AvgTrueRange(20);
avgVol = Average(Vol, VolAvgPeriod);
// ============ 3. 三重过滤信号生成 ============
// 过滤1:趋势方向(使用EMA)
trendUp = (Close > bigEma);
trendDown = (Close < bigEma);
// 过滤2:成交量确认(突破时放量)
volumeConfirm = (Vol > avgVol * VolFilterRatio);
// 过滤3:价格突破确认(收盘价突破,避免盘中毛刺)
breakHigh = (Close > bigHigh) And (Close[1] <= bigHigh);
breakLow = (Close < bigLow) And (Close[1] >= bigLow);
// ============ 4. 交易逻辑 ============
// --- 平仓逻辑 ---
If (MarketPosition == 1) // 多单
{
stopPrice = AvgEntryPrice() - atrVal * StopLossATR;
targetPrice = AvgEntryPrice() + atrVal * TakeProfitATR;
If (Low <= stopPrice)
Sell(0, Min(Open, stopPrice));
Else If (High >= targetPrice)
Sell(0, Max(Open, targetPrice));
// 趋势反转平仓
Else If (Close < bigEma)
Sell(0, Open);
}
If (MarketPosition == -1) // 空单
{
stopPrice = AvgEntryPrice() + atrVal * StopLossATR;
targetPrice = AvgEntryPrice() - atrVal * TakeProfitATR;
If (High >= stopPrice)
BuyToCover(0, Max(Open, stopPrice));
Else If (Low <= targetPrice)
BuyToCover(0, Min(Open, targetPrice));
Else If (Close > bigEma)
BuyToCover(0, Open);
}
// --- 开仓逻辑(三重过滤)---
If (MarketPosition == 0)
{
// 多头开仓:趋势向上 + 放量突破前高
If (trendUp And volumeConfirm And breakHigh)
{
Buy(Lots, Open);
}
// 空头开仓:趋势向下 + 放量跌破前低
Else If (trendDown And volumeConfirm And breakLow)
{
SellShort(Lots, Open);
}
}
// ============ 5. 绘图 ============
PlotNumeric("关键高点", bigHigh);
PlotNumeric("关键低点", bigLow);
PlotNumeric("趋势线", bigEma);
}
ai写的还是问ai吧