Repaints MTF Trend Table For ThinkOrSwim

Repaints

xxentre

New member
VIP
Can someone convert this code from Tradingview to ToS. I have found it to be useful in determining the trend.
This indicator identifies trends in multiple higher timeframes and shows them in a widget off to the right of the chart. I like using it with the 1hr, 4hr, Daily, and Weekly.
It is called MTF Trend Widget - https://www.tradingview.com/v/9XkspWiV/
Screenshot 2023-08-03 at 8.42.14 AM.png
 
Last edited by a moderator:
Can someone convert this code from Tradingview to ToS. I have found it to be useful in determining the trend.
This indicator identifies trends in multiple higher timeframes and shows them in a widget off to the right of the chart. I like using it with the 1hr, 4hr, Daily, and Weekly.
It is called MTF Trend Widget - https://www.tradingview.com/v/9XkspWiV/


Code:
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © millerrh

//@version=5
// Revision 2 - Optimizing the code for improved speed (with some help from @RozaniGhani-RG), allowing the user to control which timeframes to look at,
// and moving indicator to table so I can use a mutable variable to indicate which timeframe the user selected.
// Revision 3 - More speed optimization to limit the # of security calls.  Allow for abiilty for user to reposition the table.
// Revision 4 - Added ability to choose between close or high for pivot.

// This indicator identifies trends in multiple higher timeframes and shows them in a widget off to the right of the chart.
// It's meant to be used as an alternative filter for "trading with the trend." Typically people use moving averages of varying lengths for this
// (i.e. if over 200 MA it's an uptrend, etc.), but I wanted to see if it might be more effective to see if the higher timeframes were actually
// trending or not in a certain direction.
// For the purposes of this indicator, an uptrend is defined as higher highs and higher lows.  So if currently in a downtrend and the highs are broken,
// the indicator will flip to an uptrend because now we have a higher high.  Vice versa for downtrends.
// The user can choose the lookback period for defining these highs/lows (the pivot points).  A smaller lookback number will give you more frequent pivot points.
// The user can toggle on visibility of all historical pivot points to make sure the frequency and placement of the swing highs/lows is to their liking.
// The user can show the support/resistance lines of those most recent swing high/low points on the multiple timeframes as well.
// When these lines are breached, that is when the trends change, so you can see if you are close to changing any longer term trends.

indicator('MTF Trend Table', overlay=true)

// == USER INPUT ==
tableLocation = input.string(defval='Top', options=['Top', 'Bottom'], title='Info Table Location', group='Display',
  tooltip='Place information table on the top of the pane or the bottom of the pane.')
lookback = input.int(defval=3, title='Pivot Lookback Period', group='Pivot Points',
  tooltip='Looks for pivot points within this number of bars both left and right.')
pivotType = input.string(defval = "High/Low", title = "Pivot Type", options = ["High/Low","Close"], group = 'Pivot Points',
  tooltip = 'Choose whether the high/low or close is used for pivot points and trend calcuations.')
showPivotPoints = input.bool(title='Show Historical Pivot Points?', defval=false, group='Pivot Points',
  tooltip='Toggle this on to see the historical pivot points that were used.  Change the Lookback Period to adjust the
  frequency of these points. The pivot points are only shown for the current chart timeframe - to see the Daily pivot pionts, use the Daily timeframe, etc.')
oneSet = input.timeframe(defval='240', title='First Timeframe', group='Higher Timeframe Levels',
  tooltip='Allows you to set different time frames for looking at the trend. Important: Does not work for lower timeframes than the current
  chart timeframe.')
twoSet = input.timeframe(defval='D', title='Second Timeframe', group='Higher Timeframe Levels')
threeSet = input.timeframe(defval='W', title='Third Timeframe', group='Higher Timeframe Levels')
fourSet = input.timeframe(defval='M', title='Fourth Timeframe', group='Higher Timeframe Levels')
showMTFLevels = input.bool(title='Show Multiple Timeframe S/R Levels?', defval=true, group='Higher Timeframe Levels',
  tooltip='Displays the pivot highs and lows of higher timeframes to use as support/resistance levels. When these levels break,
  the trend will change on these higher timeframes.')
oneColorS = input.color(color.new(color.orange, 50), title='1st Timeframe    Support', group='Higher Timeframe Levels', inline='MTF1')
oneColorR = input.color(color.new(color.orange, 50), title=' Resistance', group='Higher Timeframe Levels', inline='MTF1')
twoColorS = input.color(color.new(color.blue, 50), title='2nd Timeframe   Support', group='Higher Timeframe Levels', inline='MTF2')
twoColorR = input.color(color.new(color.blue, 50), title=' Resistance', group='Higher Timeframe Levels', inline='MTF2')
threeColorS = input.color(color.new(color.white, 50), title='3rd Timeframe    Support', group='Higher Timeframe Levels', inline='MTF3')
threeColorR = input.color(color.new(color.white, 50), title=' Resistance', group='Higher Timeframe Levels', inline='MTF3')
fourColorS = input.color(color.new(color.red, 50), title='4th Timeframe    Support', group='Higher Timeframe Levels', inline='MTF4')
fourColorR = input.color(color.new(color.red, 50), title=' Resistance', group='Higher Timeframe Levels', inline='MTF4')
levelWidth = input.int(defval=1, title='Line Width (pixels)', group='Higher Timeframe Levels')

//  == DEFINE FUNCTIONS FOR USE IN MULTIPLE TIMEFRAMES (USING A TUPLE TO AVOID SO MANY SECURITY CALLS) ==
f_getHTF() =>
    var float ph = na, float pl = na, float highLevel = na, float lowLevel = na
    if pivotType == "Close"
        ph := ta.pivothigh(close, lookback, lookback)
        pl := ta.pivotlow(close, lookback, lookback)
        highLevel := ta.valuewhen(ph, close[lookback], 0)
        lowLevel := ta.valuewhen(pl, close[lookback], 0)
    else
        ph := ta.pivothigh(high, lookback, lookback)
        pl := ta.pivotlow(low, lookback, lookback)
        highLevel := ta.valuewhen(ph, high[lookback], 0)
        lowLevel := ta.valuewhen(pl, low[lookback], 0)
    barsSinceHigh = ta.barssince(ph) + lookback
    barsSinceLow = ta.barssince(pl) + lookback
    timeSinceHigh = time[barsSinceHigh]
    timeSinceLow = time[barsSinceLow]
    [ph, pl, highLevel, lowLevel, barsSinceHigh, barsSinceLow, timeSinceHigh, timeSinceLow]

[ph_01, pl_01, hL_01, lL_01, bsSH_01, bsSL_01, tSH_01, tSL_01] = request.security(syminfo.tickerid, oneSet, f_getHTF())
[ph_02, pl_02, hL_02, lL_02, bsSH_02, bsSL_02, tSH_02, tSL_02] = request.security(syminfo.tickerid, twoSet, f_getHTF())
[ph_03, pl_03, hL_03, lL_03, bsSH_03, bsSL_03, tSH_03, tSL_03] = request.security(syminfo.tickerid, threeSet, f_getHTF())
[ph_04, pl_04, hL_04, lL_04, bsSH_04, bsSL_04, tSH_04, tSL_04] = request.security(syminfo.tickerid, fourSet, f_getHTF())

// Check to ensure boxes are all higher timeframe than the chart to remove glyph and gray out box if that's the case
tfInMinutes(simple string tf = "") =>
    float chartTf =
      timeframe.multiplier * (
      timeframe.isseconds ? 1. / 60             :
      timeframe.isminutes ? 1.                  :
      timeframe.isdaily   ? 60. * 24            :
      timeframe.isweekly  ? 60. * 24 * 7        :
      timeframe.ismonthly ? 60. * 24 * 30.4375  : na)
    float result = tf == "" ? chartTf : request.security(syminfo.tickerid, tf, chartTf)
 
float chartTFInMinutes = tfInMinutes()
bool TF1Check = tfInMinutes(oneSet) < chartTFInMinutes
bool TF2Check = tfInMinutes(twoSet) < chartTFInMinutes
bool TF3Check = tfInMinutes(threeSet) < chartTFInMinutes
bool TF4Check = tfInMinutes(fourSet) < chartTFInMinutes

// Current timeframe pivots
var float phC = na, float plC = na
if pivotType == "High/Low"
    phC := ta.pivothigh(high, lookback, lookback)
    plC := ta.pivotlow(low, lookback, lookback)
else
    phC := ta.pivothigh(close, lookback, lookback)
    plC := ta.pivotlow(close, lookback, lookback)

// Plot historical pivot points for debugging and configuring the lookback period.
plot(showPivotPoints ? phC : na, style=plot.style_cross, linewidth=3, color=color.new(color.yellow, 50), offset=-lookback)
plot(showPivotPoints ? plC : na, style=plot.style_cross, linewidth=3, color=color.new(color.yellow, 50), offset=-lookback)

// == PLOT SUPPORT/RESISTANCE LINES ON THE HIGHER TIMEFRAMES ==
// Use a function to define the lines
f_line(x1, y1, y2, _color) =>
    var line id = na
    line.delete(id)
    id := line.new(x1, y1, time, y2, xloc.bar_time, extend.right, _color, width=levelWidth)
    id

// 1st Timeframe
highLine1 = TF1Check ? na : showMTFLevels ? f_line(tSH_01, hL_01, hL_01, oneColorR) : na
lowLine1 = TF1Check ? na : showMTFLevels ? f_line(tSL_01, lL_01, lL_01, oneColorS) : na
// 2nd Timeframe
highLine2 = TF2Check ? na : showMTFLevels ? f_line(tSH_02, hL_02, hL_02, twoColorR) : na
lowLine2 = TF2Check ? na : showMTFLevels ? f_line(tSL_02, lL_02, lL_02, twoColorS) : na
// 3rd Timeframe
highLine3 = TF3Check ? na : showMTFLevels ? f_line(tSH_03, hL_03, hL_03, threeColorR) : na
lowLine3 = TF3Check ? na : showMTFLevels ? f_line(tSL_03, lL_03, lL_03, threeColorS) : na
// 4th Timeframe
highLine4 = TF3Check ? na : showMTFLevels ? f_line(tSH_04, hL_04, hL_04, fourColorR) : na
lowLine4 = TF3Check ? na : showMTFLevels ? f_line(tSL_04, lL_04, lL_04, fourColorS) : na

// == TREND CALCULATIONS (USING A TUPLE TO CONSOLIDATE REPETATIVE CODE AND GENERATE MULTIPE VARIABLES WITH ONE FUNCTION ==
f_signal(highLevel, lowLevel) =>
    uptrendSignal = high > highLevel
    downtrendSignal = low < lowLevel
    inUptrend = bool(na)
    inDowntrend = bool(na)
    inUptrend := uptrendSignal[1] ? true : downtrendSignal[1] ? false : inUptrend[1]
    inDowntrend := not inUptrend
    [uptrendSignal, downtrendSignal, inUptrend, inDowntrend]

[uptrendSignal1, downtrendSignal1, inUptrend1, inDowntrend1] = f_signal(hL_01, lL_01)  // 1st Timeframe
[uptrendSignal2, downtrendSignal2, inUptrend2, inDowntrend2] = f_signal(hL_02, lL_02)  // 2nd Timeframe
[uptrendSignal3, downtrendSignal3, inUptrend3, inDowntrend3] = f_signal(hL_03, lL_03)  // 3rd Timeframe
[uptrendSignal4, downtrendSignal4, inUptrend4, inDowntrend4] = f_signal(hL_04, lL_04)  // 3rd Timeframe

// == TREND TABLE PLOTTING ==
tablePos = tableLocation == 'Top' ? position.top_right : position.bottom_right
var table trendTable = table.new(tablePos, 4, 1, border_width=3)
upColor = color.rgb(38, 166, 154)
downColor = color.rgb(240, 83, 80)
tfColor = color.new(#999999, 0)

f_fillCell(_column, _row, _cellText, _c_color) =>
    table.cell(trendTable, _column, _row, _cellText, bgcolor=color.new(_c_color, 70), text_color=_c_color, width=6)
 
// Define glyphs
glyph1 = TF1Check ? na : inUptrend1 ? '▲ ': '▼ '
glyph2 = TF2Check ? na : inUptrend2 ? '▲ ': '▼ '
glyph3 = TF3Check ? na : inUptrend3 ? '▲ ': '▼ '
glyph4 = TF4Check ? na : inUptrend3 ? '▲ ': '▼ '

 
if barstate.islast
    f_fillCell(0, 0, glyph1 + oneSet, TF1Check ? tfColor : inUptrend1 ? upColor : downColor)
    f_fillCell(1, 0, glyph2 + twoSet, TF2Check ? tfColor : inUptrend2 ? upColor : downColor)
    f_fillCell(2, 0, glyph3 + threeSet, TF3Check ? tfColor : inUptrend3 ? upColor : downColor)
    f_fillCell(3, 0, glyph4 + fourSet, TF4Check ? tfColor : inUptrend4 ? upColor : downColor)
 

// CREATE PRICE AND TIMEFRAME LABEL
// THIS DOES NOT SEEM TO BE WORKING!!! NEED TO FIGURE OUT WHY.
// lbl_txt1H = str.tostring(hL_01) + "(" + str.tostring(oneSet) + ")"
// var label label1H = na
// label1H := TF1Check ? na : showMTFLevels ? label.new(bar_index, close, text=lbl_txt1H, yloc=yloc.price, style=label.style_none, textcolor=color.new(color.orange, 50)) : na
// label.set_y(id=label1H, y=hL_01)
// label.delete(label1H)
// lbl_txt1L = str.tostring(lL_01) + "(" + str.tostring(oneSet) + ")"
// var label label1L = na
// label1L := TF1Check ? na : showMTFLevels ? label.new(bar_index, close, text=lbl_txt1L, yloc=yloc.price, style=label.style_none, textcolor=color.new(color.orange, 50)) : na
// label.set_y(id=label1L, y=lL_01)
// label.delete(label1L)

// lbl_txt2H = str.tostring(hL_02) + "(" + str.tostring(twoSet) + ")"
// var label label2H = na
// label2H := TF2Check ? na : showMTFLevels ? label.new(bar_index, close, text=lbl_txt2H, yloc=yloc.price, style=label.style_none, textcolor=color.new(color.orange, 50)) : na
// label.set_y(id=label2H, y=hL_02)
// label.delete(label2H)
// lbl_txt2L = str.tostring(lL_02) + "(" + str.tostring(twoSet) + ")"
// var label label2L = na
// label2L := TF2Check ? na : showMTFLevels ? label.new(bar_index, close, text=lbl_txt2L, yloc=yloc.price, style=label.style_none, textcolor=color.new(color.orange, 50)) : na
// label.set_y(id=label2L, y=lL_02)
// label.delete(label2L)

// lbl_txt3H = str.tostring(hL_03) + "(" + str.tostring(threeSet) + ")"
// var label label3H = na
// label3H := TF3Check ? na : showMTFLevels ? label.new(bar_index, close, text=lbl_txt3H, yloc=yloc.price, style=label.style_none, textcolor=color.new(color.orange, 50)) : na
// label.set_y(id=label3H, y=hL_03)
// label.delete(label3H)
// lbl_txt3L = str.tostring(lL_03) + "(" + str.tostring(threeSet) + ")"
// var label label3L = na
// label3L := TF3Check ? na : showMTFLevels ? label.new(bar_index, close, text=lbl_txt3L, yloc=yloc.price, style=label.style_none, textcolor=color.new(color.orange, 50)) : na
// label.set_y(id=label3L, y=lL_03)
// label.delete(label3L)
check the below.

CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © millerrh
#// Revision 2 - Optimizing the code for improved speed (with some help from @RozaniGhani-RG), allowing the user to control which timeframes to look at,
#// and moving indicator to table so I can use a mutable variable to indicate which timeframe the user selected.
#// Revision 3 - More speed optimization to limit the # of security calls.  Allow for abiilty for user to reposition the table.
#// Revision 4 - Added ability to choose between close or high for pivot.
#// This indicator identifies trends in multiple higher timeframes and shows them in a widget off to the right of the chart.
#// It's meant to be used as an alternative filter for "trading with the trend." Typically people use moving averages of varying lengths for this
#// (i.e. if over 200 MA it's an uptrend, etc.), but I wanted to see if it might be more effective to see if the higher timeframes were actually
#// trending or not in a certain direction.
#// For the purposes of this indicator, an uptrend is defined as higher highs and higher lows.  So if currently in a downtrend and the highs are broken,
#// the indicator will flip to an uptrend because now we have a higher high.  Vice versa for downtrends.
#// The user can choose the lookback period for defining these highs/lows (the pivot points).  A smaller lookback number will give you more frequent pivot points.
#// The user can toggle on visibility of all historical pivot points to make sure the frequency and placement of the swing highs/lows is to their liking.
#// The user can show the support/resistance lines of those most recent swing high/low points on the multiple timeframes as well. 
#// When these lines are breached, that is when the trends change, so you can see if you are close to changing any longer term trends.
#indicator('MTF Trend Table', overlay=true)
# Converted by Sam4Cok@Samer800    - 08/2023
#-- some bug fixes
#// == USER INPUT ==
input PivotLookback = 3;# 'Looks for pivot points within this number of bars both left and right.'
input pivotType = {default "High/Low", "Close"};
input showPivotPoints = no;
input firstTimeframe = AggregationPeriod.FIVE_MIN;
input SecondTimeframe = AggregationPeriod.HOUR;
input ThirdTimeframe = AggregationPeriod.FOUR_HOURS;
input FourthTimeframe = AggregationPeriod.DAY;
input showMtfLevels = yes;    # 'Show Multiple Timeframe S/R Levels?'
def na = Double.NaN;

def srcHi;
def srcHi1;
def srcHi2;
def srcHi3;
def srcHi4;
def srcLo;
def srcLo1;
def srcLo2;
def srcLo3;
def srcLo4;
switch (pivotType) {
case "High/Low" :
    srcHi = high;
    srcLo = low;
    srcHi1 = high(Period = firstTimeframe);
    srcLo1 = low(Period  = firstTimeframe);
    srcHi2 = high(Period = SecondTimeframe);
    srcLo2 = low(Period  = SecondTimeframe);
    srcHi3 = high(Period = ThirdTimeframe);
    srcLo3 = low(Period  = ThirdTimeframe);
    srcHi4 = high(Period = FourthTimeframe);
    srcLo4 = low(Period  = FourthTimeframe);
case "Close" :
    srcHi = close;
    srcLo = close;
    srcHi1 = close(Period = firstTimeframe);
    srcLo1 = close(Period = firstTimeframe);
    srcHi2 = close(Period = SecondTimeframe);
    srcLo2 = close(Period = SecondTimeframe);
    srcHi3 = close(Period = ThirdTimeframe);
    srcLo3 = close(Period = ThirdTimeframe);
    srcHi4 = close(Period = FourthTimeframe);
    srcLo4 = close(Period = FourthTimeframe);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if (!IsNaN(_V) and _VStop) then _V else _nan;
}
#//  == DEFINE FUNCTIONS FOR USE IN MULTIPLE TIMEFRAMES (USING A TUPLE TO AVOID SO MANY SECURITY CALLS) == 
script getHTF {
    input srcHi = high;
    input srcLo = low;
    input lookback = 3;
    def na = Double.NaN;
    def hih = highest(srcHi, lookback + 1);
    def lol = lowest(srcLo, lookback + 1);
    def ph =  findpivots(srcHi,  1, lookback, lookback);
    def pl =  findpivots(srcLo, -1, lookback, lookback);
    def highLevel_= if !IsNaN(ph) then srcHi else highLevel_[1];
    def lowLevel_ = if !IsNaN(pl) then srcLo else lowLevel_[1];
    def highLevel = if highLevel_==0 then max(hih, hih[1]) else highLevel_;
    def lowLevel  = if lowLevel_==0  then min(lol, lol[1]) else lowLevel_;
    def barsSinceHigh = if highLevel!=highLevel[1] then 0 else barsSinceHigh[1] + 1;
    def barsSinceLow  = if lowLevel !=lowLevel[1]  then 0 else barsSinceLow[1] + 1;
    def time = GetTime();
    def timeSinceHigh = GetValue(time, barsSinceHigh);
    def timeSinceLow  = GetValue(time, barsSinceLow);
    plot hL = if (time < highestAll(timeSinceHigh) or highLevel==0) then na else highLevel;
    plot lL = if (time < highestAll(timeSinceLow) or lowLevel==0) then na else lowLevel;
    plot tsH = timeSinceHigh;
    plot tsL = timeSinceLow;
}
def hL_01_  = getHTF(srcHi1, srcLo1, PivotLookback).hL;
def lL_01_  = getHTF(srcHi1, srcLo1, PivotLookback).lL;
def tSH_01_ = getHTF(srcHi1, srcLo1, PivotLookback).tsH;
def tSL_01_ = getHTF(srcHi1, srcLo1, PivotLookback).tsL;
def hL_02_  = getHTF(srcHi2, srcLo2, PivotLookback).hL;
def lL_02_  = getHTF(srcHi2, srcLo2, PivotLookback).lL;
def tSH_02_ = getHTF(srcHi2, srcLo2, PivotLookback).tsH;
def tSL_02_ = getHTF(srcHi2, srcLo2, PivotLookback).tsL;
def hL_03_  = getHTF(srcHi3, srcLo3, PivotLookback).hL;
def lL_03_  = getHTF(srcHi3, srcLo3, PivotLookback).lL;
def tSH_03_ = getHTF(srcHi3, srcLo3, PivotLookback).tsH;
def tSL_03_ = getHTF(srcHi3, srcLo3, PivotLookback).tsL;
def hL_04_  = getHTF(srcHi4, srcLo4, PivotLookback).hL;
def lL_04_  = getHTF(srcHi4, srcLo4, PivotLookback).lL;
def tSH_04_ = getHTF(srcHi4, srcLo4, PivotLookback).tsH;
def tSL_04_ = getHTF(srcHi4, srcLo4, PivotLookback).tsL;
#---
def hL_01  = hL_01_;
def lL_01  = lL_01_;
def tSH_01 = tSH_01_;
def tSL_01 = tSL_01_;

def hL_02  = hL_02_;
def lL_02  = lL_02_;
def tSH_02 = tSH_02_;
def tSL_02 = tSL_02_;

def hL_03  = hL_03_;
def lL_03  = lL_03_;
def tSH_03 = tSH_03_;
def tSL_03 = tSL_03_;

def hL_04  = hL_04_;
def lL_04  = lL_04_;
def tSH_04 = tSH_04_;
def tSL_04 = tSL_04_;
#---
def chartTFInMinutes = GetAggregationPeriod();
def time = GetTime();
def TF1Check = firstTimeframe < chartTFInMinutes;
def TF2Check = SecondTimeframe < chartTFInMinutes;
def TF3Check = ThirdTimeframe < chartTFInMinutes;
def TF4Check = FourthTimeframe < chartTFInMinutes;

def phC =  findpivots(srcHi,  1, PivotLookback, PivotLookback);
def plC =  findpivots(srcLo, -1, PivotLookback, PivotLookback);

#// Plot historical pivot points for debugging and configuring the lookback period.
plot ppH = if showPivotPoints and phC then phC else na;
plot ppL = if showPivotPoints and plC then plC else na;
ppH.SetDefaultColor(Color.YELLOW);
ppL.SetDefaultColor(Color.YELLOW);
ppH.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
ppL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);

#// 1st Timeframe
plot highLine1 = if TF1Check then na else
                 if showMTFLevels then hL_01 else na;
plot lowLine1  = if TF1Check then na else
                 if showMTFLevels then lL_01 else na;
#// 2nd Timeframe
plot highLine2 = if TF2Check then na else
                 if showMTFLevels then hL_02 else na;
plot lowLine2  = if TF2Check then na else
                 if showMTFLevels then lL_02 else na;
#// 3rd Timeframe
plot highLine3 = if TF3Check then na else
                 if showMTFLevels then hL_03 else na;
plot lowLine3  = if TF3Check then na else
                 if showMTFLevels then lL_03 else na;
#// 4th Timeframe
plot highLine4 = if TF3Check then na else
                 if showMTFLevels then hL_04 else na;
plot lowLine4  = if TF3Check then na else
                 if showMTFLevels then lL_04 else na;

highLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

highLine1.SetDefaultColor(Color.MAGENTA);
highLine2.SetDefaultColor(Color.CYAN);
highLine3.SetDefaultColor(Color.WHITE);
highLine4.SetDefaultColor(Color.RED);
lowLine1.SetDefaultColor(Color.MAGENTA);
lowLine2.SetDefaultColor(Color.CYAN);
lowLine3.SetDefaultColor(Color.WHITE);
lowLine4.SetDefaultColor(Color.RED);

#// == TREND CALCULATIONS (USING A TUPLE TO CONSOLIDATE REPETATIVE CODE AND GENERATE MULTIPE VARIABLES WITH ONE FUNCTION ==
Script tfInMinutes {
input agg = AggregationPeriod.DAY;
    def tf     = agg;
    def min_   = 60 * 1000;
    def hour_  =  min_ * 60;
    def day_   = hour_ * 24;
    def week_  = day_ * 7;
    def month_ = week_ * 4;

    def chartTf; def tfLabel;
    if tf < hour_ {
    chartTf = tf / min_;
    tfLabel = 1;
    } else
    if tf < day_ {
    chartTf = tf / hour_;
    tfLabel = 2;
    } else
    if tf < week_ {
    chartTf = tf / day_;
    tfLabel = 3;
    } else
    if tf < month_ {
    chartTf = tf / week_;
    tfLabel = 4;
    } else
    if tf {
    chartTf = tf / month_;
    tfLabel = 5;
    } else {
    chartTf = chartTf[1];
    tfLabel = tfLabel[1];
    }
    plot ctf = chartTf;
    plot ltf = tfLabel;
}
Script f_signal {
input highLevel = high;
input lowLevel  = low;
    def uptrendSignal   = high > highLevel;
    def downtrendSignal = low < lowLevel;
    def inUptrend = if uptrendSignal[1] then yes else
                    if downtrendSignal[1] then no else inUptrend[1];
    plot up = if isNaN(inUptrend) then 0 else inUptrend;
}
def inUptrend1   = f_signal(hL_01, lL_01);#  // 1st Timeframe
def inUptrend2   = f_signal(hL_02, lL_02);#  // 2nd Timeframe
def inUptrend3   = f_signal(hL_03, lL_03);#  // 3rd Timeframe
def inUptrend4   = f_signal(hL_04, lL_04);#  // 4th Timeframe

#// Define glyphs
def glyph1 = inUptrend1;
def glyph2 = inUptrend2;
def glyph3 = inUptrend3;
def glyph4 = inUptrend4;

def tf1 = tfInMinutes(firstTimeframe).ctf;
def tf2 = tfInMinutes(SecondTimeframe).ctf;
def tf3 = tfInMinutes(ThirdTimeframe).ctf;
def tf4 = tfInMinutes(FourthTimeframe).ctf;

def ltf1 = tfInMinutes(firstTimeframe).ltf;
def ltf2 = tfInMinutes(SecondTimeframe).ltf;
def ltf3 = tfInMinutes(ThirdTimeframe).ltf;
def ltf4 = tfInMinutes(FourthTimeframe).ltf;


AddLabel(!TF1Check , tf1 + if ltf1==1 then " Min" else
                           if ltf1==2 then " H" else
                           if ltf1==3 then " D" else
                           if ltf1==4 then " W" else " M", if glyph1 then color.GREEN else Color.RED);
AddLabel(!TF2Check , tf2 + if ltf2==1 then " Min" else
                           if ltf2==2 then " H" else
                           if ltf2==3 then " D" else
                           if ltf2==4 then " W" else " M", if glyph2 then color.GREEN else Color.RED);
AddLabel(!TF3Check , tf3 + if ltf3==1 then " Min" else
                           if ltf3==2 then " H" else
                           if ltf3==3 then " D" else
                           if ltf3==4 then " W" else " M", if glyph3 then color.GREEN else Color.RED);
AddLabel(!TF4Check , tf4 + if ltf4==1 then " Min" else
                           if ltf4==2 then " H" else
                           if ltf4==3 then " D" else
                           if ltf4==4 then " W" else " M", if glyph4 then color.GREEN else Color.RED);

#-- END of CODE
 
Last edited:
check the below.

CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © millerrh
#// Revision 2 - Optimizing the code for improved speed (with some help from @RozaniGhani-RG), allowing the user to control which timeframes to look at,
#// and moving indicator to table so I can use a mutable variable to indicate which timeframe the user selected.
#// Revision 3 - More speed optimization to limit the # of security calls.  Allow for abiilty for user to reposition the table.
#// Revision 4 - Added ability to choose between close or high for pivot.
#// This indicator identifies trends in multiple higher timeframes and shows them in a widget off to the right of the chart.
#// It's meant to be used as an alternative filter for "trading with the trend." Typically people use moving averages of varying lengths for this
#// (i.e. if over 200 MA it's an uptrend, etc.), but I wanted to see if it might be more effective to see if the higher timeframes were actually
#// trending or not in a certain direction.
#// For the purposes of this indicator, an uptrend is defined as higher highs and higher lows.  So if currently in a downtrend and the highs are broken,
#// the indicator will flip to an uptrend because now we have a higher high.  Vice versa for downtrends.
#// The user can choose the lookback period for defining these highs/lows (the pivot points).  A smaller lookback number will give you more frequent pivot points.
#// The user can toggle on visibility of all historical pivot points to make sure the frequency and placement of the swing highs/lows is to their liking.
#// The user can show the support/resistance lines of those most recent swing high/low points on the multiple timeframes as well.
#// When these lines are breached, that is when the trends change, so you can see if you are close to changing any longer term trends.
#indicator('MTF Trend Table', overlay=true)
# Converted by Sam4Cok@Samer800    - 08/2023
#// == USER INPUT ==
input PivotLookback = 3;# 'Looks for pivot points within this number of bars both left and right.'
input pivotType = {default "High/Low", "Close"};
input showPivotPoints = no;
input firstTimeframe = AggregationPeriod.FIVE_MIN;
input SecondTimeframe = AggregationPeriod.HOUR;
input ThirdTimeframe = AggregationPeriod.FOUR_HOURS;
input FourthTimeframe = AggregationPeriod.DAY;
input showMtfLevels = yes;    # 'Show Multiple Timeframe S/R Levels?'
def na = Double.NaN;

def srcHi;
def srcHi1;
def srcHi2;
def srcHi3;
def srcHi4;
def srcLo;
def srcLo1;
def srcLo2;
def srcLo3;
def srcLo4;
switch (pivotType) {
case "High/Low" :
    srcHi = high;
    srcLo = low;
    srcHi1 = high(Period = firstTimeframe);
    srcLo1 = low(Period  = firstTimeframe);
    srcHi2 = high(Period = SecondTimeframe);
    srcLo2 = low(Period  = SecondTimeframe);
    srcHi3 = high(Period = ThirdTimeframe);
    srcLo3 = low(Period  = ThirdTimeframe);
    srcHi4 = high(Period = FourthTimeframe);
    srcLo4 = low(Period  = FourthTimeframe);
case "Close" :
    srcHi = close;
    srcLo = close;
    srcHi1 = close(Period = firstTimeframe);
    srcLo1 = close(Period = firstTimeframe);
    srcHi2 = close(Period = SecondTimeframe);
    srcLo2 = close(Period = SecondTimeframe);
    srcHi3 = close(Period = ThirdTimeframe);
    srcLo3 = close(Period = ThirdTimeframe);
    srcHi4 = close(Period = FourthTimeframe);
    srcLo4 = close(Period = FourthTimeframe);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    def _pivotRange;
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _pivotRange = lbL + lbL;
    _VStop = if !IsNaN(dat[_pivotRange]) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                    if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if !IsNaN(_V) and _VStop then _V else _nan;
}
#//  == DEFINE FUNCTIONS FOR USE IN MULTIPLE TIMEFRAMES (USING A TUPLE TO AVOID SO MANY SECURITY CALLS) ==
script getHTF {
    input srcHi = high;
    input srcLo = low;
    input lookback = 3;
    def na = Double.NaN;
    def ph =  findpivots(srcHi,  1, lookback, lookback);
    def pl =  findpivots(srcLo, -1, lookback, lookback);
    def highLevel = if !IsNaN(ph) then srcHi else highLevel[1];
    def lowLevel  = if !IsNaN(pl) then srcLo else lowLevel[1];
    def barsSinceHigh = if highLevel!=highLevel[1] then 0 else barsSinceHigh[1] + 1;
    def barsSinceLow  = if lowLevel !=lowLevel[1]  then 0 else barsSinceLow[1] + 1;
    def time = GetTime();
    def timeSinceHigh = GetValue(time, barsSinceHigh);
    def timeSinceLow  = GetValue(time, barsSinceLow);
    plot hL = if highLevel==0 then na else highLevel;
    plot lL = if highLevel==0 then na else lowLevel;
    plot tsH = timeSinceHigh;
    plot tsL = timeSinceLow;
}

def hL_01 = getHTF(srcHi1, srcLo1, PivotLookback).hL;
def lL_01 = getHTF(srcHi1, srcLo1, PivotLookback).lL;
def tSH_01 = getHTF(srcHi1, srcLo1, PivotLookback).tsH;
def tSL_01 = getHTF(srcHi1, srcLo1, PivotLookback).tsL;

def hL_02 = getHTF(srcHi2, srcLo2, PivotLookback).hL;
def lL_02 = getHTF(srcHi2, srcLo2, PivotLookback).lL;
def tSH_02 = getHTF(srcHi2, srcLo2, PivotLookback).tsH;
def tSL_02 = getHTF(srcHi2, srcLo2, PivotLookback).tsL;

def hL_03 = getHTF(srcHi3, srcLo3, PivotLookback).hL;
def lL_03 = getHTF(srcHi3, srcLo3, PivotLookback).lL;
def tSH_03 = getHTF(srcHi3, srcLo3, PivotLookback).tsH;
def tSL_03 = getHTF(srcHi3, srcLo3, PivotLookback).tsL;

def hL_04 = getHTF(srcHi4, srcLo4, PivotLookback).hL;
def lL_04 = getHTF(srcHi4, srcLo4, PivotLookback).lL;
def tSH_04 = getHTF(srcHi4, srcLo4, PivotLookback).tsH;
def tSL_04 = getHTF(srcHi4, srcLo4, PivotLookback).tsL;

def chartTFInMinutes = GetAggregationPeriod();
def time = GetTime();
def TF1Check = firstTimeframe < chartTFInMinutes;
def TF2Check = SecondTimeframe < chartTFInMinutes;
def TF3Check = ThirdTimeframe < chartTFInMinutes;
def TF4Check = FourthTimeframe < chartTFInMinutes;

def phC =  findpivots(srcHi,  1, PivotLookback, PivotLookback);
def plC =  findpivots(srcLo, -1, PivotLookback, PivotLookback);

#// Plot historical pivot points for debugging and configuring the lookback period.
plot ppH = if showPivotPoints then phC else na;
plot ppL = if showPivotPoints then plC else na;
ppH.SetDefaultColor(Color.YELLOW);
ppL.SetDefaultColor(Color.YELLOW);
ppH.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
ppL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);

#// 1st Timeframe
plot highLine1 = if TF1Check then na else
                 if showMTFLevels and time >= highestAll(tSH_01) then hL_01 else na;
plot lowLine1  = if TF1Check then na else
                 if showMTFLevels and time >= highestAll(tSL_01) then lL_01 else na;
#// 2nd Timeframe
plot highLine2 = if TF2Check then na else
                 if showMTFLevels and time >= highestAll(tSH_02) then hL_02 else na;
plot lowLine2  = if TF2Check then na else
                 if showMTFLevels and time >= highestAll(tSL_02) then lL_02 else na;
#// 3rd Timeframe
plot highLine3 = if TF3Check then na else
                 if showMTFLevels and time >= highestAll(tSH_03) then hL_03 else na;
plot lowLine3  = if TF3Check then na else
                 if showMTFLevels and time >= highestAll(tSL_03) then lL_03 else na;
#// 4th Timeframe
plot highLine4 = if TF3Check then na else
                 if showMTFLevels and time >= highestAll(tSH_04) then hL_04 else na;
plot lowLine4  = if TF3Check then na else
                 if showMTFLevels and time >= highestAll(tSL_04) then lL_04 else na;

highLine1.SetDefaultColor(Color.MAGENTA);
highLine2.SetDefaultColor(Color.CYAN);
highLine3.SetDefaultColor(Color.WHITE);
highLine4.SetDefaultColor(Color.RED);

lowLine1.SetDefaultColor(Color.MAGENTA);
lowLine2.SetDefaultColor(Color.CYAN);
lowLine3.SetDefaultColor(Color.WHITE);
lowLine4.SetDefaultColor(Color.RED);

#// == TREND CALCULATIONS (USING A TUPLE TO CONSOLIDATE REPETATIVE CODE AND GENERATE MULTIPE VARIABLES WITH ONE FUNCTION ==
Script tfInMinutes {
input tf = AggregationPeriod.DAY;
    def min = AggregationPeriod.MIN;
    def day = AggregationPeriod.DAY;
    def week = AggregationPeriod.WEEK;
    def month = AggregationPeriod.MONTH;
    def chartTf; def tfLabel;
    if tf < day {
    chartTf = tf / min;
    tfLabel = 1;
    } else
    if tf < week {
    chartTf = tf / day;
    tfLabel = 2;
    } else
    if tf < month {
    chartTf = tf / week;
    tfLabel = 3;
    } else {
    chartTf = tf / month;
    tfLabel = 4;
    }
    plot ctf = chartTf;
    plot ltf = tfLabel;
}
Script f_signal {
input highLevel = high;
input lowLevel  = low;
    def uptrendSignal = high > highLevel;
    def downtrendSignal = low < lowLevel;
    def inUptrend = if uptrendSignal[1] then yes else
                    if downtrendSignal[1] then no else inUptrend[1];
    plot up = inUptrend;
}
def inUptrend1   = f_signal(hL_01, lL_01);#  // 1st Timeframe
def inUptrend2   = f_signal(hL_02, lL_02);#  // 2nd Timeframe
def inUptrend3   = f_signal(hL_03, lL_03);#  // 3rd Timeframe
def inUptrend4   = f_signal(hL_04, lL_04);#  // 4th Timeframe

#// Define glyphs
def glyph1 = if TF1Check then na else inUptrend1;
def glyph2 = if TF2Check then na else inUptrend2;
def glyph3 = if TF3Check then na else inUptrend3;
def glyph4 = if TF4Check then na else inUptrend3;

def tf1 = tfInMinutes(firstTimeframe).ctf;
def tf2 = tfInMinutes(SecondTimeframe).ctf;
def tf3 = tfInMinutes(ThirdTimeframe).ctf;
def tf4 = tfInMinutes(FourthTimeframe).ctf;

def ltf1 = tfInMinutes(firstTimeframe).ltf;
def ltf2 = tfInMinutes(SecondTimeframe).ltf;
def ltf3 = tfInMinutes(ThirdTimeframe).ltf;
def ltf4 = tfInMinutes(FourthTimeframe).ltf;


AddLabel(1 , tf1 + if ltf1==1 then " Min" else
                   if ltf1==2 then " D" else
                   if ltf1==3 then " W" else " M", if glyph1 then color.GREEN else Color.RED);
AddLabel(1 , tf2 + if ltf2==1 then " Min" else
                   if ltf2==2 then " D" else
                   if ltf2==3 then " W" else " M", if glyph2 then color.GREEN else Color.RED);
AddLabel(1 , tf3 + if ltf3==1 then " Min" else
                   if ltf3==2 then " D" else
                   if ltf3==3 then " W" else " M", if glyph3 then color.GREEN else Color.RED);
AddLabel(1 , tf4 + if ltf4==1 then " Min" else
                   if ltf4==2 then " D" else
                   if ltf4==3 then " W" else " M", if glyph4 then color.GREEN else Color.RED);

#-- END of CODE
Thank you this is perfect. Exactly what I wanted. Works great, however I noticed when I choose a chart with a 1 hour time the only MTF that display is the 240 min. I have set the MTF settings to 2h, 4h, D, Wk. With the chart on the 1 hr or above. The 240 MTF is the only one the will display.
If I choose a chart less than 1hr the MTF works great for all settings.
Is there a way to make the MTF timeframes display regardless of the chart being displayed?
 
Thank you this is perfect. Exactly what I wanted. Works great, however I noticed when I choose a chart with a 1 hour time the only MTF that display is the 240 min. I have set the MTF settings to 2h, 4h, D, Wk. With the chart on the 1 hr or above. The 240 MTF is the only one the will display.
If I choose a chart less than 1hr the MTF works great for all settings.
Is there a way to make the MTF timeframes display regardless of the chart being displayed?

Under the Fourth Timeframe "TF3Check" I believe should be TF4Check.
Under the Define glyphs there are 2 "inUptrend3". I changed 1 of then to inUptrend4
 
Last edited:
Under the Fourth Timeframe "TF3Check" I believe should be TF4Check.
Under the Define glyphs there are 2 "inUptrend3". I changed 1 of then to inUptrend4
see if this works.

CSS:
#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © millerrh
#// Revision 2 - Optimizing the code for improved speed (with some help from @RozaniGhani-RG), allowing the user to control which timeframes to look at,
#// and moving indicator to table so I can use a mutable variable to indicate which timeframe the user selected.
#// Revision 3 - More speed optimization to limit the # of security calls.  Allow for abiilty for user to reposition the table.
#// Revision 4 - Added ability to choose between close or high for pivot.
#// This indicator identifies trends in multiple higher timeframes and shows them in a widget off to the right of the chart.
#// It's meant to be used as an alternative filter for "trading with the trend." Typically people use moving averages of varying lengths for this
#// (i.e. if over 200 MA it's an uptrend, etc.), but I wanted to see if it might be more effective to see if the higher timeframes were actually
#// trending or not in a certain direction.
#// For the purposes of this indicator, an uptrend is defined as higher highs and higher lows.  So if currently in a downtrend and the highs are broken,
#// the indicator will flip to an uptrend because now we have a higher high.  Vice versa for downtrends.
#// The user can choose the lookback period for defining these highs/lows (the pivot points).  A smaller lookback number will give you more frequent pivot points.
#// The user can toggle on visibility of all historical pivot points to make sure the frequency and placement of the swing highs/lows is to their liking.
#// The user can show the support/resistance lines of those most recent swing high/low points on the multiple timeframes as well. 
#// When these lines are breached, that is when the trends change, so you can see if you are close to changing any longer term trends.
#indicator('MTF Trend Table', overlay=true)
# Converted by Sam4Cok@Samer800    - 08/2023
#-- some bug fixes
#// == USER INPUT ==
input PivotLookback = 3;# 'Looks for pivot points within this number of bars both left and right.'
input pivotType = {default "High/Low", "Close"};
input showPivotPoints = no;
input firstTimeframe = AggregationPeriod.FIVE_MIN;
input SecondTimeframe = AggregationPeriod.HOUR;
input ThirdTimeframe = AggregationPeriod.FOUR_HOURS;
input FourthTimeframe = AggregationPeriod.DAY;
input showMtfLevels = yes;    # 'Show Multiple Timeframe S/R Levels?'
def na = Double.NaN;

def srcHi;
def srcHi1;
def srcHi2;
def srcHi3;
def srcHi4;
def srcLo;
def srcLo1;
def srcLo2;
def srcLo3;
def srcLo4;
switch (pivotType) {
case "High/Low" :
    srcHi = high;
    srcLo = low;
    srcHi1 = high(Period = firstTimeframe);
    srcLo1 = low(Period  = firstTimeframe);
    srcHi2 = high(Period = SecondTimeframe);
    srcLo2 = low(Period  = SecondTimeframe);
    srcHi3 = high(Period = ThirdTimeframe);
    srcLo3 = low(Period  = ThirdTimeframe);
    srcHi4 = high(Period = FourthTimeframe);
    srcLo4 = low(Period  = FourthTimeframe);
case "Close" :
    srcHi = close;
    srcLo = close;
    srcHi1 = close(Period = firstTimeframe);
    srcLo1 = close(Period = firstTimeframe);
    srcHi2 = close(Period = SecondTimeframe);
    srcLo2 = close(Period = SecondTimeframe);
    srcHi3 = close(Period = ThirdTimeframe);
    srcLo3 = close(Period = ThirdTimeframe);
    srcHi4 = close(Period = FourthTimeframe);
    srcLo4 = close(Period = FourthTimeframe);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if (!IsNaN(_V) and _VStop) then _V else _nan;
}
#//  == DEFINE FUNCTIONS FOR USE IN MULTIPLE TIMEFRAMES (USING A TUPLE TO AVOID SO MANY SECURITY CALLS) == 
script getHTF {
    input srcHi = high;
    input srcLo = low;
    input lookback = 3;
    def na = Double.NaN;
    def hih = highest(srcHi, lookback + 1);
    def lol = lowest(srcLo, lookback + 1);
    def ph =  findpivots(srcHi,  1, lookback, lookback);
    def pl =  findpivots(srcLo, -1, lookback, lookback);
    def highLevel_= if !IsNaN(ph) then srcHi else highLevel_[1];
    def lowLevel_ = if !IsNaN(pl) then srcLo else lowLevel_[1];
    def highLevel = if highLevel_==0 then max(hih, hih[1]) else highLevel_;
    def lowLevel  = if lowLevel_==0  then min(lol, lol[1]) else lowLevel_;
    def barsSinceHigh = if highLevel!=highLevel[1] then 0 else barsSinceHigh[1] + 1;
    def barsSinceLow  = if lowLevel !=lowLevel[1]  then 0 else barsSinceLow[1] + 1;
    def time = GetTime();
    def timeSinceHigh = GetValue(time, barsSinceHigh);
    def timeSinceLow  = GetValue(time, barsSinceLow);
    plot hL = if (time < highestAll(timeSinceHigh) or highLevel==0) then na else highLevel;
    plot lL = if (time < highestAll(timeSinceLow) or lowLevel==0) then na else lowLevel;
    plot tsH = timeSinceHigh;
    plot tsL = timeSinceLow;
}
def hL_01_  = getHTF(srcHi1, srcLo1, PivotLookback).hL;
def lL_01_  = getHTF(srcHi1, srcLo1, PivotLookback).lL;
def tSH_01_ = getHTF(srcHi1, srcLo1, PivotLookback).tsH;
def tSL_01_ = getHTF(srcHi1, srcLo1, PivotLookback).tsL;
def hL_02_  = getHTF(srcHi2, srcLo2, PivotLookback).hL;
def lL_02_  = getHTF(srcHi2, srcLo2, PivotLookback).lL;
def tSH_02_ = getHTF(srcHi2, srcLo2, PivotLookback).tsH;
def tSL_02_ = getHTF(srcHi2, srcLo2, PivotLookback).tsL;
def hL_03_  = getHTF(srcHi3, srcLo3, PivotLookback).hL;
def lL_03_  = getHTF(srcHi3, srcLo3, PivotLookback).lL;
def tSH_03_ = getHTF(srcHi3, srcLo3, PivotLookback).tsH;
def tSL_03_ = getHTF(srcHi3, srcLo3, PivotLookback).tsL;
def hL_04_  = getHTF(srcHi4, srcLo4, PivotLookback).hL;
def lL_04_  = getHTF(srcHi4, srcLo4, PivotLookback).lL;
def tSH_04_ = getHTF(srcHi4, srcLo4, PivotLookback).tsH;
def tSL_04_ = getHTF(srcHi4, srcLo4, PivotLookback).tsL;
#---
def hL_01  = hL_01_;
def lL_01  = lL_01_;
def tSH_01 = tSH_01_;
def tSL_01 = tSL_01_;

def hL_02  = hL_02_;
def lL_02  = lL_02_;
def tSH_02 = tSH_02_;
def tSL_02 = tSL_02_;

def hL_03  = hL_03_;
def lL_03  = lL_03_;
def tSH_03 = tSH_03_;
def tSL_03 = tSL_03_;

def hL_04  = hL_04_;
def lL_04  = lL_04_;
def tSH_04 = tSH_04_;
def tSL_04 = tSL_04_;
#---
def chartTFInMinutes = GetAggregationPeriod();
def time = GetTime();
def TF1Check = firstTimeframe < chartTFInMinutes;
def TF2Check = SecondTimeframe < chartTFInMinutes;
def TF3Check = ThirdTimeframe < chartTFInMinutes;
def TF4Check = FourthTimeframe < chartTFInMinutes;

def phC =  findpivots(srcHi,  1, PivotLookback, PivotLookback);
def plC =  findpivots(srcLo, -1, PivotLookback, PivotLookback);

#// Plot historical pivot points for debugging and configuring the lookback period.
plot ppH = if showPivotPoints and phC then phC else na;
plot ppL = if showPivotPoints and plC then plC else na;
ppH.SetDefaultColor(Color.YELLOW);
ppL.SetDefaultColor(Color.YELLOW);
ppH.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
ppL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);

#// 1st Timeframe
plot highLine1 = if TF1Check then na else
                 if showMTFLevels then hL_01 else na;
plot lowLine1  = if TF1Check then na else
                 if showMTFLevels then lL_01 else na;
#// 2nd Timeframe
plot highLine2 = if TF2Check then na else
                 if showMTFLevels then hL_02 else na;
plot lowLine2  = if TF2Check then na else
                 if showMTFLevels then lL_02 else na;
#// 3rd Timeframe
plot highLine3 = if TF3Check then na else
                 if showMTFLevels then hL_03 else na;
plot lowLine3  = if TF3Check then na else
                 if showMTFLevels then lL_03 else na;
#// 4th Timeframe
plot highLine4 = if TF3Check then na else
                 if showMTFLevels then hL_04 else na;
plot lowLine4  = if TF3Check then na else
                 if showMTFLevels then lL_04 else na;

highLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

highLine1.SetDefaultColor(Color.MAGENTA);
highLine2.SetDefaultColor(Color.CYAN);
highLine3.SetDefaultColor(Color.WHITE);
highLine4.SetDefaultColor(Color.RED);
lowLine1.SetDefaultColor(Color.MAGENTA);
lowLine2.SetDefaultColor(Color.CYAN);
lowLine3.SetDefaultColor(Color.WHITE);
lowLine4.SetDefaultColor(Color.RED);

#// == TREND CALCULATIONS (USING A TUPLE TO CONSOLIDATE REPETATIVE CODE AND GENERATE MULTIPE VARIABLES WITH ONE FUNCTION ==
Script tfInMinutes {
input agg = AggregationPeriod.DAY;
    def tf     = agg;
    def min_   = 60 * 1000;
    def hour_  =  min_ * 60;
    def day_   = hour_ * 24;
    def week_  = day_ * 7;
    def month_ = week_ * 4;

    def chartTf; def tfLabel;
    if tf < hour_ {
    chartTf = tf / min_;
    tfLabel = 1;
    } else
    if tf < day_ {
    chartTf = tf / hour_;
    tfLabel = 2;
    } else
    if tf < week_ {
    chartTf = tf / day_;
    tfLabel = 3;
    } else
    if tf < month_ {
    chartTf = tf / week_;
    tfLabel = 4;
    } else
    if tf {
    chartTf = tf / month_;
    tfLabel = 5;
    } else {
    chartTf = chartTf[1];
    tfLabel = tfLabel[1];
    }
    plot ctf = chartTf;
    plot ltf = tfLabel;
}
Script f_signal {
input highLevel = high;
input lowLevel  = low;
    def uptrendSignal   = high > highLevel;
    def downtrendSignal = low < lowLevel;
    def inUptrend = if uptrendSignal[1] then yes else
                    if downtrendSignal[1] then no else inUptrend[1];
    plot up = if isNaN(inUptrend) then 0 else inUptrend;
}
def inUptrend1   = f_signal(hL_01, lL_01);#  // 1st Timeframe
def inUptrend2   = f_signal(hL_02, lL_02);#  // 2nd Timeframe
def inUptrend3   = f_signal(hL_03, lL_03);#  // 3rd Timeframe
def inUptrend4   = f_signal(hL_04, lL_04);#  // 4th Timeframe

#// Define glyphs
def glyph1 = inUptrend1;
def glyph2 = inUptrend2;
def glyph3 = inUptrend3;
def glyph4 = inUptrend4;

def tf1 = tfInMinutes(firstTimeframe).ctf;
def tf2 = tfInMinutes(SecondTimeframe).ctf;
def tf3 = tfInMinutes(ThirdTimeframe).ctf;
def tf4 = tfInMinutes(FourthTimeframe).ctf;

def ltf1 = tfInMinutes(firstTimeframe).ltf;
def ltf2 = tfInMinutes(SecondTimeframe).ltf;
def ltf3 = tfInMinutes(ThirdTimeframe).ltf;
def ltf4 = tfInMinutes(FourthTimeframe).ltf;


AddLabel(!TF1Check , tf1 + if ltf1==1 then " Min" else
                           if ltf1==2 then " H" else
                           if ltf1==3 then " D" else
                           if ltf1==4 then " W" else " M", if glyph1 then color.GREEN else Color.RED);
AddLabel(!TF2Check , tf2 + if ltf2==1 then " Min" else
                           if ltf2==2 then " H" else
                           if ltf2==3 then " D" else
                           if ltf2==4 then " W" else " M", if glyph2 then color.GREEN else Color.RED);
AddLabel(!TF3Check , tf3 + if ltf3==1 then " Min" else
                           if ltf3==2 then " H" else
                           if ltf3==3 then " D" else
                           if ltf3==4 then " W" else " M", if glyph3 then color.GREEN else Color.RED);
AddLabel(!TF4Check , tf4 + if ltf4==1 then " Min" else
                           if ltf4==2 then " H" else
                           if ltf4==3 then " D" else
                           if ltf4==4 then " W" else " M", if glyph4 then color.GREEN else Color.RED);

#-- END of CODE
 
Can anyone clean up this indicator as it causes way too much lag. It is an amazing indicator. I hope it can be cleaned up. @Ramisegal , Not sure if you would be interested in helping. I have noticed that your indicators run pretty smooth and maybe you would have some insight.
Code:
#/ This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © millerrh
#// Revision 2 - Optimizing the code for improved speed (with some help from @RozaniGhani-RG), allowing the user to control which timeframes to look at,
#// and moving indicator to table so I can use a mutable variable to indicate which timeframe the user selected.
#// Revision 3 - More speed optimization to limit the # of security calls.  Allow for abiilty for user to reposition the table.
#// Revision 4 - Added ability to choose between close or high for pivot.
#// This indicator identifies trends in multiple higher timeframes and shows them in a widget off to the right of the chart.
#// It's meant to be used as an alternative filter for "trading with the trend." Typically people use moving averages of varying lengths for this
#// (i.e. if over 200 MA it's an uptrend, etc.), but I wanted to see if it might be more effective to see if the higher timeframes were actually
#// trending or not in a certain direction.
#// For the purposes of this indicator, an uptrend is defined as higher highs and higher lows.  So if currently in a downtrend and the highs are broken,
#// the indicator will flip to an uptrend because now we have a higher high.  Vice versa for downtrends.
#// The user can choose the lookback period for defining these highs/lows (the pivot points).  A smaller lookback number will give you more frequent pivot points.
#// The user can toggle on visibility of all historical pivot points to make sure the frequency and placement of the swing highs/lows is to their liking.
#// The user can show the support/resistance lines of those most recent swing high/low points on the multiple timeframes as well.
#// When these lines are breached, that is when the trends change, so you can see if you are close to changing any longer term trends.
#indicator('MTF Trend Table', overlay=true)
# Converted by Sam4Cok@Samer800    - 08/2023
#-- some bug fixes
#// == USER INPUT ==
input PivotLookback = 11;# 'Looks for pivot points within this number of bars both left and right.'
input pivotType = {default "High/Low", "Close"};
input showPivotPoints = no;
input firstTimeframe = AggregationPeriod.FIVE_MIN;
input SecondTimeframe = AggregationPeriod.FiFTEEN_MIN;
input ThirdTimeframe = AggregationPeriod.THIRTY_MIN;
input FourthTimeframe = AggregationPeriod.HOUR;
input showMtfLevels = yes;    # 'Show Multiple Timeframe S/R Levels?'
def na = Double.NaN;

def srcHi;
def srcHi1;
def srcHi2;
def srcHi3;
def srcHi4;
def srcLo;
def srcLo1;
def srcLo2;
def srcLo3;
def srcLo4;
switch (pivotType) {
case "High/Low" :
    srcHi = high;
    srcLo = low;
    srcHi1 = high(Period = firstTimeframe);
    srcLo1 = low(Period  = firstTimeframe);
    srcHi2 = high(Period = SecondTimeframe);
    srcLo2 = low(Period  = SecondTimeframe);
    srcHi3 = high(Period = ThirdTimeframe);
    srcLo3 = low(Period  = ThirdTimeframe);
    srcHi4 = high(Period = FourthTimeframe);
    srcLo4 = low(Period  = FourthTimeframe);
case "Close" :
    srcHi = close;
    srcLo = close;
    srcHi1 = close(Period = firstTimeframe);
    srcLo1 = close(Period = firstTimeframe);
    srcHi2 = close(Period = SecondTimeframe);
    srcLo2 = close(Period = SecondTimeframe);
    srcHi3 = close(Period = ThirdTimeframe);
    srcLo3 = close(Period = ThirdTimeframe);
    srcHi4 = close(Period = FourthTimeframe);
    srcLo4 = close(Period = FourthTimeframe);
}
script FindPivots {
    input dat = close; # default data or study being evaluated
    input HL  = 0;    # default high or low pivot designation, -1 low, +1 high
    input lbL  = 5;    # default Pivot Lookback Left
    input lbR  = 1;    # default Pivot Lookback Right
    ##############
    def _nan;    # used for non-number returns
    def _BN;     # the current barnumber
    def _VStop;  # confirms that the lookforward period continues the pivot trend
    def _V;      # the Value at the actual pivot point
    ##############
    _BN  = BarNumber();
    _nan = Double.NaN;
    _VStop = if !IsNaN(dat) and lbR > 0 and lbL > 0 then
                fold a = 1 to lbR + 1 with b=1 while b do
                if HL > 0 then dat > GetValue(dat, -a) else dat < GetValue(dat, -a) else _nan;
    if (HL > 0) {
        _V = if _BN > lbL and dat == Highest(dat, lbL + 1) and _VStop
            then dat else _nan;
    } else {
        _V = if _BN > lbL and dat == Lowest(dat, lbL + 1) and _VStop
            then dat else _nan;
    }
    plot result = if (!IsNaN(_V) and _VStop) then _V else _nan;
}
#//  == DEFINE FUNCTIONS FOR USE IN MULTIPLE TIMEFRAMES (USING A TUPLE TO AVOID SO MANY SECURITY CALLS) ==
script getHTF {
    input srcHi = high;
    input srcLo = low;
    input lookback = 3;
    def na = Double.NaN;
    def hih = Highest(srcHi, lookback + 1);
    def lol = Lowest(srcLo, lookback + 1);
    def ph =  findpivots(srcHi,  1, lookback, lookback);
    def pl =  findpivots(srcLo, -1, lookback, lookback);
    def highLevel_ = if !IsNaN(ph) then srcHi else highLevel_[1];
    def lowLevel_ = if !IsNaN(pl) then srcLo else lowLevel_[1];
    def highLevel = if highLevel_ == 0 then Max(hih, hih[1]) else highLevel_;
    def lowLevel  = if lowLevel_ == 0  then Min(lol, lol[1]) else lowLevel_;
    def barsSinceHigh = if highLevel != highLevel[1] then 0 else barsSinceHigh[1] + 1;
    def barsSinceLow  = if lowLevel != lowLevel[1]  then 0 else barsSinceLow[1] + 1;
    def time = GetTime();
    def timeSinceHigh = GetValue(time, barsSinceHigh);
    def timeSinceLow  = GetValue(time, barsSinceLow);
    plot hL = if (time < HighestAll(timeSinceHigh) or highLevel == 0) then na else highLevel;
    plot lL = if (time < HighestAll(timeSinceLow) or lowLevel == 0) then na else lowLevel;
    plot tsH = timeSinceHigh;
    plot tsL = timeSinceLow;
}
def hL_01_  = getHTF(srcHi1, srcLo1, PivotLookback).hL;
def lL_01_  = getHTF(srcHi1, srcLo1, PivotLookback).lL;
def tSH_01_ = getHTF(srcHi1, srcLo1, PivotLookback).tsH;
def tSL_01_ = getHTF(srcHi1, srcLo1, PivotLookback).tsL;
def hL_02_  = getHTF(srcHi2, srcLo2, PivotLookback).hL;
def lL_02_  = getHTF(srcHi2, srcLo2, PivotLookback).lL;
def tSH_02_ = getHTF(srcHi2, srcLo2, PivotLookback).tsH;
def tSL_02_ = getHTF(srcHi2, srcLo2, PivotLookback).tsL;
def hL_03_  = getHTF(srcHi3, srcLo3, PivotLookback).hL;
def lL_03_  = getHTF(srcHi3, srcLo3, PivotLookback).lL;
def tSH_03_ = getHTF(srcHi3, srcLo3, PivotLookback).tsH;
def tSL_03_ = getHTF(srcHi3, srcLo3, PivotLookback).tsL;
def hL_04_  = getHTF(srcHi4, srcLo4, PivotLookback).hL;
def lL_04_  = getHTF(srcHi4, srcLo4, PivotLookback).lL;
def tSH_04_ = getHTF(srcHi4, srcLo4, PivotLookback).tsH;
def tSL_04_ = getHTF(srcHi4, srcLo4, PivotLookback).tsL;
#---
def hL_01  = hL_01_;
def lL_01  = lL_01_;
def tSH_01 = tSH_01_;
def tSL_01 = tSL_01_;

def hL_02  = hL_02_;
def lL_02  = lL_02_;
def tSH_02 = tSH_02_;
def tSL_02 = tSL_02_;

def hL_03  = hL_03_;
def lL_03  = lL_03_;
def tSH_03 = tSH_03_;
def tSL_03 = tSL_03_;

def hL_04  = hL_04_;
def lL_04  = lL_04_;
def tSH_04 = tSH_04_;
def tSL_04 = tSL_04_;
#---
def chartTFInMinutes = GetAggregationPeriod();
def time = GetTime();
def TF1Check = firstTimeframe < chartTFInMinutes;
def TF2Check = SecondTimeframe < chartTFInMinutes;
def TF3Check = ThirdTimeframe < chartTFInMinutes;
def TF4Check = FourthTimeframe < chartTFInMinutes;

def phC =  findpivots(srcHi,  1, PivotLookback, PivotLookback);
def plC =  findpivots(srcLo, -1, PivotLookback, PivotLookback);

#// Plot historical pivot points for debugging and configuring the lookback period.
plot ppH = if showPivotPoints and phC then phC else na;
plot ppL = if showPivotPoints and plC then plC else na;
ppH.SetDefaultColor(Color.YELLOW);
ppL.SetDefaultColor(Color.YELLOW);
ppH.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_UP);
ppL.SetPaintingStrategy(PaintingStrategy.BOOLEAN_WEDGE_DOWN);

#// 1st Timeframe
plot highLine1 = if TF1Check then na else
                 if showMtfLevels then hL_01 else na;
plot lowLine1  = if TF1Check then na else
                 if showMtfLevels then lL_01 else na;
#// 2nd Timeframe
plot highLine2 = if TF2Check then na else
                 if showMtfLevels then hL_02 else na;
plot lowLine2  = if TF2Check then na else
                 if showMtfLevels then lL_02 else na;
#// 3rd Timeframe
plot highLine3 = if TF3Check then na else
                 if showMtfLevels then hL_03 else na;
plot lowLine3  = if TF3Check then na else
                 if showMtfLevels then lL_03 else na;
#// 4th Timeframe
plot highLine4 = if TF3Check then na else
                 if showMtfLevels then hL_04 else na;
plot lowLine4  = if TF3Check then na else
                 if showMtfLevels then lL_04 else na;

highLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
highLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine1.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine2.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine3.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
lowLine4.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);

highLine1.SetDefaultColor(Color.Yellow);
highLine2.SetDefaultColor(Color.Yellow);
highLine3.SetDefaultColor(Color.Yellow);
highLine4.SetDefaultColor(Color.Yellow);
lowLine1.SetDefaultColor(Color.Yellow);
lowLine2.SetDefaultColor(Color.Yellow);
lowLine3.SetDefaultColor(Color.Yellow);
lowLine4.SetDefaultColor(Color.Yellow);

ppH.Hide();
highLine1.Hide();
highLine2.Hide();
highLine3.Hide();
highLine4.Hide();

lowLine1.Hide();
lowLine2.Hide();
lowLine3.Hide();
lowLine4.Hide();
ppL.Hide();


#// == TREND CALCULATIONS (USING A TUPLE TO CONSOLIDATE REPETATIVE CODE AND GENERATE MULTIPE VARIABLES WITH ONE FUNCTION ==
script tfInMinutes {
    input agg = AggregationPeriod.DAY;
    def tf     = agg;
    def min_   = 60 * 1000;
    def hour_  =  min_ * 60;
    def day_   = hour_ * 24;
    def week_  = day_ * 7;
    def month_ = week_ * 4;

    def chartTf;
    def tfLabel;
    if tf < hour_ {
        chartTf = tf / min_;
        tfLabel = 1;
    } else
    if tf < day_ {
        chartTf = tf / hour_;
        tfLabel = 2;
    } else
    if tf < week_ {
        chartTf = tf / day_;
        tfLabel = 3;
    } else
    if tf < month_ {
        chartTf = tf / week_;
        tfLabel = 4;
    } else
    if tf {
        chartTf = tf / month_;
        tfLabel = 5;
    } else {
        chartTf = chartTf[1];
        tfLabel = tfLabel[1];
    }
    plot ctf = chartTf;
    plot ltf = tfLabel;
}
script f_signal {
    input highLevel = high;
    input lowLevel  = low;
    def uptrendSignal   = high > highLevel;
    def downtrendSignal = low < lowLevel;
    def inUptrend = if uptrendSignal[1] then yes else
                    if downtrendSignal[1] then no else inUptrend[1];
    plot up = if IsNaN(inUptrend) then 0 else inUptrend;
}
def inUptrend1   = f_signal(hL_01, lL_01);#  // 1st Timeframe
def inUptrend2   = f_signal(hL_02, lL_02);#  // 2nd Timeframe
def inUptrend3   = f_signal(hL_03, lL_03);#  // 3rd Timeframe
def inUptrend4   = f_signal(hL_04, lL_04);#  // 4th Timeframe

#// Define glyphs
def glyph1 = inUptrend1;
def glyph2 = inUptrend2;
def glyph3 = inUptrend3;
def glyph4 = inUptrend4;

def tf1 = tfInMinutes(firstTimeframe).ctf;
def tf2 = tfInMinutes(SecondTimeframe).ctf;
def tf3 = tfInMinutes(ThirdTimeframe).ctf;
def tf4 = tfInMinutes(FourthTimeframe).ctf;

def ltf1 = tfInMinutes(firstTimeframe).ltf;
def ltf2 = tfInMinutes(SecondTimeframe).ltf;
def ltf3 = tfInMinutes(ThirdTimeframe).ltf;
def ltf4 = tfInMinutes(FourthTimeframe).ltf;


AddLabel(!TF1Check , tf1 + if ltf1 == 1 then " Min" else
                           if ltf1 == 2 then " H" else
                           if ltf1 == 3 then " D" else
                           if ltf1 == 4 then " W" else " M", if glyph1 then Color.GREEN else Color.RED);
AddLabel(!TF2Check , tf2 + if ltf2 == 1 then " Min" else
                           if ltf2 == 2 then " H" else
                           if ltf2 == 3 then " D" else
                           if ltf2 == 4 then " W" else " M", if glyph2 then Color.GREEN else Color.RED);
AddLabel(!TF3Check , tf3 + if ltf3 == 1 then " Min" else
                           if ltf3 == 2 then " H" else
                           if ltf3 == 3 then " D" else
                           if ltf3 == 4 then " W" else " M", if glyph3 then Color.GREEN else Color.RED);
AddLabel(!TF4Check , tf4 + if ltf4 == 1 then " Min" else
                           if ltf4 == 2 then " H" else
                           if ltf4 == 3 then " D" else
                           if ltf4 == 4 then " W" else " M", if glyph4 then Color.GREEN else Color.RED);
#-- END of CODE

#Just make cloud size zero to turn off clouds
input CloudSize = 0.2;
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
plot cloud1_top = highLine1;
plot cloud1_bot = highLine1 - CloudSize;

plot cloud2_top = highLine2;
plot cloud2_bot = highLine2 - CloudSize;

addcloud(cloud1_top, cloud1_bot, color.Dark_Red, color.Dark_Red, yes);
addcloud(cloud2_top, cloud2_bot, color.Dark_Red, color.Dark_Red, yes);

#cloud1_top.Hide();
#cloud1_bot.Hide();
#cloud2_top.Hide();
#cloud2_bot.Hide();
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
plot cloud3_top = highLine3;
plot cloud3_bot = highLine3 - CloudSize;

plot cloud4_top = highLine4;
plot cloud4_bot = highLine4 - CloudSize;

addcloud(cloud3_top, cloud3_bot, color.Dark_Red, color.Dark_Red, yes);
addcloud(cloud4_top, cloud4_bot, color.Dark_Red, color.Dark_Red, yes);

#cloud3_top.Hide();
#cloud3_bot.Hide();
#cloud4_top.Hide();
#cloud4_bot.Hide();
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
plot cloud5_top = lowLine1;
plot cloud5_bot = lowLine1 + CloudSize;

plot cloud6_top = lowLine2;
plot cloud6_bot = lowLine2 + CloudSize;

addcloud(cloud5_top, cloud5_bot, color.Dark_Green, color.Dark_Green, yes);
addcloud(cloud6_top, cloud6_bot, color.Dark_Green, color.Dark_Green, yes);

#cloud5_top.Hide();
#cloud5_bot.Hide();
#cloud6_top.Hide();
#cloud6_bot.Hide();
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
plot cloud7_top = lowLine3;
plot cloud7_bot = lowLine3 + CloudSize;

plot cloud8_top = lowLine4;
plot cloud8_bot = lowLine4 + CloudSize;

addcloud(cloud7_top, cloud7_bot, color.Dark_Green, color.Dark_Green, yes);
addcloud(cloud8_top, cloud8_bot, color.Dark_Green, color.Dark_Green, yes);

#cloud7_top.Hide();
#cloud7_bot.Hide();
#cloud8_top.Hide();
#cloud8_bot.Hide();

cloud1_top.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud1_top.SetDefaultColor(Color.Dark_Red);
cloud1_top.SetLineWeight(2);

cloud1_bot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud1_bot.SetDefaultColor(Color.Dark_Red);
cloud1_bot.SetLineWeight(2);

cloud2_top.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud2_top.SetDefaultColor(Color.Dark_Red);
Cloud2_top.SetLineWeight(2);

cloud2_bot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud2_bot.SetDefaultColor(Color.Dark_Red);
cloud2_bot.SetLineWeight(2);

cloud3_top.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud3_top.SetDefaultColor(Color.Dark_Red);
Cloud3_top.SetLineWeight(2);

cloud3_bot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud3_bot.SetDefaultColor(Color.Dark_Red);
cloud3_bot.SetLineWeight(2);

cloud4_top.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud4_top.SetDefaultColor(Color.Dark_Red);
Cloud4_top.SetLineWeight(2);

cloud4_bot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud4_bot.SetDefaultColor(Color.Dark_Red);
cloud4_bot.SetLineWeight(2);

cloud5_top.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud5_top.SetDefaultColor(Color.Dark_Green);
Cloud5_top.SetLineWeight(2);

cloud5_bot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud5_bot.SetDefaultColor(Color.Dark_Green);
cloud5_bot.SetLineWeight(2);

cloud6_top.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud6_top.SetDefaultColor(Color.Dark_Green);
Cloud6_top.SetLineWeight(2);

cloud6_bot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud6_bot.SetDefaultColor(Color.Dark_Green);
cloud6_bot.SetLineWeight(2);

cloud7_top.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud7_top.SetDefaultColor(Color.Dark_Green);
Cloud7_top.SetLineWeight(2);

cloud7_bot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud7_bot.SetDefaultColor(Color.Dark_Green);
cloud7_bot.SetLineWeight(2);

cloud8_top.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud8_top.SetDefaultColor(Color.Dark_Green);
Cloud8_top.SetLineWeight(2);

cloud8_bot.SetPaintingStrategy(PaintingStrategy.HORIZONTAL);
cloud8_bot.SetDefaultColor(Color.Dark_Green);
cloud8_bot.SetLineWeight(2);

@SleepyZ If you do not mind and have the time, Can you look at this one and see if it can be optimized.
 
Last edited by a moderator:
@SleepyZ If you do not mind and have the time, Can you look at this one and see if it can be optimized.

It "seems" like the script uses future bars AND MTF aggregations, so yes, there is significant lag.
No, the future bars are the essence of the study so No, they can not be taken out.

Might be an excellent trend script for swing traders, but definitely not for short-aggregation traders like yourself.
 
Last edited:

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