Conditionally set color & text of chart label


Category:
0
0

Pete –

I’m trying to create a label that shows “Long” in color green if the exponential moving average with a length of 5 (EMA5) is above the exponential moving average with a length of 21 (EMA 21) or “Short” and color red if the converse is true. What I have so far is:

input extremefast_length = 5;
input slow_length = 21;
def EMA5= ExpAverage(price[-displace], extremefast_length);
def EMA21 = ExpAverage(price[-displace], slow_length);

AddLabel(yes, “Long/Short”, if EMA5>EMA21 then Color.Green else if EMA5<EMA21 then Color.Red else Color.Black );

The above scripting will show “Long/Short” on the chart in the color green, red, or black as noted. I would prefer to have it show either “Long” in green or “Short” in red and nothing if neither.

Can this be done?

Thanks for the help!

Marked as spam
Posted by (Questions: 2, Answers: 0)
Asked on May 26, 2018 2:39 pm
434 views
0
Private answer

Your code was missing two inputs. It was missing ‘displace’ and ‘price’. I am pasting the full working code here to avoid confusion for our other viewers.

The text portion of the label is controlled the same way as the color. I took the time to correct some style issues in your code. Variables should begin with lowercase letters. And operators such as ‘=’ must have spaces between them and the variable names.

input extremefast_length = 5;
input slow_length = 21;
input displace = 0;
input price = close;
def ema5 = ExpAverage(price[-displace], extremefast_length);
def ema21 = ExpAverage(price[-displace], slow_length);
AddLabel(yes, if ema5 > ema21 then "Long" else "Short", if ema5 > ema21 then Color.Green else if ema5 < ema21 then Color.Red else Color.Black );

 

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on May 26, 2018 3:59 pm
0

Can you add additional labels to this? Say we want to see this label on several charts. If we are on a 15 minute chart see this on the 15M and anything greater and whether or not is is long or short. We would obviously have to add the study several different times and change the time frames but how would that code work?
Something like:
if period == aggregationPeriod.HOUR then “60m”
else
if period == aggregationPeriod.THIRTY_MIN then “30m”
else
if period == aggregationPeriod.TWENTY_MIN then “20m”
else
if period == aggregationPeriod.FIFTEEN_MIN then “15m”

And so on. I like how you can change the input. I like 8 and 21 and sometimes 8 and 34 depending on the time frames.

( at June 1, 2018 12:44 pm)
0

I have read your comment several times trying to understand. I am completely lost.

“Say we want to see this label on several charts”… so just add this study to each of your charts. What am I missing?

“If we are on a 15 minute chart see this on the 15M and anything greater”… I don’t have a clue how to interpret that statement.

( at June 1, 2018 1:06 pm)
0

Sorry for my lack of communicating my idea/thoughts clearly. I wanted to add various periods to this. If I have a 15 minute chart loaded and it says long but I want to see what the daily is doing without changing my chart to a daily time frame, is there a way to see this on the label? I added the input period but even if I’m on a 15 minute chart and select Daily it doesn’t help. I want to expand even further by showing all the time frames I use, 5, 15, 20,30, etc. So I’m sure I’ll have to add this study several times and change each one to the period but not sure the best way to do this.

input period = aggregationPeriod.DAY;
input fast_length = 5;
input slow_length = 21;
input displace = 0;
input price = close;
def emafast = ExpAverage(price[-displace], fast_length);
def emaslow = ExpAverage(price[-displace], slow_length);
AddLabel(yes, if emafast > emaslow then “Long” else “Short”, if emafast > emaslow then Color.Green else if emafast < emaslow then Color.Red else Color.Black );

( at June 1, 2018 5:58 pm)
0

Ok, I get it now. Thanks for restating that. I’ll get you a solution tomorrow morning. Computer maintenance prevents any work this evening.

( at June 1, 2018 6:56 pm)
0

Here is an updated version that provides inputs for the time frame of the ema’s as well as the time frame displayed in the chart label.

input extremefast_length = 5;
input slow_length = 21;
input timeFrame = AggregationPeriod.FIVE_MIN;
input labelTimeFrame = “5 Min”;
def ema5 = ExpAverage(close(period = timeFrame), extremefast_length);
def ema21 = ExpAverage(close(period = timeFrame), slow_length);
AddLabel(yes, if ema5 > ema21 then Concat(labelTimeFrame, “: Long”) else Concat(labelTimeFrame, “: Short”), if ema5 > ema21 then Color.Green else if ema5 < ema21 then Color.Red else Color.Black );

( at June 2, 2018 9:36 am)