仅显示 3 行并删除旧行

Displaying only 3 lines and deleting old ones

该脚本运行良好,但就性能而言,它不会删除过去的线条,这意味着当屏幕上出现大量新蜡烛时,它可能会崩溃。这就是我要解决的问题。

如何让它只显示3行并删除旧的?类似于下面的内容,但我不知道如何完成它。我知道我必须用数组来做,但不知道怎么做。

numberOfLines = 3

var label[] lbls = array.new_label()
var line[] lns = array.new_line()

if array.size(lns) > 0
    for i = 0 to array.size(lns) - 1
        if i > numberOfLines
            line.delete(array.remove(lns, i))

原代码

//@version=5
indicator("RSI Market Structure display only 10", overlay = true, max_bars_back = 500, max_lines_count = 500, max_labels_count = 500)

// ———————————————————— Constants {
// ————— Colors
var color GREEN         = color.green
var color RED           = color.red
var color BLUE          = color.blue
var color YELLOW        = color.yellow

// ————— Constants used in inputs
var string SHOW = "Show"
var string HIDE = "Hide"
var string SHOW_TODAY_ONLY = "Show Today Only"

// ———————————————————— Inputs {
// ————— Market Structure
var GRP1 = "Market Structure"
bool zigZagStructureInput = input.string(SHOW, "Show?", inline = "10", options = [SHOW, HIDE], group = GRP1) == SHOW
int zigZagLengthInput = input.int(7, "Length", inline = "10", group = GRP1)
color zigZagBullishColorInput = input.color(GREEN, "", inline = "11", group = GRP1)
color zigZagBearishColorInput = input.color(RED, "", inline = "11", group = GRP1)
color zigZagColorInput = input.color(BLUE, "Zig Zag", inline = "11", group = GRP1)
// }

// ———————————————————— Market Structure {
// RSI value based on inbuilt RSI
rsiValue = ta.rsi(close, zigZagLengthInput)

// Get the current state
isOverbought = rsiValue >= 80 // overbought level
isOversold = rsiValue <= 20 // oversold level

// State of the last extreme 0 for initialization, 1 = overbought, 2 = oversold
var int lastState = na

// Highest and Lowest prices since the last state change
var hh = low
var ll = high

// Labels
var label labelll = na
var label labelhh = na

// Swing lines
var line line_up = na
var line line_down = na

// We go from overbought straight to oversold  NEW DRAWINGS CREATED HERE
if zigZagStructureInput
    if lastState == 1 and isOversold
        ll := low
        labelll := label.new(bar_index, low, style = label.style_circle, color = zigZagBullishColorInput, size = size.tiny)
        labelhh_low = label.get_x(labelhh)
        labelhh_pos = label.get_y(labelhh)
        line_down := line.new(bar_index, high, labelhh_low, labelhh_pos, color = zigZagColorInput, width = 2)
    
    // We go from oversold straight to overbought NEW DRAWINGS CREATED HERE
    if lastState == 2 and isOverbought
        hh := high
        labelhh := label.new(bar_index, high, style = label.style_circle, color = zigZagBearishColorInput, size = size.tiny)
        labelll_low = label.get_x(labelll)
        labelll_pos = label.get_y(labelll)
        line_up := line.new(bar_index, high, labelll_low, labelll_pos, color = zigZagColorInput, width = 2)
        
    // If we are overbought
    if isOverbought
        if high >= hh
            hh := high
            label.set_x(labelhh, bar_index)
            label.set_y(labelhh, high)
            line.set_x1(line_up, bar_index)
            line.set_y1(line_up, high)
        lastState := 1
        
    // If we are oversold
    if isOversold
        if low <= ll
            ll := low
            label.set_x(labelll, bar_index)
            label.set_y(labelll, low)
            line.set_x1(line_down, bar_index)
            line.set_y1(line_down, low)
        lastState := 2
        
    // If last state was overbought and we are overbought
    if lastState == 1 and isOverbought
        if hh <= high
            hh := high
            label.set_x(labelhh, bar_index)
            label.set_y(labelhh, high)
            line.set_x1(line_up, bar_index)
            line.set_y1(line_up, high)
        
    // If we are oversold and the last state was oversold, move the drawings to the lowest price
    if lastState == 2 and isOversold
        if low <= ll
            ll := low
            label.set_x(labelll, bar_index)
            label.set_y(labelll, low)
            line.set_x1(line_down, bar_index)
            line.set_y1(line_down, low)
    
    // If last state was overbought
    if lastState == 1
        if hh <= high
            hh := high
            label.set_x(labelhh, bar_index)
            label.set_y(labelhh, high)
            line.set_x1(line_up, bar_index)
            line.set_y1(line_up, high)
            
    // If last stare was oversold
    if lastState == 2
        if ll >= low
            ll := low
            label.set_x(labelll, bar_index)
            label.set_y(labelll, low)
            line.set_x1(line_down, bar_index)
            line.set_y1(line_down, low)
// }

it doesn't delete the lines in the past, which means it will probably crash when a lot of new candles appear on the screen

事实并非如此; Pine 有一个自动垃圾收集器,可以在新行出现时动态删除最旧的行,专门用于提高性能。所以严格来说,除非你特别想要,否则你不需要自己做任何清理工作。如果你这样做:

Pine 最多可以在您的图表上留下大约 500 条线,在此脚本中,这是通过 indicator 函数中的 max_lines_count=500 参数指示的。最容易实现的限制就是设置 max_lines_count=3(注意它不会正好是 3,而是大约 3)。

更好的方法是通过数组。使用 built-in line.all 变量更容易 returns 一个包含图表上所有行的数组:

lineLimitInput = input(3)
if array.size(line.all) > lineLimitInput
    for i = 0 to array.size(line.all) - lineLimitInput - 1
        line.delete(array.get(line.all, i))

使用 line.all 比使用单独的数组更适合您的脚本,因为您不必将新行推入其中或在从图表中删除旧行后删除旧行。

编辑:如果你想使用一个单独的数组,逻辑也很简单:推一个新行,检查数组是否超过限制;如果是,从数组中删除第一行,然后将其删除:

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

l1 = line.new(bar_index[10], high[10], bar_index, high) // Ignored lines
l2 = line.new(bar_index[10], low[10], bar_index, low, color = color.green) // Culled lines
l2LimitInput = input(3)

var l2Array = array.new_line()
array.push(l2Array, l2) // Push each new line into array after drawing it

if array.size(l2Array) > l2LimitInput
    // Note: removing the line from the array does not delete it from the chart, and deleting it from the chart does not remove it from the array.
    // Both these things need to be done separately.
    firstLine = array.remove(l2Array, 0) 
    line.delete(firstLine)