【问题标题】:How to trigger a label based on a condition being true, but only once如何根据条件为真触发标签,但仅触发一次
【发布时间】:2021-06-03 09:30:17
【问题描述】:

我是 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”触发标签而不是我的“long”和“close”条件?

【问题讨论】:

  • 我想出了一个可行的解决方案。如果有人想插话,我很想看看其他方法。但我使用了一个 var 变量和一个开关来禁止生成重复的“长”标签,直到“关闭”信号重置开关。

标签: pine-script


【解决方案1】:

这是解决方法

 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)

【讨论】:

  • 我知道必须有一种更简单的方法来做到这一点。谢谢!
  • 不要忘记将解决方案标记为答案
猜你喜欢
  • 2022-01-03
  • 2023-03-18
  • 2019-07-14
  • 2023-03-21
  • 2020-12-11
  • 1970-01-01
  • 1970-01-01
  • 2011-08-13
  • 2020-08-25
相关资源
最近更新 更多