### Detailed Description of the Script
#### **Title:**
"**Bullish and Bearish Engulfing with Conditional Rays and Wick Markers**"
#### **Purpose:**
This Pine Script indicator identifies and marks **bullish and bearish engulfing candle patterns** on a price chart. Additionally, it plots **two rays** (lines) for each detected pattern: one at the wick's extremity and another at the midpoint of the wick and the candle body. The indicator manages the display of these rays based on specific conditions, ensuring they remain on the chart until price action invalidates them.
#### **Components and Functionality:**
1. **Inputs and Customization:**
- **Lookback Period:** The script looks back 100 candles to manage and maintain rays on the chart.
- **Ray Colors:**
- `Bullish Ray Color`: Grey (default) for the ray at the low wick of the bullish engulfing candle.
- `Bearish Ray Color`: Grey (default) for the ray at the high wick of the bearish engulfing candle.
- `Bullish Midpoint Ray Color`: Blue (default) for the ray plotted at the midpoint between the body low and the low wick of the bullish engulfing candle.
- `Bearish Midpoint Ray Color`: Orange (default) for the ray plotted at the midpoint between the body high and the high wick of the bearish engulfing candle.
2. **Engulfing Candle Detection:**
- **Bullish Engulfing:**
- Occurs when a previous bearish candle (closing lower than its open) is followed by a candle that closes above the high of the previous candle and has a low equal to or lower than the previous candle's low.
- **Bearish Engulfing:**
- Occurs when a previous bullish candle (closing higher than its open) is followed by a candle that closes below the low of the previous candle and has a high equal to or higher than the previous candle's high.
3. **Ray Plotting:**
- **Wick Rays:**
- For a **bullish engulfing candle**, a ray is plotted at the **low wick** of the bullish candle.
- For a **bearish engulfing candle**, a ray is plotted at the **high wick** of the bearish candle.
- **Midpoint Rays:**
- For a **bullish engulfing candle**, a midpoint ray is plotted at the midpoint between the **body low** (the lower of the open or close) and the **low wick**.
- For a **bearish engulfing candle**, a midpoint ray is plotted at the midpoint between the **body high** (the higher of the open or close) and the **high wick**.
4. **Ray Management and Deletion:**
- **Bullish Rays:**
- The rays (both wick and midpoint) are removed if a subsequent candle's close falls below the **bullish ray** level (low wick of the bullish engulfing candle).
- **Bearish Rays:**
- The rays (both wick and midpoint) are removed if a subsequent candle's close rises above the **bearish ray** level (high wick of the bearish engulfing candle).
5. **Visualization on the Chart:**
- **Bullish Engulfing Signal:**
- A **plus sign** in **dark grey** is plotted below the candle to signify a detected bullish engulfing pattern.
- **Bearish Engulfing Signal:**
- An **X** in **dark grey** is plotted above the candle to signify a detected bearish engulfing pattern.
### **Overall Workflow:**
1. The script detects bullish and bearish engulfing patterns based on specific candle relationships.
2. It then plots rays corresponding to the wicks and midpoints of these engulfing candles.
3. These rays remain on the chart until the price invalidates them, ensuring that only relevant and active levels are displayed to the trader.
This indicator provides traders with a visual tool to identify key reversal patterns (engulfing patterns) and the critical price levels associated with them, which can be useful for making trading decisions.