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