在 pinescript v5 中画一条线

问题描述 投票:0回答:1

我用 pine script v5 编写了一段代码,用于从看涨吞没蜡烛的开盘到右侧绘制 100 根蜡烛的水平线,但当蜡烛交叉或穿过该线时,它应该停止绘制,但它无法正常工作。谁能帮我改正我的错误吗?

//@version=5
indicator(   "_Line", overlay = true)
_White = close >= open
_Black = close <= open
_Body = (close - open)

_Eng  = _Black[2] and _White[1] and close[1] >= high[2] and _Body[1] >= 2 * _Body[2]                 

var line _Line = na 
_Level = open[1]
_Over  = ta.crossover(open, _Line.get_y2())
_Under = ta.crossunder(open, _Line.get_y2())

if _Eng
     _Line := line.new(bar_index[1], _Level, bar_index+100, _Level, width = 1)

if _Over or _Under
    line.set_x2(_Line, bar_index) 

我定义吞噬蜡烛,从吞噬蜡烛的开盘价画一条线,并期望当蜡烛在未来交叉或穿过该线时,指标停止画线,它画线非常好,但通常不会在蜡烛交叉或交叉时停止画线越过线下。

get pine-script line draw crossover
1个回答
0
投票

您需要一个标志来查看价格是否被交叉。并且只有在没有交叉的时候才更新。

var line _Line = na
var is_hit = false

_Level = open[1]
_Over  = ta.crossover(open, _Line.get_y2())
_Under = ta.crossunder(open, _Line.get_y2())

if _Eng
    _Line := line.new(bar_index[1], _Level, bar_index+100, _Level, width = 1)
    is_hit := false

is_hit := (_Over or _Under) ? true : is_hit

if (not is_hit)
    line.set_x2(_Line, bar_index)

我不确定您使用此指标的主要目标是什么,因为您正在使用

open
交叉价格,并使用
x2=bar_index+100
创建线条,因此您可能需要重新访问这些点。

© www.soinside.com 2019 - 2024. All rights reserved.