Display Moving Average (EMA or SMA) as Labels in ThinkorSwim

@acjtumio1987 Modify this to suit your purposes. Instead of "3 above 9" change the code so that it is "Price is Above or Below MA" Follow through the logic and you should be able to modify this to do what you want.

Code:
# SD_MARelationLabel
# Scott D
# Version: 01
# Original: November 17, 2020
# Last Mod: November 17, 2020


input MAshort = 3;
input MAlong = 9;
input MAtwenty = 20;
input MAfifty = 50;

def MA1 = movAvgExponential(close, MAshort);
def MA2 = movAvgExponential(close, MAlong);
def MA3 = movAvgExponential(close, MAtwenty);
def MA4 = movAvgExponential(close, MAfifty);

AddLabel(yes, if MA1 > MA1[1] then " 3 EMA RISING " else " 3 EMA FALLING ", if MA1 > MA1[1] then Color.GREEN else Color.RED);

AddLabel(yes, if MA2 > MA2[1] then " 9 EMA RISING " else " 9 EMA FALLING ", if MA2 > MA2[1] then Color.GREEN else Color.RED);

AddLabel(yes, if MA1 > MA2 then " 3 ABOVE 9 " else " 3 BELOW 9 ", if MA1 > MA2 then Color.GREEN else Color.RED);

AddLabel(yes, if MA3 > MA3[1] then " 20 EMA RISING " else " 20 EMA FALLING ", if MA3 > MA3[1] then Color.GREEN else Color.RED);

AddLabel(yes, if MA1 > MA3 then " 3 ABOVE 20 " else " 3 BELOW 20 ", if MA1 > MA3 then Color.GREEN else Color.RED);

AddLabel(yes, if MA4 > MA4[1] then " 50 EMA RISING " else " 50 EMA FALLING ", if MA4 > MA4[1] then Color.GREEN else Color.RED);

AddLabel(yes, if MA1 > MA4 then " 3 ABOVE 50 " else " 3 BELOW 50 ", if MA1 > MA4 then Color.GREEN else Color.RED);

### End Code ###
 
Modify this to suit your purposes. Instead of "3 above 9" change the code so that it is "Price is Above or Below MA" Follow through the logic and you should be able to modify this to do what you want.

Code:
# SD_MARelationLabel
# Scott D
# Version: 01
# Original: November 17, 2020
# Last Mod: November 17, 2020


input MAshort = 3;
input MAlong = 9;
input MAtwenty = 20;
input MAfifty = 50;

def MA1 = movAvgExponential(close, MAshort);
def MA2 = movAvgExponential(close, MAlong);
def MA3 = movAvgExponential(close, MAtwenty);
def MA4 = movAvgExponential(close, MAfifty);

AddLabel(yes, if MA1 > MA1[1] then " 3 EMA RISING " else " 3 EMA FALLING ", if MA1 > MA1[1] then Color.GREEN else Color.RED);

AddLabel(yes, if MA2 > MA2[1] then " 9 EMA RISING " else " 9 EMA FALLING ", if MA2 > MA2[1] then Color.GREEN else Color.RED);

AddLabel(yes, if MA1 > MA2 then " 3 ABOVE 9 " else " 3 BELOW 9 ", if MA1 > MA2 then Color.GREEN else Color.RED);

AddLabel(yes, if MA3 > MA3[1] then " 20 EMA RISING " else " 20 EMA FALLING ", if MA3 > MA3[1] then Color.GREEN else Color.RED);

AddLabel(yes, if MA1 > MA3 then " 3 ABOVE 20 " else " 3 BELOW 20 ", if MA1 > MA3 then Color.GREEN else Color.RED);

AddLabel(yes, if MA4 > MA4[1] then " 50 EMA RISING " else " 50 EMA FALLING ", if MA4 > MA4[1] then Color.GREEN else Color.RED);

AddLabel(yes, if MA1 > MA4 then " 3 ABOVE 50 " else " 3 BELOW 50 ", if MA1 > MA4 then Color.GREEN else Color.RED);

### End Code ###
Thank you
 
I trade on a 1-minute chart for scalp. 45 EMA act like 9 ema on a 5-minute chart. This way I don't have to look at the 5-minute chart. I'm not a coder, just simply copy-pasted few scripts together. I felt great making something for the first time, I only take calls if above 45 ema and puts below. I also use 9/20ema for entry and exit. Hope someone finds this script valuable, Happy Sunday everyone!!

plot EMA = ExpAverage(close,45);
EMA.AssignValueColor(if EMA > EMA[1] then Color.GREEN else Color.RED);

input MAfortyfive = 45;
def MA = movAvgExponential(close, MAfortyfive);
AddLabel(yes, if MA > MA[1] then "Above 45 EMA(Calls only) " else " Below 45 EMA(Puts only) ", if MA > MA[1] then Color.GREEN else Color.RED);

### End Code ###
 
  • Like
Reactions: ALV
@s1111 Nice idea bumping to EMA45 but why are you calculating the EMA twice when you could just as easily use the results from one calculation throughout the entire script...??? That would optimize the efficiency...

Ruby:
plot EMA = ExpAverage(close,45);

EMA.AssignValueColor(if EMA > EMA[1] then Color.GREEN else Color.RED);

AddLabel(yes, if EMA > EMA[1] then "Above 45 EMA(Calls only) " else " Below 45 EMA(Puts only) ", if EMA > EMA[1] then Color.GREEN else Color.RED);
 
So i've searched high and low for a while and have come to the conclusion that people are using scripts for this. What i'm trying to achieve is displaying various values in a label at the top of my charts such as:

13EMA, 50SMA, 200SMA, VWAP, Volume and possibly a pre market high and pre market low label and values to start.

Could someone help me out with this. New here and look forward to learning from the community.

Thanks in advance.
 
@DarthVaider Labels are very simple and easy to code in ThinkScript. They may even be the easiest part of ThinkScript. The official documentation is here - https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddLabel
@BenTen has also created our own reference here -
https://usethinkscript.com/resources/thinkscript-addlabel-adding-custom-labels-to-your-chart.8/

As a simple example, the basic label format is
AddLabel(1, your data, the color);

"1" is just boolean, i.e, true or false aka yes or no. It just says if the label should show or not. If you want the label to not show, put "0".
"your data" is where you put what you want the label to show.
"the color" is where you tell the label what color you want it.

There's a overwhelming amount of labels here on the forum as examples. Some have more complexity with the conditions than others, but they all follow the same basic method. If you want even more examples, go here - JQ's OneNote. Try your hand at it, it likely is easier than you think.
 
@DarthVaider Labels are very simple and easy to code in ThinkScript. They may even be the easiest part of ThinkScript. The official documentation is here - https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/AddLabel
@BenTen has also created our own reference here -
https://usethinkscript.com/resources/thinkscript-addlabel-adding-custom-labels-to-your-chart.8/

As a simple example, the basic label format is
AddLabel(1, your data, the color);

"1" is just boolean, i.e, true or false aka yes or no. It just says if the label should show or not. If you want the label to not show, put "0".
"your data" is where you put what you want the label to show.
"the color" is where you tell the label what color you want it.

There's a overwhelming amount of labels here on the forum as examples. Some have more complexity with the conditions than others, but they all follow the same basic method. If you want even more examples, go here - JQ's OneNote. Try your hand at it, it likely is easier than you think.


Thanks for that information. The issue I am now having is the labels are showing the incorrect values. I figured they would show the exact values in which the SMA/EMA lines are showing but they are totally different. Might you know how that can be fixed? I did start another thread for that in hopes to get some help their.
 
This indicator will help you display the following exponential moving average (EMA) and simple moving average (SMA) as labels on your chart.
  • 9, 49, and 99 EMA
  • 20 and 200 SMA
One of our users @Raundx modified it to the following:
  • 4, 9 13, 50, and 200 EMA
  • 20 and 200 SMA
I will include both versions down below and let you pick which one you want to use.

OR9Bf3D.png


Original labels

Code:
#
# SM_MovingAverageLabels
#
# version 1.0
#

#Default moving average values:
#EXPO 9 OHLC
#SIMP 20 OHLC
#EXPO 49 OHLC
#EXPO 99 OHLC
#SIMP 200 OHLC

input MA_1_length = 9; #exponential
input MA_2_length = 20; #simple
input MA_3_length = 49; #exponential
input MA_4_length = 99; #exponential
input MA_5_length = 200; #simple

input MA_1_closeType = ohlc4;
input MA_2_closeType = ohlc4;
input MA_3_closeType = ohlc4;
input MA_4_closeType = ohlc4;
input MA_5_closeType = ohlc4;

def MA_1_avg = ExpAverage(MA_1_closeType, MA_1_length);
def MA_2_avg = Average(MA_2_closeType, MA_2_length);
def MA_3_avg = ExpAverage(MA_3_closeType, MA_3_length);
def MA_4_avg = ExpAverage(MA_4_closeType, MA_4_length);
def MA_5_avg = Average(MA_5_closeType, MA_5_length);

def currentPrice = close;

def MA_1_above = if currentPrice > MA_1_avg then 1 else 0;
def MA_1_below = if currentPrice <= MA_1_avg then 1 else 0;
AddLabel(MA_1_above, MA_1_length + " ema: " + MA_1_avg, COLOR.DARK_GREEN);
AddLabel(MA_1_below, MA_1_length + " ema: " + MA_1_avg, COLOR.DARK_RED);

def MA_2_above = if currentPrice > MA_2_avg then 1 else 0;
def MA_2_below = if currentPrice <= MA_2_avg then 1 else 0;
AddLabel(MA_2_above, MA_2_length + " sma: " + MA_2_avg, COLOR.DARK_GREEN);
AddLabel(MA_2_below, MA_2_length + " sma: " + MA_2_avg, COLOR.DARK_RED);

def MA_3_above = if currentPrice > MA_3_avg then 1 else 0;
def MA_3_below = if currentPrice <= MA_3_avg then 1 else 0;
AddLabel(MA_3_above, MA_3_length + " ema: " + MA_3_avg, COLOR.DARK_GREEN);
AddLabel(MA_3_below, MA_3_length + " ema: " + MA_3_avg, COLOR.DARK_RED);

def MA_4_above = if currentPrice > MA_4_avg then 1 else 0;
def MA_4_below = if currentPrice <= MA_4_avg then 1 else 0;
AddLabel(MA_4_above, MA_4_length + " ema: " + MA_4_avg, COLOR.DARK_GREEN);
AddLabel(MA_4_below, MA_4_length + " ema: " + MA_4_avg, COLOR.DARK_RED);

def MA_5_above = if currentPrice > MA_5_avg then 1 else 0;
def MA_5_below = if currentPrice <= MA_5_avg then 1 else 0;
AddLabel(MA_4_above, MA_5_length + " sma: " + MA_5_avg, COLOR.DARK_GREEN);
AddLabel(MA_4_below, MA_5_length + " sma: " + MA_5_avg, COLOR.DARK_RED);

Modified labels

Code:
#
# SM_MovingAverageLabels
#
# version 1.0
#

#Default moving average values:
#EXPO 4 OHLC
#EXPO 9 OHLC
#EXPO 13 OHLC
#SIMP 20 OHLC
#EXPO 50 OHLC
#EXPO 200 OHLC
#SIMP 200 OHLC

input MA_1_length = 4; #exponential
input MA_2_length = 9; #exponential
input MA_3_length = 13; #exponential
input MA_4_length = 20; #simple
input MA_5_length = 50; #exponential
input MA_6_length = 200; #exponential
input MA_7_length = 200; #simple


input MA_1_closeType = ohlc4;
input MA_2_closeType = ohlc4;
input MA_3_closeType = ohlc4;
input MA_4_closeType = ohlc4;
input MA_5_closeType = ohlc4;
input MA_6_closeType = ohlc4;
input MA_7_closeType = ohlc4;

def MA_1_avg = ExpAverage(MA_1_closeType, MA_1_length);
def MA_2_avg = ExpAverage(MA_2_closeType, MA_2_length);
def MA_3_avg = ExpAverage(MA_3_closeType, MA_3_length);
def MA_4_avg = Average(MA_4_closeType, MA_4_length);
def MA_5_avg = ExpAverage(MA_5_closeType, MA_5_length);
def MA_6_avg = ExpAverage(MA_6_closeType, MA_6_length);
def MA_7_avg = Average(MA_7_closeType, MA_7_length);

def currentPrice = close;

def MA_1_above = if currentPrice > MA_1_avg then 1 else 0;
def MA_1_below = if currentPrice <= MA_1_avg then 1 else 0;
AddLabel(MA_1_above, MA_1_length + " EMA: " + MA_1_avg, COLOR. CYAN);
AddLabel(MA_1_below, MA_1_length + " EMA: " + MA_1_avg, COLOR. CYAN);

def MA_2_above = if currentPrice > MA_2_avg then 1 else 0;
def MA_2_below = if currentPrice <= MA_2_avg then 1 else 0;
AddLabel(MA_2_above, MA_2_length + " EMA: " + MA_2_avg, COLOR. YELLOW);
AddLabel(MA_2_below, MA_2_length + " EMA: " + MA_2_avg, COLOR. YELLOW);

def MA_3_above = if currentPrice > MA_3_avg then 1 else 0;
def MA_3_below = if currentPrice <= MA_3_avg then 1 else 0;
AddLabel(MA_3_above, MA_3_length + " EMA: " + MA_3_avg, COLOR. VIOLET);
AddLabel(MA_3_below, MA_3_length + " EMA: " + MA_3_avg, COLOR. VIOLET);

def MA_4_above = if currentPrice > MA_4_avg then 1 else 0;
def MA_4_below = if currentPrice <= MA_4_avg then 1 else 0;
AddLabel(MA_4_above, MA_4_length + " SMA: " + MA_4_avg, COLOR. PINK);
AddLabel(MA_4_below, MA_4_length + " SMA: " + MA_4_avg, COLOR. PINK);

def MA_5_above = if currentPrice > MA_5_avg then 1 else 0;
def MA_5_below = if currentPrice <= MA_5_avg then 1 else 0;
AddLabel(MA_5_above, MA_5_length + " EMA: " + MA_5_avg, COLOR.WHITE);
AddLabel(MA_5_below, MA_5_length + " EMA: " + MA_5_avg, COLOR.WHITE);

def MA_6_above = if currentPrice > MA_6_avg then 1 else 0;
def MA_6_below = if currentPrice <= MA_6_avg then 1 else 0;
AddLabel(MA_6_above, MA_6_length + " EMA: " + MA_6_avg, COLOR.LIGHT_GREEN);
AddLabel(MA_6_below, MA_6_length + " EMA: " + MA_6_avg, COLOR.LIGHT_GREEN);

def MA_7_above = if currentPrice > MA_7_avg then 1 else 0;
def MA_7_below = if currentPrice <= MA_7_avg then 1 else 0;
AddLabel(MA_7_above, MA_7_length + " SMA: " + MA_7_avg, COLOR.LIGHT_RED);
AddLabel(MA_7_below, MA_7_length + " SMA: " + MA_7_avg, COLOR.LIGHT_RED);

Credit:
hi @BenTen Hope you're well. Is it possible to display the MA labels for 1hr and 4hr on a chart regardless of the current timeframe?
 
hi @BenTen Hope you're well. Is it possible to display the MA labels for 1hr and 4hr on a chart regardless of the current timeframe?

They can be displayed on any chart using a timeframe lower than one hour... You can use higher aggregations on lower timeframes but not lower on higher...
 
Good Evening. Searching for a script that will provide daily 5,10, 20, 50, 200 SMA Chart Labels in the top left corner of the chart (similar to volume labels). Any help would be greatly appreciated!
 
Hey guys, I have this script that shows the 5M 9MA on my 1m chart, but i cant get it to show the bubble, even tho i have it checked in the settings. Is there a way to add it thru script? It will only show the bubble if i hover my cursor over the plotline. Also, I am only trying to get the label and bubble on the screen without the plot line. Can someone help? (Note: the only reason i have Plot AVG, is b/c i modified a code from someone else, and i don't know how to add the label without it)

# MTF Moving Average

input Period = aggregationPeriod.FIVE_MIN;
input AvgType = averageType.EXPONENTIAL;
input Length = 9;
input priceclose = close;
input ShowMAlabel = yes;

plot AVG = MovingAverage(AvgType, close(period = Period), Length);
AVG.setdefaultcolor(color.yellow);

Addlabel(ShowMAlabel, " 5M9 = "+ round(AVG) + " ", COLOR.GREEN);
 
@BasicBobby This code should remove the historical moving average plot from the chart but will keep the side bubble -
Code:
input Period = aggregationPeriod.FIVE_MIN;
input AvgType = averageType.EXPONENTIAL;
input Length = 9;
input ShowMAlabel = yes;

def AVG = MovingAverage(AvgType, close(period = Period), Length);

plot movavgbubble = if !isnan(close) and isnan(close[-1]) then avg else double.nan;
movavgbubble.setdefaultcolor(color.green);

Addlabel(ShowMAlabel, " 5M9 = "+ round(AVG) + " ", COLOR.GREEN);

To have it visible without hovering over it with your cursor, go to "Edit Studies" and move the indicator to the highest position possible on the upper pane; this will give it precedence over any other indicators. You may have to uncheck the "show last price bubble" in "Chart Settings" --> "General" as well if price is trading at the moving average area.
 
@BasicBobby This code should remove the historical moving average plot from the chart but will keep the side bubble -
Code:
input Period = aggregationPeriod.FIVE_MIN;
input AvgType = averageType.EXPONENTIAL;
input Length = 9;
input ShowMAlabel = yes;

def AVG = MovingAverage(AvgType, close(period = Period), Length);

plot movavgbubble = if !isnan(close) and isnan(close[-1]) then avg else double.nan;
movavgbubble.setdefaultcolor(color.green);

Addlabel(ShowMAlabel, " 5M9 = "+ round(AVG) + " ", COLOR.GREEN);

To have it visible without hovering over it with your cursor, go to "Edit Studies" and move the indicator to the highest position possible on the upper pane; this will give it precedence over any other indicators. You may have to uncheck the "show last price bubble" in "Chart Settings" --> "General" as well if price is trading at the moving average area.
Thank you so much! Works perfectly
 
@BasicBobby This code should remove the historical moving average plot from the chart but will keep the side bubble -
Code:
input Period = aggregationPeriod.FIVE_MIN;
input AvgType = averageType.EXPONENTIAL;
input Length = 9;
input ShowMAlabel = yes;

def AVG = MovingAverage(AvgType, close(period = Period), Length);

plot movavgbubble = if !isnan(close) and isnan(close[-1]) then avg else double.nan;
movavgbubble.setdefaultcolor(color.green);

Addlabel(ShowMAlabel, " 5M9 = "+ round(AVG) + " ", COLOR.GREEN);

To have it visible without hovering over it with your cursor, go to "Edit Studies" and move the indicator to the highest position possible on the upper pane; this will give it precedence over any other indicators. You may have to uncheck the "show last price bubble" in "Chart Settings" --> "General" as well if price is trading at the moving average area.
Actually, could you also help me add a code to where if it gets near or at the MA, the label changes color? Thanks in advance
 
Actually, could you also help me add a code to where if it gets near or at the MA, the label changes color? Thanks in advance
Sure, but you will need to specify how you want it. When you say " . . . if it gets near or at the MA . . .", what does that mean? Something like "within the upper and lower lines of a 5 period exponential keltner channel" ? Or, "below the 5-period average of the highs yet also above the 5-period average of the lows" ? I would need something concrete to work with. Preferably based off a measure of volatility or range, as using percentages doesn't carry over well between different stocks without continually adjusting the percentage.
 
Sure, but you will need to specify how you want it. When you say " . . . if it gets near or at the MA . . .", what does that mean? Something like "within the upper and lower lines of a 5 period exponential keltner channel" ? Or, "below the 5-period average of the highs yet also above the 5-period average of the lows" ? I would need something concrete to work with. Preferably based off a measure of volatility or range, as using percentages doesn't carry over well between different stocks without continually adjusting the percentage.
Sorry, im not really familiar with measures of volatility other than setting a fixed percentage, but i understand what u mean. Is it possible to just change the label color when it touches the MA then, instead of getting close/near it?
 
Sorry, im not really familiar with measures of volatility other than setting a fixed percentage, but i understand what u mean. Is it possible to just change the label color when it touches the MA then, instead of getting close/near it?
@BasicBobby I can, but first try this. It is essentially a keltner channel type of indicator in that it uses a ATR distance above and below the moving average to create a channel that price moves in and out of, and changes the color of the label. This is a rough draft, I kept all the plots visible so you can see what it is doing. Try it out and let me know if this is similar to what you are wanting - I can then hide the plots, and make any other changes.
Code:
input Period = aggregationPeriod.FIVE_MIN;
input AvgType = averageType.EXPONENTIAL;
input Length = 9;
input ShowMAlabel = yes;
input ATR_distance = 0.25;

def c = close(period = Period);
def h = high(period = Period);
def l = low(period = Period);

plot AVG = MovingAverage(AvgType, c, Length);

plot AVG_hi = avg + ATR_distance * MovingAverage(AvgType, TrueRange(h, c, l), length);
plot AVG_lo = avg - ATR_distance * MovingAverage(AvgType, TrueRange(h, c, l), length);

plot movavgbubble = if !isnan(close) and isnan(close[-1]) then avg else double.nan;
movavgbubble.setdefaultcolor(color.green);

Addlabel(ShowMAlabel, " 5M9 = "+ round(AVG) + " ", if close < avg_lo or close > avg_hi then COLOR.GREEN else color.red);

You can adjust the "ATR_distance" input to make the alert area wider or tighter according to your needs.
 

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
241 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

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?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top