Showing labels for ema


Category:
1
1

I have a study that shows labels on a 1M to Monthly time frame and changes colors depending on the if the EMA crossover is bullish or bearish. However, I like to use two different EMA crosses and want to add a label setting that allows me to see both the time frame and which EMA i’m using. This example, I have 5/21 ema. I also like 8/34 and if I add both studies to the chart I want to be able to differentiate between the two. I have the input area fixed, however I can’t get the if/else statement to work in adding the ShowEmaLabel.  This area of concern is in the P1 and P2 section of the code. I’ve attached a rough photo shop of what I’m trying to achieve.

Thanks

 

input length1 = 5;
input length2 = 21;
input price = close;
input displace = 0;
input showEMAInLabel = YES;
def na = Double.NaN;

plot fastema = ExpAverage(price[-displace], length1);
fastema.SetDefaultColor(Color.BLUE);
fastema.SetLineWeight(2);

plot slowema = ExpAverage(price[-displace], length2);
slowema.SetDefaultColor(Color.DARK_RED);
slowema.SetLineWeight(2);

def crossover = if fastema > slowema and fastema[1] <= slowema[1] then 1 else 0;
def crossunder = if fastema < slowema and fastema[1] >= slowema[1] then 1 else 0;

plot LongTrigger = if crossover then low – TickSize() else na;
plot ShortTrigger = if crossunder then high + TickSize() else na;

LongTrigger.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
LongTrigger.SetLineWeight(3);
LongTrigger.SetDefaultColor(Color.GREEN);
LongTrigger.HideBubble();
LongTrigger.HideTitle();
ShortTrigger.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
ShortTrigger.SetLineWeight(3);
ShortTrigger.SetDefaultColor(Color.RED);
ShortTrigger.HideBubble();
ShortTrigger.HideTitle();

#Trigger alerts
Alert(crossover[1], “Long Trigger”, Alert.BAR, Sound.Ding);
Alert(crossunder[1], “Short Trigger”, Alert.BAR, Sound.Ding);

def P1 = AggregationPeriod.MONTH;
def P2 = AggregationPeriod.WEEK;
def ap = GetAggregationPeriod();

# P1 ——————-
def ema11;
def ema12;
if ap <= P1 { ema11 = ExpAverage(close(period = P1), length1); ema12 = ExpAverage(close(period = P1), length2); }
else { ema11 = Double.NaN; ema12 = Double.NaN; }

AddLabel(ap <= P1, “M”, if ema11 > ema12 and ema11[1] <= ema12[1] then Color.LIME
else if ema11 < ema12 and ema11[1] >= ema12[1] then Color.RED
else if ema11 > ema12 then Color.DARK_GREEN else if ema11 < ema12 then Color.DARK_RED else Color.GRAY);

# P2 ——————-
def ema21;
def ema22;

if ap <= P2 { ema21 = ExpAverage(close(period = P2), length1); ema22 = ExpAverage(close(period = P2), length2); }
else { ema21 = Double.NaN; ema22 = Double.NaN; }
AddLabel(ap <= P2, “W”, if ema21 > ema22 and ema21[1] <= ema22[1] then Color.LIME
else if ema21 < ema22 and ema21[1] >= ema22[1] then Color.RED
else if ema21 > ema22 then Color.DARK_GREEN else if ema21 < ema22 then Color.DARK_RED else Color.GRAY);

Attachments:
Marked as spam
Posted by (Questions: 21, Answers: 47)
Asked on June 19, 2018 11:49 am
2107 views
1
Private answer

The title of your question lacks detail. You are trying to dynamically set the text of a chart label. There are tons of examples of adding labels to charts. So your question title will just get lost in that group when folks are searching for solutions.

We have two prior posts that demonstrate how to dynamically set the text of the AddLabel() method.

One of these is used in a watchlist, but the logic is exactly the same as would be applied to a chart: https://www.hahn-tech.com/ans/trailing-stop-alert-column/

This is one that is applied to a chart. Notice the question title is much more clear than what you have entered: https://www.hahn-tech.com/ans/conditionally-set-color-text-of-chart-label/

Please search before adding a new post. And try to use the terms you used in your search in the question title.

 

Marked as spam
Posted by (Questions: 37, Answers: 4118)
Answered on June 19, 2018 12:26 pm
0
I’ve got that far with the labels, I’m trying to add two conditions and two words to the a single label. If you it is bullish crossover, show the period (M) for monthly and also show what EMA’s are being used. I’m unsure how to add a second label. Maybe I can reword my question. This code doesn’t bring up an error but doesn’t show the EMA’s being used. AddLabel(ap ema12 and ema11[1] = ema12[1] then Color.RED else if ema11 > ema12 then Color.DARK_GREEN else if ema11 ema12 then Color.DARK_GREEN else if ema11 < ema12 then Color.DARK_RED else Color.GRAY);
( at June 19, 2018 1:37 pm)
0

AddLabel(yes, “M: 5/21”, Color.WHITE);
AddLabel(yes, “M: 8/34”, Color.WHITE);

I have taken out the part that dynamically sets the color to focus on the text. How is this not what you have requested? Answer that and perhaps I can provide the additional detail.

Did you want to have the text of the label automatically adjust to the input values?
input length1 = 5;
input length2 = 21;
AddLabel(yes, Concat(“M: “, Concat(Concat(length1, “/”), length2), Color.WHITE);

( at June 21, 2018 8:17 am)
0

I wanted to show label to automatically adjust to the input values and have the ability to turn that off and on. To show M521 or 521M or turn it off to only show M.

( at June 21, 2018 11:28 am)
0

Ok, so we take my last example and we insert an if statement in the text portion of the label. Let’s say you have an input named: showEMAInLabel

AddLabel(yes, if showEMAInLabel then Concat(“M: “, Concat(Concat(length1, “/”), length2) else “M”, Color.WHITE);

( at June 21, 2018 12:30 pm)
0

I’m still confused on how to show EMA in label with the time “M” or to turn it off and only show M.
input length1 = 5;
input length2 = 21;
input price = close;
input displace = 0;
input showEMAInLabel = YES;
def na = Double.NaN;

plot fastema = ExpAverage(price[-displace], length1);
fastema.SetDefaultColor(Color.BLUE);
fastema.SetLineWeight(2);

plot slowema = ExpAverage(price[-displace], length2);
slowema.SetDefaultColor(Color.DARK_RED);
slowema.SetLineWeight(2);

def crossover = if fastema > slowema and fastema[1] = slowema[1] then 1 else 0;

plot LongTrigger = if crossover then low – TickSize() else na;
plot ShortTrigger = if crossunder then high + TickSize() else na;

LongTrigger.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
LongTrigger.SetLineWeight(3);
LongTrigger.SetDefaultColor(Color.GREEN);
LongTrigger.HideBubble();
LongTrigger.HideTitle();
ShortTrigger.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
ShortTrigger.SetLineWeight(3);
ShortTrigger.SetDefaultColor(Color.RED);
ShortTrigger.HideBubble();
ShortTrigger.HideTitle();

#Trigger alerts
Alert(crossover[1], “Long Trigger”, Alert.BAR, Sound.Ding);
Alert(crossunder[1], “Short Trigger”, Alert.BAR, Sound.Ding);

def P1 = AggregationPeriod.MONTH;
def ap = GetAggregationPeriod();

# P1 ——————-

def ema11;
def ema12;

if ap ema12 then Color.DARK_GREEN else if ema11 < ema12 then Color.DARK_RED else Color.GRAY);

( at June 28, 2018 12:27 pm)
0

There was a typo in my previous solution. Here is a corrected version. Just copy and paste this into your code and it works. The only thing you need to do is adjust the color parameter. I have it set to Color.WHITE to make sure I only present the logic that is required to change the text. To avoid it being confused with the logic required to change the color.

AddLabel(yes, if showEMAInLabel then Concat(“M: “, Concat(Concat(length1, “/”), length2)) else “M”, Color.WHITE);

In future, it will be much easier to provide a solution if you submit only the very minimal (yet fully functional) piece of code for review. Consider that you could have presented a fully functional code example that consisted of about 6 lines. Three inputs, two moving averages and a label. When ever you run into trouble. Always break things down into their smallest functional pieces. It makes things much easier to unravel.

( at June 28, 2018 12:45 pm)
0

Got it. I just need to adjust the logic to change it on the various parameters now. Thank you.

AddLabel(ap ema12 and ema11[1] = ema12[1] then Color.RED
else if ema11 > ema12 then Color.DARK_GREEN else if ema11 < ema12 then Color.DARK_RED else Color.GRAY);

( at June 28, 2018 1:09 pm)
0

Hi Ryan / Peter,
This is exactly what I was looking for. I wanted to be able to plot a bubble on the chart showing the 5/21 EMA crossover for different time frames. For example I am mostly looking at the 5 min charts of stocks. In that chart I want to be able to see different time frames of EMA crossover represented in a label (bubble) on the top of the chart without me having to switch the chart to a particular time frame.
Can you please post the final working code here. I think it should be called EMA crossover study bubble (label).
Thank you for this.
P.S. Peter thank you for all your work and this website.

( at July 3, 2018 8:53 am)
0

“final working code”…. I will leave that to Ryan. I will correct the terminology you are using though. Very important to note the difference between a chart label and a chart bubble. They are entirely different tools on the platform. So let’s not confuse them. What we are dealing with in this post is a chart label. Not a bubble.

You can read more here:
AddChartBubble(): http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Look—Feel/AddChartBubble.html

AddLabel(): http://toslc.thinkorswim.com/center/reference/thinkScript/Functions/Look—Feel/AddLabel.html

( at July 3, 2018 9:09 am)
0

Thank you Peter for clarifying that. Yes it is very important to get the terminology correct that way it makes it easy to search for codes or help. Thank you attaching the links, very helpful. Hopefully Ryan gets back to me. LOL

( at July 4, 2018 10:08 pm)
0

Sammy, I couldn’t get the add label to work properly with my variations needed. I have the input option to turn off and on but the labels to show which crossover I’m using, I wasn’t able to complete. I just keep a chart up with 5/21 and another with 8/21 and know myself which one is which.

( at July 10, 2018 12:42 pm)
0

Pete / Ryan. So what I am trying to do is when I pull up a daily chart (or any time frame), I would like to have a bunch of labels on top which can show me which time frame has the 5 / 21 EMA crossover. For example, I want to be able to see in the daily chart if the 1 hour, 4 hour, weekly, monthly ( or other time frames ) for the stock in that chart has the 5 EMA is over the 21 EMA and have the label be one color for when the 5 is over the 21 and be another color for when the 5 is below the 21. Would I be able to use a part of the study above for that? Can you help me with this please. Thank you.

( at July 12, 2018 8:26 pm)
0

I can confirm the specific details you just described are impossible to accomplish on any platform. Time frames can only be referenced in one direction. You can never reference a lower time frame from a higher one. So referencing a 1 hour time frame from a daily chart is always impossible.

( at July 12, 2018 9:08 pm)
0

Pete what I meant was that if I am viewing the daily chart, without switching it to a 1 hour chart for example. Can I add a label on the chart to indicate if the 1 hour ( or 30min, or even a weekly) time frames have the 5/21 crossover. So I want the label to tell me that without me switching to that time frame.
From reading your reply I think you thought I was asking if 2 different time frames were crossing. Not asking that.
Sorry for the confusion. Thanks for your reply.

( at July 13, 2018 7:34 am)
0

No, I understood you perfectly. I will try to state this again. Time frames only work in one direction. We can show daily time frame indicators on a 1 hour chart. But it is impossible to show a 1 hour time frame indicator on a daily chart. This is common to all trading platforms. It is practically a law of physics. I have published plenty of material on multiple time frames. But in 100% of them I am working from a lower time frame chart, to plot/display higher time frame data. It is impossible to go the other direction.

( at July 13, 2018 7:42 am)
0

Oh ok got it. Ok so can I ask for what the code would be if I only view the chart on a 1 min time frame and can add label to view on a higher time frame and multiple of them, like 5 min, 30 min, 1 hour, daily, weekly, monthly. Would this be possible?

( at July 13, 2018 7:48 am)
0

Yes, this is possible. You can post a new request for this in the forum. This would be posted in the Chart Studies topic. And your question title should be between 6-8 words and clearly describe the context of your request. Try to include a phrase that you would use to search for a solution on Google.

( at July 13, 2018 8:01 am)
0

Ok great. Thank you. Will do it over the weekend.

( at July 13, 2018 8:30 am)
0

Hi Pete, I have added the question under Chart Studies.
“Add Label for 2 different EMA crossover”
I tried my best to phrase it so it can be searched in Google.
Thank you VERY MUCH for your help.

( at July 17, 2018 8:28 am)