Daily Moving Average with Percentage Diff Label For ThinkOrSwim

SteveDay

Member
Plus
SD_DailyMA - Daily Moving Average on Expansion/Entire Chart/Latest Period Only (with Percentage Diff Label)

Here is my indicator to plot the daily moving average (or any other aggregation period that you choose).

I wrote this because I wanted to declutter my charts and only needed the latest daily MA plot on the Expansion area. After adding that I then added an optional chart label which displays the percentage difference of the current CLOSE relative to the latest moving average.

I hope you find this useful.

I have many scripts which I've written and I'll be selecting the best ones, polishing them up (removing debug code, refining variable names, etc) and releasing them here on UseThinkScript.

Steve

UPDATES
v1.0.1 [2023/03/27] Revision:
I've changed how the period is selected. Previously you could choose from any of the built-in periods, but I've now limited it to Minutes, Hours, Days, Weeks, and Months. This was done because I'd forgotten to add code to add the correct abbreviation onto the study's "Data Label" & "Plot Bubbles" (ie: "200m", "200h", "200D", "200W", "200M" - will be displayed for the relevant period).



TOS shared study link:
http://tos.mx/cFHcDTt (Latest: [2023/03/27])

Code:
Code:
#==# SD_DailyMA v1.0.1
#--# -----------------
#//# Published: 2023/03/26
#//# Revised:   2023/03/27
#//# by Steve Day

#==# DESCRIPTION:
#..# This study is an alternate to ThinkOrSwim's built-in DailySMA study.
#..# What my one does differently are the following:
#..#    - You can plot the latest moving average only onto the expansion area (to help declutter your charts).
#..#    - You can choose from Simple, Exponential, Weighted, Wilder's, or Hull moving averages instead of just Simple.
#..#    - You can plot only the latest period if you wish (another decluttering aid).
#..#    - There is an optional Data Label which displays the current CLOSE's percentage difference from the latest moving average.
#..#
#..# I am open to building indicators/studies on a freelance basis. Contact [email protected] if interested.

#==# CHANGES:
#//# v1.0.1 [2023/03/27] - Changed how the period is selected and limited the number of choices to "MIN", "HOUR", "DAY", "WEEK", "MONTH".
#//#                     .. The Chart Data Label & Plot Bubble now show the appropriate period abbreviation.



input price = FundamentalType.CLOSE;
#input period = AggregationPeriod.DAY;
input period = { "MIN", "HOUR", default "DAY", "WEEK", "MONTH" };

def aggPeriod;
switch(period) {
case "MIN":
    aggPeriod = AggregationPeriod.MIN;
case "HOUR":
    aggPeriod = AggregationPeriod.HOUR;
case "DAY":
    aggPeriod = AggregationPeriod.DAY;
case "WEEK":
    aggPeriod = AggregationPeriod.WEEK;
case "MONTH":
    aggPeriod = AggregationPeriod.MONTH;
}
def cap = GetAggregationPeriod();
Assert(aggPeriod >= cap, "SD_DailyMA, 'Period' input must be greater than or equal to the chart's timeframe");

input Show = { default "Entire Chart", "Latest Period Only", "Latest on Expansion" };
input averageType = AverageType.SIMPLE;
input length = 200;
input displace = 0;


input bars_on_extension = 25;
def isExpansion = IsNaN(CLOSE);
def bn_ext = if !isExpansion then 0 else bn_ext[1] + 1;

input showDataLabel = yes;
input showPlotBubble = yes;


plot DailyMA;
DailyMA.SetDefaultColor(GetColor(2));
DailyMA.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

def avg = MovingAverage(
    averageType,
    fundamental(price, period = aggPeriod)[-displace],
    length);

def latest = if !IsNaN(close(period = aggPeriod)[-1]) then avg else latest[1];

if show == show."Latest Period Only" {
    if IsNaN(close(period = aggPeriod)[-1]) and !isExpansion {
        DailyMA = latest;
    } else {
        DailyMA = Double.NaN;
    }
} else if show == show."Latest on Expansion" then {
    if bn_ext == 0 or bn_ext > bars_on_extension then {
        DailyMA = Double.NaN;
    } else if bn_ext <= Max(1, bars_on_extension) then {
        DailyMA = latest;
    } else {
        DailyMA = Double.NaN;
    }
} else if !isExpansion then {
        DailyMA = latest;
} else {
    DailyMA = Double.NaN;
}

#//# Chart Data Label..
def diff = (GetValue(CLOSE,bn_ext) / latest) - 1;

AddLabel( showDataLabel and !IsNaN(diff),
    "" + length
    +   (   if aggPeriod == AggregationPeriod.MIN then "m "
            else if aggPeriod == AggregationPeriod.HOUR then "h "
            else if aggPeriod == AggregationPeriod.DAY then "D "
            else if aggPeriod == AggregationPeriod.WEEK then "W "
            else if aggPeriod == AggregationPeriod.MONTH then "M "
            else " "    )
    +   (   if averageType == AverageType.SIMPLE then "SMA"
            else if averageType == AverageType.EXPONENTIAL then "EMA"
            else if averageType == AverageType.WEIGHTED then "WMA"
            else if averageType == AverageType.WILDERS then "WWMA"
            else if averageType == AverageType.HULL then "HMA"
            else ""    )
    + ":" + (if diff > 0 then "+" else "") + AsPercent(diff)
    , DailyMA.TakeValueColor() );


#//# Plot Bubble..
AddChartBubble(
    showPlotBubble and  (   (show == show."Latest on Expansion" and bn_ext == bars_on_extension)
                         or (show != show."Latest on Expansion" and bn_ext == 1)
                        ),
    latest,
    "" + length
    +   (   if aggPeriod == AggregationPeriod.MIN then "m "
            else if aggPeriod == AggregationPeriod.HOUR then "h "
            else if aggPeriod == AggregationPeriod.DAY then "D "
            else if aggPeriod == AggregationPeriod.WEEK then "W "
            else if aggPeriod == AggregationPeriod.MONTH then "M "
            else " "    )

    +   (   if averageType == AverageType.SIMPLE then "SMA"
            else if averageType == AverageType.EXPONENTIAL then "EMA"
            else if averageType == AverageType.WEIGHTED then "WMA"
            else if averageType == AverageType.WILDERS then "WWMA"
            else if averageType == AverageType.HULL then "HMA"
            else ""    ) ,
    DailyMA.TakeValueColor() );
 
Last edited:
hello is it possible to make one where the label shows what the 1min 200 ema is doing on a higher time frame chart? it does not show when I put it on the 200ema 1min and my chart is on 10 min
 
hello is it possible to make one where the label shows what the 1min 200 ema is doing on a higher time frame chart? it does not show when I put it on the 200ema 1min and my chart is on 10 min
The ToS platform does not have the ability to overlay lower timeframes on higher aggregation charts.
 

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
326 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