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

@Likos Does anyone know how to make this script? I'd like to see these daily sma/ema numbers on a 1 min and 5 min chart! I'm only looking for the ma and not the volume.
 
Last edited:

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

I created these labels to reduce the lines on my chart. When price is in an uptrend - bullish above the various MA the labels turn Green and vice-versa when price is bearish.

Code:
#BRANCH INVESTMENT GROUP (BIG) ------------------------------------------------
#NOVEMBER 3, 2020

#MOVING AVERAGES-----------------------------------------------------------------
input length2 = 2;
input length5 = 5;
input length8 = 8;
input length10 = 10;
input length13 = 13;
input length21 = 21;
input length35 = 35;
input length50 = 50;
input length100 = 100;
input length200 = 200;
input over_Bought = 80;
input over_Sold = 20;
input price = close;
input averageType = AverageType.SIMPLE;
input showBreakoutSignals = no;
input averageType1 = AverageType.SIMPLE;

input crossingType = {default above, below};

def NetChgAvg = MovingAverage(averageType, price - price[1], length10);
def TotChgAvg = MovingAverage(averageType, AbsValue(price - price[1]), length10);
def ChgRatio = if TotChgAvg != 0 then NetChgAvg / TotChgAvg else 0;
def avg2 = MovingAverage(averageType1, price, length2);
def avg5 = MovingAverage(averageType1, price, length5);
def avg8 = MovingAverage(averageType1, price, length8);
def avg10 = MovingAverage(averageType1, price, length10);
def avg13 = MovingAverage(averageType1, price, length13);
def avg21 = MovingAverage(averageType1, price, length21);
def avg35 = MovingAverage(averageType1, price, length35);
def avg50 = MovingAverage(averageType1, price, length50);
def avg100 = MovingAverage(averageType1, price, length100);
def avg200 = MovingAverage(averageType1, price, length200);

#MOVING AVERGERS LABELS-----------------------------------------------------------------------------------
AddLabel (yes, if close then " [MOVING AVERAGES]" else "", Color.CYAN);
AddLabel(yes, if close < avg2 then " (2)  " +  Average (close, 2) else "", Color.RED);
AddLabel (yes, if close > avg2 then " (2)  " +  Average (close, 2) else "", Color.GREEN);
AddLabel(yes, if close < avg5 then " (5)  " +  Average (close, 5) else "", Color.RED);
AddLabel (yes, if close > avg5 then " (5)  " +  Average (close, 5) else "", Color.GREEN);
AddLabel(yes, if close < avg8 then " (8)  " +  Average (close, 8) else "", Color.RED);
AddLabel (yes, if close > avg8 then " (8)  " +  Average (close, 8) else "", Color.GREEN);
AddLabel(yes, if close < avg10 then " (10)  " +  Average (close, 10) else "", Color.RED);
AddLabel (yes, if close > avg10 then " (10)  " +  Average (close, 10) else "", Color.GREEN);
AddLabel(yes, if close < avg13 then " (13)  " +  Average (close, 13) else "", Color.RED);
AddLabel (yes, if close > avg13 then " (13)  " +  Average (close, 13) else "", Color.GREEN);
AddLabel (yes, if close < avg21 then " (21)  " +  Average (close, 21) else "", Color.RED);
AddLabel (yes, if close > avg21 then " (21)  " +  Average (close, 21) else "", Color.GREEN);
AddLabel (yes, if close < avg35 then " (35)  " +  Average (close, 35) else "", Color.RED);
AddLabel (yes, if close > avg35 then " (35)  " +  Average (close, 35) else "", Color.GREEN);
AddLabel (yes, if close < avg50 then " (50)  " +  Average (close, 50) else "", Color.RED);
AddLabel (yes, if close > avg50 then " (50)  " +  Average (close, 50) else "", Color.GREEN);
AddLabel (yes, if close < avg100 then " (100)  " +  Average (close, 100) else "", Color.RED);
AddLabel (yes, if close > avg100 then " (100)  " +  Average (close, 100) else "", Color.GREEN);
AddLabel (yes, if close < avg200 then " (200)  " +  Average (close, 200) else "", Color.RED);
AddLabel (yes, if close > avg200 then " (200)  " +  Average (close, 200) else "", Color.GREEN);

#THE END------------------------------------------------------------------------------------------------------------
 
Let's try this......BTW,....no idea where I found this? I must have a thousand scripts,.....hope it's not something posted here already or you wrote it? Hahahaha,.....

Code:
input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};

def cap = getAggregationPeriod();
def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);
} else {
    volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
    volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
    volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot VWAP = price;
plot UpperBand = price + numDevUp * deviation;
plot LowerBand = price + numDevDn * deviation;

VWAP.setDefaultColor(Color.Yellow);
VWAP.setLineWeight(2);
VWAP.hide();
UpperBand.setDefaultColor(getColor(2));
UpperBand.hide();
LowerBand.setDefaultColor(getColor(4));
LowerBand.hide();

AddLabel(yes," VWAP: "+AsText(VWAP, NumberFormat.TWO_DECIMAL_PLACES),Color.Dark_Orange);

plot EMA12 = ExpAverage(HLC3,12);#8
EMA12.setDefaultColor(Getcolor(1));
EMA12.setLineWeight(2);

plot SMA26 = simplemovingAvg(close,26);#20
SMA26.setDefaultColor(Getcolor(6));
SMA26.setLineWeight(2);

plot SMA50 = expAverage(close,50);
sMA50.setDefaultColor(createColor(255,155,21));
sMA50.setLineWeight(2);

plot SMA200 = simplemovingAvg(low,200);
SMA200.setDefaultColor(Getcolor(5));
SMA200.setLineWeight(2);


AddLabel(yes," 12 EMA: "+AsText(ExpAverage(HLC3,12), NumberFormat.TWO_DECIMAL_PLACES)+ " ",Color.Cyan);

AddLabel(yes," 26 SMA: "+AsText(simplemovingAvg(close,26), NumberFormat.TWO_DECIMAL_PLACES)+ " ",Color.Green);

AddLabel(yes," 50sMA: "+AsText(expAverage(close,50), NumberFormat.TWO_DECIMAL_PLACES),createColor(255,155,21));

AddLabel(yes," 200SMA: "+AsText(simpleMovingAvg(close,200), NumberFormat.TWO_DECIMAL_PLACES),CreateColor(255,90,51));


Input fastLength = 12;#8
Input slowLength = 26;#20
Input MACDLength = 50;#8
Input averageType = AverageType.EXPONENTIAL;


Def MACD1 = MACD(fastLength, slowLength, MACDLength, averageType).Diff;
Def MACD = round(MACD1,3);


Def bullish = MACD > 0;


AddLabel (yes, “ MACD: “ + MACD, if bullish then color.Green else if !bullish then color.Dark_Green else color.RED);
 
@islandcowgirl I only have a minute. Try the below. This is for learning.
3Nw9hjp.png

This should be tried where ever MA_1... is located. Try one thing at a time.
Change the "ohlc4" to "close".
Change "MA_1_length); to "MA_1_length, 2)"
Lastly, put a ", 2 " Before and/or after "color.cyan" )
Sorry, on mobile.
Hi, I was trying to remove ALL decimals from the MA labels by putting , 0, but keep encountering errors. Can anyone advise how to modify the script properly please? Thanks!
 
Hi, I was trying to remove ALL decimals from the MA labels by putting , 0, but keep encountering errors. Can anyone advise how to modify the script properly please? Thanks!

Without testing, I believe you'd want to use either Round(price, 0) OR AsPrice(Round(price, 0))... The first function works well everywhere except Watchlist Columns and the second should work pretty much everywhere...
 
im banging my head against a wall over here right now. I want to add my 5 different SMA to my addable, I want them to be the same color I have as the are on my chart.

im using DefineGlobalColors to make the colors, and I can't find a way to do it without writing if then else and changing the color depending on the stocks price relative to the moving average.

Code:
input price_SMA = close;
input length_10  = 10;
input length_20  = 20;
input length_50  = 50;
input length_100 = 100;
input length_200 = 200;
input displace = 0;

#10 day SMA
def SMA_10 = Average(price_SMA[-displace], length_10);
DefineGlobalColor("SMA10",  CreateColor(255, 0, 0)) ;

I have a huge string of add labels to work on, this particular code is on line 250 of my massive add label study. Been trying different combos of phrases for the last 30 minutes but can't get it to work. am also tired from driving the last 2 days, and I have the chapter 9 page up and the add label page up on thinkscript definition page.
 
@mourningwood4521 You didn't post the entire script so it's kind of hard to see what you're doing... Another option would be to search for my EMA_x5_Stack Study and see if that's easier to adapt to your needs...
 
@rad14733 all I want is for my moving average study to show on the add labels, I use the same color for my 10, 20,50,100,200 day SMA. I just want it to show up as "SMA 10 " + the value of SMA_10. I want the color to be red no matter what the value is. I want "SMA_20" + value of SMA_20, color to be green. when I type it in formatted, its not taking my code. I just want to see my moving averages as an addable instead of having to pull up that chart.

I peeped your stack x5 EMA study and it doesn't fit my needs for this.

I got it!!! im way too exhausted for this.

Code:
def SMA_10 = Average(price_SMA[-displace], length_10);
DefineGlobalColor("SMA10",  CreateColor(255, 0, 0)) ;
addlabel( yes, "SMA(10) " + SMA_10, globalcolor("SMA10"));
 
@mourningwood4521 Not sure if you've seen it but there is at least one MA Dashboard posted here in the forums that might be worth your review... Please share your final result...
 
@rad14733

How to add Moving average as is to addlabel?

the below shows the simple moving average as an addlabel on a chart.

Code:
input price_SMA = close;
input length_10  = 10;
input length_20  = 20;
input length_50  = 50;
input length_100 = 100;
input length_200 = 200;
input displace = 0;
#---------------
#10 day SMA
def SMA_10 = Average(price_SMA[-displace], length_10);
DefineGlobalColor("SMA10",  CreateColor(255, 0, 0)) ;
addlabel( yes, "SMA(10) " + SMA_10, globalcolor("SMA10"));
#20 day SMA
def SMA_20 = Average(price_SMA[-displace], length_20);
DefineGlobalColor("SMA20",  CreateColor(0, 197, 49)) ;
addlabel( yes, "SMA(20) " + SMA_20, globalcolor("SMA20"));
#50 day SMA
def SMA_50 = Average(price_SMA[-displace], length_50);
DefineGlobalColor("SMA50",  CreateColor(0, 255, 255)) ;
addlabel( yes, "SMA(50) " + SMA_50, globalcolor("SMA50"));
# 100 day SMA
def SMA_100 = Average(price_SMA[-displace], length_100);
DefineGlobalColor("SMA100",  CreateColor(255, 255, 51)) ;
addlabel( yes, "SMA(100) " + SMA_100, globalcolor("SMA100"));
#200 day SMA
def SMA_200 = Average(price_SMA[-displace], length_200);
DefineGlobalColor("SMA200",  CreateColor(255, 126, 156)) ;
addlabel( yes, "SMA(200) " + SMA_200, globalcolor("SMA200"));
 
@Optionsguy change the color to your desired color. you may have to change the terms to fit your definitions and inputs. you may also need to take a few lines out.

Code:
DefineGlobalColor("Labeloverbought",  CreateColor(204, 0, 0)) ;
DefineGlobalColor("Labelnormal",  CreateColor(225, 225, 225));
DefineGlobalColor("LabelOversold",  CreateColor(0, 204, 0));
DefineGlobalColor("labelslightred",  CreateColor(225, 51, 51));
DefineGlobalColor("labelslightgreen",  CreateColor(0, 153, 0));
AddLabel(yes,
if RSI5 is true then "RSI(5) " + RSI5 else "",
if RSI5 > over_Bought_70 then GlobalColor("Labeloverbought") else
if RSI5 < over_Sold_70   then GlobalColor("Labeloversold") else
if RSI5 > 55 and RSI5 < over_Bought_70  then GlobalColor("labelslightred") else
if RSI5 < 45 and RSI5 > over_Sold_70 then GlobalColor("labelslightgreen") else GlobalColor("Labelnormal"));
 
I have these moving average labels that I want to improve slightly. When the price is above the(for example) 9EMA I want to add an UpSignal arrow to the label that I have for the 9EMA. Then when the close is below the 9EMA I want a DownSignal arrow. I already have the code for the different labels depending on whether the close is above or below the 9EMA I just want to add the Up/DownSignal arrow.
 
@TJ_O FYI, if you provide the study that you are using and a screenshot of the chart, someone would be able to provide a real-time answer.
When you post a question without that concrete data. We can only provide hypothetical answers.

The more accepted practice is to color your labels to reflect above or below ema.
However, I have seen icons used. Below is a hypothetical snippet, it cannot be used as is because you did not provide the code to be labeled. To use: change out the thumbs for arrow icons and change the filter to whatever logic is in your label. Play w/ it... You can't break anything.
Ruby:
AddLabel(yes, if Bulltrigger then "👍" else if BearTrigger then "👎" else " ", if BearTrigger  then Color.dark_red else if Bulltrigger then Color.dark_GREEN else Color.ORANGE);
HTH
 
Last edited:
I am trying to add a label that will show the current difference between an 8ema and a 50 ema.

ie (8 ema is 60) (5o ema is 65) label show "ema diff" +5 (or just 5 and then -5 if values were reversed.
 
@earlyinout You are asking for a dollar difference? You do realize that if a $10 stock has a difference of $5 that would mean something totally different than if a $500 stock has a $5 difference. That's why you will see most strategies using percentages or standard deviations to determine if a difference is statistically significant. You certainly can do what you asking and below is the code written to your specifications. But you might want to reconsider if this is really what you want.
Ruby:
# ########################################################
# Moving Averages
def avg1 = MovingAverage(AverageType.eXPONENTIAL, HL2, 8);
def avg2 = MovingAverage(AverageType.eXPONENTIAL, HL2, 50);
def diff = avg2 - avg1 ;
AddLabel(yes, "ema diff: " +round(diff,2), if diff>0 then color.green else color.red);
# ########################################################
HTH
 
Last edited:
@earlyinout You are asking for a dollar difference? You do realize that if a $10 stock has a difference of $5 that would mean something totally different than if a $500 stock has a $5 difference. That's why you will see most strategies using percentages or standard deviations to determine if a difference is statistically significant. You certainly can do what you asking and below is the code written to your specifications. But you might want to reconsider if this is really what you want.
Ruby:
# ########################################################
# Moving Averages
def avg1 = MovingAverage(AverageType.eXPONENTIAL, HL2, 8);
def avg2 = MovingAverage(AverageType.eXPONENTIAL, HL2, 50);
def diff = avg2 - avg1 ;
AddLabel(yes, "ema diff: " +round(diff,2), if diff>0 then color.green else color.red);
# ########################################################
HTH
Thank you works perfectly!!!
 
I like to use several EMAs on my charts. I use a 8, 13, 21, 34, 55, 89, and 200.

I use other upper indicators where it makes my chart rather busy.

Is there a way to perhaps put stat bubbles on the left top of my chart when the price is above or below those EMAs, like Green when it's above and red when it's below?
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
181 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