交易策略:开仓条件是:取每天上午9:00开盘后五分钟的高点和低点,向上突破高点做,每天只做一单(第一次突破),多单平仓条件是:持仓时间(变量1)1分钟,如果低于买入价则平仓,如果持仓时间超过1分钟且价格高于买入价,则(变量2)30分钟平仓。用在1分钟的K线上交易
Params
// 交易参数
Numeric TradeVolume(1); // 交易手数
Numeric PriceOffset(2); // 价格偏移点数
// 时间参数 (基于1分钟K线数量)
Numeric RangeBars(5); // 区间计算Bar数 (9:00-9:05 = 5根1分钟K线)
Numeric QuickExitBars(1); // 快速平仓Bar数 (1分钟)
Numeric NormalExitBars(30); // 正常平仓Bar数 (30分钟)
Vars
// 基础变量
Numeric MinPoint;
Bool RangeCalculated;
Numeric DayHigh;
Numeric DayLow;
Bool TradeDoneToday;
// Bar计数变量
Numeric DayBarCount;
Numeric EntryBarCount;
Numeric MyEntryPrice;
// 信号变量
Bool BreakoutSignal;
Bool FirstBarAfterRange;
Events
OnBar(ArrayRef<Integer> indexs)
{
// ========== 基础初始化 ==========
MinPoint = MinMove * PriceScale;
// ========== Bar计数管理 ==========
DayBarCount = DayBarCount + 1;
// ========== 新交易日检测 ==========
If (Date != Date[1] Or DayBarCount > 240) // 240根Bar约为4小时交易
{
RangeCalculated = False;
TradeDoneToday = False;
DayHigh = High;
DayLow = Low;
DayBarCount = 1;
BreakoutSignal = False;
FirstBarAfterRange = False;
Commentary("========== 新交易日开始 ==========");
Commentary("日期: " + Text(Date) + ", 初始Bar计数: " + Text(DayBarCount));
}
// ========== 基于Bar计数的时间段判断 ==========
Bool IsRangePeriod = (DayBarCount >= 1 And DayBarCount <= RangeBars);
Bool IsTradingPeriod = (DayBarCount > RangeBars);
// ========== 输出关键状态信息 ==========
Commentary("Bar:" + Text(DayBarCount) +
" 区间计算:" + IIFString(IsRangePeriod, "是", "否") +
" 交易时段:" + IIFString(IsTradingPeriod, "是", "否") +
" 开盘:" + Text(Open) + " 高:" + Text(High) + " 低:" + Text(Low) + " 收:" + Text(Close));
// ========== 区间计算逻辑 ==========
If (IsRangePeriod And Not RangeCalculated)
{
// 初始化区间
If (DayBarCount == 1)
{
DayHigh = High;
DayLow = Low;
Commentary("初始化区间 - 高:" + Text(DayHigh) + " 低:" + Text(DayLow));
}
Else
{
// 更新高点
If (High > DayHigh)
{
DayHigh = High;
Commentary("更新高点: " + Text(DayHigh));
}
// 更新低点
If (Low < DayLow)
{
DayLow = Low;
Commentary("更新低点: " + Text(DayLow));
}
}
Commentary("当前区间 - 高:" + Text(DayHigh) + " 低:" + Text(DayLow));
}
// ========== 区间确定 ==========
If (DayBarCount == RangeBars + 1 And Not RangeCalculated)
{
RangeCalculated = True;
FirstBarAfterRange = True;
Commentary("========== 区间确定 ==========");
Commentary("最终区间 - 高:" + Text(DayHigh) + " 低:" + Text(DayLow));
// 绘制区间线
PlotNumeric("DayHigh", DayHigh);
PlotNumeric("DayLow", DayLow);
PlotString("RangeInfo", "区间 " + Text(DayHigh) + "/" + Text(DayLow), High, Yellow);
}
Else
{
FirstBarAfterRange = False;
}
// ========== 突破检测和交易执行 ==========
If (IsTradingPeriod And RangeCalculated And Not TradeDoneToday And MarketPosition == 0)
{
// 突破条件:收盘价突破区间高点
Bool BreakoutCondition = (Close > DayHigh);
Commentary("突破检测 - 收盘:" + Text(Close) +
" 区间高:" + Text(DayHigh) +
" 突破:" + IIFString(BreakoutCondition, "是", "否"));
If (BreakoutCondition)
{
BreakoutSignal = True;
// 计算入场价格
Numeric EntryPrice = Close + MinPoint * PriceOffset;
// 执行买入
Buy(TradeVolume, EntryPrice);
// 记录交易信息
MyEntryPrice = EntryPrice;
EntryBarCount = 0;
TradeDoneToday = True;
BreakoutSignal = False;
Commentary("========== 执行多头入场 ==========");
Commentary("入场价格: " + Text(EntryPrice));
Commentary("入场Bar: " + Text(DayBarCount));
PlotString("BuySignal", "BUY@" + Text(EntryPrice), Low, Red);
}
}
// ========== 持仓Bar计数 ==========
If (MarketPosition == 1 And BarsSinceEntry >= 0)
{
EntryBarCount = BarsSinceEntry;
// 实时盈亏计算
Numeric CurrentProfit = (Close - MyEntryPrice) / MinPoint;
Commentary("持仓状态 - Bar数:" + Text(EntryBarCount) +
" 入场价:" + Text(MyEntryPrice) +
" 当前价:" + Text(Close) +
" 盈亏:" + Text(CurrentProfit) + "点");
}
Else If (MarketPosition == 0)
{
EntryBarCount = 0;
}
// ========== 出场条件管理 ==========
If (MarketPosition == 1 And BarsSinceEntry >= 0 And Vol > 0)
{
Bool ShouldExit = False;
Numeric ExitPrice = 0;
String ExitReason = "";
// 条件1: 持仓1分钟内(1根Bar)且价格低于买入价 - 快速止损
If (EntryBarCount <= QuickExitBars And Close < MyEntryPrice)
{
ShouldExit = True;
ExitPrice = Close;
ExitReason = "快速止损(1分钟内亏损)";
}
// 条件2: 持仓超过1分钟且价格高于买入价,30分钟时平仓 - 定时止盈
Else If (EntryBarCount > QuickExitBars And
Close > MyEntryPrice And
EntryBarCount >= NormalExitBars)
{
ShouldExit = True;
ExitPrice = Close;
ExitReason = "定时止盈(30分钟)";
}
// 条件3: 额外的保护止损
Else If (Close <= MyEntryPrice - 20 * MinPoint) // 20点止损
{
ShouldExit = True;
ExitPrice = Close;
ExitReason = "保护止损";
}
// 条件4: 尾盘平仓 (下午2:55之后)
Else If (DayBarCount >= 235) // 假设9:00开始,235根Bar约为14:55
{
ShouldExit = True;
ExitPrice = Close;
ExitReason = "尾盘平仓";
}
// 执行平仓
If (ShouldExit)
{
Sell(0, ExitPrice);
Commentary("========== 多头平仓 ==========");
Commentary("平仓价格: " + Text(ExitPrice));
Commentary("持仓Bar数: " + Text(EntryBarCount));
Commentary("盈亏点数: " + Text((ExitPrice - MyEntryPrice) / MinPoint));
Commentary("平仓原因: " + ExitReason);
PlotString("ExitSignal", "EXIT@" + Text(ExitPrice), High, Blue);
}
}
// ========== 信号超时重置 ==========
// 如果到上午11:00还没有交易,重置信号状态
If (DayBarCount >= 120 And Not TradeDoneToday And BreakoutSignal) // 120根Bar约为11:00
{
BreakoutSignal = False;
Commentary("========== 重置突破信号 ==========");
Commentary("上午未触发交易,重置信号状态");
}
// ========== 强制测试逻辑 ==========
// 如果到第20根Bar还没有交易,进行强制测试
If (DayBarCount == 20 And Not TradeDoneToday And MarketPosition == 0 And RangeCalculated)
{
Commentary("========== 强制测试模式 ==========");
Commentary("当前价格: " + Text(Close) + " 区间高: " + Text(DayHigh));
// 如果价格接近区间高点,强制突破
If (Close >= DayHigh - 10 * MinPoint)
{
Numeric TestEntryPrice = Close;
Buy(TradeVolume, TestEntryPrice);
MyEntryPrice = TestEntryPrice;
EntryBarCount = 0;
TradeDoneToday = True;
Commentary("执行强制突破测试");
}
}
// ========== 状态监控输出 ==========
// 在关键时点输出状态
Bool IsKeyBar = (FirstBarAfterRange Or
BreakoutSignal Or
MarketPosition != 0 Or
Mod(DayBarCount, 30) == 0); // 每30根Bar输出一次
If (IsKeyBar)
{
Commentary("系统状态 - Bar:" + Text(DayBarCount) +
" 区间状态:" + IIFString(RangeCalculated, "已计算", "未计算") +
" 交易状态:" + IIFString(TradeDoneToday, "已交易", "未交易") +
" 信号状态:" + IIFString(BreakoutSignal, "有信号", "无信号") +
" 持仓:" + Text(MarketPosition) +
" 区间高:" + Text(DayHigh));
}
}
帮我看一下以上策略为什么没有交易信号?