Strategy · Pine Script v5 · Own pane

Noob's MTF multi-EMA

by InNoobWeTrust

This strategy draws up to three exponential moving averages, default lengths 34, 89 and 610, across as many as seven user-chosen timeframes in a separate pane.

View on GitHub

Repaints

GitHub stars4on the repository
Pine versionv5runs as is
Lines of code434
Settings40inputs
LicenceNone statedfile: MPL-2.0
Last updatetoday28 Oct 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 strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

434 lines of Pine Script v5 from .innoobwetrust/life_hacks/tradingview/mtf_multi_ema.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

mtf_multi_ema.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © InNoobWeTrust

//@version=5
strategy("Noob's MTF multi-EMA", max_bars_back = 3600, overlay=false, dynamic_requests=true, initial_capital = 100,default_qty_value = 100,default_qty_type = strategy.cash,margin_long = 5,margin_short = 5, process_orders_on_close = true)
import TradingView/ta/8
import forbidden_gun/ma_lib/2

//=============================================================================
// MTF multi-emas
//=============================================================================

// Types
//-----------------------------------------------------------------------------{
type EmaTf
    string tf
    int len
    float ema
    float ema_filled
    color clr

enum TbLoc
    top_right = "Top Right"
    top_center = "Top Center"
    top_left = "Top Left"
    bot_right = "Bottom Right"
    bot_center = "Bottom Center"
    bot_left = "Bottom Left"
    mid_right = "Middle Right"
    mid_left = "Middle Left"
//-----------------------------------------------------------------------------}

// Const
//-----------------------------------------------------------------------------{
const int TF_SIZE = 7
//-----------------------------------------------------------------------------{

// Helper functions
//-----------------------------------------------------------------------------{
ema_tf(series string tf, int len, src, simple bool smooth = false) =>
    request.security(syminfo.tickerid, tf, ta.ema(src, len), smooth ? barmerge.gaps_on : barmerge.gaps_off, smooth ? barmerge.lookahead_on : barmerge.lookahead_off)

ema_color(int tf_lvl, int max, color gradient_start, color gradient_end) =>
    color.from_gradient(tf_lvl,1,max,gradient_start,gradient_end)

pstrct_at_cross(src1,src2) =>
    pstrct = ma_lib.pair_structure(src1,src2)
    cross = ta.cross(src1,src2)
    cross ? pstrct : 0

tb_location(TbLoc loc) =>
    switch loc
        TbLoc.top_left => position.top_left
        TbLoc.top_center => position.top_center
        TbLoc.top_right => position.top_right
        TbLoc.bot_left => position.bottom_left
        TbLoc.bot_center => position.bottom_center
        TbLoc.bot_right => position.bottom_right
        TbLoc.mid_right => position.middle_right
        TbLoc.mid_left => position.middle_left

countdown(string tf) =>
    if timeframe.in_seconds(timeframe.period) < timeframe.in_seconds(tf)
        time_left = int(time_close(tf) - timenow)
    else
        na

fmt_countdown(int secs) =>
    if not na(secs)
        cntdwn = secs / 1000
        hh = int(cntdwn / 3600)
        mm = int((cntdwn % 3600) / 60)
        ss = int(cntdwn % 60)
        str.format('{0,number,00}:{1,number,00}:{2,number,00}', hh,mm,ss)
    else
        '-- : -- : --'

trend_ico(trend) =>
    switch trend
        1 => '⬆︎' // Up
        -1 => '⬇︎' // Down
        0 => '〰︎' // Tangled
        => '?' // Unknown, data not available for calculation

trend_txt_color(trend) =>
    switch trend
        1 => #26a69a
        -1 => #ef5350
        0 => color.gray
        => color.silver

trend_bg_color(trend) =>
    switch trend
        1 => color.new(#26a69a,80)
        -1 => color.new(#ef5350,80)
        0 => color.new(color.gray,80)
        => color.new(color.silver,80)

mtf_bias_bgcolor(bias,max_bias,crossover_gradient_start,crossover_gradient_end,crossunder_gradient_end,crossunder_gradient_start) =>
    switch
        bias > 0 => color.from_gradient(bias,0,max_bias,crossover_gradient_start,crossover_gradient_end)
        bias < 0 => color.from_gradient(bias,-max_bias,0,crossunder_gradient_end,crossunder_gradient_start)
        => color(na)
//-----------------------------------------------------------------------------}

// Input
//-----------------------------------------------------------------------------{
ema1_grp = "► EMA fast"
ema1_inline = "EMA fast"
ema1_enable = input.bool(true, '', group = ema1_grp, inline = ema1_inline)
ema1_len = input.int(34, '', group = ema1_grp, inline = ema1_inline)
ema1_gradient_start = input.color(color.new(color.aqua,80), '', group = ema1_grp, inline = ema1_inline)
ema1_gradient_end = input.color(color.new(color.purple,50), '', group = ema1_grp, inline = ema1_inline)
ema1_max_tf = input.int(TF_SIZE, 'Max timeframes to show', minval=1,maxval=TF_SIZE, group = ema1_grp)

ema2_grp = "► EMA slow"
ema2_inline = "EMA slow"
ema2_enable = input.bool(true, '', group = ema2_grp, inline = ema2_inline)
ema2_len = input.int(89, '', group = ema2_grp, inline = ema2_inline)
ema2_gradient_start = input.color(color.new(color.yellow,80), '', group = ema2_grp, inline = ema2_inline)
ema2_gradient_end = input.color(color.new(color.maroon,50), '', group = ema2_grp, inline = ema2_inline)
ema2_max_tf = input.int(TF_SIZE, 'Max timeframes to show', minval=1,maxval=TF_SIZE, group = ema2_grp)

ema3_grp = "► EMA auxiliary"
ema3_inline = "EMA auxiliary"
ema3_enable = input.bool(false, '', group = ema3_grp, inline = ema3_inline)
ema3_len = input.int(610, '', group = ema3_grp, inline = ema3_inline)
ema3_gradient_start = input.color(color.new(color.white,80), '', group = ema3_grp, inline = ema3_inline)
ema3_gradient_end = input.color(color.new(color.gray,50), '', group = ema3_grp, inline = ema3_inline)
ema3_max_tf = input.int(TF_SIZE, 'Max timeframes to show', minval=1,maxval=TF_SIZE, group = ema3_grp)

tf_grp = "► Timeframes"
tf_inline = "Timeframes cfg"
tf1 = input.timeframe('1', '', group = tf_grp, inline = tf_inline)
tf2 = input.timeframe('5', '', group = tf_grp, inline = tf_inline)
tf3 = input.timeframe('30', '', group = tf_grp, inline = tf_inline)
tf4 = input.timeframe('240', '', group = tf_grp, inline = tf_inline)
tf5 = input.timeframe('D', '', group = tf_grp, inline = tf_inline)
tf6 = input.timeframe('W', '', group = tf_grp, inline = tf_inline)
tf7 = input.timeframe('M', '', group = tf_grp, inline = tf_inline)

smooth = input.bool(false, 'Smooth lines', tooltip = 'Enable this option will make ema lines in higher timeframes lag behind current bar')
variable_width = input.bool(false, 'Variable line width', tooltip = 'Increase line width for higher timeframes')

summary_tb_grp = '► Trend summary table'
summary_tb_enable = input.bool(true, 'Show',group = summary_tb_grp)
summary_tb_loc = input.enum(TbLoc.top_right, 'Location',group = summary_tb_grp)

cross_grp = "► MTF crossing background (distribution/accumulation zones)"
cross_enable = input.bool(true, 'Enable',group = cross_grp)
crossunder_gradient_inline = 'Crossunder gradient color'
crossunder_gradient_start = input.color(color.new(color.orange,95), crossunder_gradient_inline, group = cross_grp, inline = crossunder_gradient_inline)
crossunder_gradient_end = input.color(color.new(color.maroon,20), '', group = cross_grp, inline = crossunder_gradient_inline)
crossover_gradient_inline = 'Crossover gradient color'
crossover_gradient_start = input.color(color.new(color.aqua,95), crossover_gradient_inline, group = cross_grp, inline = crossover_gradient_inline)
crossover_gradient_end = input.color(color.new(color.teal,20), '', group = cross_grp, inline = crossover_gradient_inline)
cross_tf_inline = 'Limit min/max timeframes'
cross_tf_min = input.int(1, cross_tf_inline, minval=1,maxval=TF_SIZE - 1,group = cross_grp,inline = cross_tf_inline)
cross_tf_max = input.int(TF_SIZE, '', minval=2,maxval=TF_SIZE,group = cross_grp,inline = cross_tf_inline)

bias_cum_win_grp = "► Bias cumulative rolling window"
bias_cum_win_len = input.int(9,'Window',group = bias_cum_win_grp)
bias_cum_win_trend_detect_length = input.int(2,'Trend detection length',group = bias_cum_win_grp)
bias_suppress_higher_tf = input.bool(true, 'Suppress higher timeframe',group = bias_cum_win_grp)

strategy_grp = "► Strategy"
strategy_position_label = input.bool(true,"Show position label",group = strategy_grp)
strategy_position_bg = input.bool(true,"Show background on ideal long/short entry",group = strategy_grp)
strategy_sl_percent = input.float(0.05, "SL percentage", minval=0.005,maxval=0.995,group = strategy_grp)
strategy_trailing_stop = input.bool(true, 'Enable trailing stop', tooltip = 'Using SL percentage as trailing stop percentage',group = strategy_grp)
strategy_cross_ema_inline = 'Only when price crossing EMA slow'
strategy_wait_ema = input.bool(true, strategy_cross_ema_inline,tooltip = 'EMA slow of nearest lower timeframe or chart timeframe. Enable this to trade-off  for less risk, higher chance to win and with more profit',group = strategy_grp,inline=strategy_cross_ema_inline)
strategy_cross_ema_window = input.int(9,'',group = strategy_grp,inline=strategy_cross_ema_inline)
strategy_long_enabled = input.bool(true, "Long action",group = strategy_grp)
strategy_short_enabled = input.bool(false, "Short action",group = strategy_grp)
//-----------------------------------------------------------------------------}

// Calculation
//-----------------------------------------------------------------------------{

// Timeframes
simple string[] tfs = array.from(tf1,tf2,tf3,tf4,tf5,tf6,tf7)
int[] lengths = array.from(ema1_len,ema2_len,ema3_len)
int[] max_tfs = array.from(ema1_max_tf,ema2_max_tf,ema3_max_tf)
color[] start_gradients = array.from(ema1_gradient_start,ema2_gradient_start,ema3_gradient_start)
color[] end_gradients = array.from(ema1_gradient_end,ema2_gradient_end,ema3_gradient_end)

newEma(int tf_idx, int ema_idx) =>
    tf = switch tf_idx
        0 => tf1
        1 => tf2
        2 => tf3
        3 => tf4
        4 => tf5
        5 => tf6
        6 => tf7
    len = switch ema_idx
        0 => ema1_len
        1 => ema2_len
        2 => ema3_len
    s_grad = start_gradients.get(ema_idx)
    e_grad = end_gradients.get(ema_idx)
    max = max_tfs.get(0)
    // F**king stupid workaround, thanks pinescript for f**king language design
    ema = switch ema_idx
        0 => ema_tf(tf,ema1_len, close, smooth)
        1 => ema_tf(tf,ema2_len, close, smooth)
        2 => ema_tf(tf,ema3_len, close, smooth)
    ema_filled = ta.valuewhen(not na(ema), ema, 0)
    ema_clr = ema_color(tf_idx + 1,max,s_grad,e_grad)
    EmaTf.new(tf, len, ema, ema_filled, ema_clr)

// EMA data
EmaTf[] ema1 = array.new<EmaTf>()
ema1.push(newEma(0,0))
ema1.push(newEma(1,0))
ema1.push(newEma(2,0))
ema1.push(newEma(3,0))
ema1.push(newEma(4,0))
ema1.push(newEma(5,0))
ema1.push(newEma(6,0))

EmaTf[] ema2 = array.new<EmaTf>()
ema2.push(newEma(0,1))
ema2.push(newEma(1,1))
ema2.push(newEma(2,1))
ema2.push(newEma(3,1))
ema2.push(newEma(4,1))
ema2.push(newEma(5,1))
ema2.push(newEma(6,1))

EmaTf[] ema3 = array.new<EmaTf>()
ema3.push(newEma(0,2))
ema3.push(newEma(1,2))
ema3.push(newEma(2,2))
ema3.push(newEma(3,2))
ema3.push(newEma(4,2))
ema3.push(newEma(5,2))
ema3.push(newEma(6,2))

// Market structure data
float[] ema_trends = array.new<float>()
for idx = 0 to TF_SIZE - 1
    fast = ema1.get(idx).ema_filled
    slow = ema2.get(idx).ema_filled
    s = ma_lib.MaStructure.new(close, fast, slow)
    ema_trends.push(ma_lib.market_structure(s))

// MTF supertrend of EMA slow of lower timeframe crossing EMA fast of higher timeframe
float[] supertrend = array.new<float>()
var min_tf_secs = timeframe.in_seconds(tfs.get(0))
float bias_val = 0.
float max_abs_bias = 0.

for idx = 0 to TF_SIZE - 1
    tf_secs = timeframe.in_seconds(tfs.get(idx))
    chart_tf_secs = timeframe.in_seconds('')
    if bias_suppress_higher_tf and tf_secs > chart_tf_secs
        supertrend.push(0)
    else if cross_tf_min > idx + 1 or cross_tf_max < idx + 2
        supertrend.push(0)
    else
        multiplier = tf_secs / min_tf_secs
        max_abs_bias += 8 * multiplier // 2^3, as we have 3 kinds of crossing
        ema_fast = ema1.get(idx).ema_filled
        ema_slow = ema2.get(idx).ema_filled
        higher_ema_fast = ema1.get(idx + 1).ema_filled
        higher_ema_slow = ema2.get(idx + 1).ema_filled
        fast_cross = pstrct_at_cross(ema_fast,higher_ema_fast)
        mixed_cross = pstrct_at_cross(ema_fast,higher_ema_slow)
        slow_cross = pstrct_at_cross(ema_slow,higher_ema_slow)
        trend = fast_cross + 2 * mixed_cross + 4 * slow_cross
        supertrend.push(trend)

        bias_val += trend * multiplier

bias_cum = math.sum(bias_val, bias_cum_win_len)

position_entry = 0
// Long entry
bias_cum_rising = ta.rising(bias_cum,bias_cum_win_trend_detect_length)
bias_cum_falling = ta.falling(bias_cum,bias_cum_win_trend_detect_length)
if bias_cum_rising
    position_entry := 1
// Short entry
else if bias_cum_falling
    position_entry := -1

position = position_entry * 2 // 2 for long and -2 for short 

// Wait for price to cross EMA slow
float ema_slow = na
for idx = 0 to TF_SIZE - 1
    tf_secs = timeframe.in_seconds(tfs.get(idx))
    chart_tf_secs = timeframe.in_seconds('')
    if tf_secs <= chart_tf_secs
        ema_slow := ema2.get(idx).ema_filled
// Suppress if price does not cross EMA slow
highest_in_window = ta.highest(high,strategy_cross_ema_window)
lowest_in_window = ta.lowest(low,strategy_cross_ema_window)
cross_ema = lowest_in_window <= ema_slow and ema_slow <= highest_in_window
if strategy_wait_ema
    if math.abs(position) == 2 and not cross_ema
        position := 0
    
//-----------------------------------------------------------------------------}

// Plot
//-----------------------------------------------------------------------------{

// ema1
ema1_tf1_plt = plot(ema1_enable and 1 < ema1_max_tf ? ema1.get(0).ema : na, 'EMA#1 TF1', ema1.get(0).clr, force_overlay = true, linewidth = variable_width ? 1 : 1)
ema1_tf2_plt = plot(ema1_enable and 2 < ema1_max_tf ? ema1.get(1).ema : na, 'EMA#1 TF2', ema1.get(1).clr, force_overlay = true, linewidth = variable_width ? 2 : 1)
ema1_tf3_plt = plot(ema1_enable and 3 < ema1_max_tf ? ema1.get(2).ema : na, 'EMA#1 TF3', ema1.get(2).clr, force_overlay = true, linewidth = variable_width ? 3 : 1)
ema1_tf4_plt = plot(ema1_enable and 4 < ema1_max_tf ? ema1.get(3).ema : na, 'EMA#1 TF4', ema1.get(3).clr, force_overlay = true, linewidth = variable_width ? 4 : 1)
ema1_tf5_plt = plot(ema1_enable and 5 < ema1_max_tf ? ema1.get(4).ema : na, 'EMA#1 TF5', ema1.get(4).clr, force_overlay = true, linewidth = variable_width ? 5 : 1)
ema1_tf6_plt = plot(ema1_enable and 6 < ema1_max_tf ? ema1.get(5).ema : na, 'EMA#1 TF6', ema1.get(5).clr, force_overlay = true, linewidth = variable_width ? 6 : 1)
ema1_tf7_plt = plot(ema1_enable and 7 < ema1_max_tf ? ema1.get(6).ema : na, 'EMA#1 TF7', ema1.get(6).clr, force_overlay = true, linewidth = variable_width ? 7 : 1)
// ema2
ema2_tf1_plt = plot(ema2_enable and 1 < ema2_max_tf ? ema2.get(0).ema : na, 'EMA#2 TF1', ema2.get(0).clr, force_overlay = true, linewidth = variable_width ? 1 : 2)
ema2_tf2_plt = plot(ema2_enable and 2 < ema2_max_tf ? ema2.get(1).ema : na, 'EMA#2 TF2', ema2.get(1).clr, force_overlay = true, linewidth = variable_width ? 2 : 2)
ema2_tf3_plt = plot(ema2_enable and 3 < ema2_max_tf ? ema2.get(2).ema : na, 'EMA#2 TF3', ema2.get(2).clr, force_overlay = true, linewidth = variable_width ? 3 : 2)
ema2_tf4_plt = plot(ema2_enable and 4 < ema2_max_tf ? ema2.get(3).ema : na, 'EMA#2 TF4', ema2.get(3).clr, force_overlay = true, linewidth = variable_width ? 4 : 2)
ema2_tf5_plt = plot(ema2_enable and 5 < ema2_max_tf ? ema2.get(4).ema : na, 'EMA#2 TF5', ema2.get(4).clr, force_overlay = true, linewidth = variable_width ? 5 : 2)
ema2_tf6_plt = plot(ema2_enable and 6 < ema2_max_tf ? ema2.get(5).ema : na, 'EMA#2 TF6', ema2.get(5).clr, force_overlay = true, linewidth = variable_width ? 6 : 2)
ema2_tf7_plt = plot(ema2_enable and 7 < ema2_max_tf ? ema2.get(6).ema : na, 'EMA#2 TF7', ema2.get(6).clr, force_overlay = true, linewidth = variable_width ? 7 : 2)
// ema3
ema3_tf1_plt = plot(ema3_enable and 1 < ema3_max_tf ? ema3.get(0).ema : na, 'EMA#3 TF1', ema3.get(0).clr, force_overlay = true, linewidth = variable_width ? 1 : 1)
ema3_tf2_plt = plot(ema3_enable and 2 < ema3_max_tf ? ema3.get(1).ema : na, 'EMA#3 TF2', ema3.get(1).clr, force_overlay = true, linewidth = variable_width ? 2 : 1)
ema3_tf3_plt = plot(ema3_enable and 3 < ema3_max_tf ? ema3.get(2).ema : na, 'EMA#3 TF3', ema3.get(2).clr, force_overlay = true, linewidth = variable_width ? 3 : 1)
ema3_tf4_plt = plot(ema3_enable and 4 < ema3_max_tf ? ema3.get(3).ema : na, 'EMA#3 TF4', ema3.get(3).clr, force_overlay = true, linewidth = variable_width ? 4 : 1)
ema3_tf5_plt = plot(ema3_enable and 5 < ema3_max_tf ? ema3.get(4).ema : na, 'EMA#3 TF5', ema3.get(4).clr, force_overlay = true, linewidth = variable_width ? 5 : 1)
ema3_tf6_plt = plot(ema3_enable and 6 < ema3_max_tf ? ema3.get(5).ema : na, 'EMA#3 TF6', ema3.get(5).clr, force_overlay = true, linewidth = variable_width ? 6 : 1)
ema3_tf7_plt = plot(ema3_enable and 7 < ema3_max_tf ? ema3.get(6).ema : na, 'EMA#3 TF7', ema3.get(6).clr, force_overlay = true, linewidth = variable_width ? 7 : 1)

// MTF bias bg and plot
bgcolor(mtf_bias_bgcolor(bias_val,max_abs_bias,crossover_gradient_start,crossover_gradient_end,crossunder_gradient_end,crossunder_gradient_start), force_overlay = true)
bias_color = switch
    bias_val > 0 => color.teal
    bias_val < 0 => color.maroon
    => color(na)
plot(bias_val, 'MTF bias', style = plot.style_columns, histbase = 0, color = bias_color)
bias_cum_color = switch
    bias_cum > 0 => color.aqua
    bias_cum < 0 => color.orange
    => color(na)
cum_plt = plot(bias_cum, 'MTF bias rolling cumulative', color = bias_cum_color)

// Ideal entry for long/short
position_color = switch
    strategy_position_bg and position_entry > 0 => strategy_position_bg ? color.new(color.green,90) : color(na)
    strategy_position_bg and position_entry < 0 => strategy_position_bg ? color.new(color.red,90) : color(na)
    => color(na)
bgcolor(position_color)

// Trend summary and countdown to bar close
var cntdwn_tb = table.new(tb_location(summary_tb_loc), 3, TF_SIZE, force_overlay = true)

if summary_tb_enable
    row = -1
    for idx = 0 to TF_SIZE - 1
        tf = tfs.get(idx)
        cntdwn_secs = countdown(tf)
        if na(cntdwn_secs)
            continue
        else
            row += 1

        countdown_text = fmt_countdown(cntdwn_secs)
        trend = ema_trends.get(idx)
        ico = trend_ico(trend)
        txt_color = trend_txt_color(trend)
        bg_color = trend_bg_color(trend)
        table.cell(cntdwn_tb,0,row,tf,text_color = color.gray,bgcolor = na)
        table.cell(cntdwn_tb,1,row,ico,text_color = txt_color,bgcolor = bg_color)
        table.cell(cntdwn_tb,2,row,countdown_text,text_color = color.gray,bgcolor = na)
//-----------------------------------------------------------------------------}

// Strategic actions
//-----------------------------------------------------------------------------{

entry_price() =>
    strategy.opentrades.entry_price(strategy.opentrades - 1)
entry_index() =>
    strategy.opentrades.entry_bar_index(strategy.opentrades - 1)
close_long(simple float position_val) =>
    if not strategy_trailing_stop and position_val == 1
        na
    else if strategy_long_enabled and strategy.opentrades.size(strategy.opentrades - 1) > 0
        label.new(bar_index - 1,high + close*0.01,text='C',xloc=xloc.bar_index,style = label.style_text_outline,size = size.tiny,color=color.gray,force_overlay = true)
        strategy.close('Long')
close_short(simple float position_val) =>
    if not strategy_trailing_stop and position_val == -1
        na
    else if strategy_short_enabled and strategy.opentrades.size(strategy.opentrades - 1) < 0
        label.new(bar_index - 1,low - close*0.01,text='C',xloc=xloc.bar_index,style = label.style_text_outline,size = size.tiny,color=color.gray,force_overlay = true)
        strategy.close('Short')
order_long() =>
    close_short(2)
    if strategy_long_enabled
        if strategy.opentrades.size(strategy.opentrades - 1) <= 0
            label.new(bar_index - 1,low - close*0.01,text='L',xloc=xloc.bar_index,style = label.style_text_outline,size = size.tiny,color=color.green,force_overlay = true)
        strategy.entry('Long',strategy.long)
        alert('Long entry!', alert.freq_once_per_bar_close)
order_short() =>
    close_long(-2)
    if strategy_short_enabled
        if strategy.opentrades.size(strategy.opentrades - 1) >= 0
            label.new(bar_index - 1,high + close*0.01,text='S',xloc=xloc.bar_index,style = label.style_text_outline,size = size.tiny,color=color.red,force_overlay = true)
        strategy.entry('Short',strategy.short)
        alert('Short entry!', alert.freq_once_per_bar_close)

// Update entry price
tick_size = syminfo.mintick * syminfo.pointvalue
sl_change = strategy_sl_percent * close
sl_ticks = sl_change / tick_size
highest_from_entry = entry_index() != 0 ? ta.highest(high, bar_index - entry_index() + 1) : high
lowest_from_entry = entry_index() != 0 ? ta.lowest(low, bar_index - entry_index() + 1) : low
sl_price = switch math.sign(strategy.opentrades.size(strategy.opentrades - 1))
    1 => highest_from_entry - sl_change > entry_price() ? highest_from_entry - sl_change : entry_price() - sl_change
    -1 => lowest_from_entry + sl_change < entry_price() ? lowest_from_entry + sl_change : entry_price() + sl_change
    => close
switch position
    2 => order_long()
    -2 => order_short()
    1 => close_long(1)
    -1 => close_short(-1)
// Trailing stop (sl_price is updated every bar to reduce risk)
if strategy_trailing_stop
    if strategy.opentrades.size(strategy.opentrades - 1) > 0
        strategy.exit('SL Long','Long',stop = sl_price)
    else if strategy.opentrades.size(strategy.opentrades - 1) < 0
        strategy.exit('SL Short', 'Short',stop = sl_price)
//-----------------------------------------------------------------------------}

Overview

Each series uses a gradient by timeframe, and optional smoothing or variable line width changes how those lines appear. A summary table can show trend direction and a countdown to the higher-timeframe close, while cross coloring marks EMA crosses. Enabling smooth requests higher-timeframe data with lookahead and no offset. No entry, exit or stop-loss rules appear in the provided code.

Details

Published by

InNoobWeTrust on GitHub

Type

Strategy, Pine Script v5

Draws

Separate pane below the chart

Category

Moving averages, Screeners & dashboards

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • Signals can change on the live bar or after it closes (repainting)
  • Reads higher-timeframe data with lookahead on, so the history can show signals that were not there in real time
  • The strategy enters trades without a stop loss

Topics

dotfiles

Settings

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

SettingTypeDefaultVariable
ema1_enablebooltrueema1_enable
ema1_lenint34ema1_len
ema1_gradient_startcolorcolor.new(color.aqua,80)ema1_gradient_start
ema1_gradient_endcolorcolor.new(color.purple,50)ema1_gradient_end
Max timeframes to showintTF_SIZEema1_max_tf
ema2_enablebooltrueema2_enable
ema2_lenint89ema2_len
ema2_gradient_startcolorcolor.new(color.yellow,80)ema2_gradient_start
ema2_gradient_endcolorcolor.new(color.maroon,50)ema2_gradient_end
Max timeframes to showintTF_SIZEema2_max_tf
ema3_enableboolfalseema3_enable
ema3_lenint610ema3_len
Show all 40 settings
SettingTypeDefaultVariable
ema3_gradient_startcolorcolor.new(color.white,80)ema3_gradient_start
ema3_gradient_endcolorcolor.new(color.gray,50)ema3_gradient_end
Max timeframes to showintTF_SIZEema3_max_tf
tf1timeframe1tf1
tf2timeframe5tf2
tf3timeframe30tf3
tf4timeframe240tf4
tf5timeframeDtf5
tf6timeframeWtf6
tf7timeframeMtf7
Smooth linesboolfalsesmooth
Variable line widthboolfalsevariable_width
Showbooltruesummary_tb_enable
LocationenumTbLoc.top_rightsummary_tb_loc
Enablebooltruecross_enable
crossunder_gradient_startcolorcolor.new(color.orange,95)crossunder_gradient_start
crossunder_gradient_endcolorcolor.new(color.maroon,20)crossunder_gradient_end
crossover_gradient_startcolorcolor.new(color.aqua,95)crossover_gradient_start
crossover_gradient_endcolorcolor.new(color.teal,20)crossover_gradient_end
cross_tf_minint1cross_tf_min
cross_tf_maxintTF_SIZEcross_tf_max
Windowint9bias_cum_win_len
Trend detection lengthint2bias_cum_win_trend_detect_length
Suppress higher timeframebooltruebias_suppress_higher_tf
Show position labelbooltruestrategy_position_label
Show background on ideal long/short entrybooltruestrategy_position_bg
SL percentagefloat0.05strategy_sl_percent
Enable trailing stopbooltruestrategy_trailing_stop
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

8 stars

This strategy calculates a StockCharts Technical Rank metric alongside an O'Neil-style relative strength rating, plotting both curves in a separate pane. It evaluates long-term, medium-term, and short-term exponential moving average deviations, rate-of-change

Strategy v5 Oscillators & momentumMoving averages
Updated 4 years ago · None stated GitHub repository of SCTR & ONeil + RSI strategy

AC- MY SCRIPT1

by hasnocool
106 stars

This strategy plots an Optimized Trend Tracker and a selectable moving-average support line on the price chart. It builds the average as SMA, EMA, WMA, TMA, VAR, WWMA, ZLEMA, or TSF, then offsets long and short stops by a percent of that average. Optional mark

Strategy v5 TrendMoving averages
Updated 2 years ago · GPL-3.0 GitHub repository of AC- MY SCRIPT1
104 stars

This overlay strategy combines a machine-learning distance model with a MACD oscillator to open trades on the price chart. It compares recent history using cosine similarity or Euclidean and Lorentzian distance, then filters direction with selectable moving-av

Strategy v5 AI / machine learningOscillators & momentum
Updated 3 years ago · MIT GitHub repository of MLCEL and MACD Strategy

This long-only trend-following strategy combines Ichimoku Kinko Hyo components with higher timeframe moving average filters. It triggers long entries when price trades above a higher timeframe EMA and a bullish Tenkan-Sen cross above Kijun-Sen occurs under fav

Strategy v5 TrendOscillators & momentum
Updated 1 year ago · MPL-2.0 GitHub repository of Ichimoku Kinko Hyo Momentum Strategy

Daily Loss Limit [Lab]

by mitchell-917
4 stars

This strategy example enforces a daily loss limit. It detects a new trading day, stores starting equity, and tracks the day's highest and lowest equity. Trading permission depends on whether the loss from the day's starting equity stays inside a percentage of

Strategy v5 Moving averagesSessions & time
Updated 11 months ago · Custom licence GitHub repository of Daily Loss Limit [Lab]
4 stars

This strategy enters a long position when a short RSI falls below a user threshold and price is above a simple moving average trend filter. It closes the long when RSI rises above a second threshold and does not set a stop loss. Position size defaults to full

Strategy v6 Oscillators & momentumMoving averages
Updated 13 days ago · MIT GitHub repository of RSI(2) pullback + TokIO check

More strategys

Trade Your Charts With a Regulated Broker

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

Compare brokers