# [Help] Declaration to display a plot ONCE a criteria has been satisfied and then keep that plot active

##### New member
Hi is there some sort of declaration or function that I can use to keep a plot active once a criteria has been satisfied?

For example, let's say price is > 5% above the 10 moving average, at that point I want to display the 20EMA.

I would use something of the following [abbreviated and condensed]:
#Where P1 is when price > 5% above 10EMA
plot EMA20 = if P1 then MovingAverage(averageType.exponential, close, 20) else double.nan;
EMA20.SetDefaultColor(COLOR.YELLOW);

Now this will display the plot only when price is at least 5% above the MA, but when it goes below, the plot isn't shown. How do I keep the plot displayed once the criteria has been fulfilled?

Kind regards and TIA.

Hi is there some sort of declaration or function that I can use to keep a plot active once a criteria has been satisfied?

For example, let's say price is > 5% above the 10 moving average, at that point I want to display the 20EMA.

I would use something of the following [abbreviated and condensed]:
#Where P1 is when price > 5% above 10EMA
plot EMA20 = if P1 then MovingAverage(averageType.exponential, close, 20) else double.nan;
EMA20.SetDefaultColor(COLOR.YELLOW);

Now this will display the plot only when price is at least 5% above the MA, but when it goes below, the plot isn't shown. How do I keep the plot displayed once the criteria has been fulfilled?

Kind regards and TIA.

the key part of this is the second line of this, which holds the value after it has been triggered.

below i have changed the variable names to make it easier to follow.

Code:
``````# example 1
def trigger = .. some true/false condition

def hold = if barnumber() == 1 then 0
else if hold[1] == 1 then hold[1]
else if trigger then 1
else hold[1];

# then look for a transition, from 0 to 1
def first = (!hold[1] and hold);

# example 2
def trigger = .. some true/false condition
# isnan(pricex[1]) , checks if the previous value was not an error. if true, then it has a valid value, so assign it to the current bar with,  then pricex[1].

def avg =  some formula
def pricex = if barnumber() == 1 then double.nan
else if !isnan(pricex[1]) then pricex[1]
else if trigger then avg
else pricex[1];
#``````

here are a couple of studies,
that on the first trigger of some variable,
it sets a variable to 1 for the remainder of the chart.

Last edited:
Thank you so much!

Hi @halcyonguy,

I'm trying to do something a little similar but plot a line on a value after the setup condition is/was true and have it stop once an indicator has crossed a plotted value. With your example1, it did plot the line after cond is true but it would continue plotting after the indic crossed above/below the value.

Code:
``````def setup = true/false condition
def indic = supercoolindicator();
def hold = if barnumber() == 1 then 0
else if hold[1] == 1 then hold[1]
else if trigger then 1
else hold[1];
plot line = if hold and indic < value then value else double.nan;?``````

Hi @halcyonguy,

I'm trying to do something a little similar but plot a line on a value after the setup condition is/was true and have it stop once an indicator has crossed a plotted value. With your example1, it did plot the line after cond is true but it would continue plotting after the indic crossed above/below the value.

Code:
``````def setup = true/false condition
def indic = supercoolindicator();
def hold = if barnumber() == 1 then 0
else if hold[1] == 1 then hold[1]
else if trigger then 1
else hold[1];
plot line = if hold and indic < value then value else double.nan;?``````

i think this will work
Code:
``````declare lower;
def trigger_cond = true/false condition
def stop_cond = true/false condition (crossing)

def initialvalue = 0;
def runvalue = 1;
def stopvalue = 2;

def linex = if barnumber() == 1 then initialvalue
else if linex[1] == stopvalue then linex[1]

else if linex[1] == runvalue and stop_cond then stopvalue

else if linex[1] == initialvalue and trigger_cond then runvalue

else linex[1];

plot line = if linex == runvalue then runvalue else initialvalue``````

87k+ Posts
274 Online

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• Exclusive members-only content
• 1 full year of unlimited support

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?