The Butterfly [theUltimator5]This is a technical analysis tool designed to automatically detect and visualize Butterfly harmonic patterns based on recent market pivot structures. This indicator uses a unique plotting and detection algorithm to find and display valid Butterfly patterns on the chart.
The indicator works in real-time and historically by identifying major swing highs and lows (pivots) based on a user-defined ZigZag length. It then evaluates whether the most recent price structure conforms to the ideal proportions of a bullish or bearish Butterfly pattern. If the ratios between price legs XA, AB, BC, and projected CD meet defined tolerances, the pattern is plotted on the chart along with a projected D point for potential reversal.
Key Features:
Automatic Pivot Detection: The script analyzes recent price action to construct a ZigZag pattern, identifying swing points as potential X, A, B, and C coordinates.
Butterfly Pattern Validation: The pattern is validated against traditional Fibonacci ratios:
--AB should be approximately 78.6% of XA.
--BC must lie between 38.2% and 88.6% of AB.
--CD is projected as a multiple of BC, with user control over the ratio (e.g., 1.618–2.24).
Bullish and Bearish Recognition: The pattern logic detects both bullish and bearish Butterflies, automatically adjusting plotting direction and color themes.
Custom Ratio Tolerance: Users can define how strictly the AB/XA and BC/AB legs must adhere to ideal ratios, using a percentage-based tolerance slider.
Fallback Detection Logic: If a new pattern is not identified in recent bars, the script performs a backward search on the last four pivots to find the most recent valid pattern.
Force Mode: A toggle allows users to force the drawing of a Butterfly pattern on the most recent pivot structure, regardless of whether the ideal Fibonacci rules are satisfied.
Dynamic Visualization:
--Clear labeling of X, A, B, C, and D points.
--Colored connecting lines and filled triangles to visualize structure.
--Optional table displaying key Fibonacci ratios and how close each leg is to ideal values.
Inputs:
Length: Controls the sensitivity of the ZigZag pivots. Smaller values result in more frequent pivots.
Tolerance (%): Adjustable threshold for acceptable deviation in AB/XA and BC/AB ratios.
CD Length Multiplier: Projects point D by multiplying the BC leg using a value between 1.618 and 2.24.
Force New Pattern: Overrides validation checks to display a Butterfly structure on recent pivots regardless of ratio accuracy.
Show Table: Enables a table showing calculated ratios and deviations from the ideal.