Indicator · Pine Script v6 · Own pane

MA Spread Histogram

by piecioshka

MA Spread Histogram calculates the difference between two configurable moving averages and plots the result as a color-coded column histogram in a separate pane.

View on GitHub

GitHub stars2on the repository
Pine versionv6current
Lines of code63
Settings6inputs
LicenceMIT
Last update12 days ago18 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

63 lines of Pine Script v6 from indicators/oscillators/ma-spread-histogram/ma-spread-histogram.pine, published under MIT. Keep the author's credit and licence lines when you reuse it.

ma-spread-histogram.pine
// This Pine Script® code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © Piotr Kowalski "piecioshka"
// Docs (EN): https://github.com/piecioshka/tradingview-pine-scripts/blob/main/indicators/oscillators/ma-spread-histogram/ma-spread-histogram.md
// Docs (PL): https://github.com/piecioshka/tradingview-pine-scripts/blob/main/indicators/oscillators/ma-spread-histogram/ma-spread-histogram.pl.md

//@version=6
indicator('MA Spread Histogram', overlay=false, max_labels_count=500)

aLen = input(50, 'Length', group='Short')
aType = input.string(title='Type', options=['SMA', 'EMA', 'WMA', 'VWMA', 'RMA'], group='Short', defval='SMA')
aSeries = switch aType
    'SMA' => ta.sma(close, aLen)
    'EMA' => ta.ema(close, aLen)
    'WMA' => ta.wma(close, aLen)
    'VWMA' => ta.vwma(close, aLen)
    'RMA' => ta.rma(close, aLen)
    => na

bLen = input(200, 'Length', group='Long')
bType = input.string(title='Type', options=['SMA', 'EMA', 'WMA', 'VWMA', 'RMA'], group='Long', defval='SMA')
bSeries = switch bType
    'SMA' => ta.sma(close, bLen)
    'EMA' => ta.ema(close, bLen)
    'WMA' => ta.wma(close, bLen)
    'VWMA' => ta.vwma(close, bLen)
    'RMA' => ta.rma(close, bLen)
    => na

gA = 'Appearance'
shapeName = input.string('Triangle', 'Marker shape', options=['Triangle', 'Arrow', 'Label arrow', 'Circle', 'Diamond'], group=gA, tooltip='Triangle - solid triangle.\nArrow - small thin arrow.\nLabel arrow - arrow-shaped label bubble.\nCircle / Diamond - neutral dot markers.')
sizeName  = input.string('Small', 'Marker size', options=['Tiny', 'Small', 'Normal', 'Large'], group=gA)

// Markers as labels - the shape is picked from an input, which plotshape() cannot do (its style must be a constant).
upStyle    = shapeName == 'Arrow' ? label.style_arrowup : shapeName == 'Label arrow' ? label.style_label_up : shapeName == 'Circle' ? label.style_circle : shapeName == 'Diamond' ? label.style_diamond : label.style_triangleup
downStyle  = shapeName == 'Arrow' ? label.style_arrowdown : shapeName == 'Label arrow' ? label.style_label_down : shapeName == 'Circle' ? label.style_circle : shapeName == 'Diamond' ? label.style_diamond : label.style_triangledown
markerSize = sizeName == 'Tiny' ? size.tiny : sizeName == 'Normal' ? size.normal : sizeName == 'Large' ? size.large : size.small

histogram = aSeries - bSeries
histColor = histogram >= 0 ? histogram > histogram[1] ? color.new(color.green, 50) : color.new(color.green, 80) : histogram < histogram[1] ? color.new(color.red, 50) : color.new(color.red, 80)

plot(histogram, 'Histogram', color=histColor, linewidth=2, style=plot.style_columns)
hline(0, 'Zero', color=color.gray, linestyle=hline.style_solid)

crossUp = ta.crossover(aSeries, bSeries)
crossDn = ta.crossunder(aSeries, bSeries)

// Short crosses long UP (histogram goes above zero) -> bullish impulse -> BUY OPEN.
// Marker below the candle (as in Accumulation / Distribution Density).
if (crossUp)
    line.new(bar_index, histogram, bar_index, histogram, color=color.new(color.green, 70), style=line.style_dashed, extend=extend.left)
    line.new(bar_index, high, bar_index, low, color=color.new(color.green, 70), style=line.style_dashed, extend=extend.right, force_overlay=true)
    label.new(bar_index, na, '', yloc=yloc.belowbar, style=upStyle, color=color.green, size=markerSize, force_overlay=true)

// Short crosses long DOWN (histogram goes below zero) -> bearish impulse -> BUY CLOSE.
// Marker above the candle.
if (crossDn)
    line.new(bar_index, histogram, bar_index, histogram, color=color.new(color.red, 70), style=line.style_dashed, extend=extend.left)
    line.new(bar_index, high, bar_index, low, color=color.new(color.red, 70), style=line.style_dashed, extend=extend.right, force_overlay=true)
    label.new(bar_index, na, '', yloc=yloc.abovebar, style=downStyle, color=color.red, size=markerSize, force_overlay=true)

// --- Alerts ---
alertcondition(crossUp, 'MA spread: cross up',   'The histogram crossed zero up - the short MA crossed the long one up (bullish impulse)')
alertcondition(crossDn, 'MA spread: cross down', 'The histogram crossed zero down - the short MA crossed the long one down (bearish impulse)')

Overview

The histogram bars highlight expanding or contracting momentum across the zero line. When the short moving average crosses above or below the long moving average, the indicator places visual marker shapes and vertical reference lines directly on the price chart using forced overlays. Users can select lengths, MA types from SMA, EMA, WMA, VWMA, and RMA, as well as marker styles and sizes.

Details

Published by

piecioshka 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.

Topics

pinescriptpinescript-indicatorspinescript-strategiestradingtradingview

Settings

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

SettingTypeDefaultVariable
Lengthint50aLen
TypestringSMAaType
Lengthint200bLen
TypestringSMAbType
Marker shapestringTriangleshapeName
Marker sizestringSmallsizeName
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

947 stars

The Derivative Oscillator calculates a double-exponentially smoothed Relative Strength Index (RSI) and subtracts its Simple Moving Average (SMA) to form the main oscillator line. A secondary signal line is plotted using an SMA of the main oscillator values. Th

Indicator v4 Oscillators & momentumMoving averages
Updated 12 days ago · GPL-3.0 GitHub repository of Derivative Oscillator

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

MACD

by Alorse
330 stars

This indicator calculates and plots the standard Moving Average Convergence Divergence oscillator in a separate pane. It computes the difference between a fast and slow moving average, derives a signal line through smoothing, and plots a histogram representing

Indicator v4 Oscillators & momentumMoving averages
Updated 2 months ago · None stated GitHub repository of MACD
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

MACD-V

by harryguiacorn
220 stars

MACD-V is a volatility-normalized momentum indicator based on research by Alex Spiroglou. It calculates the standard MACD histogram and normalizes it by dividing by the Average True Range (ATR). Plotted in a separate pane as a colored histogram, it includes us

Indicator v5 Oscillators & momentumVolatility & bands
Updated 1 year ago · None stated GitHub repository of MACD-V

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

More Oscillators & momentum 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