Indicator · Pine Script v5 · On the chart

MTF Candles

by g-moe· code credited to goodgains

MTF Candles draws higher-timeframe candles on the price chart as boxes and lines.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code345
Settings26inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 2026

Add It to Your TradingView Chart

  1. 1Copy the scriptPress Copy script above, or the copy button on the code below.
  2. 2Open the Pine EditorOpen a TradingView chart and choose Pine Editor at the bottom of the screen.
  3. 3Paste over the templateCreate a new indicator, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart, then adjust the settings with the gear icon next to its name.

Source Code

345 lines of Pine Script v5 from Tradingview/mtf-candles.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

mtf-candles.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
indicator("MTF Candles", overlay = true, max_boxes_count = 500, max_lines_count = 500)

// @keywords: mtf-candles, higher-timeframe, box-candles, five-timeframes, htf-labels, candle-body, wick-display, padding-offset, bull-bear-boxes, security-candles, multi-tf-panel, timeframe-bars, border-wick, stacked-candles, chart-offset, live-update, confirmed-candles, htf1-htf5, candle-history, mtf-visual

// visual Settings
css_bull_body = input.color(color.green, title = 'Body  ', group = 'Visual Settings', inline = '1')
css_bear_body = input.color(color.rgb(234, 21, 21), title = '', group = 'Visual Settings', inline = '1')

css_bull_border = input.color(color.lime, title = 'Border ', group = 'Visual Settings', inline = '2')
css_bear_border = input.color(color.red, title = '', group = 'Visual Settings', inline = '2')

css_bull_wick = input.color(color.lime, title = 'Wick  ', group = 'Visual Settings', inline = '3')
css_bear_wick = input.color(color.red, title = '', group = 'Visual Settings', inline = '3')

use_label   = input.bool(true, title = 'MTF Label', group = 'Visual Settings', inline = '4')
css_label   = input.color(color.new(#434651,0), title = '', group = 'Visual Settings', inline = '4')
label_size  = input.string('Medium', title = '', group = 'Visual Settings', inline = '4',
 options = ['Small', 'Medium', 'Large'])

padding     = input.int(10, title = 'Padding From Last Candle', minval = 3, maxval = 30, group = 'Visual Settings', inline = '5')
padding_b   = input.int(3, title = 'Padding Between MTF Candles', minval = 1, maxval = 10, group = 'Visual Settings', inline = '6')

// timeframe settings
use_tf1 = input.bool(true, title = 'HTF 1', group = 'Timeframe Settings', inline = '1')
use_tf2 = input.bool(true, title = 'HTF 2', group = 'Timeframe Settings', inline = '2')
use_tf3 = input.bool(true, title = 'HTF 3', group = 'Timeframe Settings', inline = '3')
use_tf4 = input.bool(true, title = 'HTF 4', group = 'Timeframe Settings', inline = '4')
use_tf5 = input.bool(true, title = 'HTF 5', group = 'Timeframe Settings', inline = '5')

tf1     = input.timeframe('5', title = '', group = 'Timeframe Settings', inline = '1')
tf2     = input.timeframe('15', title = '', group = 'Timeframe Settings', inline = '2')
tf3     = input.timeframe('30', title = '', group = 'Timeframe Settings', inline = '3')
tf4     = input.timeframe('60', title = '', group = 'Timeframe Settings', inline = '4')
tf5     = input.timeframe('D', title = '', group = 'Timeframe Settings', inline = '5')

tf1_bars= input.int(8, title = '', minval = 1, maxval = 50, group = 'Timeframe Settings', inline = '1')
tf2_bars= input.int(8, title = '', minval = 1, maxval = 50, group = 'Timeframe Settings', inline = '2')
tf3_bars= input.int(8, title = '', minval = 1, maxval = 50, group = 'Timeframe Settings', inline = '3')
tf4_bars= input.int(8, title = '', minval = 1, maxval = 50, group = 'Timeframe Settings', inline = '4')
tf5_bars= input.int(8, title = '', minval = 1, maxval = 50, group = 'Timeframe Settings', inline = '5')



// functions 
f_bull_candle(o_src, c_src) =>
    c_src >= o_src


f_label_size() =>
    ls = switch label_size
        "Small" => size.normal
        "Medium" => size.large
        "Large" => size.huge
        => size.normal


f_array_pop_push(array, value_to_add) =>
    array.shift(array)
    array.push(array, value_to_add)


f_array_boxdelete_pop(array) =>
    box.delete(array.first(array))
    array.shift(array)


f_array_linedelete_pop(array) =>
    line.delete(array.first(array))
    array.shift(array)


f_delete_all_boxes() =>
    a_allBoxes = box.all
    if array.size(a_allBoxes) > 0
        for i = 0 to array.size(a_allBoxes) - 1
            box.delete(array.get(a_allBoxes, i))


f_delete_all_lines() =>
    a_allLines = line.all
    if array.size(a_allLines) > 0
        for i = 0 to array.size(a_allLines) - 1
            line.delete(array.get(a_allLines, i))


f_delete_all_labels() =>
    a_allLabels = label.all
    if array.size(a_allLabels) > 0
        for i = 0 to array.size(a_allLabels) - 1
            label.delete(array.get(a_allLabels, i))


f_delete_all() =>
    f_delete_all_boxes()
    f_delete_all_lines()
    f_delete_all_labels()


f_timeframe_text(_timeframe) =>
    if timeframe.in_seconds(_timeframe) < 60
        _timeframe + 's'
    else if timeframe.in_seconds(_timeframe) < 3600
        _timeframe + 'm'
    else if timeframe.in_seconds(_timeframe) < 1440
        _timeframe + 'D'
    else if timeframe.in_seconds(_timeframe) < 10080
        _timeframe + 'W'
    else
        _timeframe + 'M'

f_draw_candles(o_array, h_array, l_array, c_array, box_array, line_array, label_array, start_spacing, end_spacing, label_y, timeframe) =>



    array_size = array.size(o_array)
    for i = 0 to array_size - 1
        new_x1          = bar_index + (start_spacing) + (3*i)
        new_x2          = new_x1 + 2
        new_open        = array.get(o_array, i)
        new_high        = array.get(h_array, i)
        new_low         = array.get(l_array, i)
        new_close       = array.get(c_array, i)

        is_bull_candle  = f_bull_candle(new_open, new_close)
        body_color      = is_bull_candle ? css_bull_body : css_bear_body
        border_color    = is_bull_candle ? css_bull_border : css_bear_border
        wick_color      = is_bull_candle ? css_bull_wick : css_bear_wick

        array.push(box_array, box.new(left = new_x1, top = new_open, right = new_x2, bottom = new_close,
         xloc=xloc.bar_index, border_style=line.style_solid, 
         bgcolor = body_color, border_color = border_color)
         )

        mid_time = (new_x1 + new_x2) / 2
        array.push(line_array, line.new(x1 = mid_time, y1 = new_high, x2 = mid_time, y2 = new_low,
         xloc=xloc.bar_index, style=line.style_solid, 
         color = wick_color)
         )

    middle_index = ((bar_index + start_spacing) + (bar_index + end_spacing)) / 2

    if use_label
        array.push(label_array, label.new(middle_index, label_y, text = f_timeframe_text(timeframe),
         style = label.style_none, textcolor = css_label, size = f_label_size())
         )


f_ltf_values(arr_o, arr_h, arr_l, arr_c, tf) =>
    [o,h,l,c] = request.security_lower_tf(syminfo.tickerid, tf, [open, high, low, close])
    f_array_pop_push(arr_o, array.last(o))
    f_array_pop_push(arr_h, array.last(h))
    f_array_pop_push(arr_l, array.last(l))
    f_array_pop_push(arr_c, array.last(c))


f_htf_values(arr_o, arr_h, arr_l, arr_c, tf) =>
    [o,h,l,c] = request.security(syminfo.tickerid, tf, [open, high, low, close])
    f_array_pop_push(arr_o, o)
    f_array_pop_push(arr_h, h)
    f_array_pop_push(arr_l, l)
    f_array_pop_push(arr_c, c)



// define drawing object arrays
var tf1_bodies  = array.new_box(tf1_bars, na) 
var tf2_bodies  = array.new_box(tf2_bars, na) 
var tf3_bodies  = array.new_box(tf3_bars, na) 
var tf4_bodies  = array.new_box(tf4_bars, na) 
var tf5_bodies  = array.new_box(tf5_bars, na) 

var tf1_wicks   = array.new_line(tf1_bars, na) 
var tf2_wicks   = array.new_line(tf2_bars, na) 
var tf3_wicks   = array.new_line(tf3_bars, na) 
var tf4_wicks   = array.new_line(tf4_bars, na) 
var tf5_wicks   = array.new_line(tf5_bars, na) 

var tf1_lbl     = array.new_label(1, na)
var tf2_lbl     = array.new_label(1, na)
var tf3_lbl     = array.new_label(1, na)
var tf4_lbl     = array.new_label(1, na)
var tf5_lbl     = array.new_label(1, na)


// define value arrays
var tf1_o       = array.new_float(tf1_bars, na)
var tf2_o       = array.new_float(tf2_bars, na)
var tf3_o       = array.new_float(tf3_bars, na)
var tf4_o       = array.new_float(tf4_bars, na)
var tf5_o       = array.new_float(tf5_bars, na)

var tf1_h       = array.new_float(tf1_bars, na)
var tf2_h       = array.new_float(tf2_bars, na)
var tf3_h       = array.new_float(tf3_bars, na)
var tf4_h       = array.new_float(tf4_bars, na)
var tf5_h       = array.new_float(tf5_bars, na)

var tf1_l       = array.new_float(tf1_bars, na)
var tf2_l       = array.new_float(tf2_bars, na)
var tf3_l       = array.new_float(tf3_bars, na)
var tf4_l       = array.new_float(tf4_bars, na)
var tf5_l       = array.new_float(tf5_bars, na)

var tf1_c       = array.new_float(tf1_bars, na)
var tf2_c       = array.new_float(tf2_bars, na)
var tf3_c       = array.new_float(tf3_bars, na)
var tf4_c       = array.new_float(tf4_bars, na)
var tf5_c       = array.new_float(tf5_bars, na)


// store the values 
if use_tf1
    if timeframe.in_seconds() <= timeframe.in_seconds(tf1)
        f_htf_values(tf1_o, tf1_h, tf1_l, tf1_c, tf1)
    else
        f_ltf_values(tf1_o, tf1_h, tf1_l, tf1_c, tf1)


    if use_tf2
        if timeframe.in_seconds() <= timeframe.in_seconds(tf2)
            f_ltf_values(tf2_o, tf2_h, tf2_l, tf2_c, tf2)
        else
            f_htf_values(tf2_o, tf2_h, tf2_l, tf2_c, tf2)


    // if use_tf3
    //     if timeframe.in_seconds() <= timeframe.in_seconds(tf3)
    //         f_ltf_values(tf3_o, tf3_h, tf3_l, tf3_c, tf3)
    //     else
    //         f_htf_values(tf3_o, tf3_h, tf4_l, tf3_c, tf3)


    // if use_tf4
    //     if timeframe.in_seconds() <= timeframe.in_seconds(tf4)
    //         f_ltf_values(tf4_o, tf4_h, tf4_l, tf4_c, tf4)
    //     else
    //         f_htf_values(tf4_o, tf4_h, tf4_l, tf4_c, tf4)


    // if use_tf5
    //     if timeframe.in_seconds() <= timeframe.in_seconds(tf5)
    //         f_ltf_values(tf5_o, tf5_h, tf5_l, tf5_c, tf5)
    //     else
    //         f_htf_values(tf5_o, tf5_h, tf5_l, tf5_c, tf5)


// calculate spacing
var int tf1_start   = na
var int tf1_end     = na

var int tf2_start   = na
var int tf2_end     = na

var int tf3_start   = na
var int tf3_end     = na

var int tf4_start   = na
var int tf4_end     = na

var int tf5_start   = na
var int tf5_end     = na

var int last_end    = na


if (barstate.isfirst)
    last_end := padding

    if (use_tf1)
        tf1_start := padding
        tf1_end   := tf1_start + 3 * tf1_bars
        last_end  := tf1_end

    if (use_tf2)
        tf2_start := last_end + (padding_b*3)
        tf2_end   := tf2_start + 3 * tf2_bars
        last_end  := tf2_end

    if (use_tf3)
        tf3_start := last_end + (padding_b*3)
        tf3_end   := tf3_start + 3 * tf3_bars
        last_end  := tf3_end

    if (use_tf4)
        tf4_start := last_end + (padding_b*3)
        tf4_end   := tf4_start + 3 * tf4_bars
        last_end  := tf4_end

    if (use_tf5)
        tf5_start := last_end + (padding_b*3)
        tf5_end   := tf5_start + 3 * tf5_bars


// if barstate.islast
//     label.new(bar_index, 15300, text = 
//      ('[' +
//      str.tostring(tf1_start) + ', ' + str.tostring(tf1_end) + ', ' +
//      str.tostring(tf2_start) + ', ' + str.tostring(tf2_end) + ', ' +
//      str.tostring(tf3_start) + ', ' + str.tostring(tf3_end) + ', ' +
//      str.tostring(tf4_start) + ', ' + str.tostring(tf4_end) + ', ' +
//      str.tostring(tf5_start) + ', ' + str.tostring(tf5_end) + ']'
//      )
//      )



tf1_max = nz(array.max(tf1_h), 0)
tf2_max = nz(array.max(tf2_h), 0)
tf3_max = nz(array.max(tf3_h), 0)
tf4_max = nz(array.max(tf4_h), 0)
tf5_max = nz(array.max(tf5_h), 0)

y_value = math.max(tf1_max, tf2_max, tf3_max, tf4_max, tf5_max)
// if barstate.islast
    // label.new(bar_index+3, high, text = str.tostring(timeframe.in_seconds() < timeframe.in_seconds(tf1)))
    // label.new(bar_index+3, 30500, text = str.tostring(tf2_h), color = color.white)
    // label.new(bar_index+3, 15300, text = str.tostring(tf1_c))

if barstate.islast

    f_delete_all()

    if use_tf1
        f_draw_candles(tf1_o, tf1_h, tf1_l, tf1_c, tf1_bodies, tf1_wicks, tf1_lbl, tf1_start, tf1_end, y_value, tf1)

    if use_tf2
        f_draw_candles(tf2_o, tf2_h, tf2_l, tf2_c, tf2_bodies, tf2_wicks, tf2_lbl, tf2_start, tf2_end, y_value, tf2)

    if use_tf3
        f_draw_candles(tf3_o, tf3_h, tf3_l, tf3_c, tf3_bodies, tf3_wicks, tf3_lbl, tf3_start, tf3_end, y_value, tf3)

    if use_tf4
        f_draw_candles(tf4_o, tf4_h, tf4_l, tf4_c, tf4_bodies, tf4_wicks, tf4_lbl, tf4_start, tf4_end, y_value, tf4)

    if use_tf5
        f_draw_candles(tf5_o, tf5_h, tf5_l, tf5_c, tf5_bodies, tf5_wicks, tf5_lbl, tf5_start, tf5_end, y_value, tf5)



bgcolor(barstate.islastconfirmedhistory ? color.rgb(255, 153, 0, 79) : barstate.islast ? color.rgb(255, 235, 59, 80) : na)

Overview

MTF Candles can show up to five independent timeframes, each with its own enable switch, timeframe choice, and history length of one to fifty bars. Bullish and bearish bodies, borders, and wicks use separate colors. Optional labels name each timeframe, and two padding inputs set the offset from the last chart candle and the gap between candle groups. It does not generate trade signals or exits.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

No repainting, lookahead or stacked entries were found in its code or README. That says nothing about how well it works: test it first.

Settings

The inputs you can change from the gear icon on the chart. Defaults are the author's.

SettingTypeDefaultVariable
Body  colorcolor.greencss_bull_body
css_bear_bodycolorcolor.rgb(234, 21, 21)css_bear_body
Border colorcolor.limecss_bull_border
css_bear_bordercolorcolor.redcss_bear_border
Wick  colorcolor.limecss_bull_wick
css_bear_wickcolorcolor.redcss_bear_wick
MTF Labelbooltrueuse_label
css_labelcolorcolor.new(#434651,0)css_label
label_sizestringMediumlabel_size
Padding From Last Candleint10padding
Padding Between MTF Candlesint3padding_b
HTF 1booltrueuse_tf1
Show all 26 settings
SettingTypeDefaultVariable
HTF 2booltrueuse_tf2
HTF 3booltrueuse_tf3
HTF 4booltrueuse_tf4
HTF 5booltrueuse_tf5
tf1timeframe5tf1
tf2timeframe15tf2
tf3timeframe30tf3
tf4timeframe60tf4
tf5timeframeDtf5
tf1_barsint8tf1_bars
tf2_barsint8tf2_bars
tf3_barsint8tf3_bars
tf4_barsint8tf4_bars
tf5_barsint8tf5_bars
Third-party code. ForexR did not write, test or endorse this script, and the summary above was generated automatically from its code and README. Indicators describe the past, and a strategy's backtest can look better than live trading, especially when a script repaints. Check it in the Strategy Tester and on a demo account before you rely on it.

Similar Pine Scripts

947 stars

The Heikin-Ashi Chart Identifier is an indicator that checks if the current chart uses Heikin-Ashi candles. It tracks matched candles where open equals the average of previous open and close, and verifies no gaps between bars. When conditions match, it display

Indicator v4 Candlestick patterns
Updated 12 days ago · GPL-3.0 GitHub repository of Heikin-Ashi Chart Identifier
575 stars

The indicator plots ICT displacement candles on the price chart and can switch to retracement scanning. Displacement requires a move larger than a set share of the 14-bar average high-low range, lasting at least a chosen number of bars, with optional close, pr

Indicator v6 SMC / ICT / price actionCandlestick patterns
Updated 1 year ago · MPL-2.0 GitHub repository of ICT Displacement Candles Indicator

All Candlestick Patterns

by shunjizhan
292 stars

All Candlestick Patterns detects and highlights a wide range of bullish, bearish, and neutral candlestick patterns directly on the price chart. It features optional trend filtering using either a 50-period simple moving average or a combination of 50-period an

Indicator v4 Candlestick patternsTrend
Updated 3 years ago · None stated GitHub repository of All Candlestick Patterns
285 stars

Mean Reversion Study is an indicator that identifies trade setups combining exponential moving averages, RSI extremes, and engulfing candlestick patterns. It highlights bullish setups when a bullish engulfing candle forms below the EMA while RSI is oversold, a

Indicator v4 Mean reversionCandlestick patterns
Updated 3 years ago · None stated GitHub repository of Mean Reversion Study

[RS]MTF Candle Sticks

by ricardosantos79
261 stars

This indicator overlays up to five higher-timeframe candlestick groups on the price chart. For each enabled timeframe it requests open, high, low, and close and can show up to five candles, offset by a spacing setting. The main inputs set the candle count, the

Indicator v4 Candlestick patternsSupport & resistance Repaints
Updated 4 years ago · None stated GitHub repository of [RS]MTF Candle Sticks
257 stars

This indicator detects a large set of classic candlestick patterns and plots colored labels on the price chart. Trend context uses a 50-period simple moving average, or the 50 and 200 averages stacked together, and can be switched off. Users filter bullish, be

Indicator v4 Candlestick patternsTrend
Updated 7 weeks ago · None stated GitHub repository of Snag - All Candlestick Patterns

More Candlestick patterns scripts

Related on ForexR

Trade Your Charts With a Regulated Broker

Compare brokers that connect to TradingView, with spreads, minimum deposits and leverage side by side.

Compare brokers