Pinescript中的`label.delete`似乎无法正常工作。

huangapple go评论57阅读模式
英文:

Pinescript `label.delete` does not seem to be working

问题

## 详细信息
我正在使用Pinescript创建一个日志系统,允许合并写入相同索引的日志。如下图所示,代码似乎完全正常工作:

在新蜡烛之前
![1](https://i.stack.imgur.com/IoZrE.png)

但当绘制新蜡烛时,尽管使用了`label.delete(aNewLabel[1])`命令,以前的标签并未被删除。

在绘制新蜡烛后
![2](https://i.stack.imgur.com/Y9t80.png)

是否有任何原因导致`label.delete`无法按预期工作?

## 完整代码:
```plaintext
(原代码未作翻译)

我期望每当绘制新蜡烛时,旧标签都会被删除。我尝试使用此答案中提到的策略,但未能成功。


<details>
<summary>英文:</summary>

## Details
I am creating a log system in Pinescript to allow merging together logs that are written to the same index. The code seems to work perfectly fine as can be seen in the figure:

before new candle

![1](https://i.stack.imgur.com/IoZrE.png)

but when a new candle is drawn, the previous label is not deleted despite the command `label.delete(aNewLabel[1])`.

after new candle

![2](https://i.stack.imgur.com/Y9t80.png)

Is there any reason why `label.delete` would not be working as expected?

## Full code:

var print_stack_absciss = array.new_int()
var print_stack_string = array.new_string()
var print_stack_color = array.new_color()
var print_stack_range = array.new_int(2, 0)

secondsToTime(seconds) =>
int res = 1000 * seconds
res

getDt() =>
int dt = secondsToTime(timeframe.in_seconds(timeframe.period))
dt

getBarIndexAtTime(int _time_point) =>
int m_bar_ind = na
int i = 0
for count = 0 to 5000
if time[count] >= _time_point and time[count + 1] < _time_point
m_bar_ind := count
break
// @returns
m_bar_ind

timeToIndex(theTime) =>
int index = getBarIndexAtTime(theTime)
index

daysToTime(days)=>
int theTimeDiff = math.round(days*86400000)

indexToTime(index) =>
int theTime = na
if index >= 0
theTime := time[index]
else
theTime := time - index * getDt()
// theTime := time + daysToTime(2)
theTime

getAbsciss(absciss, cfg="index") =>
int indexToUse = na
int timeToUse = na
switch cfg
"index" =>
indexToUse := absciss
timeToUse := indexToTime(absciss)
"time" =>
indexToUse := timeToIndex(absciss)
timeToUse := absciss
[indexToUse, timeToUse]

print(txt = "", color=color.gray, absciss=0, ordinate=0, where=low, size=1, cfg="index") =>
if barstate.islast
[indexToUse, timeToUse] = getAbsciss(absciss, cfg)
int lowRange = array.get(print_stack_range, 0)
int highRange = array.get(print_stack_range, 1)
if indexToUse > highRange
array.set(print_stack_range, 1, indexToUse)
if indexToUse < lowRange
array.set(print_stack_range, 0, indexToUse)
print_stack_absciss.push(timeToUse)
print_stack_color.push(color)
print_stack_string.push(txt)

GetLabels() =>
int size = array.size(print_stack_absciss)
// print(str.format("size is {0}", size))
if size > 0
for j = array.get(print_stack_range, 0) to array.get(print_stack_range, 1)
int time_j = indexToTime(j)
// print(str.format("j is {0}", j))
strings = array.new_string(0, "")
color color_i = na
for i = 0 to size - 1
// print(str.format("i is {0}", i))
int absciss_i = array.get(print_stack_absciss, i)
// print(str.format("absciss_i is {0}", absciss_i))
if absciss_i == time_j
string string_i = array.get(print_stack_string, i)
strings.push(string_i)
color_i := array.get(print_stack_color, i)
if array.size(strings)
// printB(str.format("{0}", indexToTime(j)))
aNewLabel = label.new(time_j, high[(j<0)?0:j], array.join(strings, "\n"), style = label.style_label_down, color = color.new(color.white, 100), textcolor = color_i, xloc=xloc.bar_time)
label.delete(aNewLabel[1])

//@version=5
indicator("Label Test")
print(str.format("Number is: {0}", 2), absciss = -20)
print(str.format("Test is: {0}", 3), absciss = -20)
print(str.format("Number is: {0}", 4), absciss = -10)
print(str.format("Test is: {0}", 5), absciss = -10)

GetLabels()


I was expecting that whenever a new candle is drawn, the old label would be deleted. I tried to use the strategy present in [this answer](https://stackoverflow.com/a/72378506/20200618) but I did not have success in doing so.

</details>


# 答案1
**得分**: 1

我已找到避免重复标签打印的最佳方法(看起来几乎像阴影)是首先定义一个变量 label,然后立即删除标签。然后使用 label.new 来创建新的标签。剩下的就是最后一个标签。注意:使用 Var 来定义变量。以下是一个示例:

```pinescript
var label myLabel = na
label.delete(myLabel)

myLabel := label.new(x = bar_index, y = high, color = blue, size = size.auto, textcolor = color.white, text = "这是标签文本", style = label.style_label_left)
英文:

I've found the best way to avoid repeated label printing (which looks almost like a shadow) is to first define a var label and then immediately delete the label. Then use label.new to make your new label. What remains is the last label. Note: Use Var to define the variable. Here's an example:

var label myLabel = na
label.delete(myLabel) 

myLabel := label.new(x = bar_index, y = high, color=blue, size=size.auto, textcolor = color.white, text = &quot;This is label text&quot;, style=label.style_label_left)

答案2

得分: 0

你需要创建一个var label并将它传递给你的函数。在函数内部,你应该调用label.set*()函数来修改现有的标签。

GetLabels(lbl) =>
    int size = array.size(print_stack_absciss)
    // print(str.format("size is {0}", size))
    if size > 0
        for j = array.get(print_stack_range, 0) to array.get(print_stack_range, 1)
            int time_j = indexToTime(j)
            // print(str.format("j is {0}", j))
            strings = array.new_string(0, "")
            color color_i = na
            for i = 0 to size - 1
                // print(str.format("i is {0}", i))
                int absciss_i = array.get(print_stack_absciss, i)
                // print(str.format("absciss_i is {0}", absciss_i))
                if absciss_i == time_j
                    string string_i = array.get(print_stack_string, i)
                    strings.push(string_i)
                    color_i := array.get(print_stack_color, i)
            if array.size(strings)
                // printB(str.format("{0}", indexToTime(j)))
                label.set_xy(lbl, time_j, high[(j<0)?0:j])
                label.set_text(lbl, array.join(strings, "\n"))
                label.set_textcolor(lbl, color_i)

var label lbl = label.new(na, na, na, style = label.style_label_down, color = color.new(color.white, 100), xloc=xloc.bar_time)

GetLabels(lbl)

不包括代码部分的翻译如上。

英文:

You need to create a var label and pass it to your function. In there, you should call label.set*() functions to modify the existing label.

GetLabels(lbl) =&gt;
    int size = array.size(print_stack_absciss)
    // print(str.format(&quot;size is {0}&quot;, size))
    if size &gt; 0
        for j = array.get(print_stack_range, 0) to array.get(print_stack_range, 1)
            int time_j = indexToTime(j)
            // print(str.format(&quot;j is {0}&quot;, j))
            strings = array.new_string(0, &quot;&quot;)
            color color_i = na
            for i = 0 to size - 1
                // print(str.format(&quot;i is {0}&quot;, i))
                int absciss_i = array.get(print_stack_absciss, i)
                // print(str.format(&quot;absciss_i is {0}&quot;, absciss_i))
                if absciss_i == time_j
                    string string_i = array.get(print_stack_string, i)
                    strings.push(string_i)
                    color_i := array.get(print_stack_color, i)
            if array.size(strings)
                // printB(str.format(&quot;{0}&quot;, indexToTime(j)))
                label.set_xy(lbl, time_j, high[(j&lt;0)?0:j])
                label.set_text(lbl, array.join(strings, &quot;\n&quot;))
                label.set_textcolor(lbl, color_i)

var label lbl = label.new(na, na, na, style = label.style_label_down, color = color.new(color.white, 100), xloc=xloc.bar_time)

GetLabels(lbl)

huangapple
  • 本文由 发表于 2023年2月24日 07:34:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551345.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定