如何根据条件为真触发标签,但只有一次

How to trigger a label based on a condition being true, but only once

我是 Pine 脚本的新手,请多多包涵。我试图弄清楚如何在“多头”条件变为真时绘制购买标签,但这只是第一次,而不是对于条件为真的每个柱。所以基本上,“strategy.entry”和“strategy.close”以相同的方式打开和关闭单个仓位。

 if (longConditions)
     strategy.entry("Long", true)
     longLabel = label.new(
      x=bar_index, 
      y=na, 
      text="Long",
      xloc=xloc.bar_index,
      yloc=yloc.belowbar, 
      color=color.green, 
      textcolor=color.white, 
      style=label.style_label_up, 
      size=size.normal)

if (closeLongConditions)
    strategy.close("Long")
    closeLongLabel = label.new(
     x=bar_index, 
     y=na, 
     text="Close",
     xloc=xloc.bar_index,
     yloc=yloc.abovebar, 
     color=color.red, 
     textcolor=color.white, 
     style=label.style_label_down, 
     size=size.normal)

有没有办法让“strategy.entry”和“strategy.close”触发标签而不是我的“长”和“关闭”条件?

这是解决方法

 if (longConditions and strategy.position_size < 0)
     strategy.entry("Long", true)
     longLabel = label.new(
      x=bar_index, 
      y=na, 
      text="Long",
      xloc=xloc.bar_index,
      yloc=yloc.belowbar, 
      color=color.green, 
      textcolor=color.white, 
      style=label.style_label_up, 
      size=size.normal)

if (closeLongConditions and strategy.position_size>0)
    strategy.close("Long")
    closeLongLabel = label.new(
     x=bar_index, 
     y=na, 
     text="Close",
     xloc=xloc.bar_index,
     yloc=yloc.abovebar, 
     color=color.red, 
     textcolor=color.white, 
     style=label.style_label_down, 
     size=size.normal)