Multi-timeframe (MTF) Moving Average Indicator for ThinkorSwim

@lindosskier Just for a good measure, grab the copy of the code from my original post, and overwrite (select all, delete and past) in your study one more time, you don't have to repeat this on every instance of the study, just once and save it will do.

Now once that is done, make sure you select have all the timeframes you desire to see, configured and save the studyset.

when you are on 1Min charts you should see all the selected timeframes from 1m to all they way up to 1D. (Please note they will only change color, if the active candle on the timeframe has one of the four signals, Buy/Momentum UP/Momentum Down and Sell, if there is no active signal it will remain dark, you should be able to see the configured timeframe, but barley.)

In-order to see a higher frames such as W or M or even Q on 1Min charts, your chart should present the data. ex: 1D, 1Min Configured timeframe will not have data more than a Day. hence you don't see W, M or Q even when the Study set is configured with a set for that timeframe.

if you must have a 1M data on that chart please configure a chart timeframe for 20D,1Min. Similarly if you must see the Q Data, I suggest your use a 90D Chart with desired Min configuration.


Hope this helps. and finally if you want a matching study of the original code you posted (matching colors as the MTF Study) let me know. I can post the modified code.
 

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

@SuryaKiranC thank you! now that you explained all these things, it makes sense and that's how the code works. Can I ask for 1 last modification? How can we have the background of the bubbles/labels show the color that the candles are, at their respective time frames, AT ALL TIMES. So, if the 5min. candle is yellow and the 60min. candle is red, have the 5min and 60min. bubbles/labels, colors be and REMAIN yellow and red, until they change. Thank you once again, I appreciate your help.
 
@SuryaKiranC thank you! now that you explained all these things, it makes sense and that's how the code works.
Can I ask for 1 last modification? How can we have the background of the bubbles/labels show the color that the candles are, at their respective time frames, AT ALL TIMES. So, if the 5min. candle is yellow and the 60min. candle is red, have the 5min and 60min. bubbles/labels, colors be and REMAIN yellow and red, until they change. Thank you once again, I appreciate your help.

alright refresh the code in your Multi_EMAStudy.ts again from the post

https://usethinkscript.com/threads/...rage-indicator-for-thinkorswim.135/post-40067 (I updated the code in the same comment instead of making a new post, this way anybody new grabbing the code will be less confused, which version to grab.)

Also use the following as your chart study, basically add another study for the same Study Set, with the following code, Just so all the colors match up for you.
Code:
#Custom_EMAStudy.ts
#Mr Slim Miller at askSLIM dot com
#SlimRibbonCustom_markos9-7-18

input ShowChartBubbles = yes;

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

def price = close;
input superfast_length = 8;
input fast_length = 13;
input slow_length = 21;
input displace = 0;

def mov_avg8 = ExpAverage(close[-displace], superfast_length);
def mov_avg13 = ExpAverage(close[-displace], fast_length);
def mov_avg21 = ExpAverage(close[-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;
AddChartBubble(ShowChartBubbles and Buy_Signal, low , "Long", GlobalColor("Buy"),no);
Buy_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
Buy_Signal.SetLineWeight(4);
Buy_Signal.SetDefaultColor(GlobalColor("Buy"));
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(GlobalColor("MomentumDown"));
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;
AddChartBubble(ShowChartBubbles and Sell_Signal, high , "Short", GlobalColor("Sell"),up=yes);
Sell_Signal.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
Sell_Signal.SetLineWeight(4);
Sell_Signal.SetDefaultColor(GlobalColor("Sell"));
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(GlobalColor("MomentumUp"));
Momentum_Up.HideTitle();
Alert(condition = sellsignal[1] == 1 and sellsignal == 0, text = "Momentum_Up", sound = Sound.Bell, "alert type" = Alert.BAR);

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();
AssignPriceColor(if Colorbars == 4 then GlobalColor("Buy") else if Colorbars == 3 then GlobalColor("MomentumDown") else if Colorbars == 2 then GlobalColor("MomentumUp") else if Colorbars == 1 then GlobalColor("Sell") else Color.CURRENT);
#end
 
Last edited:
@SuryaKiranC the colors stay plotted on the background of each bubble/label, which is great, but a lot of them do not match the actual colors of the candles. For example, you can see from the picture below that the 30 and 60 min. bubbles show red, where in reality are green. That's from using the original code (which I copied and pasted again, since you changed it). The last piece of code you posted does not change anything, it just plots long or short on the candles, when they change colors. Once again, thank you for your help in trying to figure this out.

 
@lindosskier Can you capture and post the part of the screen that is useful, with out the white?

Anyways, from what I can see your current chart is Green candle and the label us green. What is the issue here you were trying to point out?
are you saying if you are in 30 and 60 min charts at the time you are looking at this indicator they were green candles but the indicator is showing red? if yes you got to show . me that charts too.

@lindosskier if you are comparing with the timeframe, please do it with out any study, not even the orginal. the original study may be painting green or red based on some condition not the the plain charts. What I am trying to mimic is no study original candle color and paint the color for the 4 signals defined only.
 
Last edited by a moderator:
@SuryaKiranC I see what you are saying and yes, your code achieves exactly that. But what I am trying to accomplish is to use the study that paints the candles green, yellow, or red, based on the study I provided. The idea is to use somewhat of a trend and stay in a trade, as long as the candles are at least yellow on different timeframes. Are you able to change the code to use the candle colors based on that study I provided, for the different timeframes? Thank you very much for your efforts!

The idea is that when i see the 5min become yellow, followed by the 15min becoming yellow and the 30min. becoming yellow (based on the 8/13/21 EMA study I provided ), then I know that there is a trend change most likely and to get out of a position. That's what I am trying to find out. Thank you!

Hi @SuryaKiranC , hope all is well. I found this piece of code that plots the Squeeze indicator colors on the top of the page, with the different timeframes. Was wondering if this would be easier for you, if you could replace the parts of the code that are for the squeeze and replace with the triple EMA study I provided.
Thanks again for all your help, much appreciated!

Code:
input dStr ="4 hours";


script MySqueeze{
def length  = 20;
def AtrMult = 1.4;
def SdMult  = 2;

input period ="Daily";


def valueClose   =  close(period = period);
def valueHigh   =  high(period = period);
def valueLow   =  low(period = period);

def SD = StDev(valueClose, length);

def Avg = Average(valueClose, length);

def ATR = Average(TrueRange(valueHigh, valueClose, valueLow), length);

def SDup = Avg + (SdMult * SD);

def ATRup = Avg + (AtrMult * ATR);



plot Squeeze = if SDup < ATRup

               then 1

               else 0;


}


def dSQ= MySqueeze(dStr);
AddLabel(yes, dStr, if dSQ
then  Color.RED else  Color.GREEN); # display label red if has squeeze
 
Last edited by a moderator:
is there a way to get this to work on a mobile device ? I would like to overlay the 4hr 21 SMA on my 1 hr chart and 15 min chart
 
is there something out there that will work Ben ?
It's the App that is the problem... It doesn't support MTF... You'd have to switch to a different platform if any of them support MTF... Not sure if any other App's can use TDA accounts...
 
I'm trying to show the 4 daily ema on all time frames. It works on my laptop but on my mobile device
the plot is just slightly off. What is the problem? Thanks

Code:
declare weak_volume_dependency;

input length = 4;
input averageType = AverageType.exponential;
input agg = aggregationPeriod.day;

def average = MovingAverage(averageType, close(period = agg), length);

plot sma = average;
sma.SetLineWeight(5);
sma.SetDefaultColor(GetColor(1));

addlabel(yes, "4EMA: " + average, color.red);
 
I have created an indicated that plots the 5min 9ema on a 1 min chart and it works but it looks kinda choppy. Can someone look at my code and see if there is a way to SMOOTH it out please?

Code:
input Period = AggregationPeriod.Five_Min;
input AvgType = AverageType.EXPONENTIAL;
input Length = 9;
plot AVG = MovingAverage(AvgType, close(period = Period), Length);
AVG.SetDefaultColor(Color.cyan);
 
update....

I figured out you can just use a 45ema on the 1 min which will be very close to the 9ema on the 5min chart. Its not exact but close enough for my use.
 
Last edited:
@BenTen Do you know if there's a way to turn this into a lower chart study for EMA crossovers?.

Example idea: If you're using a 1 minute chart as your primary chart, but you want to only take trades when the 9 EMA is above the 20 EMA on the 5 and 15 minute charts, it would be cool to have a lower chart study that has a row for the 5 minute status and then another row for the 15 minute status. Instead of using EMA lines, you would use a Green dot that shows up for each 1 minute candle when the 9 EMA is above the 20 EMA for each additional timeframe (ex: 5 and 15 minute) you are monitoring?

Is this possible and easy to build?
 
Provided below is thinkscript someone helped me build. It is a lower chart study that shows Green Dots if the EMA is Greater than another EMA for multiple timeframes. This is great if you have a trend based strategy and you only want to take trades when a short-term EMA (e.g. 9) is above the long-term EMA (e.g. 20) on multiple-timeframes. You could do this with multiple charts using the standard EMA lines, but that takes up a lot of space. This allows you to see the same info in a different visualization.

There's one problem... It doesn't show multiple rows. It combines everything into one row. If that works for you then great. If you know how to break each chart timeframe into multiple dot rows, please reply.

Code:
declare lower;

input timeFrameOne = AggregationPeriod.FIFTEEN_MIN;
input timeFrameTwo = AggregationPeriod.FIFTEEN_MIN;
input timeFrameThree = AggregationPeriod.FIFTEEN_MIN;

input maLengthOne = 5;
input maTypeOne = AverageType.EXPONENTIAL;
input maLengthTwo = 20;
input maTypeTwo = AverageType.EXPONENTIAL;

def maOne = MovingAverage(maTypeOne, close(period = timeFrameOne), maLengthOne);
def maTwo = MovingAverage(maTypeTwo, close(period = timeFrameOne), maLengthTwo);
def maThree = MovingAverage(maTypeTwo, close(period = timeFrameOne), maLengthTwo);

#Row 1
plot rowOne = if !IsNaN(close) then 0 else Double.NaN;
rowOne.SetPaintingStrategy(PaintingStrategy.POINTS);
rowOne.AssignValueColor(if maOne > maTwo then Color.GREEN else Color.RED);
rowOne.SetLineWeight(3);

#Row 2
plot rowTwo = if !IsNaN(close) then 0 else Double.NaN;
rowTwo.SetPaintingStrategy(PaintingStrategy.POINTS);
rowTwo.AssignValueColor(if maOne > maTwo then Color.GREEN else Color.RED);
rowTwo.SetLineWeight(3);

#Row 3
plot rowThree = if !IsNaN(close) then 0 else Double.NaN;
rowThree.SetPaintingStrategy(PaintingStrategy.POINTS);
rowThree.AssignValueColor(if maOne > maTwo then Color.GREEN else Color.RED);
rowThree.SetLineWeight(3);
 
@GetRichOrDieTrying Lets start with the first few lines of code:

You have to define each agg period by itself 5_min, 10_min, 15_min

Next you have to define each Data point for each agg period:

Code:
def CloseA = Close(period = agg1);

You then have to define each of the MA's for each Agg period:

Code:
Def MA1A = MovingAverage(maTypeOne, closeA, maLengthOne);
Def MA2A = MovingAverage(maTypetwo, closeA, maLengthTwo);
Def MA1B = MovingAverage(maTypeOne, closeB, maLengthOne);
Def MA2B = MovingAverage(maTypetwo, closeB, maLengthTwo);

and so forth

You may want to look at this indicator

https://usethinkscript.com/threads/bias_w_3_mtf.4457/#post-41123
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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