Fibonacci Retracement in Pine Script
Horizontal levels at fixed fractions of the last swing, 23.6 to 78.6 percent, marking how much of a move price has given back and where a pullback is likely to pause.
Definition
A Fibonacci retracement divides the distance between a swing low and a swing high into fixed fractions and draws a horizontal line at each: 23.6, 38.2, 50, 61.8 and 78.6 percent of the move. It belongs to the ratio-levels family, where the levels come from a proportion of a chosen move rather than from a formula on the previous period. After an up leg the lines show how much of the rise a pullback has given back; after a down leg they show how much of the fall a bounce has recovered.
The levels are useful because pullbacks in trends tend to be proportional. A shallow retracement to 23.6 or 38.2 percent says the trend is strong and buyers are impatient; a retracement to 50 or 61.8 percent is a normal correction; a move past 78.6 percent says the leg is mostly undone and the trend is in doubt. Because so many traders draw the same lines from the same obvious swings, orders gather at them and the reactions are partly self-fulfilling.
The ratios come from the Fibonacci sequence, named after Leonardo of Pisa, in which each number is the sum of the previous two. The ratio of adjacent numbers converges on 0.618, the golden ratio; 0.382 is 1 minus 0.618, 0.236 is 0.382 times 0.618, and 0.786 is the square root of 0.618. The 50 percent level is not a Fibonacci ratio but has been included since early chartists noted that half retracements are common. Standard sets add 100 and extensions beyond it.
On the chart the lines are flat and fixed until a new swing is chosen. Price tends to move between them in steps, pausing or reversing at one, then running to the next. The 50 to 61.8 zone gets the most attention and often produces the deepest reaction in a healthy trend. A bar that closes through 61.8 and keeps going usually means the retracement is turning into a reversal. The implementation redraws the set each time a new swing confirms, so the levels update as the structure changes.
Reading the chart
The retracement is only as good as the swing it is drawn from, so identification begins with the leg, not the lines.
- Choose the leg. It should be an obvious impulse between a clear swing low and a clear swing high, the kind of move other traders would also pick; small wobbles inside it are not legs.
- Draw from the start of the leg to its end. For an up leg the 0 percent line is the high and 100 percent is the low, so the levels sit below the high in order: 23.6, 38.2, 50, 61.8, 78.6.
- Decide on wicks or bodies and stay consistent. Wick-to-wick captures the full extent of the move; body-to-body ignores the spikes that were rejected. Mixing the two shifts every level on the chart.
- Watch the first pause. A pullback that stalls at 38.2 with a rejection candle is a strong trend; one that slides to 61.8 and holds is normal; one that closes through 78.6 puts the leg in question.
- Look for confluence. A retracement level that coincides with a moving average, a prior swing, a pivot or an anchored VWAP is a stronger zone than the ratio alone.
- Redraw when the structure changes. Once price makes a new swing beyond the leg's end, the old set is stale, and the retracement should be drawn from the new leg.
The maths
Each level is the end of the leg moved back towards its start by a fixed fraction of the leg's size.
The implementation finds the last confirmed swing high and swing low with 10 bars on each side, and the later of the two decides the leg's direction: a high after a low is an up leg, a low after a high is a down leg.
A swing confirms only after the right-hand bars have printed, so the set lags by that many bars and the levels update when a new swing confirms, not on the bar the swing happened.
The 61.8 line is drawn heavier, and the script marks a long when price closes back above 61.8 during an up-leg pullback and a short when it closes below 61.8 during a down-leg bounce.
Extensions at 127.2, 161.8 and 261.8 percent project beyond the leg's end for targets; they use the same span with ratios above 1.
Inputs that matter
| Input | Typical | What changing it does |
|---|---|---|
| Swing strength (bars left) | 10 | More bars make the swings larger and the leg longer, so the levels change less often; fewer bars pick up smaller legs and redraw frequently. |
| Confirmation (bars right) | 10 | More confirmation bars make the swing more reliable but delay the redraw; fewer confirm sooner at the risk of a swing that is later exceeded. |
| Levels shown | 23.6, 38.2, 50, 61.8, 78.6 | Adding 100 and the extensions gives targets beyond the leg; removing the shallow levels keeps the chart to the zone that matters most. |
In practice
- As a pullback entry map in a trend: the rule goes long on a pullback into the 50 to 61.8 zone of the last up leg when it shows a rejection candle, with the stop below 78.6 and the leg's high as the first target.
- As a trend-strength read: the depth of each retracement, shallow at 38.2 or deep at 61.8, tells you how eager the trend's participants are and whether the next leg is likely to be strong.
- As an invalidation line: a close through 78.6 says the leg is mostly undone and the trend is in doubt; trend-continuation trades planned from the levels are abandoned there rather than defended.
- As confluence with other levels: a retracement that coincides with a moving average, a prior day level or an anchored VWAP is a higher-quality zone than either reference alone.
- As a target tool: the extensions at 127.2 and 161.8 percent of the retraced leg give profit targets beyond the leg's end that many other traders also use, so reactions there are common.
- As a mechanical trigger: the script marks a long when price reclaims 61.8 after an up-leg pullback and a short when it loses 61.8 on a down-leg bounce, which turns the zone into an alert.
Where it goes wrong
- Drawing from minor swings. A retracement of a three-bar wobble means nothing; draw from legs another trader would pick without prompting.
- Expecting exact reactions. The levels are zones of a few ticks, and price often overshoots one before turning; use the zone and a candle, not the line and a limit order.
- Covering the chart in sets. Five retracements from five legs give a level every few ticks and no information; keep the one from the current leg and redraw when it changes.
- Switching between wicks and bodies. Each choice moves the levels; pick one and use it on every leg.
Compared with
Support & Resistance. Support and resistance are drawn where price actually reversed; retracement levels are proportions of a move and can sit where price has never traded before. The two are strongest together, when a ratio level lands on a prior reversal.
Pivot Points. Pivots come from a formula on the previous day's prices and are the same for everyone; retracement levels depend on which swing you choose. Both are ratio levels, but pivots are objective and retracements need judgement.
Anchored VWAP. An anchored VWAP from the start of the leg shows where the leg's average money is; a retracement shows fixed fractions of the leg's price range. When the anchored line crosses the 50 or 61.8 zone the two agree on a cost-based pullback level.
Moving Average. A moving average is dynamic support that moves every bar; a retracement is fixed to the leg. In a trend the two often meet, and a pullback that touches the 50 level and the 50-day average at once is a well-known confluence.
Build it in Quant
Type one of these in the Quant desk, or click it. A request that fits a checked template is assembled on the spot without a credit; anything beyond the blanks goes to the copilot, which edits on top of the checked script.
Questions
Which Fibonacci level is the most important?
The 61.8, followed by the 50 and the 38.2. The 50 to 61.8 zone is where the deepest healthy pullbacks tend to end, which is why the implementation marks there. The 78.6 matters as an invalidation line: a close through it says the leg is mostly undone and the trend is in doubt.
Do Fibonacci levels actually work?
They work as a map of proportional pullbacks, and partly because so many traders watch the same levels from the same obvious swings. There is nothing magical about the numbers; what matters is that a 50 or 61.8 percent retracement is a normal correction in a trend and traders act around it. Treat them as zones and confirm on price.
Should I draw from wicks or from bodies?
Either, consistently. Wick-to-wick captures the full extent of the move and is the more common choice; body-to-body ignores spikes that were quickly rejected. What breaks the tool is switching between them, because each choice moves every level. The implementation uses the swing high and swing low, which are wick extremes.
How do I pick the right swing?
Pick the leg that is obvious on the timeframe you trade: a clear impulse from a swing low to a swing high that other traders would draw too. If you have to search for it, it is not the leg. The implementation uses the last confirmed swing high and swing low with 10 bars on each side, so it draws from the structure automatically.
What happens when price goes past 78.6?
The leg is mostly undone and the retracement is close to becoming a full reversal. Trend-continuation trades planned from the levels are usually abandoned there. A move back up through the levels, with 61.8 reclaimed, is the sign that the pullback has ended after all.
Does the implementation repaint?
The levels are drawn from confirmed swings, so a set does not vanish once drawn. But a swing only confirms after the right-hand bars have printed, so the set updates a few bars after the swing happened, and a new swing later redraws it. That lag is the cost of not drawing from unconfirmed highs and lows.