//------------------------------------------------------------------------
// 简称: TS02_HighLow
// 名称: 10天周期前期高低点策略(线条样式修正版)
// 类别: 公式应用
// 类型: 用户应用
// 输出: Void
//------------------------------------------------------------------------
Params
Numeric LookbackPeriod(10); // 回看周期,10天
Numeric InitialStop(3); // 初始止损倍数
Numeric Fund(1000); // 初始资金
Enum<String> TradeMode(["多空都做","只做多","只做空"]); // 交易模式
Numeric TimeP(0.1430); // 尾盘平仓时间
Enum<String> OvernightMode(["默认隔夜","尾盘平仓"]); // 隔夜模式
Vars
// 前期高低点相关变量
Series<Numeric> PrevHigh; // 前期高:10天内收盘价最高那天的开盘价
Series<Numeric> PrevLow; // 前期低:10天内收盘价最低那天的开盘价
// 交易状态变量
Series<Numeric> MarketPos; // 持仓状态
Numeric TradeUnit(1); // 交易手数
Series<Numeric> ATR; // ATR指标,用于止损
Series<Numeric> StopLine; // 止损线
// 绘图相关
Plot HighLowPlot; // 高低点绘图对象
Defs
// 计算指定周期内收盘价最高的K线开盘价
Numeric GetMaxCloseOpen(Numeric period) {
Numeric maxClose;
Numeric resultOpen;
Numeric i;
maxClose = Highest(Close, period); // 获取周期内最高收盘价
resultOpen = Open[0];
// TB开拓者正确的while循环语法
i = 0;
while (i < period) {
if (Close[i] == maxClose) {
resultOpen = Open[i];
break; // 找到后退出循环
}
i = i + 1;
}
return resultOpen;
}
// 计算指定周期内收盘价最低的K线开盘价
Numeric GetMinCloseOpen(Numeric period) {
Numeric minClose;
Numeric resultOpen;
Numeric i;
minClose = Lowest(Close, period); // 获取周期内最低收盘价
resultOpen = Open[0];
// 正确的循环语法
i = 0;
while (i < period) {
if (Close[i] == minClose) {
resultOpen = Open[i];
break; // 找到后退出循环
}
i = i + 1;
}
return resultOpen;
}
Events
OnReady() {
SetBackBarMaxCount(1 + 365);
}
OnInit() {
// 初始化绘图设置 - 使用正确的线条样式枚举
HighLowPlot.setOption("PrevHigh", "color", Red());
HighLowPlot.setOption("PrevHigh", "width", Enum_2Pix());
HighLowPlot.setOption("PrevHigh", "line-style", 1); // 1表示虚线
HighLowPlot.setOption("PrevLow", "color", Green());
HighLowPlot.setOption("PrevLow", "width", Enum_2Pix());
HighLowPlot.setOption("PrevLow", "line-style", 1); // 1表示虚线
}
OnBar(ArrayRef<Integer> indexs) {
// 计算ATR用于止损
Numeric TR = MAX(MAX(HIGH - LOW, ABS(CLOSE[1] - HIGH)), ABS(CLOSE[1] - LOW));
ATR = Average(TR, 14);
// 计算交易手数
TradeUnit = Max(1, IntPart(Fund / (Open / Rollover * ContractUnit * BigPointValue * 10 / 100)));
// 确保有足够的K线数据
if (CurrentBar < LookbackPeriod) {
return;
}
// 计算前期高和前期低
PrevHigh = GetMaxCloseOpen(LookbackPeriod);
PrevLow = GetMinCloseOpen(LookbackPeriod);
// 绘制高低点横线
HighLowPlot.line("PrevHigh", Date + Time, PrevHigh);
HighLowPlot.line("PrevLow", Date + Time, PrevLow);
// 交易逻辑
MarketPos = MarketPosition;
if (PrevHigh > PrevLow) {
// 多单开仓条件
if (Close > PrevLow && MarketPos != 1 && TradeMode != "只做空") {
Buy(TradeUnit, Open);
StopLine = Open - InitialStop * ATR;
PlotString("信号", "多", Low - 3 * MinMove * PriceScale, Green());
}
// 多单平仓条件
if (Close < PrevHigh && MarketPos == 1) {
Sell(0, Open);
PlotString("信号", "平多", High + 3 * MinMove * PriceScale, Red());
}
// 开盘价站上前期高,继续开多单
if (Open > PrevHigh && MarketPos != 1 && TradeMode != "只做空") {
Buy(TradeUnit, Open);
StopLine = Open - InitialStop * ATR;
PlotString("信号", "续多", Low - 3 * MinMove * PriceScale, Green());
}
// 开盘价跌破前期高,开空单
if (Open < PrevHigh && MarketPos != -1 && TradeMode != "只做多") {
SellShort(TradeUnit, Open);
StopLine = Open + InitialStop * ATR;
PlotString("信号", "空", High + 3 * MinMove * PriceScale, Red());
}
// 开盘价跌破前期低,继续开空单
if (Open < PrevLow && MarketPos != -1 && TradeMode != "只做多") {
SellShort(TradeUnit, Open);
StopLine = Open + InitialStop * ATR;
PlotString("信号", "续空", High + 3 * MinMove * PriceScale, Red());
}
}
// 止损逻辑
if (MarketPos == 1 && Low <= StopLine) {
Sell(0, Min(Open, StopLine));
PlotString("止损", "多止损", Low - 3 * MinMove * PriceScale, Yellow());
}
if (MarketPos == -1 && High >= StopLine) {
BuyToCover(0, Max(Open, StopLine));
PlotString("止损", "空止损", High + 3 * MinMove * PriceScale, Yellow());
}
// 尾盘平仓逻辑
if (OvernightMode == "尾盘平仓" && Time >= TimeP && Time <= 0.1500) {
if (MarketPos == 1) {
Sell(0, Open);
PlotString("清仓", "尾盘清多", High + 3 * MinMove * PriceScale, Cyan());
}
if (MarketPos == -1) {
BuyToCover(0, Open);
PlotString("清仓", "尾盘清空", High + 3 * MinMove * PriceScale, Cyan());
}
}
}
//------------------------------------------------------------------------
// 修正说明:
// 1. 将Enum_DashLine()替换为TB开拓者支持的数值1(表示虚线样式)
// 2. TB中线条样式使用数值表示:0=实线,1=虚线,2=点线,3=点划线等
// 3. 保持了绘图功能和策略逻辑的完整性
//------------------------------------------------------------------------
把 close 换成一个常量,因为它在盘中会一直不断的变化。具体请看视频: https://video.tbquant.net/video?id=video444