仅显示 3 行并删除旧行

nop*_*nop 1 pine-script

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

\n

如何让它只显示3行并删除旧的?类似于下面的内容,但我不知道具体如何实现。我知道我必须用数组来做到这一点,但不知道如何做。

\n
numberOfLines = 3\n\nvar label[] lbls = array.new_label()\nvar line[] lns = array.new_line()\n\nif array.size(lns) > 0\n    for i = 0 to array.size(lns) - 1\n        if i > numberOfLines\n            line.delete(array.remove(lns, i))\n
Run Code Online (Sandbox Code Playgroud)\n

原始代码

\n
//@version=5\nindicator("RSI Market Structure display only 10", overlay = true, max_bars_back = 500, max_lines_count = 500, max_labels_count = 500)\n\n// \xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94 Constants {\n// \xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94 Colors\nvar color GREEN         = color.green\nvar color RED           = color.red\nvar color BLUE          = color.blue\nvar color YELLOW        = color.yellow\n\n// \xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94 Constants used in inputs\nvar string SHOW = "Show"\nvar string HIDE = "Hide"\nvar string SHOW_TODAY_ONLY = "Show Today Only"\n\n// \xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94 Inputs {\n// \xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94 Market Structure\nvar GRP1 = "Market Structure"\nbool zigZagStructureInput = input.string(SHOW, "Show?", inline = "10", options = [SHOW, HIDE], group = GRP1) == SHOW\nint zigZagLengthInput = input.int(7, "Length", inline = "10", group = GRP1)\ncolor zigZagBullishColorInput = input.color(GREEN, "", inline = "11", group = GRP1)\ncolor zigZagBearishColorInput = input.color(RED, "", inline = "11", group = GRP1)\ncolor zigZagColorInput = input.color(BLUE, "Zig Zag", inline = "11", group = GRP1)\n// }\n\n// \xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94\xe2\x80\x94 Market Structure {\n// RSI value based on inbuilt RSI\nrsiValue = ta.rsi(close, zigZagLengthInput)\n\n// Get the current state\nisOverbought = rsiValue >= 80 // overbought level\nisOversold = rsiValue <= 20 // oversold level\n\n// State of the last extreme 0 for initialization, 1 = overbought, 2 = oversold\nvar int lastState = na\n\n// Highest and Lowest prices since the last state change\nvar hh = low\nvar ll = high\n\n// Labels\nvar label labelll = na\nvar label labelhh = na\n\n// Swing lines\nvar line line_up = na\nvar line line_down = na\n\n// We go from overbought straight to oversold  NEW DRAWINGS CREATED HERE\nif zigZagStructureInput\n    if lastState == 1 and isOversold\n        ll := low\n        labelll := label.new(bar_index, low, style = label.style_circle, color = zigZagBullishColorInput, size = size.tiny)\n        labelhh_low = label.get_x(labelhh)\n        labelhh_pos = label.get_y(labelhh)\n        line_down := line.new(bar_index, high, labelhh_low, labelhh_pos, color = zigZagColorInput, width = 2)\n    \n    // We go from oversold straight to overbought NEW DRAWINGS CREATED HERE\n    if lastState == 2 and isOverbought\n        hh := high\n        labelhh := label.new(bar_index, high, style = label.style_circle, color = zigZagBearishColorInput, size = size.tiny)\n        labelll_low = label.get_x(labelll)\n        labelll_pos = label.get_y(labelll)\n        line_up := line.new(bar_index, high, labelll_low, labelll_pos, color = zigZagColorInput, width = 2)\n        \n    // If we are overbought\n    if isOverbought\n        if high >= hh\n            hh := high\n            label.set_x(labelhh, bar_index)\n            label.set_y(labelhh, high)\n            line.set_x1(line_up, bar_index)\n            line.set_y1(line_up, high)\n        lastState := 1\n        \n    // If we are oversold\n    if isOversold\n        if low <= ll\n            ll := low\n            label.set_x(labelll, bar_index)\n            label.set_y(labelll, low)\n            line.set_x1(line_down, bar_index)\n            line.set_y1(line_down, low)\n        lastState := 2\n        \n    // If last state was overbought and we are overbought\n    if lastState == 1 and isOverbought\n        if hh <= high\n            hh := high\n            label.set_x(labelhh, bar_index)\n            label.set_y(labelhh, high)\n            line.set_x1(line_up, bar_index)\n            line.set_y1(line_up, high)\n        \n    // If we are oversold and the last state was oversold, move the drawings to the lowest price\n    if lastState == 2 and isOversold\n        if low <= ll\n            ll := low\n            label.set_x(labelll, bar_index)\n            label.set_y(labelll, low)\n            line.set_x1(line_down, bar_index)\n            line.set_y1(line_down, low)\n    \n    // If last state was overbought\n    if lastState == 1\n        if hh <= high\n            hh := high\n            label.set_x(labelhh, bar_index)\n            label.set_y(labelhh, high)\n            line.set_x1(line_up, bar_index)\n            line.set_y1(line_up, high)\n            \n    // If last stare was oversold\n    if lastState == 2\n        if ll >= low\n            ll := low\n            label.set_x(labelll, bar_index)\n            label.set_y(labelll, low)\n            line.set_x1(line_down, bar_index)\n            line.set_y1(line_down, low)\n// }\n
Run Code Online (Sandbox Code Playgroud)\n

bee*_*der 7

它不会删除过去的行,这意味着当屏幕上出现大量新蜡烛时它可能会崩溃

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

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

更好的方法是通过数组。使用内置line.all变量会更容易,该变量返回包含图表上所有线条的数组:

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))
Run Code Online (Sandbox Code Playgroud)

对于您的脚本来说,使用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)
Run Code Online (Sandbox Code Playgroud)