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

@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.
Works reallly well, Thanks! Can it also be added to the code that it changes color when it touches the MA too?
 

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

Works reallly well, Thanks! Can it also be added to the code that it changes color when it touches the MA too?
@BasicBobby I'm glad you like it! This code should make the label turn a light blue color if price is equal to/touches the moving average. It probably will be very quick in changing color or "blink", especially on active stocks. I also made the plots hidden by default. As always, test it before using to make sure it is performing according to your expectations.
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);
AVG.hide();

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);
AVG_hi.hide();
AVG_lo.hide();

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 then color.cyan else if close < avg_lo or close > avg_hi then COLOR.GREEN else color.red);
 
@BasicBobby I'm glad you like it! This code should make the label turn a light blue color if price is equal to/touches the moving average. It probably will be very quick in changing color or "blink", especially on active stocks. I also made the plots hidden by default. As always, test it before using to make sure it is performing according to your expectations.
For some reason, the label is not changing when touching the MA
 
@BasicBobby See if this works better for your purposes.
Code:
input Period = aggregationPeriod.FIVE_MIN;
input AvgType = averageType.EXPONENTIAL;
input Length = 9;
input ShowMAlabel = yes;
input ATR_distance = 0.25;
input ATR_touch_distance = 0.05;

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

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

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);
AVG_hi.hide();
AVG_lo.hide();

plot AVG_hi_touch = avg + ATR_touch_distance * MovingAverage(AvgType, TrueRange(h, c, l), length);
plot AVG_lo_touch = avg - ATR_touch_distance * MovingAverage(AvgType, TrueRange(h, c, l), length);
AVG_hi_touch.hide();
AVG_lo_touch.hide();

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_hi_touch and close > AVG_lo_touch then color.cyan else if close < avg_lo or close > avg_hi then COLOR.GREEN else color.red);
 
@BasicBobby After I learned that the first iteration was not showing cyan, I loaded it on my charts and noticed that price would cross above/below the average so quickly that it would never show when price touched the average. (Touched, as far as I understand it, means "the same price as" or "equal to"). To attempt to remedy this, I made a small "envelope" around the average so that the label would have time to change colors, if even for a brief moment.

Changing the touch ATR distance to 0 essentially makes the "touch zone" the same as a single price value. As an example, if the average is at 302.01, price would have to be at exactly 302.01 in order for the label to turn cyan. It recreates the issue I saw with the first "touch" version of the code. Try leaving the ATR at 0.05 and let me know if it works the way you are trying to have it.

Also, if my understanding of "touching the average" is different from yours, please correct me and explain what you see it as so that I can try to code it.
 
So yes, We have the same understanding of touch avg. I tried changing the ATR touch distance to 0.05, and it shows cyan for a brief moment, then switches between cyan and red. If its not possible to just have the cyan label when price touches, that's fine then. Thank you so much for all your help with this code!
 
@BasicBobby You're welcome, I enjoyed doing this.

You can try to adjust the touch ATR setting a bit smaller, maybe 0.025 or so. But at some point the zone will get too small and price will blow right through it without alerting. If you want to, experiment with it and see how small it can be made yet still give an alert/color change.
 
HI, Is there a way to add VWAP to this??
There's no need to cram the VWAP into this script for it to appear in your setup.
You can just add the VWAP label script to your chart.
https://usethinkscript.com/threads/vwap-label-with-upper-lower-bands-for-thinkorswim.8248/

You can use the forum search function to find other vwap labels:
https://usethinkscript.com/threads/search-the-forum.12626/

If you're specifically interested in setting up triggers based on the interaction of moving averages and the VWAP, you can start with a script like this:
https://usethinkscript.com/threads/vwap-crossover-ema.12776/
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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