OPEN-SOURCE SCRIPT
Güncellendi

The Butterfly [theUltimator5]

2 658
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.
Sürüm Notları
I made extensive updates to this indicator (and plan on some more)

1) I simplified the user inputs to make it more beginner friendly. Tolerances have been removed, and the table is automatically toggled off by default
2) I added a secondary chart pattern to search for: the Head and Shoulders
3) Users can now select between the Butterfly and Head and Shoulders to search for the most recent occurrence of either pattern. By default, the indicator will search the most recent bars and display a pattern if one exists. If a pattern does not exist on the current chart, it will go back in time until it finds a valid pattern.
4) I added labels (which can be turned off if you want) that display the name of the identified pattern and the sentiment (bullish or bearish)
5) I added a toggle switch to turn off labels on the patterns to reduce clutter if the labels are too intrusive.

I plan on adding additional chart patterns to automatically detect from in the future.
Sürüm Notları
Minor fix - Updated Inverse Head and Shoulders calculation since there was a conditional value causing it to never trigger.

Feragatname

Bilgiler ve yayınlar, TradingView tarafından sağlanan veya onaylanan finansal, yatırım, işlem veya diğer türden tavsiye veya tavsiyeler anlamına gelmez ve teşkil etmez. Kullanım Şartları'nda daha fazlasını okuyun.