Multi-timeframe (MTF) Moving Average Indicator for ThinkorSwim

Not working, says that one plot needs to be defined? I need this to show the 20 day, 100 day and 200 day.

Am ready to transfer once this is done, appreciate you looking at it.
 
@wcsharron Here you go...

Code:
input aP = AggregationPeriod.DAY;
input length1 = 20;
input length2 = 100;
input length3 = 200;

plot ema_1 = expaverage(close(period = aP), length1);
plot ema_2 = expaverage(close(period = aP), length2);
plot ema_3 = expaverage(close(period = aP), length3);

It won't work on tick charts though.
 
@wcsharron

Check this out.

PKFpUSq.png


Code:
#CustomSMA #Written on Demand
#Author SuryaKiranC
#Version 0.1

input aP = AggregationPeriod.DAY;

input length1 = 21;
input length2 = 100;
input length3 = 200;

def ma1 = Average(close(period = aP), length1);
def ma2 = Average(close(period = aP), length2);
def ma3 = Average(close(period = aP), length3);

plot m1 = ma1;
plot m2 = ma2;
plot m3 = ma3;

Agreed, code is crude. Can be refine to make it look a bit more elegant, also can add paint style and color
Works on Tick charts too. ;-)
 
Thank you SuraKiraC - TradeGeek seems to have figured it out.

When i tried yours, it shows up on the Tick, but does not start correctly? Shows 15m into the day.

Again, appreciated the effort.
 
@wcsharron no worries. As far as working on a Tick chart though, you need to specify what tick size you are working with. Basically, unless the defined tick size is meet, it will not start the plot. for sake of simplicity, try 1 tick.
-Surya
 
@wcsharron Just expand your tick chart to show several days then you should be fine. If you show the max 5 days then you can go up to a 4220 tick chart and it will show at the beginning of the most recent day (market open). You can do that and use the code that @SuryaKiranC posted. I was going to post basically the same thing. If you need to have specific type of moving average, colors, labels, etc., just ask.
 
@wcsharron @tradegeek After a bit of thought process, I think you are better off with labels than Plot.

check out labels study code below. This seems to work our regardless of Time or Tick charts.

Code:
#
# SM_MovingAverageLabels
#
# version 1.2
#
declare on_volume;

#Default moving average values:
#SIMP 3 Close.DAY
#SIMP 8 Close.DAY
#SIMP 20 Close.DAY
#SIMP 50 Close.DAY
#SIMP 200 Close.DAY


input aP = AggregationPeriod.DAY;

input MA_1_length = 3; #simple
input MA_2_length = 8; #simple
input MA_3_length = 20; #simple
input MA_4_length = 50; #simple
input MA_5_length = 200; #simple

def MA_1_closeType = close(period = aP);
def MA_2_closeType = close(period = aP);
def MA_3_closeType = close(period = aP);
def MA_4_closeType = close(period = aP);
def MA_5_closeType = close(period = aP);

def MA_1_avg = round(Average(MA_1_closeType, MA_1_length),2);
def MA_2_avg = round(Average(MA_2_closeType, MA_2_length),2);
def MA_3_avg = round(Average(MA_3_closeType, MA_3_length),2);
def MA_4_avg = round(Average(MA_4_closeType, MA_4_length),2);
def MA_5_avg = round(Average(MA_5_closeType, MA_5_length),2);

def currentPrice = close;

AddLabel(1, "SMA" + " ", Color.YELLOW);

def MA_1_above = if currentPrice > MA_1_avg then 1 else 0;
def MA_1_below = if currentPrice <= MA_1_avg then 1 else 0;
AddLabel(MA_1_above, MA_1_length + ": " + MA_1_avg + " ", Color.GREEN);
AddLabel(MA_1_below, MA_1_length + ": " + MA_1_avg + " ", Color.RED);

def MA_2_above = if currentPrice > MA_2_avg then 1 else 0;
def MA_2_below = if currentPrice <= MA_2_avg then 1 else 0;
AddLabel(MA_2_above, MA_2_length + ": " + MA_2_avg + " ", Color.GREEN);
AddLabel(MA_2_below, MA_2_length + ": " + MA_2_avg + " ", Color.RED);

def MA_3_above = if currentPrice > MA_3_avg then 1 else 0;
def MA_3_below = if currentPrice <= MA_3_avg then 1 else 0;
AddLabel(MA_3_above, MA_3_length + ": " + MA_3_avg + " ", Color.GREEN);
AddLabel(MA_3_below, MA_3_length + ": " + MA_3_avg + " ", Color.RED);

def MA_4_above = if currentPrice > MA_4_avg then 1 else 0;
def MA_4_below = if currentPrice <= MA_4_avg then 1 else 0;
AddLabel(MA_4_above, MA_4_length + ": " + MA_4_avg + " ", Color.GREEN);
AddLabel(MA_4_below, MA_4_length + ": " + MA_4_avg + " ", Color.RED);

def MA_5_above = if currentPrice > MA_5_avg then 1 else 0;
def MA_5_below = if currentPrice <= MA_5_avg then 1 else 0;
AddLabel(MA_4_above, MA_5_length + ": " + MA_5_avg + " ", Color.GREEN);
AddLabel(MA_4_below, MA_5_length + ": " + MA_5_avg + " ", Color.RED);

#EXPO 3 Close
#EXPO 8 Close
#EXPO 20 Close
#EXPO 50 Close
#EXPO 200 Close

input EMA_1_length = 3; #exponential
input EMA_2_length = 8; #exponential
input EMA_3_length = 20; #exponential
input EMA_4_length = 50; #exponential
input EMA_5_length = 200; #exponential

def EMA_1_closeType = close;
def EMA_2_closeType = close;
def EMA_3_closeType = close;
def EMA_4_closeType = close;
def EMA_5_closeType = close;

def EMA_1_avg = round(ExpAverage(EMA_1_closeType, EMA_1_length),2);
def EMA_2_avg = round(ExpAverage(EMA_2_closeType, EMA_2_length),2);
def EMA_3_avg = round(ExpAverage(EMA_3_closeType, EMA_3_length),2);
def EMA_4_avg = round(ExpAverage(EMA_4_closeType, EMA_4_length),2);
def EMA_5_avg = round(ExpAverage(EMA_5_closeType, EMA_5_length),2);

AddLabel(1, "EMA" + " ", Color.YELLOW);

def EMA_1_above = if currentPrice > EMA_1_avg then 1 else 0;
def EMA_1_below = if currentPrice <= EMA_1_avg then 1 else 0;
AddLabel(EMA_1_above, EMA_1_length + ": " + EMA_1_avg + " ", Color.GREEN);
AddLabel(EMA_1_below, EMA_1_length + ": " + EMA_1_avg + " ", Color.RED);

def EMA_2_above = if currentPrice > EMA_2_avg then 1 else 0;
def EMA_2_below = if currentPrice <= EMA_2_avg then 1 else 0;
AddLabel(EMA_2_above, EMA_2_length + ": " + EMA_2_avg + " ", Color.GREEN);
AddLabel(EMA_2_below, EMA_2_length + ": " + EMA_2_avg + " ", Color.RED);

def EMA_3_above = if currentPrice > EMA_3_avg then 1 else 0;
def EMA_3_below = if currentPrice <= EMA_3_avg then 1 else 0;
AddLabel(EMA_3_above, EMA_3_length + ": " + EMA_3_avg + " ", Color.GREEN);
AddLabel(EMA_3_below, EMA_3_length + ": " + EMA_3_avg + " ", Color.RED);

def EMA_4_above = if currentPrice > EMA_4_avg then 1 else 0;
def EMA_4_below = if currentPrice <= EMA_4_avg then 1 else 0;
AddLabel(EMA_4_above, EMA_4_length + ": " + EMA_4_avg + " ", Color.GREEN);
AddLabel(EMA_4_below, EMA_4_length + ": " + EMA_4_avg + " ", Color.RED);

def EMA_5_above = if currentPrice > EMA_5_avg then 1 else 0;
def EMA_5_below = if currentPrice <= EMA_5_avg then 1 else 0;
AddLabel(EMA_4_above, EMA_5_length + ": " + EMA_5_avg + " ", Color.GREEN);
AddLabel(EMA_4_below, EMA_5_length + ": " + EMA_5_avg + " ", Color.RED);

**Note: This code has 5 of SMA and EMA labels defined and all SMA are fixed to AggregatePeriod.Day where as EMA are current candle.

-Surya
 
Last edited:
Hey folks, hope everyone is well.

Is there a way to have a 15 min Hull moving average on say a 2 min chart ? I see several MTF studies on the forum. I use several for charting and TA. I think that a higher time frame HullMA on a scalping chart would be an interesting idea.

Thank you
 
@Pensar Thanks Pensar ! I'm looking it over now. I've never coded a TOS script, so im assuming anywhere where it says "EMA" I should change to "HullMA" , not sure if thats the correct syntax

I found this off the forum, and it does exactly what I need it to do. However, is there a way to add uptrend and downtrend colours onto this code ? The vanilla HullMA on tos changes colour depending on trend.
 
Last edited by a moderator:
@stockula Assuming you want the candles to be red when below the moving average and green when above it.

Add this snippet to the end of your script:

Code:
AssignPriceColor(if close > AVG then color.green else color.red);
 
Thanks for the reply Ben,

I was actually trying to replicate the vanilla hullMA in tos. The HullMA curve itself changes colour when the input trend changes direction. Very useful for scalping when taking with confluence.

Tried posting a pic but unable to link..

Thanks folks
 
@binhvesting Sure, add the following to the bottom of your script:

Code:
input label = yes;
AddLabel(label and yes, Concat("Moving average = ", AVG), color.orange);
 
Hi @Pelonsax. First of all, great job on the #TheStrat code for TOS, thank you for that. I have seen the color coded labels, based on the color of the candle at various time frames on the top left side of the charts. I have this color coded triple EMA, where if the 8 is higher than both the 13 and 21 EMAs, then it's green. If it is below, it's red and in between yellow. Is it possible to code these labels at the top, where it can show me the color of the candles/bars at various timeframes, based on this code? Thank you in advance for your help.

Code:
#Mr Slim Miller at askSLIM dot com
#SlimRibbonCustom_markos9-7-18
input price = close;

input superfast_length = 8;

input fast_length = 13;

input slow_length = 21;

input displace = 0;



def mov_avg8 = ExpAverage(price[-displace], superfast_length);

def mov_avg13 = ExpAverage(price[-displace], fast_length);

def mov_avg21 = ExpAverage(price[-displace], slow_length);



#moving averages

Plot Superfast = mov_avg8;

plot Fast = mov_avg13;

plot Slow = mov_avg21;



def buy = mov_avg8 > mov_avg13 and mov_avg13 > mov_avg21 and low > mov_avg8;

def stopbuy = mov_avg8 <= mov_avg13;

def buynow = !buy[1] and buy;

def buysignal = CompoundValue(1, if buynow and !stopbuy then 1 else if buysignal[1]==1 and stopbuy then 0 else buysignal[1], 0);



plot Buy_Signal = buysignal[1] == 0 and buysignal==1;

Buy_signal.setpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);

Buy_signal.setdefaultColor(color.dark_GREEN);

Buy_signal.hidetitle();

Alert(condition = buysignal[1] == 0 and buysignal == 1, text = "Buy Signal", sound = Sound.Bell, "alert type" = Alert.BAR);



plot Momentum_Down = buysignal[1] ==1 and buysignal==0;

Momentum_down.setpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

Momentum_Down.setdefaultColor(color.yellow);

Momentum_down.hidetitle();

Alert(condition = buysignal[1] == 1 and buysignal == 0, text = "Momentum_Down", sound = Sound.Bell, "alert type" = Alert.BAR);



def sell = mov_avg8 < mov_avg13 and mov_avg13 < mov_avg21 and high < mov_avg8;

def stopsell = mov_avg8 >= mov_avg13;

def sellnow = !sell[1] and sell;

def sellsignal = CompoundValue(1, if sellnow and !stopsell then 1 else if sellsignal[1]==1 and stopsell then 0 else sellsignal[1], 0);



Plot Sell_Signal = sellsignal[1] ==0 and sellsignal;

Sell_signal.setpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_down);

sell_signal.setDefaultColor(color.red);

Sell_signal.hidetitle();

Alert(condition = sellsignal[1] == 0 and sellsignal == 1, text = "Sell Signal", sound = Sound.Bell, "alert type" = Alert.BAR);



Plot Momentum_Up = sellsignal[1]==1 and sellSignal==0;

Momentum_up.setpaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_up);

Momentum_up.setDefaultColor(color.yellow);

Momentum_up.hidetitle();

Alert(condition = sellsignal[1] == 1 and sellSignal == 0, text = "Momentum_Up", sound = Sound.Bell, "alert type" = Alert.BAR);



plot Colorbars = if buysignal ==1 then 1 else if sellsignal ==1 then 2 else if buysignal ==0 or sellsignal==0 then 3 else 0;

colorbars.hide();

Colorbars.definecolor("Buy_Signal_Bars", color.dark_green);

Colorbars.definecolor("Sell_Signal_Bars", color.red);

Colorbars.definecolor("Neutral", color.yellow);



AssignPriceColor(if Colorbars ==1 then colorbars.color("buy_signal_bars") else if colorbars ==2 then colorbars.color("Sell_Signal_bars") else  colorbars.color("neutral"));



#end

How could I have the 1min. chart show the Bubbles with the respective colors of the EMAs described above, for the 5min. 15min., 30min, 60min, 4hr, daily, weekly and monthly?

So, if the 5min. is green based on the fact that the 8>13>21 EMAs, then show the small Bubble on the top left corner with a 5min label and the background color being green, etc.

Thank you in advance for your help!
 
Last edited:
@lindosskier You would need a companion code, Based on the same logic. You could still use the above (☝️) to plot the lines and levels of EMA on the chart, for the current time frame. However, you need the below added multiple times to your TOS chart and specify each time frame you are interested in tracking this.

Code:
#Author : SuryaKiranC 
#FileName : Multi_EMASTUDY.ts
#Based on original code for EMA Plot from the below author. 
#Mr Slim Miller at askSLIM dot com
#SlimRibbonCustom_markos9-7-18

input period = AggregationPeriod.DAY;

input superfast_length = 8;
input fast_length = 13;
input slow_length = 21;
input displace = 0;

DefineGlobalColor("Buy", CreateColor(177, 245, 83));
DefineGlobalColor("Sell", CreateColor(179, 17, 5));
DefineGlobalColor("MomentumDown", CreateColor(235, 16, 122));
DefineGlobalColor("MomentumUp", CreateColor(72, 190, 194));

script EMA_Col {
    input aP = AggregationPeriod.DAY;
    plot price = close(period = aP) >= open(period = aP);
    price.hide();
}

script EMA_MTF {

    input aP = AggregationPeriod.DAY;
    def price = close(period = aP);
    input superfast_length = 8;
    input fast_length = 13;
    input slow_length = 21;
    input displace = 0;

    def mov_avg8 = ExpAverage(price[-displace], superfast_length);
    def mov_avg13 = ExpAverage(price[-displace], fast_length);
    def mov_avg21 = ExpAverage(price[-displace], slow_length);

    def buy = mov_avg8 > mov_avg13 and mov_avg13 > mov_avg21 and low(period = aP) > mov_avg8;
    def stopbuy = mov_avg8 <= mov_avg13;
    def buynow = !buy[1] and buy;
    def buysignal = CompoundValue(1, if buynow and !stopbuy then 1 else if buysignal[1] == 1 and stopbuy then 0 else buysignal[1], 0);

    def sell = mov_avg8 < mov_avg13 and mov_avg13 < mov_avg21 and high(period = aP) < mov_avg8;
    def stopsell = mov_avg8 >= mov_avg13;
    def sellnow = !sell[1] and sell;
    def sellsignal = CompoundValue(1, if sellnow and !stopsell then 1 else if sellsignal[1] == 1 and stopsell then 0 else sellsignal[1], 0);

    def Buy_Signal = buysignal[1] == 0 and buysignal == 1;
    def Momentum_Down = buysignal[1] == 1 and buysignal == 0;
    def Sell_Signal = sellsignal[1] == 0 and sellsignal;
    def Momentum_Up = sellsignal[1] == 1 and sellsignal == 0;

    def OHL3 = (open + high + low) / 3 ;

    #def result = if Buy_Signal == 1 then 4 else if Momentum_Down then 3 else if Momentum_Up then 2 else if Sell_Signal then 1 else 0;
    plot Colorbars = if Buy_Signal == 1 then 4 else if Momentum_Down then 3 else if Momentum_Up then 2 else if Sell_Signal then 1 else 0 ;
    Colorbars.Hide();
}

def cP = GetAggregationPeriod();
def EMA;
def EMA_Color;

if period >= cP {
    EMA = EMA_MTF(aP = period, superfast_length = superfast_length, fast_length = fast_length, slow_length = slow_length, displace = displace );
    EMA_Color = EMA_Col (ap = period);
} else {
    EMA = Double.NaN;
    EMA_Color = Double.NaN; 
}

AddLabel(!IsNaN(EMA) and !IsNaN(EMA_Color), if period == AggregationPeriod.MONTH then "M" 
else if period == AggregationPeriod.WEEK then "W" 
else if period == AggregationPeriod.FOUR_DAYS then "4D" 
else if period == AggregationPeriod.THREE_DAYS then "3D" 
else if period == AggregationPeriod.TWO_DAYS then "2D" 
else if period == AggregationPeriod.DAY then "D" 
else if period == AggregationPeriod.FOUR_HOURS then "4H" 
else if period == AggregationPeriod.TWO_HOURS then "2H" 
else if period == AggregationPeriod.HOUR then "60m"
else if period == AggregationPeriod.THIRTY_MIN then "30m" 
else if period == AggregationPeriod.TWENTY_MIN then "20m" 
else if period == AggregationPeriod.FIFTEEN_MIN then "15m"
else if period == AggregationPeriod.TEN_MIN then "10m" 
else if period == AggregationPeriod.FIVE_MIN then "5m" 
else if period == AggregationPeriod.FOUR_MIN then "4m" 
else if period == AggregationPeriod.THREE_MIN then "3m" 
else if period == AggregationPeriod.TWO_MIN then "2m" 
else if period == AggregationPeriod.MIN then "1m" else "", if EMA == 4 then GlobalColor("Buy") else if EMA == 3 then GlobalColor("MomentumDown") else if EMA == 2 then GlobalColor("MomentumUp") else if EMA == 1 then GlobalColor("Sell") else if EMA_Color == 1 then Color.GREEN else Color.RED); 
#end
 
Last edited:
@lindosskier Please note, I have 4 colors defined here in the modified code, and they are as following, difference is both Momentum_UP and Momentum_Down were using "Yellow" as a color in original code and that has changed.

DefineGlobalColor("Buy", Color.GREEN);
DefineGlobalColor("Sell", Color.RED);
DefineGlobalColor("MomentumDown", Color.YELLOW);
DefineGlobalColor("MomentumUp", Color.CYAN);

lzdvnFM.png


AFCOun7.png
 
Last edited by a moderator:
@SuryaKiranC thank you so very much for your help, I really do. But I have no coding experience whatsoever, so when I add the piece of code you wrote, nothing really changes in my chart. In addition, I noticed that on the chart above, only the 1min. label is colored red, but the other timeframes are not colored at all. Can you please help me understand further, how to be able to fix these 2 items? Thank you again, much appreciated!
 

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