Converting Indicator To Multi-TimeFrame (MTF) In ThinkOrSwim

MerryDay

Administrative
Staff member
Staff
VIP
Lifetime

Converting Indicators To Multi-TimeFrame --A Tutorial

Technically, changing an indicator to MTF is relatively easy.
If you just what the close from the 15min agg put on your 5min chart just add the following to the beginning of your script.
Ruby:
input agg = AggregationPeriod.FIFTEEN_MIN ;
def close = close(period = agg);

The problem is determining what "price" you want to make into MTF.
Adding the below to the beginning of your script will work for most scenarios.
Ruby:
input agg = AggregationPeriod.FIFTEEN_MIN ;
def close = close(period = agg);
def open= open(period = agg);
def high= high(period = agg);
def low= low(period = agg);
def volume= volume(period = agg);
def hl2= hl2(period = agg);
def hlc3= hlc3(period = agg);
def ohlc4= ohlc4(period = agg);
Change the aggregation period to one of the 18 preset timeframes:
https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/AggregationPeriod
 
Last edited:
This can be adapted to any study but I generally use 5/30 charts intraday (find main move on 30 and look for entries on the small time frame) and day/week. This will automatically set the anchor indicator to the higher
Code:
declare lower;
def AP = GetAggregationPeriod();
def AP1 = if AP >= AggregationPeriod.thirty_min then AggregationPeriod.Week else  AggregationPeriod.thirty_min;
input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;

def min_low = lowest(low(period = AP1), percentKLength);
def max_high = highest(high(period = AP1), percentKLength);
def rel_diff = close(period = AP1) - (max_high + min_low)/2;
def diff = max_high - min_low;

def avgrel = expaverage(expaverage(rel_diff, percentDLength), percentDLength);
def avgdiff = expaverage(expaverage(diff, percentDLength), percentDLength);

plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
smi.setDefaultColor(getColor(1));

plot AvgSMI = expaverage(smi, percentDLength);
avgsmi.setDefaultColor(getcolor(5));

plot overbought = over_bought;
overbought.setDefaultColor(getcolor(5));

plot oversold = over_sold;
oversold.setDefaultColor(getcolor(5));

or for you TMO users
Code:
# TMO ((T)rue (M)omentum (O)scilator)
# Mobius
# V01.05.2018
#hint: TMO calculates momentum using the delta of price. Giving a much better picture of trend, tend reversals and divergence than momentum oscillators using price.

declare lower;

input length = 14;
input calcLength = 5;
input smoothLength = 3;
def AP = GetAggregationPeriod();

def MTF = if AP >= AggregationPeriod.thirty_min then AggregationPeriod.Week else  AggregationPeriod.thirty_min;

def o = open(period = MTF);
def c = close(period = MTF);
def data = fold i = 0 to length
           with s
           do s + (if c > GetValue(o, i)
                   then 1
                   else if c < GetValue(o, i)
                        then - 1
                        else 0);
def EMA5 = ExpAverage(data, calcLength);
plot Main = ExpAverage(EMA5, smoothLength);
plot Signal = ExpAverage(Main, smoothLength);
Main.AssignValueColor(if Main > Signal
                           then Color.Green
                           else Color.RED);
Signal.AssignValueColor(if Main > Signal
                             then Color.Green
                             else Color.RED);
Signal.HideBubble();
Signal.HideTitle();
AddCloud(Main, Signal,Color.Dark_Green, Color.Dark_RED);

addCloud(Main, Signal, color.green, color.red);
plot zero = if isNaN(c) then double.nan else 0;
     zero.SetDefaultColor(Color.gray);
     zero.hideBubble();
     zero.hideTitle();
plot ob = if isNaN(c) then double.nan else round(length * .7);
     ob.SetDefaultColor(Color.gray);
     ob.HideBubble();
     ob.HideTitle();
plot os = if isNaN(c) then double.nan else -round(length * .7);
     os.SetDefaultColor(Color.gray);
     os.HideBubble();
     os.HideTitle();
addCloud(ob, length, color.red, color.red, no);
addCloud(-length, os, color.green, color.green);


Would also be good to keep a 200 daily SMA on all your charts except higher times than a daily.
 
This is my first request, so please pardon if I am not asking this in the correct format. I am wondering if this very simple Price Action Indicator is able to MTF'd. I dont know if there is a general script that is used to MTF any thinkscript code or if it is deeper than that. Any assistance would be great!!!

#begin code#

declare lower;

plot PAIN = ((close - open) + (close - high) + (close - low)) / 2;
PAIN.SetDefaultColor(GetColor(1));

#end code#
 
if you want price data based on a time different from the chart time, you can use secondary aggregation to specify the time. for each price variable you intend to use , define a variable , that is equal to the modified version. then use that variable in your formula. then repeat all of that for each desired time.

def close60 = close(period = aggregation.hour);
def open60 = ...
def high60 = ...
def low60 = ...
plot PAIN60 = ((close60 - open60) + (close60 - high60) + (close60 - low60)) / 2;

https://tlc.thinkorswim.com/center/...ants/AggregationPeriod/AggregationPeriod-HOUR
 
if you want price data based on a time different from the chart time, you can use secondary aggregation to specify the time. for each price variable you intend to use , define a variable , that is equal to the modified version. then use that variable in your formula. then repeat all of that for each desired time.

def close60 = close(period = aggregation.hour);
def open60 = ...
def high60 = ...
def low60 = ...
plot PAIN60 = ((close60 - open60) + (close60 - high60) + (close60 - low60)) / 2;

https://tlc.thinkorswim.com/center/...ants/AggregationPeriod/AggregationPeriod-HOUR
So are you saying it should look like this:


def close60 = close(period = aggregation.hour);
def open60 = open(period = aggregation.hour);
def high60 = high(period = aggregation.hour);
def low60 = low(period = aggregation.hour);
plot PAIN60 = ((close60 - open60) + (close60 - high60) + (close60 - low60)) / 2;
#end code

If so, I am getting this error message, so I am pretty sure I am doing something wrong :(

No such constant: aggregation.hour at 16:30
Expected class com.devexperts.tos.thinkscript.Any at 16:15
No such constant: aggregation.hour at 17:28
Expected class com.devexperts.tos.thinkscript.Any at 17:14
 
Last edited:
So are you saying it should look like this:


def close60 = close(period = aggregation.hour);
def open60 = open(period = aggregation.hour);
def high60 = high(period = aggregation.hour);
def low60 = low(period = aggregation.hour);
plot PAIN60 = ((close60 - open60) + (close60 - high60) + (close60 - low60)) / 2;
#end code

If so, I am getting this error message, so I am pretty sure I am doing something wrong :(

No such constant: aggregation.hour at 16:30
Expected class com.devexperts.tos.thinkscript.Any at 16:15
No such constant: aggregation.hour at 17:28
Expected class com.devexperts.tos.thinkscript.Any at 17:14

oops , was typing too fast on my cell earlier, and didn't pay attention. ( i'm too used to copy/paste code snippits and not spelling them out)
thanks generic
 
Just trying to plot 3 simple ema's (34 in high, low, close) in multi time frame but for some reason it just shows the current chart time frame and wont chart the hourly time frame on a 5 minute. It plots it like its a 5 min for some reason cant get the input time period to work. Any help would be very appreciated!

input Period = aggregationPeriod.HOUR;


plot ema1 = ExpAverage (high, 34);
plot ema2 = ExpAverage (close, 34);
plot ema3 = ExpAverage (low, 34);plot Data = close;
 
I am trying to MTF this particular code and think I am doing something wrong
Code:
input price = close;
input length = 9;
input bar_plus = 7;

plot TSF = Inertia(price, length) + LinearRegressionSlope(price, length) * bar_plus;
TSF.SetDefaultColor(GetColor(8));


This is what I am adding to the original script, but it doesnt seem to working
Code:
input aggregationPeriod = AggregationPeriod.week;
def price = close(period = aggregationPeriod);

Am I doing something wrong?
 
Last edited by a moderator:
@younglove06 here is your fix.

BTW, you would want to refer to the this a HT of HTF than MTF, the later stands for Multi, as there is only one Higher Time Frame, Why do you want to call this MTF? now if you are plotting multiple of these on a same chart. the later makes sense.

Ruby:
input aP = AggregationPeriod.DAY;

input length = 9;
input bar_plus = 7;

def price = close(period = aP);

plot TSF = Inertia(price, length) + LinearRegressionSlope(price, length) * bar_plus;
TSF.SetDefaultColor(GetColor(8));
 
Last edited:
@younglove06 here is your fix.

BTW, you would want to refer to the this a HT of HTF than MTF, the later stands for Multi, as there is only one Higher Time Frame, Why do you want to call this MTF? now if you are plotting multiple of these on a same chart. the later makes sense.

Ruby:
input aP = AggregationPeriod.DAY;

input length = 9;
input bar_plus = 7;

def price = close(period = aP);

plot TSF = Inertia(price, length) + LinearRegressionSlope(price, length) * bar_plus;
TSF.SetDefaultColor(GetColor(8));
Yes, thats the plan. Multiple of these on the same chart :)
 
I am trying to add a higher timeframe to the following code. It is really just a code that plots an arrow when the heikin ashi candle switches from uptrend to downtrend. But I am struggling with adding the command to be able to add a higher timeframe.

Here is the code:

def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def haColor = haClose > haOpen;
plot trendUp = haColor and !haColor[1];
trendUp.SetDefaultColor(Color.CYAN);
trendUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot trendDown = !haColor and haColor[1];
trendDown.SetDefaultColor(Color.MAGENTA);
trendDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

I am using this command to try to add it, but not sure what goes after the "input"

input = AggregationPeriod.TWO_HOURS;
 
Last edited by a moderator:
I am trying to add a higher timeframe to the following code. It is really just a code that plots an arrow when the heikin ashi candle switches from uptrend to downtrend. But I am struggling with adding the command to be able to add a higher timeframe.

Here is the code:

def haClose = ohlc4;
def haOpen = if haOpen[1] == 0 then haClose[1] else (haOpen[1] + haClose[1]) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def haColor = haClose > haOpen;
plot trendUp = haColor and !haColor[1];
trendUp.SetDefaultColor(Color.CYAN);
trendUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot trendDown = !haColor and haColor[1];
trendDown.SetDefaultColor(Color.MAGENTA);
trendDown.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);

I am using this command to try to add it, but not sure what goes after the "input"

input = AggregationPeriod.TWO_HOURS;
Piece of cake. Just follow the instructions in the first post.
 
Merry Thank you this is awesome, are close, open, high, low and volume; and combination of these the only things you would need to (Period = Agg), or do we need to do any thing with averages.

If so could you also post a more complexed code in the example at the top, one which includes averages and all of the different considerations
 
Last edited:
TSI Dashboard For ThinkOrSwim
Here is my TSI Dashboard.
You should be able to amend it to meet your needs.

This version is superior to the Tradestation code you listed. Your code only looks at above and below zero. This takes all the conditions into account.

I use the True Strength dashboard to weed out the false signals of other studies.
7xPzgeb.png

Ruby:
###########################################################################
#TSItsiTSI
#Settings
#standard = 25,13,8
#quicker = 13,7,8
#daily, weekly & monthly = 40,20,8

#A quick review of the BUY signals:
#1. TSI crosses above zero.
#2. TSI crosses above moving average.
#3. TSI makes higher low (below zero) while price makes a lower low

#And the SELL signals:
#1. TSI crosses below zero
#2. TSI crosses below moving average
#3. TSI makes a lower high (above zero) while price makes a higher high
#4. Trend of TSI breaks down
declare lower;
input longLength = 25;
input shortLength = 13;
input signalLength = 8;
input averageType = AverageType.EXPONENTIAL;

def diff = close - close[1];
def doubleSmoothedAbsDiff = MovingAverage(averageType, MovingAverage(averageType, AbsValue(diff), longLength), shortLength);

def TSI = if doubleSmoothedAbsDiff == 0 then 0
      else 100 * (MovingAverage(averageType, MovingAverage(averageType, diff, longLength), shortLength)) / doubleSmoothedAbsDiff;

def Signal = MovingAverage(averageType, TSI, signalLength);
##########################################################################
#TSI Short Term

input longLengths = 8;
input shortLengths = 8;
input signalLengths = 3;

def diffs = close - close[1];
def doubleSmoothedAbsDiffs = MovingAverage(averageType, MovingAverage(averageType, AbsValue(diffs), longLengths), shortLengths);

def TSIs;
TSIs = if doubleSmoothedAbsDiffs == 0 then 0
      else 100 * (MovingAverage(averageType, MovingAverage(averageType, diffs, longLengths), shortLengths)) / doubleSmoothedAbsDiffs;
##########################################################################
#TSI Long Term
input longLengthl = 40;
input shortLengthl = 20;
input signalLengthl = 8;

def diffl = close - close[1];
def doubleSmoothedAbsDiffl = MovingAverage(averageType, MovingAverage(averageType, AbsValue(diffl), longLengthl), shortLengthl);

def TSIl;
TSIl = if doubleSmoothedAbsDiffl == 0 then 0
      else 100 * (MovingAverage(averageType, MovingAverage(averageType, diffl, longLengthl), shortLengthl)) / doubleSmoothedAbsDiffl;
######################################################################
#TSI Horizontal Line
def bullish = TSIl > TSIl[1] and TSIs > TSIs[1] and TSI > TSI[1];
def bearish = TSIl < TSIl[1] and TSIs < TSIs[1] and TSI < TSI[1];
def bar = barNumber();

plot TSI_Dashboard = 0.00;
TSI_Dashboard.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
TSI_Dashboard.SetLineWeight(2);
TSI_Dashboard.AssignValueColor(if bullish then Color.cyan else if bearish then Color.dark_orange else Color.GRAY);
can you help with coding MTF in one indicator
for (5 min, 30 min, 1 Hrs, 4 Hrs, Daily, weekly) horizontal line for standard 21, 16, 8 TIS? thank you @MerryDay
 
Last edited:
Merry Thank you this is awesome, are close, open, high, low and volume; and combination of these the only things you would need to (Period = Agg), or do we need to do any thing with averages.

If so could you also post a more complexed code in the example at the top, one which includes averages and all of the different considerations

So I was able to get the answer from the TOS P2P lounge if any one was wondering. In short the answer is yes, once your in script your looking to modify the green high lighted variables. I say this is case your looking to mod something like the AO which doesnt have H, L, C, etc... But if you run into something like a WilliamsAD (WAD) which has no H, L, C, etc... you will want to break the code down and work the MTF angle for the basic script.
 
Last edited:
So if I wanted to make an indicator that uses the Day aggregation and shows input from smaller time frames it's not possible right? I've been trying and there's no errors in the code but the indicator is blank and there's an error message top left of screen saying 'secondary period cannot be less than primary' ..
 
So if I wanted to make an indicator that uses the Day aggregation and shows input from smaller time frames it's not possible right? I've been trying and there's no errors in the code but the indicator is blank and there's an error message top left of screen saying 'secondary period cannot be less than primary' ..
That is right. You can post a higher timeframe on a lower chart. But you can't post a lower timeframe on a higher chart.
 

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