策略为什么没信号?解决方案
原贴:策略为什么没信号?https://bbs.tbquant.net/thread/20251120172537747181 交易策略:开仓条件是:取每天上午9:00开盘后五分钟的高点和低点,向上突破高点做,每天只做一单(第一次突破),多单平仓条件是:持仓时间(变量1)1分钟,如果低于买入价则平仓,如果持仓时间超过1分钟且价格高于买入价,则(变量2)30分钟平仓。用在1分钟的K线上交易重新写了代码,多空开仓,一天只开一次仓。回测有信号,是否可行?//------------------------------------------------------------------------// 简称: FiveMinBreakout_V2// 名称: 5分钟突破交易策略V2版本// 类别: 日内突破策略// 类型: 用户应用// 输出: Void//------------------------------------------------------------------------Params Numeric BreakoutMinutes(5); // 突破区间分钟数 Numeric HoldTimeLong(30); // 长期持仓时间(K线数) Numeric CloseTime(0.145500); // 收盘平仓时间 Numeric Lots(1); // 交易手数 Vars // 交易日管理变量 Series<Integer> CurrentDay(0); // 当前交易日 Series<Bool> NewDayFlag(False); // 新交易日标志 // 突破区间变量 Series<Numeric> MorningHigh(0); // 上午突破区间高点 Series<Numeric> MorningLow(0); // 上午突破区间低点 Series<Bool> RangeDefined(False); // 区间已定义标志 Series<Bool> TradeDone(False); // 当日已交易标志 Series<Integer> MorningBarsCount(0); // 上午K线计数 Series<Integer> TradeDirection(0); // 交易方向: 0-无, 1-多头, -1-空头 // 交易管理变量 Series<Numeric> EntryPrice(0); // 入场价格 Series<Integer> EntryBarIndex(0); // 入场Bar索引 Series<Integer> BarsSinceEntry(0); // 入场后经过的K线数 Series<Bool> PositionActive(False); // 仓位活跃标志 Numeric i; // 循环变量Events OnBar(ArrayRef<Integer> indexs) { // ====== 1. 交易日管理 ====== if (Date != CurrentDay) { // 新交易日重置所有标志 CurrentDay = Date; NewDayFlag = True; RangeDefined = False; TradeDone = False; PositionActive = False; MorningHigh = 0; MorningLow = 0; MorningBarsCount = 0; BarsSinceEntry = 0; TradeDirection = 0; Commentary("新交易日开始: " + Text(Date)); } else { NewDayFlag = False; } // ====== 2. 定义突破区间 ====== // 重置上午K线计数 if (Time < 0.090000) { MorningBarsCount = 0; } // 在9:00到9:00+BreakoutMinutes期间计算突破区间 Numeric BreakoutEndTime = 0.090000 + (BreakoutMinutes / 10000.0); if (Time >= 0.090000 && Time <= BreakoutEndTime && !RangeDefined) { MorningBarsCount = MorningBarsCount + 1; // 更新高低点 if (MorningBarsCount == 1) { // 第一根K线,初始化高低点 MorningHigh = High; MorningLow = Low; } else { // 后续K线,更新高低点 if (High > MorningHigh) MorningHigh = High; if (Low < MorningLow) MorningLow = Low; } Commentary("第" + Text(MorningBarsCount) + "根K线 - 高点:" + Text(MorningHigh) + " 低点:" + Text(MorningLow)); // 如果是最后一根K线,标记区间已定义 if (Time >= BreakoutEndTime || MorningBarsCount >= BreakoutMinutes) { RangeDefined = True; Commentary(Text(BreakoutMinutes) + "分钟区间定义完成 - 最终高点:" + Text(MorningHigh) + " 低点:" + Text(MorningLow)); } } // 突破区间结束后如果还没有定义区间,则标记为定义 if (Time > BreakoutEndTime && !RangeDefined && MorningHigh > 0) { RangeDefined = True; Commentary("区间定义完成(超时) - 高点:" + Text(MorningHigh) + " 低点:" + Text(MorningLow)); } // ====== 3. 交易信号生成 ====== if (Time > BreakoutEndTime && RangeDefined && !TradeDone && !PositionActive) { // 向上突破做多信号 - 只有当没有交易方向或当前是向上突破时 if (High > MorningHigh && Vol > 0 && TradeDirection >= 0) { // 开多仓 Buy(Lots, Open); EntryPrice = Open; EntryBarIndex = CurrentBar; BarsSinceEntry = 0; TradeDone = True; PositionActive = True; TradeDirection = 1; // 标记为多头方向 Commentary("向上突破开多仓! 价格:" + Text(EntryPrice) + " 突破高点:" + Text(MorningHigh)); } // 向下突破做空信号 - 只有当没有交易方向或当前是向下突破时 else if (Low < MorningLow && Vol > 0 && TradeDirection <= 0) { // 开空仓 SellShort(Lots, Open); EntryPrice = Open; EntryBarIndex = CurrentBar; BarsSinceEntry = 0; TradeDone = True; PositionActive = True; TradeDirection = -1; // 标记为空头方向 Commentary("向下突破开空仓! 价格:" + Text(EntryPrice) + " 突破低点:" + Text(MorningLow)); } } // ====== 4. 更新持仓K线计数 ====== if (PositionActive) { BarsSinceEntry = CurrentBar - EntryBarIndex; } // ====== 5. 多头仓位管理 ====== if (MarketPosition == 1 && PositionActive) { // 条件1: 持仓K线数≥HoldTimeLong,平仓 if (BarsSinceEntry >= HoldTimeLong) { Sell(0, Close); // 使用收盘价平仓 PositionActive = False; Commentary("多单长期持仓平仓! 持仓K线数:" + Text(BarsSinceEntry)); } } // ====== 6. 空头仓位管理 ====== if (MarketPosition == -1 && PositionActive) { // 条件1: 持仓K线数≥HoldTimeLong,平仓 if (BarsSinceEntry >= HoldTimeLong) { BuyToCover(0, Close); // 使用收盘价平仓 PositionActive = False; Commentary("空单长期持仓平仓! 持仓K线数:" + Text(BarsSinceEntry)); } } // ====== 7. 收盘前平仓 ====== if (Time >= CloseTime && MarketPosition != 0) { if (MarketPosition == 1) { Sell(0, Open); Commentary("收盘前平多仓"); } else if (MarketPosition == -1) { BuyToCover(0, Open); Commentary("收盘前平空仓"); } PositionActive = False; TradeDone = True; } // ====== 8. 绘图和监控 ====== if (RangeDefined) { // 绘制突破区间 PlotNumeric("MorningHigh", MorningHigh); PlotNumeric("MorningLow", MorningLow); } if (PositionActive) { // 绘制入场价格线 PlotNumeric("EntryPrice", EntryPrice); PlotNumeric("BarsHeld", BarsSinceEntry); if (MarketPosition == 1) { Commentary("多单持仓中 - 入场价:" + Text(EntryPrice) + " 当前价:" + Text(Close) + " 持仓K线数:" + Text(BarsSinceEntry)); } else if (MarketPosition == -1) { Commentary("空单持仓中 - 入场价:" + Text(EntryPrice) + " 当前价:" + Text(Close) + " 持仓K线数:" + Text(BarsSinceEntry)); } } }