adamio
adamio

Reputation: 57

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

I'm new to Pine script so bear with me. I'm trying to figure out how to plot a buy label when a "long" condition becomes true, but only the first time and not for every bar that the condition is true. So basically, the same way "strategy.entry" and "strategy.close" open and close a single position.

 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)

Is there a way to have "strategy.entry" and "strategy.close" trigger a label instead of my "long" and "close" conditions?

Upvotes: 3

Views: 2947

Answers (1)

this is the workaround

 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)

Upvotes: 0

Related Questions