Indicator · Pine Script v4 · On the chart

Price vs MA

by be-thomas

This indicator builds a table of simple moving averages and places it on the price chart.

View on GitHub

GitHub stars65on the repository
Pine versionv4older, still runs
Lines of code41
Settings8inputs
LicenceGPL-3.0
Last update4 days ago24 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 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

41 lines of Pine Script v4 from examples/v4/tradingview-docs/v4-essential-tables-03.pine, published under GPL-3.0. Keep the author's credit and licence lines when you reuse it.

v4-essential-tables-03.pine
//@version=4
study("Price vs MA", "", true)

var string GP1 = "Moving averages"
int     i_masQty    = input(20, "Quantity", minval = 1, maxval = 40, group = GP1, tooltip = "1-40")
int     i_masStart  = input(20, "Periods begin at", minval = 2, maxval = 200, group = GP1, tooltip = "2-200")
int     i_masStep   = input(20, "Periods increase by", minval = 1, maxval = 100, group = GP1, tooltip = "1-100")

var string GP2 = "Display"
string  i_tableYpos = input("top", "Panel position", inline = "11", options = ["top", "middle", "bottom"], group = GP2)
string  i_tableXpos = input("right", "", inline = "11", options = ["left", "center", "right"], group = GP2)
color   i_c_bull    = input(color.new(color.green, 30), "Bull", inline = "12", group = GP2)
color   i_c_bear    = input(color.new(color.red, 30), "Bear", inline = "12", group = GP2)
color   i_c_neutral = input(color.new(color.gray, 30), "Neutral", inline = "12", group = GP2)

// ————— Produces a string format usable with `tostring()` to restrict precision to ticks.
f_tickFormat() =>
    string _s = tostring(syminfo.mintick)
    _s := str.replace_all(_s, "25", "00")
    _s := str.replace_all(_s, "5",  "0")
    _s := str.replace_all(_s, "1",  "0")

var table panel = table.new(i_tableYpos + "_" + i_tableXpos, 2, i_masQty + 1)
if barstate.islast
    // Table header.
    table.cell(panel, 0, 0, "MA", bgcolor = i_c_neutral)
    table.cell(panel, 1, 0, "Value", bgcolor = i_c_neutral)

int period = i_masStart
for _i = 1 to i_masQty
    // ————— Call MAs on each bar.
    float _ma = sma(close, period)
    // ————— Only execute table code on last bar.
    if barstate.islast
        // Period in left column.
        table.cell(panel, 0, _i, tostring(period), bgcolor = i_c_neutral)
        // If MA is between the open and close, use neutral color. If close is lower/higher than MA, use bull/bear color.
        _c_bg = close > _ma ? open < _ma ? i_c_neutral : i_c_bull : open > _ma ? i_c_neutral : i_c_bear
        // MA value in right column.
        table.cell(panel, 1, _i, tostring(_ma, f_tickFormat()), text_color = color.black, bgcolor = _c_bg)
    period += i_masStep

Overview

Price vs MA calculates a user-chosen number of SMAs whose periods start at a base length and increase by a fixed step. On the last bar it lists each period and its SMA value, coloring the value bull, bear, or neutral according to whether price is above, below, or straddling the average. Quantity, starting period, step, panel position, and colors are adjustable. It does not plot lines or issue trade signals.

Details

Published by

be-thomas on GitHub

Type

Indicator, Pine Script v4

Draws

On the price chart

Category

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

algorithmic-tradingbacktestingcompilerfintechjavascriptpinescripttechnical-analysistradingview

Settings

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

SettingTypeDefaultVariable
Quantityint20i_masQty
Periods begin atint20i_masStart
Periods increase byint20i_masStep
Panel positionstringtopi_tableYpos
i_tableXposstringrighti_tableXpos
Bullcolorcolor.new(color.green, 30)i_c_bull
Bearcolorcolor.new(color.red, 30)i_c_bear
Neutralcolorcolor.new(color.gray, 30)i_c_neutral
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

This overlay indicator plots a Supertrend from an ATR length and factor, four exponential moving averages, and a selectable VWAP or TWAP taken from a chosen timeframe on the price chart. It also shows MACD and stochastic with adjustable lengths, smoothing, ver

Indicator v5 Oscillators & momentumMoving averages
Updated 1 year ago · MPL-2.0 GitHub repository of Momentum Setup: Vijay Thakare Option Buying Scalping Setup
257 stars

This indicator plots a 200-period weighted moving average (WMA) on the main chart and monitors price deviations across multiple timeframes. It retrieves daily, 15-minute, 5-minute, and 2-minute price data to track distance from the 200 WMA relative to key sess

Indicator v5 Moving averagesSessions & time Repaints
Updated 6 weeks ago · None stated GitHub repository of Donald SPX - Options Trading
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

8zz反指標v2

by hansai-art
121 stars

The 8zz Contrarian Indicator plots reverse sentiment trading signals directly on the price chart based on tracked retail post activity. It generates bullish signals during retail panic and bearish signals during market euphoria. The indicator features adjustab

Indicator v6 Moving averagesOscillators & momentum
Updated 3 months ago · None stated GitHub repository of 8zz反指標v2
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 8 weeks 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

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