乐于分享
好东西不私藏

三线合一线策略模型(附带源码)

三线合一线策略模型(附带源码)

三线合一线策略是一个经典的趋势爆发策略。其核心逻辑是捕捉市场从“震荡整理”到“趋势启动”的临界点:

  1. 粘合阶段
    :短、中、长三条均线纠缠在一起,波动率极低,表示市场能量正在积蓄。
  2. 发散阶段
    :三条均线开始按顺序排列(多头或空头)并迅速发散,表示趋势正式爆发。
  3. 交易信号
    :在确认经历过“粘合”且刚刚出现“有序发散”时顺势进场,止损止盈跟踪保护。
//------------------------------------------------------------------------// 简称: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<NumericMA_Short;    Series<NumericMA_Middle;    Series<NumericMA_Long;    // ========== 粘合判断 ==========    Series<NumericMA_Max;           // 三线最大值    Series<NumericMA_Min;           // 三线最小值    Series<NumericMA_Avg;           // 三线平均值    Series<NumericCoincide_Rate;    // 粘合度 (最大偏离/平均值)    Series<BoolIsCoinciding;        // 是否处于粘合状态    Series<IntegerCoincide_Count;   // 粘合持续 K 线数    // ========== 发散判断 ==========    Series<BoolBull_Diverge;        // 多头发散 (短>中>长)    Series<BoolBear_Diverge;        // 空头发散 (短<中<长)    // ========== 交易信号 ==========    Series<BoolLong_Signal;         // 做多信号    Series<BoolShort_Signal;        // 做空信号    Series<BoolHasCoincided;        // 是否经历过粘合    // ========== 持仓记录 ==========    Series<IntegerEntryBar;    Series<NumericEntryPrice;    Series<IntegerLastTradeBar;    Series<NumericSignal_Buy;    Series<NumericSignal_Sell;    Series<NumericSignal_SellShort;    Series<NumericSignal_BuyCover;    // ========== 普通变量 ==========    Integer Lots;    String strLog;    Numeric BuyPrice;    Numeric SellPrice;    Numeric Highest_Price;            // 持仓期间最高价    Numeric Lowest_Price;             // 持仓期间最低价Events    OnInit()    {        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 * 1001+ "%");        Print("粘合最少 K 线:" + Text(Coincide_Bars));        Print("止损:" + IIFString(UseStopLoss==1Text(StopLoss_Tick+ "跳""关闭"));        Print("止盈:" + IIFString(UseTakeProfit==1Text(TakeProfit_Tick+ "跳""关闭"));        Print("═══════════════════════════════════════");    }    OnBar(ArrayRef<Integer> indexs)    {        Range[0:DataCount-1]        {            // ========== 1. 计算三条均线 ==========            MA_Short = Average(CloseMA_Short_Period);            MA_Middle = Average(CloseMA_Middle_Period);            MA_Long = Average(CloseMA_Long_Period);            // ========== 2. 计算粘合度 ==========            // 找出三线中的最大值和最小值            MA_Max = Max(MA_ShortMax(MA_MiddleMA_Long));            MA_Min = Min(MA_ShortMin(MA_MiddleMA_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_PriceHighest_Price = High;                If(Low < Lowest_PriceLowest_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(1IntPart(Fund * RiskPercent / (Close * ContractUnit * BigPointValue)));                // ========== 交易逻辑 ==========                // --- 开多条件 ---                If(Long_Signal And MarketPosition <= 0)                {                    Buy(LotsBuyPrice);                    EntryBar = CurrentBar;                    EntryPrice = BuyPrice;                    LastTradeBar = CurrentBar;                    Signal_Buy = Low;                    Highest_Price = High;                    Lowest_Price = Low;                    strLog = "🟢 三线合一线开多 | 粘合:" + Text(Coincide_Count+ "K 线 | 粘合度:" + Text(Coincide_Rate * 1002+ "%";                    Print(strLog);                }                // --- 开空条件 ---                If(Short_Signal And MarketPosition >= 0)                {                    SellShort(LotsSellPrice);                    EntryBar = CurrentBar;                    EntryPrice = SellPrice;                    LastTradeBar = CurrentBar;                    Signal_SellShort = High;                    Highest_Price = High;                    Lowest_Price = Low;                    strLog = "🔴 三线合一线开空 | 粘合:" + Text(Coincide_Count+ "K 线 | 粘合度:" + Text(Coincide_Rate * 1002+ "%";                    Print(strLog);                }                // --- 平多条件 ---                If(MarketPosition > 0)                {                    Bool Close_Long = False;                    String Close_Reason = "";                    // 条件 1: 均线死叉 (短期下穿中期) - 趋势反转                    If(CrossUnder(MA_ShortMA_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(0SellPrice);                        LastTradeBar = CurrentBar;                        Signal_Sell = High;                        Print("🔴 平多 | 原因:" + Close_Reason);                    }                }                // --- 平空条件 ---                If(MarketPosition < 0)                {                    Bool Close_Short = False;                    String Close_Reason = "";                    // 条件 1: 均线金叉 (短期上穿中期) - 趋势反转                    If(CrossOver(MA_ShortMA_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(0BuyPrice);                        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 * 1002+ "% | 粘合 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策略修改和重写的权利//------------------------------------------------------------------------

📊 策略核心逻辑说明

阶段
特征
策略行为
粘合期
三线纠缠,粘合度≤2%
观望,统计粘合 K 线数,不操作
发散期
三线有序排列,粘合度上升
顺势开仓(多头发散做多,空头发散做空)
持有期
均线保持排列
持仓,跟踪最高/最低价用于移动止盈
出场期
短期下穿中期/触发止损止盈
平仓离场

⚙️ 关键参数设置指南

参数
默认值
说明
调整建议
MA_Short/Middle/Long
5/10/20
三条均线周期
短线交易可改为 3/7/15;长线可改为 10/20/60
Coincide_Threshold
0.02
粘合阈值 (2%)
震荡市可调大到 3% 以过滤更多噪音;趋势市可调小
Coincide_Bars
3
最少粘合 K 线数
增加此值可减少假突破,但会减少交易次数
StopLoss_Tick
20
止损跳数
必须

根据品种波动率调整(参考下表)
TakeProfit_Tick
40
止盈跳数
建议设为止损的 1.5~2 倍

📋 PriceTick (最小变动价位) 参考表

注意PriceTick 参数必须根据您交易的品种手动设置,否则止损止盈计算将错误。

品种
代码
PriceTick
品种
代码
PriceTick
螺纹钢
rb
1
沪铜
cu
10
热卷
hc
1
黄金
au
0.02
铁矿石
i
0.5
白银
ag
1
焦炭
j
0.5
股指
IF/IC
0.2
豆粕
m
1
原油
sc
0.1
棕榈油
p
2
甲醇
MA
1

💡 使用建议

  1. 观察副图
    :加载策略后,副图会显示粘合度曲线。当曲线长时间在阈值线(虚线)下方运行后突然向上突破,且主图均线呈现有序排列时,是最佳入场点。
  2. 参数优化
    :不同品种的“股性”不同。建议在实盘前,使用该品种过去 1-2 年的数据进行回测,微调 Coincide_Threshold 和 Coincide_Bars 以找到最佳平衡点。
  3. 适用行情
    :该策略专为**“震荡后的突破”**设计。在单边大趋势中表现优异,但在无序震荡市中可能会产生少量磨损(通过粘合天数过滤已大幅降低此风险)。
  4. 资金管理
    :建议 RiskPercent 设置为 0.1 (10%) 到 0.2 (20%),切勿过大,以防连续止损造成大幅回撤。