Converting Indicator To Multi-TimeFrame (MTF) In ThinkOrSwim

MerryDay

Staff member
Staff
VIP Converting Indicators To Multi-TimeFrame --A Tutorial

Step 1: Add the definition statements in column 2 to your study
 column 1 column 2 aggregation period input agg = AggregationPeriod.THIRTY_MIN; close def C = Close(Period =agg); open def O = Open(Period = agg); high def H = High(Period = agg); low def L = Low(Period = agg); volume def V = Volume(Period = agg); ohlc4 def price = ohlc4(Period=agg);

Step 2: In your study that you are converting, replace all names in column1 with the names in column2
When converting long scripts, using a code editor that has a global replace function is essential in order to find and replace all names.

Original Code for TOS PriceAndVolumeTrend The MTF version for TOS PriceAndVolumeTrend  Last edited:

subharmonic

Member
VIP
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();

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();

Would also be good to keep a 200 daily SMA on all your charts except higher times than a daily.

• MerryDay

younglove06

New member
2019 Donor
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#

halcyonguy

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

younglove06

New member
2019 Donor
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:

halcyonguy

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

Shane

New member
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;

MerryDay

Staff member
Staff
VIP
See post#1 --Easy Steps to creating a MTF indicator

younglove06

New member
2019 Donor
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:

SuryaKiranC

Well-known member

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

New member
2019 Donor
Thank you!!! I'll update this on my TOS when I get to my laptop younglove06

New member
2019 Donor

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 younglove06

New member
2019 Donor
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 == 0 then haClose else (haOpen + haClose) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def haColor = haClose > haOpen;
plot trendUp = haColor and !haColor;
trendUp.SetDefaultColor(Color.CYAN);
trendUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot trendDown = !haColor and haColor;
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:

MerryDay

Staff member
Staff
VIP
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 == 0 then haClose else (haOpen + haClose) / 2;
def haHigh = Max(high, Max(haClose, haOpen));
def haLow = Min(low, Min(haClose, haOpen));
def haColor = haClose > haOpen;
plot trendUp = haColor and !haColor;
trendUp.SetDefaultColor(Color.CYAN);
trendUp.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
plot trendDown = !haColor and haColor;
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.

Williasmsmd19

New member
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:

APOT7

Member
VIP
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. 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;
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;
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;
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 and TSIs > TSIs and TSI > TSI;
def bearish = TSIl < TSIl and TSIs < TSIs and TSI < TSI;
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:

Williasmsmd19

New member
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:
• MerryDay

tibby42

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

Staff member
Staff
VIP