OPEN-SOURCE SCRIPT

Auto Fibonacci Retrace

66
NOTE: This script is for educational purposes only.

This Pine Script v6 indicator automates the drawing of Fibonacci retracement levels on a TradingView chart based on detected pivot highs and lows. It's designed to identify the most recent swing points in a price trend and plot horizontal lines at standard Fibonacci ratios (0%, 23.6%, 38.2%, 50%, 61.8%, 78.6%, 100%), along with optional labels for each level. The script is useful for traders who want dynamic, hands-free Fib retracements that update as new pivots form, helping to spot potential support/resistance zones without manual intervention.
Key Features

Automatic Pivot Detection: Uses TradingView's built-in ta.pivothigh and ta.pivotlow functions to find recent swing highs and lows. The sensitivity is adjustable via user inputs for "Left Bars" and "Right Bars" (default: 5 each), which define how many bars are checked on either side to confirm a pivot.
Trend Direction Awareness: Determines if the current swing is an uptrend (recent high after low) or downtrend (recent low after high) and orients the Fib levels accordingly—starting from the low in uptrends or high in downtrends.
Dynamic Drawing:
Plots dashed horizontal lines extending to the right of the chart for each Fib level.
Colors are predefined for visual distinction (e.g., blue for 23.6%, orange for 61.8%).
Lines and labels are cleared and redrawn only when a new pivot is detected or on initial load to prevent chart clutter.

Customizable Labels: Optional labels show the percentage (e.g., "61.8%") and can be positioned on the "Left" (at the swing start) or "Right" (pinned to the current bar, updating dynamically). Labels use semi-transparent backgrounds for readability.
Performance Optimizations: Uses arrays to manage lines and labels efficiently, with reverse-indexed loops for safe deletion. The max_bars_back=500 ensures it handles historical data without excessive computation.
User Inputs:
Left/Right Bars: Tune pivot detection (higher values for major trends, lower for shorter swings).
Show Fib Levels/Labels: Toggle visibility.
Label Position: "Left" or "Right" for placement flexibility.

Usage Instructions

Adding to Chart: Copy-paste into TradingView's Pine Editor, save as a new indicator, and add it to your chart via the "Indicators" menu.
Customization: Adjust inputs in the indicator settings panel. For example, set Left/Right Bars to 10 for daily charts in strong trends.
Best Practices:
Use on trending markets (e.g., stocks, forex, crypto like BTC/USD); avoid choppy sideways action.
Combine with other indicators (e.g., RSI for overbought/oversold confirmation) for better trade signals.
Test on historical data—zoom out to see how it redraws on past swings.

Limitations: Relies on pivot functions, so it may lag slightly (pivots confirm after "Right Bars"). Not a trading strategy—use for analysis only. No alerts built-in, but you can add alertcondition if extending it.
Potential Enhancements: Add extensions (e.g., 161.8%), user-defined levels, or alerts on price touches via simple modifications.

This script provides a clean, efficient way to visualize Fib retracements automatically, saving time compared to manual drawing. If you need further tweaks or integration into a full strategy, let me know!

Exención de responsabilidad

The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.