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

如何在15分钟K线图中引用1分钟价格数据设置止损

在15分钟K线脚本中引用1分钟数据实现动态止损

你当前用strategy.exit的问题在于,15分钟框架的脚本默认仅在每根15分钟K线闭合时执行计算,没法实时响应1分钟级别的价格变动。要解决这个问题,核心是用Pine Script的request.security()函数跨时间框架获取1分钟数据,让脚本能跟踪1分钟级别的价格变化来触发止损。

具体实现步骤:

  1. 获取1分钟框架的实时价格
    用request.security()拉取当前标的的1分钟收盘价(或最低价,根据你的止损逻辑选择):

    // 获取1分钟最新收盘价
    minuteClose = request.security(syminfo.tickerid, "1", close)
    

    参数说明:

    • syminfo.tickerid:指定当前交易标的
    • "1":目标时间框架,这里代表1分钟
    • close:要获取的价格字段(也可以换成low跟踪1分钟最低价)
  2. 跟踪持仓后的1分钟价格波动
    如果你的止损逻辑需要参考持仓期间的1分钟最低价,需要用变量持续更新:

    // 初始化变量,用var避免每次K线重置
    var float entryPrice = na
    var float lowestMinutePrice = na
    
    // 开仓时记录入场价并初始化1分钟最低价
    if (strategy.entry("Buy", strategy.long))
        entryPrice := close
        lowestMinutePrice := close
    
    // 持仓期间持续更新1分钟最低价
    if (strategy.position_size > 0)
        lowestMinutePrice := math.min(lowestMinutePrice, minuteClose)
    
  3. 基于1分钟价格触发止损
    替换原来固定的strategy.exit,改用实时1分钟价格判断止损条件:

    // 假设lossAmount是你设定的止损幅度
    stopPrice = entryPrice - lossAmount
    
    // 当1分钟价格跌破止损价时立刻平仓
    if (strategy.position_size > 0 and minuteClose < stopPrice)
        strategy.close("Buy", comment="Stop Loss Long")
    

关键说明:

  • request.security()会在每根1分钟K线更新时把数据同步到15分钟框架的脚本中,这样你的止损逻辑就能实时响应1分钟级别的价格变动,不用等15分钟K线闭合。
  • 如果需要更精准的止损(比如跟踪1分钟内的瞬时低点),可以把close换成low,获取1分钟K线的最低价。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.08 13:02:16