Indicator · Pine Script v5 · Own pane

MA Based Trend

by ryangaraygay

The script calculates three moving averages with configurable types, lengths, and price sources.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code58
Settings13inputs
LicenceMIT
Last update1 year ago10 Apr 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

58 lines of Pine Script v5 from ma-based-trend.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

ma-based-trend.pine
//@version=5
indicator("MA Based Trend", shorttitle = "MA_TREND", overlay=false)

getAverageType(averageType, averageSource, averageLength) =>
    switch str.upper(averageType)
        "SMA"  => ta.sma(averageSource, averageLength)
        "EMA"  => ta.ema(averageSource, averageLength)
        "VWMA" => ta.vwma(averageSource, averageLength)
        => runtime.error("Moving average type '" + averageType + "' not found!"), na

a_type = input.string("SMA", "high timeframe", ["SMA","EMA","VWMA"], "", "1", "")
b_type = input.string("EMA", "med timeframe", ["SMA","EMA","VWMA"], "", "2", "")
c_type = input.string("EMA", "low timeframe", ["SMA","EMA","VWMA"], "", "3", "")

a_length = input.int(defval = 105, title = "length", group ="", inline = "1")
b_length = input.int(defval = 45, title = "length", group ="", inline = "2")
c_length = input.int(defval = 9, title = "length", group ="", inline = "3")

a_series = input.string("close", "source", ["open","close","hlc3"], "", "1", "")
b_series = input.string("close", "source", ["open","close","hlc3"], "", "2", "")
c_series = input.string("close", "source", ["open","close","hlc3"], "", "3", "")

getSeries(seriesName) =>
    switch str.upper(seriesName)
        "CLOSE"  => close
        "OPEN"  => open
        "HLC3"  => hlc3
        => runtime.error("Series '" + seriesName + "' not found!"), na

a = getAverageType(a_type, getSeries(a_series), a_length)
b = getAverageType(b_type, getSeries(b_series), b_length)
c = getAverageType(c_type, getSeries(c_series), c_length)

state_one = c > b and b > a
state_two = b > c and c > a
state_three = a > c and c > b
state_four = a > b and b > c

var color_maxup = input.color(color.new(color.green, 30), "bullish")
var color_up = input.color(color.new(color.blue, 30), "bullish-neutral")
var color_maxdown = input.color(color.new(color.red, 30), "bearish")
var color_down = input.color(color.new(color.orange, 30), "bearish-neutral")
var color_undefined = color.gray

volume_color = if state_one
    color_maxup
else if state_two
    color_up
else if state_three
    color_down
else if state_four
    color_maxdown
else
    color_undefined

plot(1, "Moving Average Based Trend", volume_color, style = plot.style_columns)

alertcondition(volume_color != volume_color[1], "MA-based Trend Changed Alert", "Moving Average based Trend has changed.")

Overview

MA Based Trend evaluates trend alignment by comparing the relative order of fast, medium, and slow averages across four market states. In a separate pane, it plots color-coded columns indicating bullish, neutral, or bearish conditions. An alert triggers when the trend state shifts. The indicator does not draw on the price chart and includes no execution logic.

Details

Published by

ryangaraygay on GitHub

Draws

Separate pane below the 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
high timeframestringSMAa_type
med timeframestringEMAb_type
low timeframestringEMAc_type
lengthint105a_length
lengthint45b_length
lengthint9c_length
sourcestringclosea_series
sourcestringcloseb_series
sourcestringclosec_series
bullishcolorcolor.new(color.green, 30)color_maxup
bullish-neutralcolorcolor.new(color.blue, 30)color_up
bearishcolorcolor.new(color.red, 30)color_maxdown
Show all 13 settings
SettingTypeDefaultVariable
bearish-neutralcolorcolor.new(color.orange, 30)color_down
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

Beep Boop

by magic8bot
409 stars

This indicator calculates a modified MACD oscillator and signal line alongside a trend EMA filter, plotting the output as a stylized column histogram in a separate pane. Instead of plotting true MACD distance, it quantifies positive and negative histogram mome

Indicator v4 Oscillators & momentumTrend
Updated 3 years ago · MIT GitHub repository of Beep Boop
228 stars

Multi-Timeframe Analysis Suite calculates trend direction and momentum conditions across five user-selected timeframes using moving averages, RSI, and Stochastic oscillators. It evaluates moving average slope and oscillator levels to compute trend alignment, m

Indicator v6 Screeners & dashboardsTrend
Updated 3 months ago · None stated GitHub repository of Multi-Timeframe Analysis Suite

KamaFama

by Mastaaa1987
141 stars

KamaFama is an indicator that translates a Freqtrade trading strategy into Pine Script for visualization. It combines Kaufman Adaptive Moving Average (KAMA), MAMA/FAMA calculations derived via Hilbert Transform, Williams %R, and correlation trend filters to ev

Indicator v6 Moving averagesOscillators & momentum
Updated 6 weeks ago · MIT GitHub repository of KamaFama

MultiIndicator

by capissimo
114 stars

MultiIndicator combines multiple technical indicators into a single separate pane to save chart space. It calculates and displays market regime detection based on ordinary least squares regression, a Holt exponential smoothing crossover, normalized RSI, ADX, a

Indicator v4 Oscillators & momentumTrend
Updated 1 year ago · None stated GitHub repository of MultiIndicator
99 stars

Calculates the Archer Moving Averages Trends (AMAT) indicator by comparing fast and slow Exponential Moving Averages (EMAs) alongside their slope directions. A bullish trend (+1) is identified when the fast EMA exceeds the slow EMA with both rising, while a be

Indicator v6 TrendMoving averages
Updated 4 weeks ago · Apache-2.0 GitHub repository of Archer Moving Averages Trends (AMAT)

Colored fill

by be-thomas
65 stars

This indicator plots two simple moving averages of close, one over five bars and one over twenty bars, in a separate pane. It shades the area between the two plots green when the shorter average is above the longer average and red when it is below. There are n

Indicator v4 Moving averagesTrend
Updated 6 days ago · GPL-3.0 GitHub repository of Colored fill

More Trend 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