How to show one arrow on plot for the first time condition is met?

ssaeed73

Member
Hi guy,

Hoping someone can help me out. I have a study where I look for a long and a short condition to be met. The long and short conditions can occur multiple times in succession. For example, and I am making this up, I am looking at the 9EMA>13EMA and so every bar if that is true the long condition is met. Now I just want to put an arrow on the plot the first time the condition is met and not show it again until after the short condition occurs. Likewise for the short condition only one arrow and no more until a long condition occurs.

is there a simple way to do this? I use the long and short conditions for a strategy and it correctly plots the entry and exit arrows once but when I use the code as a study and add Up or Down arrows for the same conditions I see that the arrows are repeated multiple times.

Thanks,

@ssaeed73 This is a method that may help you. Assign your conditions to the respective "up" or "down" variables - the arrows should alternate back and forth, with only 1 arrow per direction each time.

Code:
``````def up = your condition 1;
def down = your condition 2;

def Hold_Up_Value = if up then 1 else if !down then Hold_Up_Value[1] else 0;
def Hold_Dn_Value = if down then 1 else if !up then Hold_Dn_Value[1] else 0;

plot Signal_Up = if up and Hold_Dn_Value[1] == 1 then low else Double.NaN;
Signal_Up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot Signal_Dn = if down and Hold_Up_Value[1] == 1 then high else Double.NaN;
Signal_Dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);``````

@Pensar Thank you Sir! That worked beautifully, much appreciated.

@Pensar could you please help me out one more time? I have the following code implemented for my watchlist now. How can I set it up so it doesn't show me NaN in the watchlist window all the time? I would like the window to be black and when SignalUp condition is met it shades the cell green as in the code below. My understanding is your code is setting Signal_UP to the low value of the stock otherwise NaN. How to modify this to be binary 1 or 0? Do I just change Low to "1" and "else Double.NaN" to 0?

Code:
``````def up = if isopen then Long5 else 0;
def down = FiveMinExit;

def Hold_Up_Value = if up then 1 else if !down then Hold_Up_Value[1] else 0;
def Hold_Dn_Value = if down then 1 else if !up then Hold_Dn_Value[1] else 0;

plot Signal_Up = if up and Hold_Dn_Value[1] == 1 then low else Double.NaN;

AssignBackgroundColor(if Signal_Up then color.green else color.black);``````

@ssaeed73 Changing the "low" to "1" and "double.nan" to "0" sounds correct - try it and see what happens.

I'm trying to figure out a way of keeping track when I get an enter signal and when i get an exit signal. I want to count or keep track of when i get a signal so i can only have one Exit down arrow. If i dont get an Entry Signal then there should be no Exit signal. I'm trying to avoid having numerous exit down arrows on my chart... So, if you were to look at the chart there should be a pair of Entry and Exit Signals.

Code:
``````declare upper;

def a = MovAvgTwoLines("fast length" = 35)."fastAvg" crosses above MovAvgTwoLines("slow length" = 200)."slowAvg";
def b = MovAvgTwoLines("fast length" = 35)."fastAvg" crosses above reference VWAP()."VWAP";

def count = If Enter then count or keep track of Enter Signal
If Exit then reset count to zero;

plot Enter = a and b within 2 bars and count == 0;

plot Exit = ExitSignal == 1 and count !=0;

Enter.SetPaintingStrategy(PaintingStrategy. BOOLEAN_ARROW_UP);
Enter.SetDefaultColor(Color.green);
Enter.SetLineWeight(5);

Exit.SetPaintingStrategy(PaintingStrategy. BOOLEAN_ARROW_DOWN);
Exit.SetDefaultColor(Color.red);
Exit.SetLineWeight(5);``````

Awesome! Yeah. That did it. Thank you!

@ssaeed73 This is a method that may help you. Assign your conditions to the respective "up" or "down" variables - the arrows should alternate back and forth, with only 1 arrow per direction each time.

Code:
``````def up = your condition 1;
def down = your condition 2;

def Hold_Up_Value = if up then 1 else if !down then Hold_Up_Value[1] else 0;
def Hold_Dn_Value = if down then 1 else if !up then Hold_Dn_Value[1] else 0;

plot Signal_Up = if up and Hold_Dn_Value[1] == 1 then low else Double.NaN;
Signal_Up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot Signal_Dn = if down and Hold_Up_Value[1] == 1 then high else Double.NaN;
Signal_Dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);``````

Thank you for a great method!

I used your code for something similar that I am trying to accomplish. I can see the arrows on desktop but not mobile. Do you (or anyone) know what could be the issue? Thanks!

Code:
``````input showBreakoutSignals = yes;
input aggPeriod = AggregationPeriod.THIRTY_MIN;

def sellsignals = sell condition;

def sellcondition = if sellsignals then 1 else if !buysignals then sellcondition[1] else 0;

plot UpSignal = if buysignals and sellcondition[1] == 1 then low else double.nan;
plot DownSignal = if sellsignals and buycondition[1] == 1 then high else double.nan;

UpSignal.SetDefaultColor(Color.WHITE);
UpSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
UpSignal.SetHiding(!showBreakoutSignals);

DownSignal.SetDefaultColor(Color.CYAN);
DownSignal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
DownSignal.SetHiding(!showBreakoutSignals);``````

@pine The mobile version doesn't allow multiple aggregation periods so that is most likely your problem... The code should only work for you on a 30m chart, unless it gags on any reference to AggregationPeriod...

@pine The mobile version doesn't allow multiple aggregation periods so that is most likely your problem... The code should only work for you on a 30m chart, unless it gags on any reference to AggregationPeriod...

Ok, that makes sense. The condition is based on the 30 min aggregation, while chart is on a lower time frame. Thank you for your response.

I'm new to thinkscript but I think I've found a much simpler solution to this issue. In this case I'm going to use "close > ExpAverage(close, 9)" as a buy signal as it would normally cause a buy signal every time the close is above the 9EMA. In any case, you should be able to replace the condition with whatever your condition for a buy signal is. See below:

Code:
``````def BuySig = close > ExpAverage(close, 9);

Plot can also be written like so:

Code:
``plot BuySignal = BuySig and !BuySig[1];``

What this code is doing is plotting an arrow when BuySig of the current bar is true and BuySig of the bar before the current bar is false. This way if BuySig is both true for the current bar and the bar before the current bar then it reads it as "false" and doesn't plot an arrow. Hope that helps.

Last edited:
I'm new to thinkscript but I think I've found a much simpler solution to this issue. In this case I'm going to use "close > ExpAverage(close, 9)" as a buy signal as it would normally cause a buy signal every time the close is above the 9EMA. In any case, you should be able to replace the condition with whatever your condition for a buy signal is. See below:

Code:
``````def BuySig = close > ExpAverage(close, 9);

Plot can also be written like so:

Code:
``plot BuySignal = BuySig and !BuySig[1];``

What this code is doing is plotting an arrow when BuySig of the current bar is true and BuySig of the bar before the current bar is false. This way if BuySig is both true for the current bar and the bar before the current bar then it reads it as "false" and doesn't plot an arrow. Hope that helps.
@Jman831 I'm surprised. I actually didn't think this would work until I tried to prove it wrong. Nice!

Hi Team,

I am trying to get a arrow and alert plotted everytime when the Hull Moving Average line changes color. Below is my code but I am not seeing any arrows on my part. Any help is highly appreciated.

#Hull Moving Average

input price = close;
input length = 21;
input displace = 1;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

plot Bullish = HMA [0] > HMA [1];

AssignPriceColor(if HMA > HMA [1] then Color.GREEN else Color.RED);

plot UpSignal = if HMA > HMA [1] then Bullish else Double.Nan;
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

Hi Team,

I am trying to get a arrow and alert plotted everytime when the Hull Moving Average line changes color. Below is my code but I am not seeing any arrows on my part. Any help is highly appreciated.

#Hull Moving Average

input price = close;
input length = 21;
input displace = 1;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

plot Bullish = HMA [0] > HMA [1];

AssignPriceColor(if HMA > HMA [1] then Color.GREEN else Color.RED);

plot UpSignal = if HMA > HMA [1] then Bullish else Double.Nan;
UpSignal.SetDefaultColor(Color.UPTICK);
UpSignal.SetPaintingStrategy(PaintingStrategy.ARROW_UP);

this
plot Bullish = HMA [0] > HMA [1];
doesn't do anything. it's just a boolean value. it is the same as upsignal.

this
plot UpSignal =
is plotting a boolean value, but you are using normal arrows. so they will appear at \$0 and \$1.

look at the original formula for determining line color.
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

that checks 1 condition, to determine a line color between 2 bars.
you need to look at 3 bars and 2 conditions , to see if line direction changed.

you have to think about when you want the signal, pick the reference bar, then use the appropriate offsets.
if you use offsets that are referenced from the 2nd bar, you still have to wait for the 3rd bar to finish before having a valid signal. it won't produce a signal 1 bar earlier.

Code:
``````def na = double.nan;

#Hull Moving Average

input price = close;
input length = 21;
input displace = 1;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

# signal reference is 3rd bar
def revup3 = HMA[2] > HMA[1] and HMA[1] < HMA[0];
def revdwn3 = HMA[2] < HMA[1] and HMA[1] > HMA[0];

# signal reference is 2nd bar
def revup2 = HMA[1] > HMA[0] and HMA[0] < HMA[-1];
def revdwn2 = HMA[1] < HMA[0] and HMA[0] > HMA[-1];

# normal arrow
plot zup = if revup2 then low else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP)

# boolean arrow
plot zdwn = revdwn2;
zdwn.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_down)
#``````

this
plot Bullish = HMA [0] > HMA [1];
doesn't do anything. it's just a boolean value. it is the same as upsignal.

this
plot UpSignal =
is plotting a boolean value, but you are using normal arrows. so they will appear at \$0 and \$1.

look at the original formula for determining line color.
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

that checks 1 condition, to determine a line color between 2 bars.
you need to look at 3 bars and 2 conditions , to see if line direction changed.

you have to think about when you want the signal, pick the reference bar, then use the appropriate offsets.
if you use offsets that are referenced from the 2nd bar, you still have to wait for the 3rd bar to finish before having a valid signal. it won't produce a signal 1 bar earlier.

Code:
``````def na = double.nan;

#Hull Moving Average

input price = close;
input length = 21;
input displace = 1;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

# signal reference is 3rd bar
def revup3 = HMA[2] > HMA[1] and HMA[1] < HMA[0];
def revdwn3 = HMA[2] < HMA[1] and HMA[1] > HMA[0];

# signal reference is 2nd bar
def revup2 = HMA[1] > HMA[0] and HMA[0] < HMA[-1];
def revdwn2 = HMA[1] < HMA[0] and HMA[0] > HMA[-1];

# normal arrow
plot zup = if revup2 then low else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP)

# boolean arrow
plot zdwn = revdwn2;
zdwn.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_down)
#``````
thanks for the code really apppreciate it. I tried the code and got invalid statement error at Line " zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP)" and Line "zdwn.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_down)"

I only got the arrows working when I commented these two lines. and I wasn't getting the down arrows consistently so I change the below line.

plot zdwn = if revdwn2 then high else na;

I am not a programmer so please let me know if the changes are right?

@ssaeed73 This is a method that may help you. Assign your conditions to the respective "up" or "down" variables - the arrows should alternate back and forth, with only 1 arrow per direction each time.

Code:
``````def up = your condition 1;
def down = your condition 2;

def Hold_Up_Value = if up then 1 else if !down then Hold_Up_Value[1] else 0;
def Hold_Dn_Value = if down then 1 else if !up then Hold_Dn_Value[1] else 0;

plot Signal_Up = if up and Hold_Dn_Value[1] == 1 then low else Double.NaN;
Signal_Up.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
plot Signal_Dn = if down and Hold_Up_Value[1] == 1 then high else Double.NaN;
Signal_Dn.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);``````
I know this is old but I just wanted to say thanks for posting this solution. As a newbie to solving thinkscript, it definitely taught me something new.

this
plot Bullish = HMA [0] > HMA [1];
doesn't do anything. it's just a boolean value. it is the same as upsignal.

this
plot UpSignal =
is plotting a boolean value, but you are using normal arrows. so they will appear at \$0 and \$1.

look at the original formula for determining line color.
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

that checks 1 condition, to determine a line color between 2 bars.
you need to look at 3 bars and 2 conditions , to see if line direction changed.

you have to think about when you want the signal, pick the reference bar, then use the appropriate offsets.
if you use offsets that are referenced from the 2nd bar, you still have to wait for the 3rd bar to finish before having a valid signal. it won't produce a signal 1 bar earlier.

Code:
``````def na = double.nan;

#Hull Moving Average

input price = close;
input length = 21;
input displace = 1;

plot HMA = MovingAverage(AverageType.HULL, price, length)[-displace];

HMA.DefineColor("Up", GetColor(1));
HMA.DefineColor("Down", GetColor(0));
HMA.AssignValueColor(if HMA > HMA[1] then HMA.color("Up") else HMA.color("Down"));

# signal reference is 3rd bar
def revup3 = HMA[2] > HMA[1] and HMA[1] < HMA[0];
def revdwn3 = HMA[2] < HMA[1] and HMA[1] > HMA[0];

# signal reference is 2nd bar
def revup2 = HMA[1] > HMA[0] and HMA[0] < HMA[-1];
def revdwn2 = HMA[1] < HMA[0] and HMA[0] > HMA[-1];

# normal arrow
plot zup = if revup2 then low else na;
zup.SetPaintingStrategy(PaintingStrategy.ARROW_UP)

# boolean arrow
plot zdwn = revdwn2;
zdwn.SetPaintingStrategy(PaintingStrategy.boolean_ARROW_down)
#``````
any way to get just the arrows to work on TOS mobile with its limitations? I could get the down arrow to work but not the up arrow. thought it woud be nice since you cant have color change stuff.

Not the exact question you're looking for?

87k+ Posts
312 Online

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?