Multi-timeframe (MTF) Moving Average Indicator for ThinkorSwim

W

wcsharron

New member
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.
 
S

SuryaKiranC

Member
what are you trying to do?? I could make it in few mins. @wcsharron.
 
T

tradegeek

Active member
2019 Donor
VIP
@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.
 
W

wcsharron

New member
TradeGeek for the win!

Send me a PM and i am happy to send back some appreciation via Venmo
 
S

SuryaKiranC

Member
@wcsharron

Check this out.



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. ;-)
 
W

wcsharron

New member
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.
 
S

SuryaKiranC

Member
@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
 
T

tradegeek

Active member
2019 Donor
VIP
@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.
 
S

SuryaKiranC

Member
@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:
stockula

stockula

New member
VIP
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
 
P

Pensar

Active member
VIP
@stockula The code posted here can be adjusted to what you are looking for. Change the aggregation and average type in the settings.
 
Last edited by a moderator:
stockula

stockula

New member
VIP
@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:
BenTen

BenTen

Administrative
Staff
VIP
@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);
 
stockula

stockula

New member
VIP
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
 
rad14733

rad14733

Well-known member
VIP
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
@stockula

Have you seen THIS Thinkscript...???
 
B

binhvesting

New member
VIP
Hi @BenTen, is it possible to add labels to this awesome indicator, and maybe the option to turn on and off both labels and the lines?
 
BenTen

BenTen

Administrative
Staff
VIP
@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);
 
L

lindosskier

Member
Hi @BenTen, 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.

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!
 
S

SuryaKiranC

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

Similar threads

Top