如果条穿过它,则取消绘图

Cancel plot if bar crosses it

我正在尝试用下面的代码绘制一些线,但是无法弄清楚如何让它们停止进一步绘制,如果(或一次)另一根蜡烛穿过它,因为目前它只是直接穿过它们。我也在努力解决如何提前将所有线均等地延长到 30 根柱线的问题,而不管它们在图表上的什么位置,这样我就可以为它们添加价格标签。

您会注意到它正在绘制看跌蜡烛的高点,但我也想以同样的方式绘制/不绘制看涨蜡烛的低点。

我希望能够控制这将绘制多少蜡烛,我认为这与输入函数有关?

我四处寻找是否这一切都需要通过数组或循环甚至两者来完成?但我在尝试研究它时总是碰壁。

如有任何帮助,我们将不胜感激

//@version=5
indicator('123456', overlay=true)

bars_back = input(-30, 'Bars Forward')
n = input(1, 'back')


//Lines
bear = open > close
highestHigh = if bear
    ta.highest(high, n)
bullline = line.new(bar_index - bars_back, highestHigh, bar_index, highestHigh, extend=extend.none, color=color.red)

Blake,我们需要将这些行放入一个数组中;行列表,以便我们可以对它们作为一个组执行操作。当我们将东西存储在列表中时,我们可以 运行 循环遍历列表并检查是否满足条件。示例:香蕉是否在列表中,如果是,则删除香蕉。这样我们就不需要知道列表中的项目在哪里或者列表有多长,只要有一个列表我们可以检查。所以我接受了你的意见,如果满足你的条件,我们会将它们添加到列表中。然后我们可以 运行 在列表上循环几次,看看 a) 高点是否突破了你的线,这样我们就可以停止绘制并从列表中删除,以及 b) 如果它们仍在列表中,我们会为每个更新 x2以便它们的端点在右边距对齐。我在代码中加入了注释来解释每一行在做什么。

//@version=5
indicator("My Script", overlay=true, max_lines_count=500)

bars = input(30, 'Bars Forward') // make this positive. No need to draw our lines backwards 
n = input(1, 'back')

var line[]  highs = array.new_line()  // declare an empty array to store our lines in 
var label[] lab   = array.new_label() // declare an empty array to store our labels in 

//Lines

bear = open > close // our condition 
highestHigh = ta.highest(high, n) // move this out of local scope - can be calculated each bar 

if bear     // instead of just drawing a line, we push it into an array, a list of lines so we can loop through the list and perform actions on all lines 
    array.unshift(highs, line.new(bar_index, highestHigh, bar_index + bars, highestHigh, extend=extend.none, color=color.red)) // unshift just adds the line to the top of the shopping list 
    array.unshift(lab, label.new(bar_index+bars, highestHigh, "Vol: " + str.tostring(volume, "#.00"), color=na, style=label.style_label_down, textcolor=color.red)) // push a label with displaying volume into a list

for i = (array.size(highs) > 0 ? array.size(highs)-1 : na) to 0                   
    l = line.get_price(array.get(highs,i), bar_index) 
    if high > l                                       
        line.set_x2(array.get(highs,i), bar_index)    
        array.remove(highs, i)                        
        label.delete(array.get(lab, i))               
        array.remove(lab, i)                          

for x = (array.size(highs) > 0 ? array.size(highs)-1 : na) to 0                        
    line.set_x2(array.get(highs, x), bar_index+bars)  
    label.set_x(array.get(lab,   x), bar_index+bars)   
 

干杯,祝你编码和交易好运

我想你想要这样的东西。该算法将线收集到一个数组中,并检查每个柱上数组的所有元素是否烛台穿过数组中的线,如果是,则固定当前柱上的 x2 坐标,如果不是,则扩展其 x2按当前柱线 + 偏移量坐标。 查看示例代码

//@version=5

MAX_LINES_COUNT = 100

indicator('My Script', overlay=true, max_lines_count=MAX_LINES_COUNT)

lines_forward_offset = input.int(30, 'Bars Forward', minval=0)
lines_count = input.int(10, 'Count Of Lines', minval=0, maxval=MAX_LINES_COUNT)

var fixedLines = array.new_line()
var allLines = array.new_line()

cross(value) =>
    high >= value and low <= value

// checking the number of lines drawn
if array.size(allLines) > lines_count
    removedLine = array.shift(allLines)
    if array.includes(fixedLines, removedLine)
        indexOfRemovedLine = array.indexof(fixedLines, removedLine)
        array.remove(fixedLines, indexOfRemovedLine)
    line.delete(removedLine)

// setting for all lines the index of the current bar + offset as a coordinate x2
// except for those that were fixed after crossing the candle
for currentLine in allLines
    if array.includes(fixedLines, currentLine)
        continue
        
    line.set_x2(currentLine, bar_index+lines_forward_offset)
    linePrice = line.get_y1(currentLine)
    if cross(linePrice)
        line.set_x2(currentLine, bar_index)
        array.push(fixedLines, currentLine)

bearishHigh = open > close ? high : na
if not na(bearishHigh)
    l1 = line.new(bar_index, bearishHigh, bar_index, bearishHigh, extend=extend.none, color=color.red)
    array.push(allLines, l1)