首页 > 解决方案 > 如何根据条件为真触发标签,但仅触发一次

问题描述

我是 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”条件?

标签: pine-script

解决方案


这是解决方法

 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)

推荐阅读