Strategy · Pine Script v6 · On the chart

MTF Trend Alignment Strategy

by shubhamtaywade82

A strategy that goes long when price is above a moving average on all four timeframes and short when it is below on all four.

View on GitHub

Archived by its author

GitHub stars1on the repository
Pine versionv6current
Lines of code68
Settings9inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago31 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 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

68 lines of Pine Script v6 from strategies/agent-examples/mtf-trend-alignment-strategy.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

mtf-trend-alignment-strategy.pine
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Strategy derived from pinescript-agents/examples/simple/mtf-trend-alignment.pine for backtesting in pinescripts-collections.

//@version=6
strategy("MTF Trend Alignment [Agent Example]", shorttitle="MTFTrStr", overlay=true, margin_long=100, margin_short=100, default_qty_type=strategy.percent_of_equity, default_qty_value=10, commission_type=strategy.commission.percent, commission_value=0.1)

ma_type = input.string("EMA", "MA Type", options=["SMA", "EMA", "WMA"], group="Settings")
ma_length = input.int(20, "MA Length", minval=5, maxval=200, group="Settings")

tf1 = input.timeframe("15", "Timeframe 1", group="Timeframes")
tf2 = input.timeframe("60", "Timeframe 2", group="Timeframes")
tf3 = input.timeframe("240", "Timeframe 3", group="Timeframes")
tf4 = input.timeframe("1D", "Timeframe 4", group="Timeframes")

exit_when_misaligned = input.bool(true, "Exit When Score < 4 / > -4", group="Risk")
bullColorInput = input.color(#089981, "Bullish Color", group="Visuals")
bearColorInput = input.color(#f23645, "Bearish Color", group="Visuals")

get_ma(src, length, ma_type_) =>
    switch ma_type_
        "SMA" => ta.sma(src, length)
        "EMA" => ta.ema(src, length)
        "WMA" => ta.wma(src, length)
        => ta.ema(src, length)

get_trend(ma_value, close_value) =>
    if close_value > ma_value
        1
    else if close_value < ma_value
        -1
    else
        0

ma_tf1 = request.security(syminfo.tickerid, tf1, get_ma(close, ma_length, ma_type), lookahead=barmerge.lookahead_off)
ma_tf2 = request.security(syminfo.tickerid, tf2, get_ma(close, ma_length, ma_type), lookahead=barmerge.lookahead_off)
ma_tf3 = request.security(syminfo.tickerid, tf3, get_ma(close, ma_length, ma_type), lookahead=barmerge.lookahead_off)
ma_tf4 = request.security(syminfo.tickerid, tf4, get_ma(close, ma_length, ma_type), lookahead=barmerge.lookahead_off)

close_tf1 = request.security(syminfo.tickerid, tf1, close, lookahead=barmerge.lookahead_off)
close_tf2 = request.security(syminfo.tickerid, tf2, close, lookahead=barmerge.lookahead_off)
close_tf3 = request.security(syminfo.tickerid, tf3, close, lookahead=barmerge.lookahead_off)
close_tf4 = request.security(syminfo.tickerid, tf4, close, lookahead=barmerge.lookahead_off)

trend_tf1 = get_trend(ma_tf1, close_tf1)
trend_tf2 = get_trend(ma_tf2, close_tf2)
trend_tf3 = get_trend(ma_tf3, close_tf3)
trend_tf4 = get_trend(ma_tf4, close_tf4)

trend_score = trend_tf1 + trend_tf2 + trend_tf3 + trend_tf4

longSignal = trend_score == 4
shortSignal = trend_score == -4

if longSignal
    strategy.entry("Long", strategy.long)
if shortSignal
    strategy.entry("Short", strategy.short)

if exit_when_misaligned
    if strategy.position_size > 0 and trend_score < 4
        strategy.close("Long")
    if strategy.position_size < 0 and trend_score > -4
        strategy.close("Short")

plot(trend_score, "Trend Score", color=trend_score > 0 ? bullColorInput : trend_score < 0 ? bearColorInput : color.new(#2a2e39, 0), linewidth=2)
hline(4, "All Bullish", color=color.new(bullColorInput, 35))
hline(-4, "All Bearish", color=color.new(bearColorInput, 35))
bgcolor(trend_score == 4 ? color.new(bullColorInput, 92) : trend_score == -4 ? color.new(bearColorInput, 92) : na, title="Full Alignment")

Overview

The defaults are 15 minutes, 1 hour, 4 hours and 1 day, using an SMA, EMA or WMA. An option closes the position as soon as the alignment is lost. There is no stop loss or take profit. It plots the alignment score and tints the background at full alignment.

Details

Published by

shubhamtaywade82 on GitHub

Type

Strategy, Pine Script v6

Draws

On the price chart

Category

Trend, Moving averages

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
MA TypestringEMAma_type
MA Lengthint20ma_length
Timeframe 1timeframe15tf1
Timeframe 2timeframe60tf2
Timeframe 3timeframe240tf3
Timeframe 4timeframe1Dtf4
Exit When Score < 4 / > -4booltrueexit_when_misaligned
Bullish Colorcolor#089981bullColorInput
Bearish Colorcolor#f23645bearColorInput
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

Doge SMA

by hackingthemarkets
474 stars

Doge SMA is a long-only moving-average crossover strategy drawn on the price chart. It calculates a fast and a slow simple moving average of close and enters a long position when the fast average crosses above the slow average, but only inside a user-defined s

Strategy v4 TrendMoving averages
Updated 4 years ago · None stated GitHub repository of Doge SMA

This strategy calculates and plots a fast Exponential Moving Average (EMA) and a slow EMA directly on the main price chart. It enters long trades when the fast EMA crosses above the slow EMA and exits when the fast EMA crosses below the slow EMA. Inputs allow

Strategy v6 TrendMoving averages
Updated yesterday · AGPL-3.0 GitHub repository of Forven S01508 — Simple EMA20/50 Cross
330 stars

2 EMA/SMA + RSI Strategy [Alorse] combines three configurable moving averages with a short-period Relative Strength Index filter. It plots fast, slow, and trend moving averages on the price chart. Long entries trigger when the fast moving average is above the

Strategy v4 TrendOscillators & momentum
Updated 2 months ago · None stated GitHub repository of 2 EMA/SMA + RSI Strategy [Alorse]

Acrypto - Weighted Strategy

by AlbertoCuadra
318 stars

This strategy plots on the price chart and opens long or short positions when a weighted score from MACD, Stochastic RSI, RSI, Supertrend and a moving-average crossover exceeds a user-set trigger. Each component has its own weight and candle delay. Entries can

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · Custom licence GitHub repository of Acrypto - Weighted Strategy
257 stars

This strategy calculates dynamic support and resistance levels using pivot highs and lows alongside a custom smoothed trend filter and a 3-period EMA. It identifies entry opportunities when price breaks out relative to the trend filter and short-term EMA cross

Strategy v5 TrendSupport & resistance Repaints
Updated 6 weeks ago · None stated GitHub repository of 30s - ES 1m Strategy Candle Bar

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