A very specific and well-defined problem! I'll break down the solution into manageable parts, and we'll implement it in PineScript (TradingView's programming language). Here's the code, followed by explanations:
Fibonacci Retracement Drawer (Clean Impulse Moves Only)
//@version=5
indicator("Clean Fib Retracement", overlay=true)
// Parameters
length = input.int(15, "Pivot Length", minval=5, maxval=50) // X bars for pivot detection
maxBarsForward = input.int(12, "Max Bars Forward", minval=5, maxval=20) // Limit fib lines to this many bars
fibLevels = array.new_float(0) // 0.0, 0.4, 0.7, 1.0
// Initialize Fibonacci levels
array.push(fibLevels, 0.0)
array.push(fibLevels, 0.4)
array.push(fibLevels, 0.7)
array.push(fibLevels, 1.0)
// Detect Pivot Points
var float pivotLowPrice = na
var float pivotHighPrice = na
var int pivotLowIndex = na
var int pivotHighIndex = na
// Bullish Pivot (Low to High)
if ta.lowest(low, length) == low
pivotLowPrice := low
pivotLowIndex := bar_index
if ta.highest(high, length) == high and bar_index > pivotLowIndex + 1
pivotHighPrice := high
pivotHighIndex := bar_index
// Bearish Pivot (High to Low)
if ta.highest(high, length) == high
pivotHighPrice := high
pivotHighIndex := bar_index
if ta.lowest(low, length) == low and bar_index > pivotHighIndex + 1
pivotLowPrice := low
pivotLowIndex := bar_index
// Draw Fibonacci Retracement
var line[] fibLines = array.new_line(0)
var bool fibDrawn = false
var float startPrice = na
var float endPrice = na
var int startIndex = na
if (not na(pivotLowPrice) and not na(pivotHighPrice) and pivotHighIndex > pivotLowIndex) // Bullish
startPrice := pivotLowPrice
endPrice := pivotHighPrice
startIndex := pivotLowIndex
fibDrawn := true
else if (not na(pivotHighPrice) and not na(pivotLowPrice) and pivotLowIndex > pivotHighIndex) // Bearish
startPrice := pivotHighPrice
endPrice := pivotLowPrice
startIndex := pivotHighIndex
fibDrawn := true
if fibDrawn
// Clear previous lines
for i = 0 to array.size(fibLines) - 1
line.delete(array.get(fibLines, i))
array.clear(fibLines)
// Draw new Fibonacci lines
for i = 0 to array.size(fibLevels) - 1
level = array.get(fibLevels, i)
price = startPrice + (endPrice - startPrice) * level
l = line.new(startIndex, price, bar_index + maxBarsForward, price, extend=extend.none, color=color.blue, style=line.style_dotted)
array.push(fibLines, l)
// Reset flags
fibDrawn := false
pivotLowPrice := na
pivotHighPrice := na
// Optional: Highlight 0.4-0.7 Reaction Zone
reactZoneUpper = startPrice + (endPrice - startPrice) * 0.7
reactZoneLower = startPrice + (endPrice - startPrice) * 0.4
if not na(startPrice) and bar_index > startIndex and bar_index < startIndex + maxBarsForward
if low < reactZoneUpper and high > reactZoneLower
box.new(startIndex, reactZoneUpper, bar_index, reactZoneLower, bgcolor=color.new(color.green, 90), border_style=line.style_none)
// Cleanup
if bar_index > startIndex + maxBarsForward + 5
startPrice := na
endIndex := na
Let's go through the logic step by step:
- Parameters: We define two inputs:
length: The number of bars to consider for pivot detection (default: 15).
maxBarsForward: The maximum number of bars to extend the Fibonacci lines (default: 12).
- Fibonacci Levels: We store the 4 Fibonacci levels (0.0, 0.4, 0.7, 1.0) in an array
fibLevels.
- Pivot Detection:
- We use
ta.lowest() and ta.highest() to detect the lowest low and highest high within the length bars.
- For a bullish pivot, we wait for a
lowest low (pivot low) followed by a highest high (pivot high) after the low.
- For a bearish pivot, we do the opposite:
highest high followed by lowest low.
- We store the prices and bar indices (
pivotLowPrice, pivotHighPrice, pivotLowIndex, pivotHighIndex) for later use.
- Draw Fibonacci Retracement:
- We check if both pivot prices are valid and if the high comes after the low (bullish) or vice versa (bearish).
- We set
startPrice, endPrice, and startIndex based on the pivot prices and indices.
- We clear any previous Fibonacci lines and draw new ones using
line.new():
- We loop through
fibLevels and calculate the price for each level.
- We create a line from
startIndex to bar_index + maxBarsForward (limiting the extension).
- We store the lines in an array
fibLines.
- Optional: Highlight 0.4-0.7 Reaction Zone:
- We calculate the upper and lower bounds of the reaction zone (0.7 and 0.4 levels).
- If the current bar is within the
maxBarsForward limit and price touches the reaction zone, we draw a semi-transparent green box.
- Cleanup:
- After
maxBarsForward + 5 bars, we reset startPrice and startIndex to na, effectively removing the Fibonacci lines.
Key benefits:
- No repainting: We only draw Fibonacci lines after a confirmed pivot is detected.
- Clean visual representation: Lines are limited to
maxBarsForward bars, keeping the chart readable.
- Reliable move detection: We use
ta.lowest() and ta.highest() to detect meaningful pivot points.
Tips and Variations:
- Adjust
length and maxBarsForward to suit your trading style and timeframe.
- Experiment with different Fibonacci levels or add more (e.g., 0.236, 0.618).
- For a more conservative approach, wait for price to close within the 0.4-0.7 zone before highlighting the reaction area.
- Combine this script with other indicators or price action confirmations for a trading strategy.
Happy trading and charting!