Indicator · Pine Script v5 · On the chart

MA Based Trend BarColor

by ryangaraygay

MA Based Trend BarColor plots three configurable moving averages directly on the price chart.

View on GitHub

GitHub stars2on the repository
Pine versionv5runs as is
Lines of code94
Settings16inputs
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

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

ma-based-barcolor.pine
//@version=5
indicator("MA Based Trend BarColor", shorttitle = "MA_TREND_BARCOLOR", overlay=true)

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)

ht_color = input.color(color.red, "Color", inline = "1")
mt_color = input.color(color.orange, "Color", inline = "2")
lt_color = input.color(color.green, "Color", inline = "3")

p1 = plot(a, "slow MA", color=ht_color)
p2 = plot(b, "mid MA", color=mt_color)
p3 = plot(c, "fast MA", color=lt_color)

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, 0), "bullish", group = "Trend-based Bar Color")
var color_up = input.color(color.new(color.blue, 0), "bullish-neutral", group = "Trend-based Bar Color")
var color_maxdown = input.color(color.new(color.red, 0), "bearish", group = "Trend-based Bar Color")
var color_down = input.color(color.new(color.orange, 0), "bearish-neutral", group = "Trend-based Bar Color")
var color_undefined = color.new(color.gray, 30)

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

bodyColor = close > open ? na : volume_color

// barcolor(volume_color)
// plotcandle(open, high, low, close, color=bodyColor, wickcolor = volume_color, bordercolor = volume_color)

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

band_color = c > b ? color.new(color.green, 95) : color.new(color.red, 95)
fill(p3, p2, band_color)
// fill(p3, p2, color.new(color.gray, 90))

// var string GP2 = "MA Slope Summary"
// string  tableYposInput = input.string("top", "Panel position", inline = "11", options = ["top", "middle", "bottom"], group = GP2)
// string  tableXposInput = input.string("right", "", inline = "11", options = ["left", "center", "right"], group = GP2)
// slope_length = input.int(5, title = "Lookback", tooltip = "Lookback when computing MA slopes")

// slow_slope = TK.slope(a, slope_length)
// mid_slope = TK.slope(b, slope_length)
// fast_slope = TK.slope(c, slope_length)

// var table panel = table.new(tableYposInput + "_" + tableXposInput, 2, 5)//, border_color = color.black, border_width = 1)

// if barstate.islast
//     table.cell(panel, 0, 1, text = "", text_size = size.small)
//     table.cell(panel, 1, 1, "slope", text_color = color_undefined, text_size = size.small, text_halign = text.align_right)
//     table.cell(panel, 0, 4, text = a_type + "-" + str.tostring(a_length), text_size = size.small)
//     table.cell(panel, 1, 4, str.tostring(slow_slope, "0.00"), text_color = slow_slope > 0 ? color_up : color_down, text_size = size.large, text_halign = text.align_right)
//     table.cell(panel, 0, 3, text = b_type + "-" + str.tostring(b_length), text_size = size.small)
//     table.cell(panel, 1, 3, str.tostring(mid_slope, "0.00"), text_color = mid_slope > 0 ? color_up : color_down, text_size = size.large, text_halign = text.align_right)
//     table.cell(panel, 0, 2, text = c_type + "-" + str.tostring(c_length), text_size = size.small)
//     table.cell(panel, 1, 2, str.tostring(fast_slope, "0.00"), text_color = fast_slope > 0 ? color_up : color_down, text_size = size.large, text_halign = text.align_right)

Overview

Users can customize the calculation type (SMA, EMA, or VWMA), source price, and lookback length for fast, medium, and slow periods. The script evaluates the stacked alignment of the three lines to classify market conditions into four trend states: bullish, bullish-neutral, bearish-neutral, and bearish. It also draws a dynamic colored fill between the fast and medium averages based on their crossover relationship.

Details

Published by

ryangaraygay on GitHub

Type

Indicator, Pine Script v5

Draws

On the price chart

Category

Trend, Moving averages

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
Colorcolorcolor.redht_color
Colorcolorcolor.orangemt_color
Colorcolorcolor.greenlt_color
Show all 16 settings
SettingTypeDefaultVariable
bullishcolorcolor.new(color.green, 0)color_maxup
bullish-neutralcolorcolor.new(color.blue, 0)color_up
bearishcolorcolor.new(color.red, 0)color_maxdown
bearish-neutralcolorcolor.new(color.orange, 0)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

948 stars

The Adaptive Laguerre Filter calculates a smoothed, dynamically adjusted trendline plotted directly on the main price chart. It dynamically adjusts its damping factor (alpha) based on recent price range variations and median percentile calculations over a spec

Indicator v4 TrendMoving averages
Updated 10 days ago · GPL-3.0 GitHub repository of Adaptive Laguerre Filter
330 stars

This script plots three moving averages on the price chart: a fast EMA or SMA with 10 periods, a slow EMA or SMA with 20 periods, and a trend EMA or SMA with 100 periods. It signals trend changes by drawing a white cross when the fast moving average crosses ab

Indicator v4 TrendMoving averages
Updated 2 months ago · None stated GitHub repository of 3 EMA/SMA + Cross [Alorse]
318 stars

This indicator plots four customizable moving averages directly on the main price chart using default lengths of 3, 8, 21, and 50 periods. It fills the space between adjacent averages in green or maroon depending on trend alignment. Additionally, it computes B

Indicator v5 Moving averagesTrend
Updated 2 months ago · MIT GitHub repository of Multiple Moving Averages

NVDA Trend Ribbon

by longsurf-ai
277 stars

This indicator plots three exponential moving averages and ATR volatility envelopes on the price chart. It compares the fast and slow EMAs to shade a trend ribbon and marks confirmed upward and downward crossovers with triangle labels. Settings change the thre

Indicator v6 TrendMoving averages
Updated yesterday · MIT GitHub repository of NVDA Trend Ribbon
257 stars

Donald SPX - Options Trading is an overlay indicator designed for intraday SPX analysis. It plots a 200-period Weighted Moving Average (WMA) on the price chart while evaluating multi-timeframe market structure across Daily, 15-minute, 5-minute, and 2-minute ba

Indicator v5 Moving averagesTrend Repaints
Updated 6 weeks ago · None stated GitHub repository of Donald SPX - Options Trading

60K均線

by TaichiS
219 stars

This multi-timeframe indicator calculates and plots 20-period and 60-period simple moving averages from the 60-minute timeframe directly onto the price chart. The 20-period average displays as a green line, while the 60-period average appears in blue. It serve

Indicator v4 Moving averagesTrend Repaints
Updated 5 years ago · None stated GitHub repository of 60K均線

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