仅显示3行并删除旧行



脚本运行得很好,但就性能而言,它没有删除过去的行,这意味着当屏幕上出现很多新的蜡烛时,它可能会崩溃。这就是我想要解决的问题。

如何使它只显示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)
// }

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

事实并非如此;Pine有一个自动垃圾收集器,当新的行专门为性能而出现时,它会动态删除最旧的行。所以严格来说,除非你特别想,否则你不需要自己做任何清理

在最大值下,Pine可以在图表上留下大约500行,在本脚本中,这是通过indicator函数中的max_lines_count=500参数指示的。实现最简单的限制就是设置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))

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

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

//@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)

最新更新