Strategy · Pine Script v5 · On the chart

Fibonacci Pullback Strategy

by Basant1Saini· code credited to TradingTools

Fibonacci Pullback Strategy is a trend-following system that identifies retracements within established trends.

View on GitHub

GitHub stars1on the repository
Pine versionv5runs as is
Lines of code91
Settings12inputs
LicenceNone statedfile: MPL-2.0
Last update8 months ago4 Mar 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

91 lines of Pine Script v5 from 10_Fibonacci_Pullback/10_Fibonacci_Pullback.pine, published under MPL-2.0 (file header). Keep the author's credit and licence lines when you reuse it.

10_Fibonacci_Pullback.pine
// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0
// © TradingTools

//@version=5
strategy("Fibonacci Pullback Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100)

// ─── Inputs ───
swingLength     = input.int(20, "Swing Detection Length", group="Fibonacci")
fib382          = input.float(0.382, "Fib 38.2%", group="Fibonacci")
fib500          = input.float(0.500, "Fib 50.0%", group="Fibonacci")
fib618          = input.float(0.618, "Fib 61.8%", group="Fibonacci")
fibBuffer       = input.float(0.5, "Fib Zone Buffer (%)", step=0.1, group="Fibonacci")
emaTrend        = input.int(50, "Trend EMA Length", group="Filters")
rsiLength       = input.int(14, "RSI Length", group="Filters")
rsiOB           = input.float(70, "RSI Overbought", group="Filters")
rsiOS           = input.float(30, "RSI Oversold", group="Filters")
atrLength       = input.int(14, "ATR Length", group="Risk Management")
atrMult         = input.float(2.0, "ATR Multiplier for SL", step=0.1, group="Risk Management")
rrRatio         = input.float(2.5, "Risk:Reward Ratio", step=0.1, group="Risk Management")

// ─── Swing High/Low Detection ───
swingHigh = ta.highest(high, swingLength)
swingLow  = ta.lowest(low, swingLength)

// ─── Fibonacci Levels ───
range_hl = swingHigh - swingLow

// Uptrend fibs (retracement from high)
fib382_up = swingHigh - range_hl * fib382
fib500_up = swingHigh - range_hl * fib500
fib618_up = swingHigh - range_hl * fib618

// Downtrend fibs (retracement from low)
fib382_dn = swingLow + range_hl * fib382
fib500_dn = swingLow + range_hl * fib500
fib618_dn = swingLow + range_hl * fib618

// ─── Trend & RSI ───
emaVal  = ta.ema(close, emaTrend)
rsiVal  = ta.rsi(close, rsiLength)
atrVal  = ta.atr(atrLength)

bullTrend = close > emaVal
bearTrend = close < emaVal

// ─── Fib Zone Check ───
bufferAmt = close * (fibBuffer / 100)

// In uptrend: price pulling back INTO the golden zone (38.2–61.8)
inBullFibZone = low <= (fib382_up + bufferAmt) and low >= (fib618_up - bufferAmt)
// In downtrend: price pulling back UP into the golden zone
inBearFibZone = high >= (fib382_dn - bufferAmt) and high <= (fib618_dn + bufferAmt)

// ─── Entry Conditions ───
// Long: Uptrend + price in fib pullback zone + bullish candle + RSI not overbought
bullishCandle  = close > open
bearishCandle  = close < open

longCondition  = bullTrend and inBullFibZone and bullishCandle and rsiVal < rsiOB and rsiVal > rsiOS
shortCondition = bearTrend and inBearFibZone and bearishCandle and rsiVal > rsiOS and rsiVal < rsiOB

// ─── Execute Trades ───
if longCondition and strategy.position_size == 0
    sl = close - atrVal * atrMult
    tp = close + (close - sl) * rrRatio
    strategy.entry("Fib Long", strategy.long)
    strategy.exit("Fib Long Exit", "Fib Long", stop=sl, limit=tp)

if shortCondition and strategy.position_size == 0
    sl = close + atrVal * atrMult
    tp = close - (sl - close) * rrRatio
    strategy.entry("Fib Short", strategy.short)
    strategy.exit("Fib Short Exit", "Fib Short", stop=sl, limit=tp)

// ─── Plotting ───
plot(emaVal, "Trend EMA", color=color.new(color.white, 30), linewidth=2)

plot(bullTrend ? fib382_up : na, "38.2% (Bull)", color=color.new(color.green, 40), style=plot.style_linebr)
plot(bullTrend ? fib500_up : na, "50.0% (Bull)", color=color.new(color.yellow, 40), style=plot.style_linebr)
plot(bullTrend ? fib618_up : na, "61.8% (Bull)", color=color.new(color.orange, 40), style=plot.style_linebr)

plot(bearTrend ? fib382_dn : na, "38.2% (Bear)", color=color.new(color.red, 40), style=plot.style_linebr)
plot(bearTrend ? fib500_dn : na, "50.0% (Bear)", color=color.new(color.yellow, 40), style=plot.style_linebr)
plot(bearTrend ? fib618_dn : na, "61.8% (Bear)", color=color.new(color.orange, 40), style=plot.style_linebr)

plotshape(longCondition, title="Fib Long", location=location.belowbar, style=shape.arrowup, color=color.green, size=size.small, text="FIB")
plotshape(shortCondition, title="Fib Short", location=location.abovebar, style=shape.arrowdown, color=color.red, size=size.small, text="FIB")

// ─── Alerts ───
alertcondition(longCondition, title="Fib Pullback Long", message="Fibonacci: Bullish pullback entry in golden zone")
alertcondition(shortCondition, title="Fib Pullback Short", message="Fibonacci: Bearish pullback entry in golden zone")

Overview

Fibonacci Pullback Strategy calculates dynamic Fibonacci levels (38.2%, 50.0%, and 61.8%) based on recent swing highs and lows over a specified lookback period. Trades are filtered using a trend-defining EMA and RSI bounds. Long or short positions trigger when price enters the Fibonacci golden zone and forms a directional candle. Positions exit using an ATR-multiplier stop loss and a fixed risk-to-reward take profit. The EMA and Fibonacci retracement lines are plotted on the main price chart.

Details

Published by

Basant1Saini on GitHub

Code credit

TradingTools, as written in the script's header

Draws

On the price chart

Markets

Any symbol

Timeframes

M15-H1, H4 and above

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
Swing Detection Lengthint20swingLength
Fib 38.2%float0.382fib382
Fib 50.0%float0.500fib500
Fib 61.8%float0.618fib618
Fib Zone Buffer (%)float0.5fibBuffer
Trend EMA Lengthint50emaTrend
RSI Lengthint14rsiLength
RSI Overboughtfloat70rsiOB
RSI Oversoldfloat30rsiOS
ATR Lengthint14atrLength
ATR Multiplier for SLfloat2.0atrMult
Risk:Reward Ratiofloat2.5rrRatio
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 strategy overlays the price chart and looks for VWAP pullbacks. It goes long when a short SMA crosses above VWAP after RSI has been oversold within ten bars, and short on the opposite cross after RSI has been overbought. Optional Minervini-style moving-av

Strategy v5 TrendOscillators & momentum
Updated 2 years ago · GPL-3.0 GitHub repository of Advanced VWAP-Momentum Pullback Trend Qualifier

This strategy trades 1-minute futures contracts on Binance. It plots pivot high and low resistance and support levels on the price chart, along with an EMA and a custom Cto indicator. Long and short entries trigger on EMA and Cto crossovers combined with price

Strategy v5 TrendMoving averages Repaints
Updated 2 years ago · None stated GitHub repository of Binancash Futures Strategy 1M Candles

This strategy identifies reversal opportunities based on liquidity sweeps and multi-gate confluence scoring. It evaluates setups using mandatory conditions such as swing sweeps, body and ATR filters, and dual EMA higher-timeframe trend alignment, alongside opt

Strategy v6 SMC / ICT / price actionTrend
Updated 3 days ago · MIT GitHub repository of Reversal Engine Score [WavesUnchained] Strategy
7 stars

This strategy plots dynamic pivot-based support and resistance trendlines on the price chart and trades breakouts beyond those lines. By default it allows longs only when EMA 50 is above EMA 200 and an RSI-SMA momentum filter is met, with an ATR entry buffer.

Strategy v6 TrendBreakout
Updated 12 months ago · None stated GitHub repository of Easy Trendline - XAUUSD 30m

Optimizer v1

by QuantGeekDev
7 stars

This strategy generates long signals on the price chart using multiple technical indicators. It computes EMA 70 and 100, SMA 70, ADX with DMI, CCI, and MACD values. A long entry occurs when EMA crossover condition, SMA condition, ADX above 15, CCI above 30, an

Strategy v5 TrendOscillators & momentum
Updated 3 years ago · None stated GitHub repository of Optimizer v1

This strategy combines MACD momentum signals with support and resistance levels from pivot points and an EMA100 trend filter for long positions. It plots the 100-period EMA on the price chart and signals when price breaks above the EMA or forms bullish pivot p

Strategy v4 TrendOscillators & momentum
Updated 3 years ago · Apache-2.0 GitHub repository of MACD + Support / Resistance Long Strategy

More Support & resistance 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