Indicator · Pine Script v5 · On the chart

Trend Detector

by MarcosACH

Trend Detector can plot up to three moving averages, each with its own type (SMA, EMA, RMA, WMA or VWMA), length, source and timeframe, defaulting to 15, 60 and 240 minutes.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code131
Settings31inputs
LicenceNone statedfile: MPL-2.0
Last update1 year ago8 Jul 2025

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

131 lines of Pine Script v5 from trend_detector.pinescript, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

trend_detector.pinescript
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © MarcosACH
// Created by Marcos Achaval
// Last updated: July 17, 2024

//@version=5
indicator(title = "Trend Detector", shorttitle = "Trend Detector", overlay = true)


//#region ------ USER INPUTS
// Trend Detector
bool trendDetectorSwitch = input.bool(defval = true, title = "Plot", group = "Trend Detector")
string trendDetectorPosition = input.string(defval = position.top_right, title = "Position", 
     options = [position.top_left, position.top_center, position.top_right, position.middle_left, position.middle_center, 
                 position.middle_right, position.bottom_left, position.bottom_center, position.bottom_right], group = "Trend Detector")
color trendDetectorFrameColor = input.color(defval = color.new(color.white, 80), title = "Frame Color", group = "Trend Detector")
int trendDetectorFrameWidth = input.int(defval = 2, title = "Frame Width", minval = 0, maxval = 5, group = "Trend Detector")
color trendDetectorBorderColor = input.color(defval = color.new(color.white, 80), title = "Border Color", group = "Trend Detector")
int trendDetectorBorderWidth = input.int(defval = 2, title = "Border Width", minval = 0, maxval = 5, group = "Trend Detector")
color trendDetectorTextColor = input.color(defval = color.new(color.white, 60), title = "Text Color", group = "Trend Detector")

// Fast MA
bool ma1Switch = input.bool(defval = false, title = "Plot", group = "Fast MA")
bool ma1ShowTrendData = input.bool(defval = true, title = "Show Trend Data", tooltip = "This will show the trend data on the 'Trend Detector' table", group = "Fast MA")
string ma1Type = input.string(defval = "EMA", title = "Type", options = ["SMA", "EMA", "RMA", "WMA", "VWMA"], group = "Fast MA")
int ma1Length = input.int(defval = 20, title = "Length", group = "Fast MA")
ma1Source = input.source(defval = close, title = "Source", group = "Fast MA")
ma1Timeframe = input.timeframe(defval = "15", title = "Timeframe", group = "Fast MA")
color ma1Color = input.color(defval = color.new(color.red, 0), title = "Color", group = "Fast MA")
bool ma1WaitTF = input.bool(defval = true, title = "Wait TF Close", tooltip = "This will wait until the selected timeframe closes to plot the MA, which will remove the gaps", group = "Fast MA")

// Mid MA
bool ma2Switch = input.bool(defval = false, title = "Plot", group = "Mid MA")
bool ma2ShowTrendData = input.bool(defval = true, title = "Show Trend Data", tooltip = "This will show the trend data on the 'Trend Detector' table", group = "Mid MA")
string ma2Type = input.string(defval = "EMA", title = "Type", options = ["SMA", "EMA", "RMA", "WMA", "VWMA"], group = "Mid MA")
int ma2Length = input.int(defval = 20, title = "Length", group = "Mid MA")
ma2Source = input.source(defval = close, title = "Source", group = "Mid MA")
ma2Timeframe = input.timeframe(defval = "60", title = "Timeframe", group = "Mid MA")
color ma2Color = input.color(defval = color.new(color.orange, 0), title = "Color", group = "Mid MA")
bool ma2WaitTF = input.bool(defval = true, title = "Wait TF Close", tooltip = "This will wait until the selected timeframe closes to plot the MA, which will remove the gaps", group = "Mid MA")

// Slow MA
bool ma3Switch = input.bool(defval = false, title = "Plot", group = "Slow MA")
bool ma3ShowTrendData = input.bool(defval = true, title = "Show Trend Data", tooltip = "This will show the trend data on the 'Trend Detector' table", group = "Slow MA")
string ma3Type = input.string(defval = "EMA", title = "Type", options = ["SMA", "EMA", "RMA", "WMA", "VWMA"], group = "Slow MA")
int ma3Length = input.int(defval = 20, title = "Length", group = "Slow MA")
ma3Source = input.source(defval = close, title = "Source", group = "Slow MA")
ma3Timeframe = input.timeframe(defval = "240", title = "Timeframe", group = "Slow MA")
color ma3Color = input.color(defval = color.new(color.yellow, 0), title = "Color", group = "Slow MA")
bool ma3WaitTF = input.bool(defval = true, title = "Wait TF Close", tooltip = "This will wait until the selected timeframe closes to plot the MA, which will remove the gaps", group = "Slow MA")
//#endregion


//#region ------ FUNCTIONS
// @function Converts a string timeframe into a user-friendly format.
// @param timeframe (string) The timeframe to convert.
// @returns (string) The formatted timeframe.
method formatTF(string timeframe) =>
    if str.tonumber(timeframe) < 60
        timeframe + "M"  // Minutes
    else if str.tonumber(timeframe) >= 60
        str.tostring(str.tonumber(timeframe) / 60) + "H"  // Hours
    else
        timeframe
//#endregion


//#region ----- CALCULATIONS
float ma1Exp = ma1Type == "SMA" ? ta.sma(ma1Source, ma1Length) : ma1Type == "EMA" ? ta.ema(ma1Source, ma1Length) : ma1Type == "RMA" ? ta.rma(ma1Source, ma1Length) : ma1Type == "WMA" ? ta.wma(ma1Source, ma1Length) : ta.vwma(ma1Source, ma1Length)
float ma2Exp = ma2Type == "SMA" ? ta.sma(ma2Source, ma2Length) : ma2Type == "EMA" ? ta.ema(ma2Source, ma2Length) : ma2Type == "RMA" ? ta.rma(ma2Source, ma2Length) : ma2Type == "WMA" ? ta.wma(ma2Source, ma2Length) : ta.vwma(ma2Source, ma2Length)
float ma3Exp = ma3Type == "SMA" ? ta.sma(ma3Source, ma3Length) : ma3Type == "EMA" ? ta.ema(ma3Source, ma3Length) : ma3Type == "RMA" ? ta.rma(ma3Source, ma3Length) : ma3Type == "WMA" ? ta.wma(ma3Source, ma3Length) : ta.vwma(ma3Source, ma3Length)

float ma1Value = request.security(syminfo.tickerid, ma1Timeframe, ma1Exp, ma1WaitTF ? barmerge.gaps_on : barmerge.gaps_off)
float ma2Value = request.security(syminfo.tickerid, ma2Timeframe, ma2Exp, ma2WaitTF ? barmerge.gaps_on : barmerge.gaps_off)
float ma3Value = request.security(syminfo.tickerid, ma3Timeframe, ma3Exp, ma3WaitTF ? barmerge.gaps_on : barmerge.gaps_off)
//#endregion


//#region ------ VARIABLES
float ma1NonNaNValue = fixnan(ma1Value)
float ma2NonNaNValue = fixnan(ma2Value)
float ma3NonNaNValue = fixnan(ma3Value)

array<bool> maShowTrendDataSwitches = array.new_bool()
maShowTrendDataSwitches.push(ma1ShowTrendData), maShowTrendDataSwitches.push(ma2ShowTrendData), maShowTrendDataSwitches.push(ma3ShowTrendData)

int trueCounter = 0
for value in maShowTrendDataSwitches
    if value
        trueCounter += 1

var table trendDetector = table.new(trendDetectorPosition, 2, trueCounter + 1)
//#endregion


//#region ------ PLOTTING
plot(ma1Switch ? ma1Value : na, color = ma1Color, display = ma1Switch ? display.all : display.none)
plot(ma2Switch ? ma2Value : na, color = ma2Color, display = ma2Switch ? display.all : display.none)
plot(ma3Switch ? ma3Value : na, color = ma3Color, display = ma3Switch ? display.all : display.none)

if trueCounter > 0 and trendDetectorSwitch
    trendDetector.set_frame_color(trendDetectorFrameColor)
    trendDetector.set_frame_width(trendDetectorFrameWidth)
    trendDetector.set_border_color(trendDetectorBorderColor)
    trendDetector.set_border_width(trendDetectorBorderWidth)
    trendDetector.cell_set_text(0, 0, "Term")
    trendDetector.cell_set_text_color(0, 0, trendDetectorTextColor)
    trendDetector.cell_set_text(1, 0, "Trend")
    trendDetector.cell_set_text_color(1, 0, trendDetectorTextColor)

    int i = 1
    if ma1ShowTrendData
        trendDetector.cell_set_text(0, i, "Short\n" + ma1Timeframe.formatTF())
        trendDetector.cell_set_text_color(0, i, trendDetectorTextColor)
        trendDetector.cell_set_text(1, i, close >= ma1NonNaNValue ? "Up ▲" : "Down ▼")
        trendDetector.cell_set_text_color(1, i, close >= ma1NonNaNValue ? color.new(color.green, 60) : color.new(color.red, 60))
        i += 1

    if ma2ShowTrendData
        trendDetector.cell_set_text(0, i, "Mid\n" + ma2Timeframe.formatTF())
        trendDetector.cell_set_text_color(0, i, trendDetectorTextColor)
        trendDetector.cell_set_text(1, i, close >= ma2NonNaNValue ? "Up ▲" : "Down ▼")
        trendDetector.cell_set_text_color(1, i, close >= ma2NonNaNValue ? color.new(color.green, 60) : color.new(color.red, 60))
        i += 1

    if ma3ShowTrendData
        trendDetector.cell_set_text(0, i, "Long\n" + ma3Timeframe.formatTF())
        trendDetector.cell_set_text_color(0, i, trendDetectorTextColor)
        trendDetector.cell_set_text(1, i, close >= ma3NonNaNValue ? "Up ▲" : "Down ▼")
        trendDetector.cell_set_text_color(1, i, close >= ma3NonNaNValue ? color.new(color.green, 60) : color.new(color.red, 60))
//#endregion

Overview

A table lists whether each selected average is trending up or down. An option waits for the chosen timeframe to close before plotting, which removes gaps. Table position, frame, border and text colors are adjustable. It draws on the price chart.

Details

Published by

MarcosACH on GitHub

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Trend, Moving averages, Screeners & dashboards

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.

Topics

pinescriptpinescript-algorithmspinescript-indicatorstechnical-analysistechnical-analysis-indicatorstrading-algorithmstrading-indicatorstradingvewtradingview

Settings

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

SettingTypeDefaultVariable
PlotbooltruetrendDetectorSwitch
Positionstringposition.top_righttrendDetectorPosition
Frame Colorcolorcolor.new(color.white, 80)trendDetectorFrameColor
Frame Widthint2trendDetectorFrameWidth
Border Colorcolorcolor.new(color.white, 80)trendDetectorBorderColor
Border Widthint2trendDetectorBorderWidth
Text Colorcolorcolor.new(color.white, 60)trendDetectorTextColor
Plotboolfalsema1Switch
Show Trend Databooltruema1ShowTrendData
TypestringEMAma1Type
Lengthint20ma1Length
Sourcesourceclosema1Source
Show all 31 settings
SettingTypeDefaultVariable
Timeframetimeframe15ma1Timeframe
Colorcolorcolor.new(color.red, 0)ma1Color
Wait TF Closebooltruema1WaitTF
Plotboolfalsema2Switch
Show Trend Databooltruema2ShowTrendData
TypestringEMAma2Type
Lengthint20ma2Length
Sourcesourceclosema2Source
Timeframetimeframe60ma2Timeframe
Colorcolorcolor.new(color.orange, 0)ma2Color
Wait TF Closebooltruema2WaitTF
Plotboolfalsema3Switch
Show Trend Databooltruema3ShowTrendData
TypestringEMAma3Type
Lengthint20ma3Length
Sourcesourceclosema3Source
Timeframetimeframe240ma3Timeframe
Colorcolorcolor.new(color.yellow, 0)ma3Color
Wait TF Closebooltruema3WaitTF
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

218 stars

TradersPost Trading Checklist is an overlay indicator that evaluates a suite of popular technical conditions for long and short trade setups. It checks session-anchored VWAP, fast and slow EMA alignments and crosses, 200 SMA, RSI overbought or oversold conditi

Indicator v6 Screeners & dashboardsTrend
Updated 3 months ago · None stated GitHub repository of TradersPost Trading Checklist
92 stars

This indicator plots five moving averages of a chosen source on the price chart, defaulting to EMA lengths 10, 20, 30, 40 and 50. The user can switch the average type to SMA, WMA or HMA and recolor each line. Wide translucent glows sit under the lines, and sof

Indicator v5 TrendMoving averages
Updated 2 months ago · Custom licence GitHub repository of Moon Dev Rainbow MA Ribbon 10-50

EMA Ribbon Overlay

by iamrichardD
57 stars

Calculates exponential moving averages for fast, pivot, and slow lengths along with optional 50, 100, and 200 simple moving average context lines. It visualizes bullish and bearish ribbon fills on the main price chart alongside MACD calculations to generate ea

Indicator v6 TrendMoving averages
Updated 1 year ago · Apache-2.0 GitHub repository of EMA Ribbon Overlay
53 stars

This indicator analyzes historical candle direction and price range over a configurable lookback window to gauge market trend. It categorizes trend direction using candle count, price range sum, or both. Signals are filtered using a Simple Moving Average to en

Indicator v6 TrendOscillators & momentum
Updated 2 months ago · MPL-2.0 GitHub repository of Candle Counter + MA + RSI + Mode
43 stars

This indicator evaluates market momentum and risk by calculating trend metrics and volatility stats, displaying them in an on-chart table pane. It measures short-term and long-term trend strength via two MACD configurations built on three customizable EMA leng

Indicator v5 TrendVolatility & bands
Updated 1 year ago · None stated GitHub repository of Trend Strength & Volatility

Market Monitor - 15m

by chris-c-thomas
14 stars

Market Monitor - 15m is a directional bias chart overlay designed for 15-minute intraday chart analysis. It calculates a triple exponential moving average ribbon with EMA cloud shading, session VWAP with multiplier bands, prior day levels, a 30-minute opening

Indicator v6 TrendMoving averages
Updated 6 months ago · MIT GitHub repository of Market Monitor - 15m

More indicators

Trade Your Charts With a Regulated Broker

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

Compare brokers