How Far Price Is From MTF Moving Average For ThinkOrSwim

I created this indicator to allow 4 different moving averages across many common timeframes to be put on one chart. I use this on a 5 min chart to have labels to help me see how far the current price is intra day to the 5/21/50/200 EMA on the daily and weekly chart.

Hope people can use this

Code:
#declare lower;

input aggregationPeriod1 = {Five_Min, Fifteen_Min, default One_Hour, Four_Hour, Daily, Weekly, Monthly};
input length1 = 200;

input averageType1 = {default EMA, SMA, HMA};
input aggregationPeriod2 = {Five_Min, Fifteen_Min, One_Hour, Four_Hour, default Daily, Weekly, Monthly};
input length2 = 200;
input averageType2 = {default EMA, SMA, HMA};

input aggregationPeriod3 = {Five_Min, Fifteen_Min, One_Hour, Four_Hour, Daily, default Weekly, Monthly};
input length3 = 200;
input averageType3 = {default EMA, SMA, HMA};

input aggregationPeriod4 = {Five_Min, Fifteen_Min, One_Hour, Four_Hour, Daily, Weekly, default Monthly};
input length4 = 200;
input averageType4 = {default EMA, SMA, HMA};





input ShowLabel1 = yes;
input ShowLabel2 = yes;
input ShowLabel3 = yes;
input ShowLabel4 = yes;
input ShowLabel5 = yes;

input VwapType = {default DAY, WEEK, MONTH};


def VWAPRef = reference VWAP("time frame" = VwapType)."VWAP";


def VWAPRef1a = if !IsNaN(VWAPRef) then VWAPRef else VWAPRef1a[1];

def VWAP1 = VWAPRef1a;
def VWAPpct1 = (close / VWAPRef1a) - 1;
plot VWAPdist1 = VWAPpct1;






def AvgType1;
switch (averageType1){
case EMA:
    AvgType1 =  AverageType.EXPONENTIAL;
case SMA:
    AvgType1 =  AverageType.SIMPLE;
case HMA:
    AvgType1 =  AverageType.HULL;
}

def AvgType2;
switch (averageType2){
case EMA:
    AvgType2 =  AverageType.EXPONENTIAL;
case SMA:
    AvgType2 =  AverageType.SIMPLE;
case HMA:
    AvgType2 =  AverageType.HULL;
}

def AvgType3;
switch (averageType3){
case EMA:
    AvgType3 =  AverageType.EXPONENTIAL;
case SMA:
    AvgType3 =  AverageType.SIMPLE;
case HMA:
    AvgType3 =  AverageType.HULL;
}

def AvgType4;
switch (averageType4){
case EMA:
    AvgType4 =  AverageType.EXPONENTIAL;
case SMA:
    AvgType4 =  AverageType.SIMPLE;
case HMA:
    AvgType4 =  AverageType.HULL;
}

###MA1 Data###
def AggPer1;

switch (aggregationPeriod1){
case Five_Min:
    AggPer1 = AggregationPeriod.FIVE_MIN;
case Fifteen_Min:
    AggPer1 = AggregationPeriod.FIFTEEN_MIN;
case One_Hour:
    AggPer1 = AggregationPeriod.HOUR;
case Four_Hour:
    AggPer1 = AggregationPeriod.FOUR_HOURS;
case Daily:
    AggPer1 = AggregationPeriod.DAY;
case Weekly:
    AggPer1 = AggregationPeriod.WEEK;
case Monthly:
    AggPer1 = AggregationPeriod.MONTH;
}

def pricetype1 = close( period = AggPer1);
def avg1 = MovingAverage(AvgType1, pricetype1, length1);
def avg1a = if !IsNaN(avg1) then avg1 else avg1a[1];
def MA1 = avg1a;
def pct1 = (close / avg1a) - 1;
plot dist1 = pct1;



###MA2 Data###
def AggPer2;

switch (aggregationPeriod2){
case Five_Min:
    AggPer2 = AggregationPeriod.FIVE_MIN;
case Fifteen_Min:
    AggPer2 = AggregationPeriod.FIFTEEN_MIN;
case One_Hour:
    AggPer2 = AggregationPeriod.HOUR;
case Four_Hour:
    AggPer2 = AggregationPeriod.FOUR_HOURS;
case Daily:
    AggPer2 = AggregationPeriod.DAY;
case Weekly:
    AggPer2 = AggregationPeriod.WEEK;
case Monthly:
    AggPer2 = AggregationPeriod.MONTH;
}

def pricetype2 = close( period = AggPer2);
def avg2 = MovingAverage(AvgType2, pricetype2, length2);
def avg2a = if !IsNaN(avg2) then avg2 else avg2a[1];
def MA2 = avg2a;
def pct2 = (close / avg2a) - 1;
plot dist2 = pct2;

###MA3 Data###
def AggPer3;

switch (aggregationPeriod3){
case Five_Min:
    AggPer3 = AggregationPeriod.FIVE_MIN;
case Fifteen_Min:
    AggPer3 = AggregationPeriod.FIFTEEN_MIN;
case One_Hour:
    AggPer3 = AggregationPeriod.HOUR;
case Four_Hour:
    AggPer3 = AggregationPeriod.FOUR_HOURS;
case Daily:
    AggPer3 = AggregationPeriod.DAY;
case Weekly:
    AggPer3 = AggregationPeriod.WEEK;
case Monthly:
    AggPer3 = AggregationPeriod.MONTH;
}

def pricetype3 = close( period = AggPer3);
def avg3 = MovingAverage(AvgType3, pricetype3, length3);
def avg3a = if !IsNaN(avg3) then avg3 else avg3a[1];
def MA3 = avg3a;
def pct3 = (close / avg3a) - 1;
plot dist3 = pct3;

###MA4 Data###
def AggPer4;

switch (aggregationPeriod4){
case Five_Min:
    AggPer4 = AggregationPeriod.FIVE_MIN;
case Fifteen_Min:
    AggPer4 = AggregationPeriod.FIFTEEN_MIN;
case One_Hour:
    AggPer4 = AggregationPeriod.HOUR;
case Four_Hour:
    AggPer4 = AggregationPeriod.FOUR_HOURS;
case Daily:
    AggPer4 = AggregationPeriod.DAY;
case Weekly:
    AggPer4 = AggregationPeriod.WEEK;
case Monthly:
    AggPer4 = AggregationPeriod.MONTH;
}

def pricetype4 = close( period = AggPer4);
def avg4 = MovingAverage(AvgType4, pricetype4, length4);
def avg4a = if !IsNaN(avg4) then avg4 else avg4a[1];
def MA4 = avg4a;
def pct4 = (close / avg4a) - 1;
plot dist4 = pct4;

###Labels###

AddLabel(ShowLabel1, length1 + " " + (if AggPer1 == AggregationPeriod.FIVE_MIN then "5 Min" else if AggPer1 == AggregationPeriod.FIFTEEN_MIN then "15 Min" else if AggPer1 == AggregationPeriod.HOUR then "1 Hour" else if AggPer1 == AggregationPeriod.FOUR_HOURS then "4 Hour" else if AggPer1 == AggregationPeriod.DAY then "Day" else if AggPer1 == AggregationPeriod.WEEK then "Week" else if AggPer1 == AggregationPeriod.MONTH then "Month" else "") + " " + (if AvgType1 == 1 then "EMA" else if AvgType1 == 0 then "SMA" else if avgType1 == 4 then "HMA" else "") + " " + MA1 + " pct: " + AsPercent(dist1) , if pct1 > 0 then Color.GREEN else Color.RED);
;

AddLabel(ShowLabel2, length2 + " " + (if AggPer2 == AggregationPeriod.FIVE_MIN then "5 Min" else if AggPer2 == AggregationPeriod.FIFTEEN_MIN then "15 Min" else if AggPer2 == AggregationPeriod.HOUR then "1 Hour" else if AggPer2 == AggregationPeriod.FOUR_HOURS then "4 Hour" else if AggPer2 == AggregationPeriod.DAY then "Day" else if AggPer2 == AggregationPeriod.WEEK then "Week" else if AggPer2 == AggregationPeriod.MONTH then "Month" else "") + " " + (if AvgType2 == 1 then "EMA" else if AvgType2 == 0 then "SMA" else if avgType2 == 4 then "HMA" else "") + " " + MA2 + " pct: " + AsPercent(dist2) , if pct2 > 0 then Color.GREEN else Color.RED);
;

AddLabel(ShowLabel3, length3 + " " + (if AggPer3 == AggregationPeriod.FIVE_MIN then "5 Min" else if AggPer3 == AggregationPeriod.FIFTEEN_MIN then "15 Min" else if AggPer3 == AggregationPeriod.HOUR then "1 Hour" else if AggPer3 == AggregationPeriod.FOUR_HOURS then "4 Hour" else if AggPer3 == AggregationPeriod.DAY then "Day" else if AggPer3 == AggregationPeriod.WEEK then "Week" else if AggPer3 == AggregationPeriod.MONTH then "Month" else "") + " " + (if AvgType3 == 1 then "EMA" else if AvgType3 == 0 then "SMA" else if avgType3 == 4 then "HMA" else "") + " " + MA3 + " pct: " + AsPercent(dist3) , if pct3 > 0 then Color.GREEN else Color.RED);
;

AddLabel(ShowLabel4, length4 + " " + (if AggPer4 == AggregationPeriod.FIVE_MIN then "5 Min" else if AggPer4 == AggregationPeriod.FIFTEEN_MIN then "15 Min" else if AggPer4 == AggregationPeriod.HOUR then "1 Hour" else if AggPer4 == AggregationPeriod.FOUR_HOURS then "4 Hour" else if AggPer4 == AggregationPeriod.DAY then "Day" else if AggPer4 == AggregationPeriod.WEEK then "Week" else if AggPer4 == AggregationPeriod.MONTH then "Month" else "") + " " + (if AvgType4 == 1 then "EMA" else if AvgType4 == 0 then "SMA" else if avgType4 == 4 then "HMA" else "") + " " + MA4 + " pct: " + AsPercent(dist4) , if pct4 > 0 then Color.GREEN else Color.RED);
;

AddLabel(ShowLabel5, (if VwapType == VwapType.DAY then "Daily" else if VwapType == VwapType.WEEK then "Weekly" else if VwapType == VwapType.MONTH then "Monthly"  else "") + " VWAP " + VWAP1 + " pct: " + AsPercent(VWAPdist1) , if VWAPpct1 > 0 then Color.GREEN else Color.RED);
;
 

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

I call this column "PctFromEMA200Day". I use this as a daily watchlist column that I have hooked up to a scanner that shows stocks that have a stacked EMA (open is above 8 ema which is above 21 EMA which is above 50 EMA which is above 200 EMA.) to see how far away the current close on a daily chart is away from the 200 EMA (length1, average type, and agg can be adjusted by setting the "default" value for each).
I sort the watchlist column in ascending order to get the tickers that are closest to the 200 EMA with the thesis of catching a potential swing early. See NUAI back in Sept 22, 2025 for an example).

Code:
#declare lower;

input aggregationPeriod1 = {Five_Min, Fifteen_Min,  One_Hour, Four_Hour,  default Daily,  Weekly, Monthly};
input length1 = 200;

input averageType1 = {default EMA, SMA, HMA};


input ShowLabel1 = yes;


input VwapType = { default DAY,  WEEK, MONTH};


def VWAPRef = reference VWAP("time frame" = VwapType)."VWAP";


def VWAPRef1a = if !IsNaN(VWAPRef) then VWAPRef else VWAPRef1a[1];

def VWAP1 = VWAPRef1a;
def VWAPpct1 = (close / VWAPRef1a) - 1;
def VWAPdist1 = VWAPpct1;






def AvgType1;
switch (averageType1){
case EMA:
    AvgType1 =  AverageType.EXPONENTIAL;
case SMA:
    AvgType1 =  AverageType.SIMPLE;
case HMA:
    AvgType1 =  AverageType.HULL;
}


def AggPer1;

switch (aggregationPeriod1){
case Five_Min:
    AggPer1 = AggregationPeriod.FIVE_MIN;
case Fifteen_Min:
    AggPer1 = AggregationPeriod.FIFTEEN_MIN;
case One_Hour:
    AggPer1 = AggregationPeriod.HOUR;
case Four_Hour:
    AggPer1 = AggregationPeriod.FOUR_HOURS;
case Daily:
    AggPer1 = AggregationPeriod.DAY;
case Weekly:
    AggPer1 = AggregationPeriod.WEEK;
case Monthly:
    AggPer1 = AggregationPeriod.MONTH;
}

def pricetype1 = close( period = AggPer1);
def avg1 = MovingAverage(AvgType1, pricetype1, length1);
def avg1a = if !IsNaN(avg1) then avg1 else avg1a[1];
def MA1 = avg1a;
def pct1 = (close / avg1a) - 1;
plot dist1 = if pct1<0 then 9999 else pct1 ;




###Labels###

I call this column "PriceEMA_50D". This is setup to see whether a high, low, or bid is above an EMA (I set for bid price and 50 EMA). The column color will be different depending on which setup you have. Right now I use this as the bid price against my MOMO scanner to make sure the stocks moving are at least above the 50 day EMA as column data updates real time.

Code:
def EMAPrice = movAvgExponential(price = close,length = 50, displace = 0);
def h=high;
def l = low;
def b = bid();
def HighaboveHMA = h >EMAPrice;
def LowaboveHMA = l >EMAPrice;
def BidAboveEMA = b >EMAPrice;
input Priceuse = { default "bid", "High/Low"};



plot PriceHMARelationship =
if HighaboveHMA and LowaboveHMA and Priceuse ==Priceuse."High/Low"  then 3 else
if HighaboveHMA and !LowaboveHMA and Priceuse ==Priceuse."High/Low"  then 2 else
if !HighaboveHMA and LowaboveHMA and Priceuse ==Priceuse."High/Low"  then 1 else
 if !HighaboveHMA and !LowaboveHMA and Priceuse ==Priceuse."High/Low"  then 0 else
if Priceuse ==Priceuse."Bid"  and BidAboveEMA then 4
else
-1;
#TrixDiff.assignValueColor(if TrixDiff <=-0.0001  then color.red else  if  TrixDiff >=0.0001  then color.green else Color.white);

PriceHMARelationship.assignValueColor(if

PriceHMARelationship == 4 then Color.cyan
else if PriceHMARelationship == 3 then Color.green else if PriceHMARelationship ==2 then color.yellow else
 if PriceHMARelationship ==1 then color.yellow else if PriceHMARelationship ==0 then color.red else color.black);

AssignBackgroundColor(
if

PriceHMARelationship == 4 then Color.cyan
else
if PriceHMARelationship == 3 then Color.green else if PriceHMARelationship ==2 then color.yellow else
 if PriceHMARelationship ==1 then color.yellow else if PriceHMARelationship ==0 then color.red else color.black);
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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