### **📌 Detailed Explanation of the TradingView Indicator Code**
This **Pine Script v5** indicator automatically **detects trend lines** based on pivot highs and pivot lows. It helps traders visualize **support and resistance levels** using dynamic trend lines.
---
## **🔹 How the Indicator Works** The indicator identifies **key pivot points** in price action and then **draws trend lines** connecting them. It works as follows:
1. **Detects Pivot Highs and Lows**: - A **pivot high** is a local maximum where the price is higher than surrounding bars. - A **pivot low** is a local minimum where the price is lower than surrounding bars.
2. **Stores the Last Two Pivot Points**: - The script remembers the last **two pivot highs** and **two pivot lows**. - These points are used to **draw resistance and support lines** dynamically.
3. **Plots Resistance and Support Lines**: - The script continuously **updates** and **extends** the trend lines to the right as new pivots are found. - **Red Line (Resistance):** Connects the last two pivot highs. - **Green Line (Support):** Connects the last two pivot lows.
---
## **🔹 Code Breakdown**
### **1️⃣ Inputs for User Customization** ```pinescript leftLen = input.int(2, "Left Pivot Length") rightLen = input.int(2, "Right Pivot Length") highLineColor = input.color(color.red, "Resistance Line Color") lowLineColor = input.color(color.green, "Support Line Color") ``` - **leftLen & rightLen:** Define how many bars on the left and right should be used to confirm a pivot. - **highLineColor:** Sets the color of the resistance trend line (default: **red**). - **lowLineColor:** Sets the color of the support trend line (default: **green**).
---
### **2️⃣ Detect Pivot Highs & Lows** ```pinescript pivotHigh = ta.pivothigh(leftLen, rightLen) pivotLow = ta.pivotlow(leftLen, rightLen) ``` - `ta.pivothigh(leftLen, rightLen)`: Detects a **pivot high** if it's the highest price in a certain range. - `ta.pivotlow(leftLen, rightLen)`: Detects a **pivot low** if it's the lowest price in a certain range.
---
### **3️⃣ Store the Last Two Pivot Points** #### **🔺 Storing Resistance (Pivot Highs)** ```pinescript var float lastPivotHigh1 = na var int lastPivotHighIndex1 = na var float lastPivotHigh2 = na var int lastPivotHighIndex2 = na ``` - These variables store **the last two pivot highs** and their **bar indices** (position on the chart).
#### **🔻 Storing Support (Pivot Lows)** ```pinescript var float lastPivotLow1 = na var int lastPivotLowIndex1 = na var float lastPivotLow2 = na var int lastPivotLowIndex2 = na ``` - These variables store **the last two pivot lows** and their **bar indices**.
---
### **4️⃣ Update Pivot Points When New Ones Are Found** #### **Updating Resistance (Pivot Highs)** ```pinescript if not na(pivotHigh) lastPivotHigh2 := lastPivotHigh1 lastPivotHighIndex2 := lastPivotHighIndex1 lastPivotHigh1 := pivotHigh lastPivotHighIndex1 := bar_index - rightLen ``` - If a new **pivot high** is found: - The **previous pivot** becomes `lastPivotHigh2`. - The **new pivot** becomes `lastPivotHigh1`. - The index (`bar_index - rightLen`) marks where the pivot occurred.
#### **Updating Support (Pivot Lows)** ```pinescript if not na(pivotLow) lastPivotLow2 := lastPivotLow1 lastPivotLowIndex2 := lastPivotLowIndex1 lastPivotLow1 := pivotLow lastPivotLowIndex1 := bar_index - rightLen ``` - Similar to pivot highs, this section updates **pivot lows** dynamically.
---
### **5️⃣ Create and Update Trend Lines** #### **🔺 Drawing the Resistance Line** ```pinescript var line highLine = na if not na(lastPivotHigh2) and not na(lastPivotHigh1) if na(highLine) highLine := line.new(lastPivotHighIndex2, lastPivotHigh2, lastPivotHighIndex1, lastPivotHigh1, color=highLineColor, extend=extend.right) else line.set_xy1(highLine, lastPivotHighIndex2, lastPivotHigh2) line.set_xy2(highLine, lastPivotHighIndex1, lastPivotHigh1) line.set_color(highLine, highLineColor) ``` - If **two pivot highs** exist: - **First time:** Creates a new **resistance line** connecting them. - **Updates dynamically:** Adjusts the line when a new pivot appears.
#### **🔻 Drawing the Support Line** ```pinescript var line lowLine = na if not na(lastPivotLow2) and not na(lastPivotLow1) if na(lowLine) lowLine := line.new(lastPivotLowIndex2, lastPivotLow2, lastPivotLowIndex1, lastPivotLow1, color=lowLineColor, extend=extend.right) else line.set_xy1(lowLine, lastPivotLowIndex2, lastPivotLow2) line.set_xy2(lowLine, lastPivotLowIndex1, lastPivotLow1) line.set_color(lowLine, lowLineColor) ``` - Same logic applies for **support levels**, creating or updating a **green trend line**.
---
## **🔹 How to Use This Indicator** 1. **Apply the script in TradingView**: - Open **Pine Script Editor** → Paste the code → Click **"Add to Chart"**.
2. **Interpret the Lines**: - **Red line (Resistance):** Price may struggle to break above it. - **Green line (Support):** Price may bounce off it.
3. **Trading Strategy**: - **Breakout Strategy:** - If the price **breaks resistance**, expect a bullish move. - If the price **breaks support**, expect a bearish move. - **Reversal Trading:** - Look for **bounces off support/resistance** for potential reversals.
---
## **🔹 Key Features of This Indicator** ✅ **Automatically detects pivot highs and lows.** ✅ **Draws real-time trend lines for support and resistance.** ✅ **Updates dynamically with new price action.** ✅ **Customizable settings for pivot sensitivity and colors.**
This indicator is useful for **trend traders, breakout traders, and support/resistance traders**. 🚀
Let me know if you need **further improvements or additional features!** 😊