OPEN-SOURCE SCRIPT

Dynamic Trend Line Pro

📌 Detailed Explanation of the TradingView Indicator Code**

This **Pine Script v6** indicator dynamically **draws trend lines** based on pivot highs and pivot lows, helping traders visualize market trends in real-time.

---

## **🔹 How the Indicator Works**
This script detects key **pivot points** (local highs and lows) in the price chart and connects them to draw **trend lines**. Here's a breakdown of the process:

1. **Detects Pivot Highs and Lows**:
- **Pivot High:** A local maximum where the price is higher than surrounding bars.
- **Pivot Low:** A local minimum where the price is lower than surrounding bars.

2. **Stores the Last Pivot Points**:
- The script remembers the **last pivot high** and **last pivot low** points to **draw dynamic trend lines**.

3. **Draws Trend Lines**:
- The **uptrend** (support) line is drawn from two **pivot lows**.
- The **downtrend** (resistance) line is drawn from two **pivot highs**.
- Trend lines are continuously updated and extended.

---

## **🔹 Code Breakdown**

### **1️⃣ Inputs for User Customization**
```pinescript
leftLen = input.int(2, 'Left Pivots')
rightLen = input.int(2, 'Right Pivots')
lineThickness = input.int(3, 'Trend Line Thickness')
lineTransparency = input.int(20, 'Line Transparency')
```
- **leftLen & rightLen:** Set the number of bars on the left and right of the pivot point to confirm its validity.
- **lineThickness:** Controls the **thickness** of the trend line.
- **lineTransparency:** Adjusts the **opacity** of the trend line (0 = fully opaque, 100 = fully transparent).

---

### **2️⃣ Detect Pivot Highs and Lows**
```pinescript
pivotHigh = ta.pivothigh(leftLen, rightLen)
pivotLow = ta.pivotlow(leftLen, rightLen)
```
- **ta.pivothigh(leftLen, rightLen):** Identifies a **pivot high**, where the price is higher than the surrounding bars.
- **ta.pivotlow(leftLen, rightLen):** Identifies a **pivot low**, where the price is lower than the surrounding bars.

---

### **3️⃣ Store the Last Pivot Points**
#### **Storing the Last Pivot High (Resistance)**
```pinescript
var float lastHP = na
var int lastHPIndex = na
```
- These variables store the most recent **pivot high** (`lastHP`) and its **bar index** (`lastHPIndex`).

#### **Storing the Last Pivot Low (Support)**
```pinescript
var float lastLP = na
var int lastLPIndex = na
```
- These variables store the most recent **pivot low** (`lastLP`) and its **bar index** (`lastLPIndex`).

---

### **4️⃣ Update Pivot Points When New Ones Are Found**
#### **Updating Pivot Highs (Resistance)**
```pinescript
if not na(pivotHigh)
lastHP := pivotHigh
lastHPIndex := bar_index - rightLen
```
- If a new **pivot high** is found:
- Update `lastHP` with the new pivot high.
- Update `lastHPIndex` with the current bar index.

#### **Updating Pivot Lows (Support)**
```pinescript
if not na(pivotLow)
lastLP := pivotLow
lastLPIndex := bar_index - rightLen
```
- Similarly, if a new **pivot low** is found:
- Update `lastLP` with the new pivot low.
- Update `lastLPIndex` with the current bar index.

---

### **5️⃣ Detect Trend Direction**
```pinescript
trendUp = lastLP > lastLP[1] and not na(lastLP[1])
trendDown = lastHP < lastHP[1] and not na(lastHP[1])
```
- **Uptrend Condition:** If the most recent pivot low (`lastLP`) is higher than the previous one, it indicates an uptrend.
- **Downtrend Condition:** If the most recent pivot high (`lastHP`) is lower than the previous one, it indicates a downtrend.

---

### **6️⃣ Drawing Trend Lines**
#### **Drawing the Uptrend (Support) Line**
```pinescript
if trendUp
if na(trendLine)
trendLine := line.new(lastLPIndex[1], lastLP[1], lastLPIndex, lastLP, color=color.new(color.green, lineTransparency), width=lineThickness, extend=extend.right)
else
line.set_xy1(trendLine, lastLPIndex[1], lastLP[1])
line.set_xy2(trendLine, lastLPIndex, lastLP)
line.set_color(trendLine, color.new(color.green, lineTransparency))
line.set_width(trendLine, lineThickness)
```
- **Uptrend Line:** If the uptrend is detected (`trendUp`), the script either creates a new **green trend line** connecting the last two pivot lows or updates the existing one with the latest points.

#### **Drawing the Downtrend (Resistance) Line**
```pinescript
if trendDown
if na(trendLine)
trendLine := line.new(lastHPIndex[1], lastHP[1], lastHPIndex, lastHP, color=color.new(color.red, lineTransparency), width=lineThickness, extend=extend.right)
else
line.set_xy1(trendLine, lastHPIndex[1], lastHP[1])
line.set_xy2(trendLine, lastHPIndex, lastHP)
line.set_color(trendLine, color.new(color.red, lineTransparency))
line.set_width(trendLine, lineThickness)
```
- **Downtrend Line:** If the downtrend is detected (`trendDown`), the script either creates a new **red trend line** connecting the last two pivot highs or updates the existing one with the latest points.

---

## **🔹 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 Trend Lines**:
- **Green Line (Support):** Indicates potential support levels. Price may **bounce** off this line.
- **Red Line (Resistance):** Indicates potential resistance levels. Price may **struggle** to break above this line.

3. **Trading Strategy**:
- **Breakout Strategy:**
- If the price **breaks resistance** (red line), it may signal a **bullish** move.
- If the price **breaks support** (green line), it may signal a **bearish** move.
- **Reversal Strategy:**
- Look for **bounces** off support or resistance for potential reversals.

---

## **🔹 Key Features of This Indicator**
✅ **Automatically detects pivot highs and lows.**
✅ **Real-time updates** as new pivot points form.
✅ **Customizable settings** for line thickness and transparency.
✅ Helps traders visualize key **support** and **resistance** levels.

This indicator is perfect for **trend traders**, **support/resistance traders**, and anyone interested in **breakout** or **reversal strategies**. 🚀

Clause de non-responsabilité