Multi-timeframe (MTF) Moving Average Indicator for ThinkorSwim

I want to apply a moving average from a much higher timeframe onto a 10 minute chart however ToS won't allow a moving average greater than 2000. Is there anyway to bypass this restriction?
What higher time frame are you looking at that requires a 2000 moving average? This makes no sense...Typically a higher timeframe will require a smaller moving average and a lower timeframe will require a higher moving average to be equivalent. For example...a 20 MA on 15 min is equal to 60 MA on 5 min...so what is the initial time frame that you're looking at and the moving average to be equal to 2000 MA on lower timeframes as in your case 10 min...which is not that low of a timeframe...
 

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

I was looking to show say the 100sma from the 1 minute chart on all my charts below the daily. Ive been working on the code below, but I get this weird error at the top left of the chart within TOS (warning: secondary periods cannot be less than primary). But what happens is it only shows the MA on the 1m chart, and that warning message above on any other time frame.

Any ideas or feedback is welcome?

Code:
DefineGlobalColor("White", CreateColor(255, 255, 255));

input show100Line = Yes;
input LineWidthLIS = 2;
input openingPMTime  = 0400.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.
input openingRTHTime  = 0800.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.
input openingTime  = 0930.0; #hint OrMeanS: Begin Mean Period. Usually Market Open EST.
input endOfDayTime = 1600.0; #hint end of normal trading hours 4pm est.
input bDebugMode = Yes;


def na = Double.NaN;
def day = GetDay();
def isDaily = If (GetAggregationPeriod() == AggregationPeriod.DAY, yes, no);
def isIntraDay = If (GetAggregationPeriod() < AggregationPeriod.DAY, yes, no);
def isToday = If (day == GetLastDay() and SecondsFromTime(openingPMTime) >= 0, yes, no);
def isPreMarket = If (day == GetLastDay() and (SecondsTillTime(openingPMTime) < 0 and SecondsFromTime(openingTime) > 0) , yes, no);
def bShowChartBubble = if (isToday and isBelow15m, Yes, No);

#Define the D, Wkly, Mthly values
def SMA_100_on_1m = MovingAverage(averageType.SIMPLE, close(period = AggregationPeriod.MIN), SMAPeriod100);


#============================================================================================================
#Plot the 100 Line
#============================================================================================================
plot PlotSMA100 = if show100Line and isIntraDay then SMA_100_on_1m else na;
     PlotSMA100.setDefaultColor(GlobalColor("White"));
     PlotSMA100.setLineWeight(LineWidthLIS);
     PlotSMA100.setPaintingStrategy(paintingStrategy.LINE);
     PlotSMA100.HideTitle();
     PlotSMA100.HideBubble();

NFLX showing 1m (white line is the sma moving average)

D1FhuEj.png


NFLX showing 5m, no white line with error/warning message secondary periods cannot be less than primary

FJBUViV.png


@horserider You and I worked on some other scripts before, just wondering if you have any thoughts on this one. TYIA buddy.
 
Last edited by a moderator:
I basically want my 5 Minute 20SMA to change color when it's RISING/FALLING in my 1 Minute Chart.
(And various other time frames).

I have this which the 20SMA changes color when the Trend/Slope is Rising/Falling on a single time frame.
https://tos.mx/vWQCesV
Code:
plot SMA = Average(close, 20);
SMA.AssignValueColor(if SMA[1] < SMA then Color.GREEN else Color.RED);

And I know TOS already has a MTF SMA(thinkscript below), but I just don't know how to put it together for the MTF 20SMA Color to Change based on Trend/Slope.
input price = FundamentalType.CLOSE;
input aggregationPeriod = AggregationPeriod.DAY;
input length = 9;
input displace = 0;
input showOnlyLastPeriod = no;

plot DailySMA;

if showOnlyLastPeriod and !IsNaN(close(period = aggregationPeriod)[-1]) {
    DailySMA = Double.NaN;
} else {
    DailySMA = Average(fundamental(price, period = aggregationPeriod)[-displace], length);
}

DailySMA.SetDefaultColor(GetColor(1));
DailySMA.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

Please advise and Thank You in Advance!
 
Last edited by a moderator:
Here you go.

Code:
# MTF Moving Average

input Period = AggregationPeriod.FIVE_MIN;
input AvgType = AverageType.SIMPLE;
input Length = 20;
input priceclose = close;

plot AVG = MovingAverage(AvgType, close(period = Period), Length);
AVG.SetDefaultColor(Color.YELLOW);

#AVG.AssignValueColor(if AVG > AVG[1] then Color.GREEN else Color.RED);

#def sState1 = if AVG > AVG[1] then 100 else -100;
def sState2 = if AVG > AVG[1] then 100 else -100;
AVG.AssignValueColor(if sState2 == 100 then Color.GREEN else Color.RED);
AVG.SetLineWeight(2);
 
I actually tried that and the prices were different because I used Wkly SMA how would I change the EMA to SMA @BenTen . Thank You

@BenTen forget it i figured it out thanks
 
Last edited by a moderator:
Hello all, i can honestly say this forum has made the difference in my trading, much appreciated.

I have searched and searched and can not find what i am targeting. I use 5m and 100t charts, i am looking to bring in a 20 day or 200 day EMA or SMA into the 5m charts. I can do it on the daily charts via the standard TOS studies, but it looks at periods, so when i want to see the 20 day on an intraday chart, there is not an option.

Anyone know how to do this?

Anything is very appreciated and hopefully others find value in this study.
 
@wcsharron Here's something simple I put together a while back. It plots three daily moving averages of your choice and identifies them on the chart. The identifying bubbles can be turned off. It may do what you want.

Code:
#Daily Simple Moving Averages with Identifying Bubbles
#Pensar

#Simple Moving Averages
input avg_1 = 20;
input avg_2 = 50;
input avg_3 = 200;
input avg_type = AverageType.SIMPLE;

def c = close(period = AggregationPeriod.DAY);

plot ma1 = MovingAverage(avg_type,c,avg_1);
     ma1.SetDefaultColor(Color.YELLOW);
plot ma2 = MovingAverage(avg_type,c,avg_2);
     ma2.SetDefaultColor(Color.DARK_ORANGE);
plot ma3 = MovingAverage(avg_type,c,avg_3);
     ma3.SetDefaultColor(Color.GREEN);

#Bubbles
input show_bubbles = yes;

def x = !IsNaN(close[-25]) and IsNaN(close[-26]);

AddChartBubble(show_bubbles and x, ma1, avg_1 + " MA", Color.YELLOW, if ma1 > c then yes else no);
AddChartBubble(show_bubbles and x, ma2, avg_2 + " MA", Color.DARK_ORANGE, if ma2 > c then yes else no);
AddChartBubble(show_bubbles and x, ma3, avg_3 + " MA", Color.GREEN, if ma3 > c then yes else no);

#end code
 
How can I accomplish this with range bars?

Ex: I am trading 8 Range, and want to plot moving averages from 21 Range onto my chart.

*to change to the range bar type go to settings->Time Axis->Aggregation Type:Range, Price Range:8, Range Type:Range Bars

Thanks
 
@adii800 ThinkScript does not permit higher timeframe indicators based on range or tick bars. This list here - https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AggregationPeriod - is unfortunately all that is available. :(

A workaround might be to increase the length of the moving averages on the 8 Range chart until they are similar to the 21 Range chart moving averages. For instance, the 21 SMA on the 21 Range chart looks comparable to the 100 SMA on the 8 Range chart when looking at /NQ.

If you find a way to do it, let me know! Ive wanted to do this for a while with tick charts.
 
I am trying to get the 3-minute 72 EMA and 89 EMA on the 5-minute chart.
The script below works for the 1-, 2-, and 3-minute charts only. Anything higher I get message "Secondary period cannot be less than primary period."
Is there a way to get this to work?

Code:
input price = close;
input displace = 0;
input length1 = 72;
input length2 = 89;
input period = AggregationPeriod.THREE_MIN;
input averageType = AverageType.EXPONENTIAL;

plot EMA72 = MovingAverage(averageType, close(period = period) [-displace], length = length1);

plot EMA89 = MovingAverage(averageType, close(period = period) [-displace], length = length2);
 
Ok, i am looking to have 20 day, 100 day and 200 day Daily moving averages coded into whatever time frame i am looking at.

So if i am on the 1 day time frame - easy, TOS has that as a canned report. However when i move to a 5m chart or a 100 tick chart, the 20 day etc are not an option as it is looking at the current candles for the look back.

anyone who can figure this out has 50 via venmo coming and i am sure appreciation from others, so that will be nice.

Thanks!
-Chris
 
Correct. The daily does not translate into 5m or 100 tick time frames, so this needs to be custom coded, which i can not do ( don't know how ) but really happy to help those that can.

Thanks!
 
@wcsharron Try something like this.

Code:
input aP = AggregationPeriod.DAY;
    input length1 = 21;
    input length2 = 34;
    def ema1 = expaverage(close(period = aP), length1);
    def ema2 = expaverage(close(period = aP), length2);

You can do the same with simple too, if that is what you are looking for.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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