PROTECTED SOURCE SCRIPT

Harmonic Pattern Detector [The_lurker]

226

📊 Harmonic Pattern Detector

An advanced indicator designed to automatically detect and visualize harmonic patterns on price charts with high accuracy. It is based on Fibonacci ratios, ZigZag structures, and pivot points, offering traders precise identification of potential reversal zones.

The indicator supports detection of 13 major harmonic patterns: Gartley, Butterfly, Bat, Crab, Deep Crab, AB=CD, Cypher, Shark, Three Drives, Wolfe Waves, 5-0, Black Swan, and Anti-Gartley.

Each pattern is matched using ideal Fibonacci ratios with a customizable error margin. Patterns are validated only if the similarity score (accuracy) is greater than or equal to 80%, ensuring reliable signals.

🔎 How It Works:

1️⃣ Pivot Point Detection
The indicator uses a function to find major swing highs and lows (`findPivots`), based on a configurable pattern length (default is 18 candles). These pivots are stored in arrays: `pivotPrices`, `pivotBars`, and `pivotDirections`.

2️⃣ ZigZag Construction
With the pivots detected, the indicator constructs a ZigZag structure by connecting the relevant price points using `buildZigZag`. These lines represent the fundamental price movements forming harmonic shapes.

3️⃣ Fibonacci Ratio Calculation
From the ZigZag, the indicator extracts points labeled X, A, B, C, and D, then calculates key Fibonacci ratios:

* XAB = |B-A| / |X-A|
* ABC = |C-B| / |A-B|
* XAD = |D-A| / |X-A|

4️⃣ Pattern Matching Algorithm
Each pattern has its ideal Fibonacci ranges. The ratios are compared with these ranges using adjustable error margins:

ERROR_MARGIN_MIN = (100 - DETECT_ERROR_MARGIN_PERCENT) / 100
ERROR_MARGIN_MAX = (100 + DETECT_ERROR_MARGIN_PERCENT) / 100

If all required ratios for a pattern fall within these bounds, the pattern is considered a potential match.

5️⃣ Similarity Score Calculation
To evaluate accuracy, each pattern gets a similarity score by comparing actual vs. ideal ratios. For example:
simXAB = min(xabRatio / idealXAB, idealXAB / xabRatio)
simABC = min(abcRatio / idealABC, idealABC / abcRatio)
simXAD = min(xadRatio / idealXAD, idealXAD / xadRatio)
similarity = (simXAB + simABC + simXAD) / 3

Only patterns with a similarity ≥ MIN\_RATING\_PERCENT (default 80%) are shown.

6️⃣ Visual Output
When a pattern is detected, it is displayed by connecting the points X→A→B→C→D. A label is drawn at point D showing:

* Pattern Name
* Emoji (e.g., 🦋 for Gartley)
* Similarity percentage (e.g., "92%")

Patterns are color-coded:

🟢 Green for Bullish
🔴 Red for Bearish

⚙️ Configurable Settings:

* ENABLE_PATTERN_DETECTION`: Toggle to enable or disable pattern detection
* PATTERN_LENGTH_INPUT`: Number of candles to consider for structure
* DETECT_ERROR_MARGIN_PERCENT`: Controls allowed deviation from ideal Fibonacci ratios
* MIN_RATING_PERCENT`: Minimum similarity percentage to display a pattern (e.g., 80%)

🎨 Display Customization:

* Customize ZigZag lines (solid, dotted, dashed)
* Control thickness, color, and style of lines
* Adjust label font size, position, and visibility
* Enable or disable specific pattern types
* Modify Fibonacci levels used per pattern

Labels include emojis for easier recognition:
🦋 Gartley | 🐝 Butterfly | 🦈 Shark | 🐺 Wolfe Waves | 🦢 Black Swan | ⚡ Anti-Gartley

📚 Pattern Definitions:

Gartley: XAB ≈ 0.618, ABC ≈ 0.382–0.886, XAD ≈ 0.786
Butterfly: XAB ≈ 0.786, XAD ≈ 1.272
Bat: XAB ≈ 0.382–0.50, XAD ≈ 0.886
Crab / Deep Crab: XAD ≈ 1.618–1.902
AB=CD: AB equals CD (symmetrical moves)
Cypher: ABC ≈ 1.13–1.414, XAD ≈ 0.786
Shark: XAD ≈ 1.0–1.13
Three Drives: Three legs with Fibonacci extensions
Wolfe Waves: Geometrically aligned points, XAD ≈ 1.272–1.618
5-0: CD retraces 50% of previous wave
Black Swan: Rare, sharp pattern, XAD ≈ 3.618–4.236
Anti-Gartley: Reverse Gartley with custom ratios

📈 Target Markets:

This indicator is effective for analyzing:
✔ Forex
✔ Stocks
✔ Crypto
✔ Commodities (Gold, Oil, etc.)

Suitable for multiple trading styles:

* Scalping
* Swing Trading
* Position Trading

✨ Key Features:

✅ 13 harmonic patterns supported
✅ Automatic detection with accuracy scoring
✅ Adjustable error margins
✅ Fully customizable display
✅ Pattern-specific toggles
✅ Bullish/Bearish color-coded output
✅ Informative labels with pattern name, emoji, and similarity

⚠️ Disclaimer:
This indicator is for educational and analytical purposes only. It does not constitute financial, investment, or trading advice. Use it in conjunction with your own strategy and risk management. Neither TradingView nor the developer is liable for any financial decisions or losses.

كاشف الأنماط التوافقية 📊

مؤشر متطور مصمم للكشف التلقائي عن الأنماط التوافقية على مخططات الأسعار وتصورها بدقة عالية. يعتمد على نسب فيبوناتشي، وهياكل الزجزاج، ونقاط الارتكاز، مما يوفر للمتداولين تحديدًا دقيقًا لمناطق الانعكاس المحتملة.

يدعم المؤشر الكشف عن 13 نمطًا توافقيًا رئيسيًا: جارتلي، الفراشة، الخفاش، السلطعون، السلطعون العميق، AB=CD، السايفر، القرش، ثلاثة محركات، موجات وولف، 5-0، البجعة السوداء، ومضاد جارتلي.

يتم مطابقة كل نمط باستخدام نسب فيبوناتشي مثالية مع هامش خطأ قابل للتخصيص. لا يتم التحقق من صحة الأنماط إلا إذا كانت درجة التشابه (الدقة) أكبر من أو تساوي 80%، مما يضمن إشارات موثوقة.

🔎 كيفية العمل:

1️⃣ اكتشاف نقاط الارتكاز
يستخدم المؤشر دالة للعثور على قمم وقيعان التأرجح الرئيسية (`findPivots`)، بناءً على طول نمط قابل للتخصيص (الطول الافتراضي هو 18 شمعة). تُخزّن هذه النقاط المحورية في مصفوفات: `pivotPrices`، `pivotBars`، `pivotDirections`.

2️⃣ بناء ZigZag
بعد اكتشاف النقاط المحورية، يُنشئ المؤشر بنية ZigZag بربط نقاط السعر ذات الصلة باستخدام `buildZigZag`. تُمثل هذه الخطوط تحركات الأسعار الأساسية مُشكّلةً أشكالًا توافقية.

3️⃣ حساب نسبة فيبوناتشي
من ZigZag، يستخرج المؤشر النقاط المُسمّاة X، A، B، C، وD، ثم يحسب نسب فيبوناتشي الرئيسية:

* XAB = |B-A| / |X-A|
* ABC = |C-B| / |A-B|
* XAD = |D-A| / |X-A|

4️⃣ خوارزمية مطابقة الأنماط
لكل نمط نطاقات فيبوناتشي مثالية. تُقارن النسب بهذه النطاقات باستخدام هوامش خطأ قابلة للتعديل:

ERROR_MARGIN_MIN = (100 - DETECT_ERROR_MARGIN_PERCENT) / 100
ERROR_MARGIN_MAX = (100 + DETECT_ERROR_MARGIN_PERCENT) / 100

إذا وقعت جميع النسب المطلوبة للنمط ضمن هذه الحدود، يُعتبر النمط مطابقًا محتملًا.

5️⃣ حساب درجة التشابه
لتقييم الدقة، يحصل كل نمط على درجة تشابه بمقارنة النسب الفعلية بالنسب المثالية. على سبيل المثال:
simXAB = min(xabRatio / idealXAB, idealXAB / xabRatio)
simABC = min(abcRatio / idealABC, idealABC / abcRatio)
simXAD = min(xadRatio / idealXAD, idealXAD / xadRatio)
التشابه = (simXAB + simABC + simXAD) / 3

يتم عرض الأنماط التي يكون تشابهها ≥ MIN\_RATING\_PERCENT (الافتراضي 80%) فقط.

6️⃣ المخرجات المرئية
عند اكتشاف نمط، يتم عرضه بتوصيل النقاط X→A→B→C→D. يتم رسم علامة عند النقطة D تُظهر:

* اسم النمط
* رمز تعبيري (مثل 🦋 لنمط جارتلي)
* نسبة التشابه (مثل "92%))

الأنماط مُرمَّزة بالألوان:

🟢 أخضر للارتفاع
🔴 أحمر للانخفاض

⚙️ إعدادات قابلة للتخصيص:

* تمكين كشف النمط: تفعيل أو تعطيل كشف النمط
* إدخال طول النمط: عدد الشموع المُراد حسابها للهيكل
* كشف الخطأ: نسبة الهامش: يتحكم في الانحراف المسموح به عن نسب فيبوناتشي المثالية
* الحد الأدنى لنسبة التشابه لعرض النمط (مثل 80%)

🎨 تخصيص العرض:

* تخصيص خطوط متعرجة (متصلة، منقطة، متقطعة)
* التحكم في السُمك واللون ونمط الخطوط
* ضبط حجم خط التسمية وموضعه ووضوحه
* تفعيل أو تعطيل أنواع أنماط محددة
* تعديل مستويات فيبوناتشي المستخدمة لكل نمط

تتضمن التسميات رموزًا تعبيرية لتسهيل التعرف عليها:
🦋 جارتلي | 🐝 فراشة | 🦈 سمكة قرش | 🐺 موجات وولف | 🦢 بجعة سوداء | ⚡ نمط مضاد لـ غارتلي

📚 تعريفات الأنماط:

غارتلي: XAB ≈ 0.618، ABC ≈ 0.382–0.886، XAD ≈ 0.786
فراشة: XAB ≈ 0.786، XAD ≈ 1.272
خفاش: XAB ≈ 0.382–0.50، XAD ≈ 0.886
سرطان البحر/سرطان البحر العميق: XAD ≈ 1.618–1.902
AB=CD: AB يساوي CD (حركات متماثلة)
سايفر: ABC ≈ 1.13–1.414، XAD ≈ 0.786
شارك: XAD ≈ 1.0–1.13
ثلاثة أرجل: ثلاثة أرجل مع فيبوناتشي امتدادات
موجات وولف: نقاط متوازية هندسيًا، XAD ≈ 1.272–1.618
5-0: تصحيح CD بنسبة 50% من الموجة السابقة
البجعة السوداء: نمط نادر وحاد، XAD ≈ 3.618–4.236
مضاد جارتلي: جارتلي معكوس بنسب مخصصة

📈 الأسواق المستهدفة:

هذا المؤشر فعال لتحليل:
✔ الفوركس
✔ الأسهم
✔ العملات المشفرة
✔ السلع (الذهب، النفط، إلخ)

مناسب لأنماط تداول متعددة:

* المضاربة السريعة
* تداول التأرجح
* تداول المراكز

✨ الميزات الرئيسية:

✅ دعم 13 نمطًا توافقيًا
✅ كشف تلقائي مع تسجيل الدقة
✅ هوامش خطأ قابلة للتعديل
✅ شاشة قابلة للتخصيص بالكامل
✅ مفاتيح تبديل خاصة بالأنماط
✅ مخرجات مرمزة بالألوان للصعود/الهبوط
✅ تسميات توضيحية مع النمط الاسم، والرموز التعبيرية، والتشابه

⚠️ إخلاء مسؤولية:
هذا المؤشر لأغراض تعليمية وتحليلية فقط. لا يُمثل نصيحة مالية أو استثمارية أو تداولية. استخدمه بالتزامن مع استراتيجيتك الخاصة وإدارة المخاطر. لا يتحمل TradingView ولا المطور مسؤولية أي قرارات مالية أو خسائر.

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.