Pine has ta.mom for the Momentum indicator, so it’s a one-liner — with a couple of
useful additions. See the explainer for the concept.
What you’ll need
- A TradingView account (free tier is fine)
- The Pine Editor
The source
//@version=5
indicator("AlgoGen Momentum", shorttitle="AG MOM", overlay=false)
length = input.int(10, "Length", minval=1)
smooth = input.int(1, "Smoothing (1 = raw)")
momVal = ta.mom(close, length) // close - close[length]
momLine = ta.sma(momVal, smooth)
col = momLine >= 0 ? #0ca30c : #d03b3b
plot(momLine, "Momentum", style=plot.style_columns, color=col)
hline(0, "Zero", color=color.gray)
alertcondition(ta.crossover(momLine, 0), "MOM up", "Momentum crossed above zero")
alertcondition(ta.crossunder(momLine, 0), "MOM down", "Momentum crossed below zero")
ta.mom(close, length) is the difference form (close − close[length]), oscillating
around zero. Colouring the histogram by sign makes the momentum regime obvious, and a
short smoothing tames the noise — matching the output chart.
The ratio form
If you want the 100-centered ratio version instead:
momRatio = 100 * close / close[length]
plot(momRatio, "MOM ratio", color=#2a78d6)
hline(100, "Base", color=color.gray)
A trend-filtered strategy
Momentum zero-crosses whipsaw in ranges, so gate them with a trend filter:
//@version=5
strategy("AlgoGen Momentum System", overlay=true)
momVal = ta.mom(close, 10)
uptrend = close > ta.sma(close, 200)
if ta.crossover(momVal, 0) and uptrend
strategy.entry("Long", strategy.long)
if ta.crossunder(momVal, 0)
strategy.close("Long")
Only take the up-cross while price is above its 200-SMA — the Strategy Tester will show how much the filter cuts the range-bound noise.
Gotchas
ta.momis the difference form (around 0); the ratio form (around 100) you build yourself. Don’t mix the two scales.- No fixed levels. Momentum is unbounded; judge extremes relative to recent range, not a fixed threshold.
ta.momvsta.roc.ta.momis absolute points;ta.rocis percent — pick the one that matches how you’re comparing.
Same indicator elsewhere: Python, MQL5, EasyLanguage, NinjaScript. Then test a Momentum rule in AlgoGen.
This post is educational, not financial advice. Indicators describe the past; they don’t predict the future. Backtest anything before you risk real money on it.
Sources and further reading
- Pine Script built-ins (TradingView)
