三线合一线策略模型(附带源码)
三线合一线策略是一个经典的趋势爆发策略。其核心逻辑是捕捉市场从“震荡整理”到“趋势启动”的临界点:
- 粘合阶段
:短、中、长三条均线纠缠在一起,波动率极低,表示市场能量正在积蓄。 - 发散阶段
:三条均线开始按顺序排列(多头或空头)并迅速发散,表示趋势正式爆发。 - 交易信号
:在确认经历过“粘合”且刚刚出现“有序发散”时顺势进场,止损止盈跟踪保护。
//------------------------------------------------------------------------// 简称:ThreeLine_Coincide// 名称:三线合一线趋势爆发策略// 类别:趋势跟踪// 类型:用户应用// 输出:Void// 描述:三均线粘合后发散时开仓,捕捉趋势启动点//------------------------------------------------------------------------Params// ========== 均线参数 ==========Numeric MA_Short_Period(5); // 短期均线Numeric MA_Middle_Period(10); // 中期均线Numeric MA_Long_Period(20); // 长期均线// ========== 粘合判断参数 ==========Numeric Coincide_Threshold(0.02); // 粘合阈值 (2%=三线最大偏离不超过 2%)Numeric Coincide_Bars(3); // 粘合持续最少 K 线数// ========== 发散确认参数 ==========Numeric Diverge_Threshold(0.01); // 发散阈值 (1%=确认趋势启动)// ========== 资金参数 ==========Numeric Fund(1000000); // 初始资金Numeric RiskPercent(0.2); // 单笔风险比例Numeric PriceTick(1); // 最小变动价位 (根据品种设置)Numeric PriceOffset(1); // 委托偏移跳数// ========== 止损止盈 ==========Numeric StopLoss_Tick(20); // 止损跳数Numeric TakeProfit_Tick(40); // 止盈跳数Numeric UseStopLoss(1); // 是否使用止损 (1=使用,0=不使用)Numeric UseTakeProfit(1); // 是否使用止盈 (1=使用,0=不使用)Vars// ========== 均线序列 ==========Series<Numeric> MA_Short;Series<Numeric> MA_Middle;Series<Numeric> MA_Long;// ========== 粘合判断 ==========Series<Numeric> MA_Max; // 三线最大值Series<Numeric> MA_Min; // 三线最小值Series<Numeric> MA_Avg; // 三线平均值Series<Numeric> Coincide_Rate; // 粘合度 (最大偏离/平均值)Series<Bool> IsCoinciding; // 是否处于粘合状态Series<Integer> Coincide_Count; // 粘合持续 K 线数// ========== 发散判断 ==========Series<Bool> Bull_Diverge; // 多头发散 (短>中>长)Series<Bool> Bear_Diverge; // 空头发散 (短<中<长)// ========== 交易信号 ==========Series<Bool> Long_Signal; // 做多信号Series<Bool> Short_Signal; // 做空信号Series<Bool> HasCoincided; // 是否经历过粘合// ========== 持仓记录 ==========Series<Integer> EntryBar;Series<Numeric> EntryPrice;Series<Integer> LastTradeBar;Series<Numeric> Signal_Buy;Series<Numeric> Signal_Sell;Series<Numeric> Signal_SellShort;Series<Numeric> Signal_BuyCover;// ========== 普通变量 ==========Integer Lots;String strLog;Numeric BuyPrice;Numeric SellPrice;Numeric Highest_Price; // 持仓期间最高价Numeric Lowest_Price; // 持仓期间最低价EventsOnInit(){Range[0:DataCount-1]{AddDataFlag(Enum_Data_RolloverBackWard());AddDataFlag(Enum_Data_RolloverRealPrice());AddDataFlag(Enum_Data_AutoSwapPosition());SetOrderMap2MainSymbol();}LastTradeBar = -100;Coincide_Count = 0;HasCoincided = False;Highest_Price = 0;Lowest_Price = 999999;Print("═══════════════════════════════════════");Print("📈 三线合一线策略已启动");Print("═══════════════════════════════════════");Print("均线参数:" + Text(MA_Short_Period) + "/" + Text(MA_Middle_Period) + "/" + Text(MA_Long_Period));Print("粘合阈值:" + Text(Coincide_Threshold * 100, 1) + "%");Print("粘合最少 K 线:" + Text(Coincide_Bars));Print("止损:" + IIFString(UseStopLoss==1, Text(StopLoss_Tick) + "跳", "关闭"));Print("止盈:" + IIFString(UseTakeProfit==1, Text(TakeProfit_Tick) + "跳", "关闭"));Print("═══════════════════════════════════════");}OnBar(ArrayRef<Integer> indexs){Range[0:DataCount-1]{// ========== 1. 计算三条均线 ==========MA_Short = Average(Close, MA_Short_Period);MA_Middle = Average(Close, MA_Middle_Period);MA_Long = Average(Close, MA_Long_Period);// ========== 2. 计算粘合度 ==========// 找出三线中的最大值和最小值MA_Max = Max(MA_Short, Max(MA_Middle, MA_Long));MA_Min = Min(MA_Short, Min(MA_Middle, MA_Long));MA_Avg = (MA_Short + MA_Middle + MA_Long) / 3;// 粘合度 = (最大值 - 最小值) / 平均值If(MA_Avg > 0){Coincide_Rate = (MA_Max - MA_Min) / MA_Avg;}Else{Coincide_Rate = 0;}// 判断是否粘合IsCoinciding = Coincide_Rate <= Coincide_Threshold;// 统计粘合持续 K 线数If(IsCoinciding){Coincide_Count = Coincide_Count + 1;HasCoincided = True;}Else{Coincide_Count = 0;}// ========== 3. 判断发散状态 ==========// 多头排列:短>中>长Bull_Diverge = MA_Short > MA_Middle And MA_Middle > MA_Long;// 空头排列:短<中<长Bear_Diverge = MA_Short < MA_Middle And MA_Middle < MA_Long;// ========== 4. 生成交易信号 ==========// 核心逻辑:必须经历过粘合 + 当前形成有序排列 + 粘合持续时间达标Long_Signal = Bull_Diverge And HasCoincided And Coincide_Count >= Coincide_Bars;Short_Signal = Bear_Diverge And HasCoincided And Coincide_Count >= Coincide_Bars;// ========== 5. 初始化变量 ==========Signal_Buy = InvalidNumeric();Signal_Sell = InvalidNumeric();Signal_SellShort = InvalidNumeric();Signal_BuyCover = InvalidNumeric();// ========== 6. 更新持仓极值 (用于移动止盈) ==========If(MarketPosition != 0){If(High > Highest_Price) Highest_Price = High;If(Low < Lowest_Price) Lowest_Price = Low;}Else{// 无持仓时重置极值Highest_Price = 0;Lowest_Price = 999999;}// ========== 7. 防重复下单 ==========If(CurrentBar - LastTradeBar < 1){// 间隔太短,跳过}Else{// ========== 计算委托价格 ==========BuyPrice = High + PriceOffset * PriceTick;SellPrice = Low - PriceOffset * PriceTick;// ========== 资金管理 ==========Lots = Max(1, IntPart(Fund * RiskPercent / (Close * ContractUnit * BigPointValue)));// ========== 交易逻辑 ==========// --- 开多条件 ---If(Long_Signal And MarketPosition <= 0){Buy(Lots, BuyPrice);EntryBar = CurrentBar;EntryPrice = BuyPrice;LastTradeBar = CurrentBar;Signal_Buy = Low;Highest_Price = High;Lowest_Price = Low;strLog = "🟢 三线合一线开多 | 粘合:" + Text(Coincide_Count) + "K 线 | 粘合度:" + Text(Coincide_Rate * 100, 2) + "%";Print(strLog);}// --- 开空条件 ---If(Short_Signal And MarketPosition >= 0){SellShort(Lots, SellPrice);EntryBar = CurrentBar;EntryPrice = SellPrice;LastTradeBar = CurrentBar;Signal_SellShort = High;Highest_Price = High;Lowest_Price = Low;strLog = "🔴 三线合一线开空 | 粘合:" + Text(Coincide_Count) + "K 线 | 粘合度:" + Text(Coincide_Rate * 100, 2) + "%";Print(strLog);}// --- 平多条件 ---If(MarketPosition > 0){Bool Close_Long = False;String Close_Reason = "";// 条件 1: 均线死叉 (短期下穿中期) - 趋势反转If(CrossUnder(MA_Short, MA_Middle)){Close_Long = True;Close_Reason = "均线死叉";}// 条件 2: 固定止损If(UseStopLoss == 1 And Close - EntryPrice < -StopLoss_Tick * PriceTick){Close_Long = True;Close_Reason = "止损";}// 条件 3: 固定止盈If(UseTakeProfit == 1 And Close - EntryPrice > TakeProfit_Tick * PriceTick){Close_Long = True;Close_Reason = "止盈";}// 条件 4: 回撤止盈 (从最高点回撤一定幅度)If(UseTakeProfit == 1 And Highest_Price - Close > TakeProfit_Tick * PriceTick * 0.5){Close_Long = True;Close_Reason = "回撤止盈";}If(Close_Long){Sell(0, SellPrice);LastTradeBar = CurrentBar;Signal_Sell = High;Print("🔴 平多 | 原因:" + Close_Reason);}}// --- 平空条件 ---If(MarketPosition < 0){Bool Close_Short = False;String Close_Reason = "";// 条件 1: 均线金叉 (短期上穿中期) - 趋势反转If(CrossOver(MA_Short, MA_Middle)){Close_Short = True;Close_Reason = "均线金叉";}// 条件 2: 固定止损If(UseStopLoss == 1 And EntryPrice - Close < -StopLoss_Tick * PriceTick){Close_Short = True;Close_Reason = "止损";}// 条件 3: 固定止盈If(UseTakeProfit == 1 And EntryPrice - Close > TakeProfit_Tick * PriceTick){Close_Short = True;Close_Reason = "止盈";}// 条件 4: 回撤止盈If(UseTakeProfit == 1 And Close - Lowest_Price > TakeProfit_Tick * PriceTick * 0.5){Close_Short = True;Close_Reason = "回撤止盈";}If(Close_Short){BuyToCover(0, BuyPrice);LastTradeBar = CurrentBar;Signal_BuyCover = Low;Print("🟢 平空 | 原因:" + Close_Reason);}}}// ========== 8. 绘图 ==========// 主图:三条均线PlotNumeric("MA_Short", MA_Short);PlotNumeric("MA_Middle", MA_Middle);PlotNumeric("MA_Long", MA_Long);// 信号标记If(Signal_Buy != InvalidNumeric()){PlotNumeric("Signal_Buy", Signal_Buy * 0.995);}If(Signal_Sell != InvalidNumeric()){PlotNumeric("Signal_Sell", Signal_Sell * 1.005);}If(Signal_SellShort != InvalidNumeric()){PlotNumeric("Signal_SellShort", Signal_SellShort * 1.005);}If(Signal_BuyCover != InvalidNumeric()){PlotNumeric("Signal_BuyCover", Signal_BuyCover * 0.995);}// 副图 1: 粘合度曲线PlotNumeric("Coincide_Rate", Coincide_Rate * 100);PlotNumeric("Coincide_Threshold", Coincide_Threshold * 100);// 状态显示Commentary("📈 三线合一 | 粘合度:" + Text(Coincide_Rate * 100, 2) + "% | 粘合 K 线:" + Text(Coincide_Count) +" | 排列:" + IIFString(Bull_Diverge, "多头", IIFString(Bear_Diverge, "空头", "混乱")) +" | 持仓:" + Text(MarketPosition) +" | 信号:" + IIFString(Long_Signal, "🟢多", IIFString(Short_Signal, "🔴空", "⏸️")));}}//------------------------------------------------------------------------// 编译版本 2026/4/3 223106// 版权所有 ZYF1836612// 更改声明 TradeBlazer Software保留对TradeBlazer平台// 每一版本的TradeBlazer策略修改和重写的权利//------------------------------------------------------------------------
📊 策略核心逻辑说明
|
|
|
|
|---|---|---|
| 粘合期 |
|
|
| 发散期 |
|
|
| 持有期 |
|
|
| 出场期 |
|
|
⚙️ 关键参数设置指南
|
|
|
|
|
|---|---|---|---|
MA_Short/Middle/Long |
|
|
|
Coincide_Threshold |
|
|
|
Coincide_Bars |
|
|
|
StopLoss_Tick |
|
|
必须
|
TakeProfit_Tick |
|
|
|
📋 PriceTick (最小变动价位) 参考表
注意:PriceTick 参数必须根据您交易的品种手动设置,否则止损止盈计算将错误。
|
|
|
|
|
|
|
|---|---|---|---|---|---|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
💡 使用建议
- 观察副图
:加载策略后,副图会显示粘合度曲线。当曲线长时间在阈值线(虚线)下方运行后突然向上突破,且主图均线呈现有序排列时,是最佳入场点。 - 参数优化
:不同品种的“股性”不同。建议在实盘前,使用该品种过去 1-2 年的数据进行回测,微调 Coincide_Threshold和Coincide_Bars以找到最佳平衡点。 - 适用行情
:该策略专为**“震荡后的突破”**设计。在单边大趋势中表现优异,但在无序震荡市中可能会产生少量磨损(通过粘合天数过滤已大幅降低此风险)。 - 资金管理
:建议 RiskPercent设置为 0.1 (10%) 到 0.2 (20%),切勿过大,以防连续止损造成大幅回撤。
夜雨聆风