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

Pinescript v5高时间框架影线区多盒子绘制问题求助

问题解决:HTF影线盒子指标优化

需求回顾

  • 每次符合条件时独立绘制盒子,不受已有盒子影响
  • upBox在出现高于其upHiLine的新高时停止延伸

修改后的完整代码

//@version=5
indicator("Intraday wicks", overlay=true, max_bars_back=200, max_boxes_count=500)

// 输入参数
tfInput = input.timeframe("15", "高时间框架(HTF)")

// 用数组存储多个盒子和线条,避免覆盖原有实例
var line[] upHiLines = array.new_line()
var line[] upLoLines = array.new_line()
var box[] upBoxes = array.new_box()
var line[] downHiLines = array.new_line()
var line[] downLoLines = array.new_line()
var box[] downBoxes = array.new_box()

// 跟踪当前活跃的upBox/线条索引
var int activeUpIdx = na
var int activeDownIdx = na

// 检测HTF新K线并获取对应数据
newTF = ta.change(time(tfInput))
htfUp = request.security(syminfo.tickerid, tfInput, close > open)
htfDown = request.security(syminfo.tickerid, tfInput, close < open)
htfHigh = request.security(syminfo.tickerid, tfInput, high)
htfClose = request.security(syminfo.tickerid, tfInput, close)
htfLow = request.security(syminfo.tickerid, tfInput, low)

// 处理阳线盒子逻辑
if newTF and htfUp
    // 创建新的阳线盒子和线条
    newUpHiLine = line.new(bar_index, htfHigh, bar_index, htfHigh, color=color.white, width=1)
    newUpLoLine = line.new(bar_index, htfClose, bar_index, htfClose, color=color.white, width=1)
    newUpBox = box.new(bar_index, htfHigh, bar_index, htfClose, border_color=na, bgcolor=color.rgb(47,93,229))
    // 加入数组存储
    array.push(upHiLines, newUpHiLine)
    array.push(upLoLines, newUpLoLine)
    array.push(upBoxes, newUpBox)
    // 更新当前活跃索引
    activeUpIdx = array.size(upBoxes) - 1

// 延伸或停止阳线盒子
if not na(activeUpIdx)
    currentUpHi = line.get_y1(array.get(upHiLines, activeUpIdx))
    // 检测是否出现高于当前upHi的新高
    if high > currentUpHi
        // 停止延伸,标记活跃索引为无效
        activeUpIdx := na
    else
        // 继续延伸线条和盒子
        line.set_x2(array.get(upHiLines, activeUpIdx), bar_index)
        line.set_x2(array.get(upLoLines, activeUpIdx), bar_index)
        box.set_right(array.get(upBoxes, activeUpIdx), bar_index)

// 处理阴线盒子逻辑
if newTF and htfDown
    // 创建新的阴线盒子和线条
    newDownHiLine = line.new(bar_index, htfClose, bar_index, htfClose, color=color.white, width=1)
    newDownLoLine = line.new(bar_index, htfLow, bar_index, htfLow, color=color.white, width=1)
    newDownBox = box.new(bar_index, htfClose, bar_index, htfLow, border_color=na, bgcolor=color.rgb(234,70,125))
    // 加入数组存储
    array.push(downHiLines, newDownHiLine)
    array.push(downLoLines, newDownLoLine)
    array.push(downBoxes, newDownBox)
    // 更新当前活跃索引
    activeDownIdx = array.size(downBoxes) - 1

// 延伸阴线盒子(可自行添加新低停止逻辑)
if not na(activeDownIdx)
    line.set_x2(array.get(downHiLines, activeDownIdx), bar_index)
    line.set_x2(array.get(downLoLines, activeDownIdx), bar_index)
    box.set_right(array.get(downBoxes, activeDownIdx), bar_index)

关键修改说明

  • 多盒子管理:使用数组存储所有盒子和线条实例,避免单个变量覆盖旧盒子,实现每次符合条件都生成独立盒子
  • 新高检测逻辑:在阳线盒子活跃期间,实时判断当前K线高点是否超过盒子上边界,触发后立即停止延伸该盒子
  • HTF数据准确性:改用request.security获取高时间框架的K线数据,避免低时间框架下的状态判断误差
  • 活跃实例跟踪:用索引变量跟踪当前需要延伸的盒子,确保仅最新符合条件的盒子在持续延伸,旧盒子保持固定

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 02:22:54