首页 > 解决方案 > 是否可以使用 line.new() 函数绘制水平线

问题描述

我从一个开源脚本中借用了代码。它绘制枢轴高/低趋势线。我还希望它从每个枢轴上绘制水平支撑/阻力,但我不确定它是如何完成的。

我尝试了一些可能性,但因为 line.new() 没有 offset 或 show_last,我无法解决我的问题。

问题/解决方案:x2 需要在枢轴之后设置,但 bar_index[-1] 是不可能的

我有一些不好的结果:(1)这条线是从枢轴后面的前一个柱子画出来的。(2) 确认枢轴点后,在 20 根柱线后绘制线。(3) 除最近的枢轴外,绘制线。

这是代码。top3 是枢轴高变量。第一个 if 语句是我写的。接近尾声的第二个 if 语句用于对角线趋势线

ltop3 = valuewhen(top3, top3, 1)
bst3 = 0
bst3 := top3 ? 1 : nz(bst3[1]) + 1
float t_angle3 = 0.0
t_angle3 := t_angle3[1]
if not na(ltop3) and not na(top3)
    line tline = na
    line hline = na
    dt = time - time[1]
    if top3
        hline := line.new(bar_index - bst3 - rb3, high[bst3 + rb3], bar_index - rb3, high[bst3 + rb3], color = color.purple, extend = extend.right)
    if ltop3 > top3
        tline := line.new(bar_index - bst3[1] - rb3, high[bst3[1] + rb3], bar_index - rb3, high[rb3], color = color.red, extend = extend.right)

链接是图像。使用上面的代码会产生 (1) 不好的结果,其中线是从前一个高点而不是枢轴绘制的

标签: pine-script

解决方案


固定的。我将 y1 和 y2 设置为枢轴高点,x1 向后偏移 25 根柱线,因此从枢轴高点开始

    if top3
        hline := line.new(bar_index[25], top3, bar_index, top3, color = color.purple, extend = extend.right)

推荐阅读