Strategy · Pine Script v5 · On the chart

Pivot Trendlines

by g-moe· code credited to goodgains

This strategy overlays two SMA lines with a cloud fill to show trend direction.

View on GitHub

GitHub stars3on the repository
Pine versionv5runs as is
Lines of code98
Settings12inputs
LicenceNone statedfile: MPL-2.0
Last update5 months ago27 May 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 strategy, select all of the template text and paste. Press Save and give it a name.
  4. 4Add it to the chartPress Add to chart. The trades and results appear in the Strategy Tester tab; adjust the settings with the gear icon.

Source Code

98 lines of Pine Script v5 from Tradingview/pivot-trendlines-v8.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

pivot-trendlines-v8.pine
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © goodgains


//@version=5
strategy("Pivot Trendlines", overlay=true, margin_long=100, margin_short=100, default_qty_value = 100)

// @keywords: sma-cloud-trend, bollinger-band-touch, rsi-extreme-entry, pivot-swing-stops, long-short-strategy, ma-trend-filter, bb-upper-lower, swing-length-stops, valuewhen-pivot, barcolor-position, strategy-entry-exit, mean-reversion-rsi, band-reversal-signals, dual-sma-overlay, position-stop-tracking, alt-lowest-highest-stop, trend-ma-cloud, oversold-overbought-rsi, pivot-stop-placement, v8-pivot-strategy


// moving averages
input_ma1length = input.int(20, title = 'MA 1', group = 'MA Settings', inline = '1')
input_ma1price = input.source(close, title = '', group = 'MA Settings', inline = '1')

input_ma2length = input.int(50, title = 'MA 2', group = 'MA Settings', inline = '2')
input_ma2price = input.source(close, title = '', group = 'MA Settings', inline = '2')

ma1_value = ta.sma(input_ma1price, input_ma1length)
ma2_value = ta.sma(input_ma2price, input_ma2length)

p_ma1 = plot(ma1_value, title = 'MA 1', color = color.orange) 
p_ma2 = plot(ma2_value, title = 'MA 2', color = color.yellow) 

ma_trend = ma1_value >= ma2_value ? 1 : -1 
fill(p_ma1, p_ma2, title = 'MA Cloud', color = ma_trend == 1 ? color.new(color.teal, 70) : color.new(color.red, 70))


// bollinger band
input_bblength = input.int(20, title = 'BB Length', group = 'BB Settings')
input_bbmult = input.float(2.00, title = 'BB Mult', group = 'BB Settings')
input_bbsrc = input.source(close, title = 'BB Source', group = 'BB Settings')
[bb_middle, bb_upper, bb_lower] = ta.bb(input_bbsrc, input_bblength, input_bbmult)

p_bbupper = plot(bb_upper, title = 'BB Upper', color = color.new(color.gray,85))
p_bblower = plot(bb_lower, title = 'BB Lower', color = color.new(color.gray,85))
bb_sell = high >= bb_upper
bb_buy = low <= bb_lower


// rsi
input_rsisource = input.source(close, title = 'RSI Source', group = 'RSI Settings')
input_rsilength = input.int(14, title = 'RSI Length', group = 'RSI Settings')
input_rsibuy = input.int(30, title = 'RSI Buy', group = 'RSI Settings')
input_rsisell = input.int(70, title = 'RSI Sell', group = 'RSI Settings')
rsi_value = ta.rsi(input_rsisource, input_rsilength)

rsi_buy = rsi_value <= input_rsibuy
rsi_sell = rsi_value >= input_rsisell


// swing 
input_swinglength = input.int(5, title = 'Swing Length For Stops', group = 'Swing Settings')

csh = ta.valuewhen(ta.pivothigh(high, input_swinglength, input_swinglength), high[input_swinglength], 0)
csl = ta.valuewhen(ta.pivotlow(low, input_swinglength, input_swinglength), low[input_swinglength], 0)
// plot(csh, color = color.red)
// plot(csl, color = color.green)

// entry logic
var bool long_entry = false
var bool long_exit = false

var bool short_entry = false
var bool short_exit = false

long_entry := (ma_trend == 1 and bb_buy and rsi_buy) and strategy.position_size <= 0 
short_entry := (ma_trend == -1 and bb_sell and rsi_sell) and strategy.position_size >= 0


// exit logic
var float long_stop = na
var float short_stop = na

alt_longstop = ta.lowest(low, input_swinglength)
alt_shortstop = ta.highest(high, input_swinglength)

long_stop := long_entry ? math.min(csl, alt_longstop) : long_stop[1]
short_stop := short_entry ? math.max(csh, alt_shortstop) : short_stop[1]

barcolor(strategy.position_size > 0 ? color.green : strategy.position_size < 0 ? color.red : na)

// send orders
if long_entry
    strategy.entry(id="LE", comment="LE", direction = strategy.long)

if short_entry
    strategy.entry(id="SS_E", comment="SS_E", direction =  strategy.short)

if long_entry or strategy.position_size > 0
    strategy.exit("LE", comment = 'LX', limit = bb_upper, stop = long_stop)
if short_entry or strategy.position_size < 0
    strategy.exit("SS_E", comment = 'SS_X', limit = bb_lower, stop = short_stop)


// plot
p_entry = plot(strategy.position_avg_price, title = 'Entry Line', color = color.blue, style = plot.style_linebr)
p_stop = plot(strategy.position_size > 0 ? long_stop : strategy.position_size < 0 ? short_stop : na
 , title = 'Stop Line', color = color.fuchsia, style = plot.style_linebr)

Overview

Pivot Trendlines detects band reversals with Bollinger Bands and uses RSI for mean-reversion signals. Long entries trigger when the trend is bullish, price touches the lower band, and RSI is below the buy level, with no open position. Short entries occur in the opposite case. Stops are set at the minimum of recent swing lows or the lowest low over the swing length for longs, and maximum for shorts. Bars are colored green or red according to position. Settings control the two MA lengths, Bollinger parameters, RSI levels, and swing length for stops. It draws all elements directly on the price chart.

Details

Published by

g-moe on GitHub

Code credit

goodgains, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

Any timeframe

Risk Notes

  • The strategy enters trades without a stop loss

Settings

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

SettingTypeDefaultVariable
MA 1int20input_ma1length
input_ma1pricesourcecloseinput_ma1price
MA 2int50input_ma2length
input_ma2pricesourcecloseinput_ma2price
BB Lengthint20input_bblength
BB Multfloat2.00input_bbmult
BB Sourcesourcecloseinput_bbsrc
RSI Sourcesourcecloseinput_rsisource
RSI Lengthint14input_rsilength
RSI Buyint30input_rsibuy
RSI Sellint70input_rsisell
Swing Length For Stopsint5input_swinglength
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

106 stars

This trading strategy combines three Exponential Moving Averages (8, 14, and 50 periods by default) with Stochastic RSI and Average True Range (ATR). It enters a long position when EMA 1 is above EMA 2, EMA 2 is above EMA 3, and the Stochastic RSI %K line cros

Strategy v4 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of 3 x EMA + Stochastic RSI + ATR

Momentum Rider DAILY FINAL

by greenpeas007
58 stars

This strategy combines exponential moving averages (EMA 9, 21, 50), RSI, MACD, and Supertrend indicators to enter long positions. It triggers trades using six dynamic setups based on momentum breakouts, EMA crossovers, and pullbacks. Position sizing is compute

Strategy v6 TrendOscillators & momentum Repaints
Updated 4 months ago · Custom licence GitHub repository of Momentum Rider DAILY FINAL

CombinedBinHAndClucV6

by viktors1982
36 stars

CombinedBinHAndClucV6 is a trading strategy based on the popular BinHV45 and ClucMay72018 algorithms. It calculates dual Bollinger Bands, Exponential Moving Averages, Relative Strength Index, Average True Range, and volume metrics across primary and one-hour h

Strategy v4 Volatility & bandsOscillators & momentum Repaints
Updated 2 years ago · None stated GitHub repository of CombinedBinHAndClucV6

BB Swing

by benso87
24 stars

This strategy calculates Bollinger Bands, a configurable moving average slope, Stochastic RSI, ATR, and ADX directly on the price chart. It evaluates trend bias and momentum conditions to generate long and short swing entries using percent-of-equity sizing. Th

Strategy v5 Volatility & bandsOscillators & momentum
Updated 4 years ago · MPL-2.0 GitHub repository of BB Swing

This strategy trades long and short on the price chart using a multi-timeframe TradingLatino setup. It requires EMA 10 to cross or sit above or below EMA 55 on the one-hour, four-hour, and daily charts, together with a positive ADX slope, a directional monitor

Strategy v5 TrendMoving averages
Updated 1 year ago · None stated GitHub repository of Ruckard ABCBitcoin TradingLatino A010

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