You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在三个时间周期同步绘制交易水平线与止损线?

跨时间周期同步买入/止损水平线的实现方案

是否有现成设置实现全周期线条同步?

主流交易软件(如MT4/MT5、TradingView)没有内置的一键设置能实现跨周期同步自定义水平线——这类线条默认绑定当前图表周期。你需要通过自定义指标/脚本,实现跨周期数据共享与线条绘制。


编码实现方案(以TradingView和MT5为例)

1. TradingView(Pine Script)

核心逻辑:用var变量在15秒周期存储价格,通过request.security()在更高周期拉取数据并绘制线条。

//@version=5
indicator("跨周期同步买入/止损线", overlay=true, shorttitle="MultiTimeframe Levels")

// 持久化存储15秒周期的买入价、止损价
var float buyPrice = na
var float stopLossPrice = na

// 仅在15秒周期更新价格(示例用手动触发,可替换为自动信号逻辑)
if timeframe.isseconds and timeframe.period == "15"
    // 手动触发买入价记录
    if input.bool("记录当前价为买入价", false)
        buyPrice := close
    // 手动触发止损价记录
    if input.bool("记录当前价为止损价", false)
        stopLossPrice := close

// 跨周期拉取15秒周期的价格数据
mtfBuyPrice = request.security(syminfo.tickerid, "15", buyPrice)
mtfStopLoss = request.security(syminfo.tickerid, "15", stopLossPrice)

// 绘制水平线
if not na(mtfBuyPrice)
    hline(mtfBuyPrice, "买入价", color=color.green, linestyle=hline.style_solid)
if not na(mtfStopLoss)
    hline(mtfStopLoss, "止损价", color=color.red, linestyle=hline.style_dashed)

代码说明:

  • var关键字确保变量在图表刷新时不重置,保留历史值
  • request.security()实现跨周期数据调用
  • 手动输入框用于测试,实际可替换为自动交易信号触发逻辑(如出现买入信号时,将对应价格赋值给buyPrice)

2. MT5(MQL5)

核心逻辑:通过全局变量实现跨周期数据共享,再用指标读取数据绘制线条。

第一步:15秒周期存储价格脚本
//+------------------------------------------------------------------+
//| 15秒周期存储买入/止损价脚本                                       |
//+------------------------------------------------------------------+
void OnStart()
{
    // 示例:获取当前买价作为买入价,下浮20点作为止损价
    double buyPrice = SymbolInfoDouble(_Symbol, SYMBOL_BID);
    double stopLoss = buyPrice - 20 * Point;
    
    // 存入全局变量,供其他周期读取
    GlobalVariableSet("MTF_BuyPrice", buyPrice);
    GlobalVariableSet("MTF_StopLoss", stopLoss);
    
    Print("已存储买入价: ", buyPrice, " | 止损价: ", stopLoss);
}
第二步:跨周期绘制线条指标
//+------------------------------------------------------------------+
//| 跨周期绘制买入/止损线指标                                         |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 0

void OnCalculate(const int rates_total,
                 const int prev_calculated,
                 const datetime &time[],
                 const double &open[],
                 const double &high[],
                 const double &low[],
                 const double &close[],
                 const long &tick_volume[],
                 const long &volume[],
                 const int &spread[])
{
    // 读取全局变量中的价格数据
    double buyPrice = GlobalVariableGet("MTF_BuyPrice");
    double stopLoss = GlobalVariableGet("MTF_StopLoss");
    
    // 绘制买入价水平线
    if(buyPrice != 0)
    {
        ObjectCreate(0, "MTF_BuyLine", OBJ_HLINE, 0, 0, buyPrice);
        ObjectSetInteger(0, "MTF_BuyLine", OBJPROP_COLOR, clrGreen);
        ObjectSetInteger(0, "MTF_BuyLine", OBJPROP_STYLE, STYLE_SOLID);
    }
    
    // 绘制止损价水平线
    if(stopLoss != 0)
    {
        ObjectCreate(0, "MTF_StopLossLine", OBJ_HLINE, 0, 0, stopLoss);
        ObjectSetInteger(0, "MTF_StopLossLine", OBJPROP_COLOR, clrRed);
        ObjectSetInteger(0, "MTF_StopLossLine", OBJPROP_STYLE, STYLE_DASH);
    }
}

代码说明:

  • GlobalVariableSet/Get实现跨图表、跨周期的数据共享
  • 将脚本在15秒周期运行,指标加载到所有需要显示线条的周期图表即可

通用注意事项

  • 所有平台的实现核心都是跨周期数据共享+本地线条绘制
  • 自动交易场景下,需添加信号去重逻辑,避免重复赋值
  • 部分平台支持直接在指标中指定数据源周期(如TradingView的request.security),无需额外存储变量

内容的提问来源于stack exchange,提问作者Adam B

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 06:46:08