PROTECTED SOURCE SCRIPT
Vegas plus by stanley

This Pine Script implements a comprehensive trend-following strategy known popularly as the **Vegas Tunnel Method**. It combines multiple Exponential Moving Averages (EMAs) to define trends, pullbacks, and breakouts.
Here is a step-by-step walkthrough of how the code works, broken down by its components and logic.
---
### 1. The Anatomy (The Indicators)
The script uses three distinct groups of Moving Averages to define the market structure.
#### A. The Fast EMAs (The Trigger & Exit)
* **EMA 12 (Signal):** The fastest line. It is used to trigger entries (crossing the tunnel).
* **EMA 21 (Exit):** Used as a trailing stop. If the price crosses this line against your trade, the script signals an exit.
* **EMA 55 (Filter):** A medium-term filter, often used visually to gauge trend health.
#### B. The "Hero" Tunnel (The Action Zone)
* **EMAs 144 & 169 & 200:** These creates the main "Tunnel."
* **Function:** This acts as dynamic Support and Resistance.
* **Bullish:** If the 144 (Top) is above the 200 (Bottom), the tunnel is painted Blue.
* **Bearish:** If the 144 is below the 200, it is painted Red.
#### C. The "Anchor" Tunnel (The Deep Trend)
* **EMAs 576 & 676:** This creates a massive, slow-moving background tunnel.
* **Function:** It tells you the long-term trend. Generally, you only want to take Buy signals if price is above this Anchor, though the script logic focuses primarily on the Hero tunnel for triggers.
---
### 2. State Memory (`var` Variables)
This is a sophisticated part of the script. It uses `var` variables to "remember" where the price was in the past.
* `originPrice`: Remembers if the price was last seen **Above** (1) or **Below** (-1) the tunnel.
* `originEMA`: Remembers if the EMA 12 was last seen **Above** (1) or **Below** (-1) the tunnel.
**Why is this needed?**
To distinguish between a **Breakout** (crossing from Bear to Bull) and a **Pullback** (already Bull, dipped into tunnel, and coming back out).
---
### 3. The Four Entry Triggers
The script looks for four specific scenarios to generate a Buy or Sell signal. You can turn these on/off in the settings.
#### Trigger 1: Price U-Turn (Trend Continuation)
* **Logic:** The Price was *already* above the tunnel (`originPrice == 1`), dipped down, and is now crossing back up (`crossover`).
* **Meaning:** This is a classic "Buy the Dip" signal within an existing trend.
#### Trigger 2: EMA U-Turn (Lagging Confirmation)
* **Logic:** Similar to Trigger 1, but uses the **EMA 12** line instead of the Price candle.
* **Meaning:** This is safer but slower. It waits for the average price to curl back out of the tunnel.
#### Trigger 3: Breakthrough (Momentum Shift)
* **Logic:** The EMA 12 was previously *below* the tunnel (`originEMA == -1`) and has just crossed *above* it (`crossover`).
* **Meaning:** This is a Trend Reversal signal. The market has shifted from Bearish to Bullish.
#### Trigger 4: Wick Rejection (Touch & Go)
* **Logic:**
1. Price is generally above the tunnel.
2. The `Low` of the current candle touches the tunnel.
3. The `Low` of the *previous* candle did NOT touch the tunnel.
4. The candle closes *outside* (above) the tunnel.
* **Meaning:** The price tested the support zone and was immediately rejected (bounced off), leaving a wick.
---
### 4. Trade Management (State Machine)
The script uses a variable called `tradeState` to manage signals so they don't spam your chart.
* `tradeState = 0`: Flat (No position).
* `tradeState = 1`: Long.
* `tradeState = -1`: Short.
**The Rules:**
1. **Entry:** If `validLong` is triggered AND `tradeState` is not already 1 -> Change state to 1 (Long) and plot a **BUY** label.
2. **Holding:** If you are already in State 1, the script ignores new Buy signals.
3. **Exit:** If `tradeState` is 1 AND price closes below EMA 21 -> Change state to 0 (Flat) and plot an **Exit L** label.
---
### 5. Visual Summary
* **Green Label:** Buy Signal (Long Entry).
* **Red Label:** Sell Signal (Short Entry).
* **Grey X:** Exit Signal (Close the position).
* **Blue/Red Tunnel:** The "Hero" tunnel (144/169/200).
* **Grey Background Tunnel:** The "Anchor" tunnel (576/676).
### How to read the signals:
You are looking for the price to interact with the **Hero Tunnel** (the thinner, brighter one).
1. **Trend:** Look at the slope of the Anchor (thick grey) tunnel.
2. **Setup:** Wait for price to come back to the Hero Tunnel.
3. **Trigger:** Wait for a **Green Label**. This means the price dipped into the tunnel and is now blasting out (U-Turn), or has rejected the tunnel (Wick), or has broken through a new trend (Breakthrough).
4. **Exit:** Close the trade when the **Grey X** appears (Price crosses the EMA 21).
Here is a step-by-step walkthrough of how the code works, broken down by its components and logic.
---
### 1. The Anatomy (The Indicators)
The script uses three distinct groups of Moving Averages to define the market structure.
#### A. The Fast EMAs (The Trigger & Exit)
* **EMA 12 (Signal):** The fastest line. It is used to trigger entries (crossing the tunnel).
* **EMA 21 (Exit):** Used as a trailing stop. If the price crosses this line against your trade, the script signals an exit.
* **EMA 55 (Filter):** A medium-term filter, often used visually to gauge trend health.
#### B. The "Hero" Tunnel (The Action Zone)
* **EMAs 144 & 169 & 200:** These creates the main "Tunnel."
* **Function:** This acts as dynamic Support and Resistance.
* **Bullish:** If the 144 (Top) is above the 200 (Bottom), the tunnel is painted Blue.
* **Bearish:** If the 144 is below the 200, it is painted Red.
#### C. The "Anchor" Tunnel (The Deep Trend)
* **EMAs 576 & 676:** This creates a massive, slow-moving background tunnel.
* **Function:** It tells you the long-term trend. Generally, you only want to take Buy signals if price is above this Anchor, though the script logic focuses primarily on the Hero tunnel for triggers.
---
### 2. State Memory (`var` Variables)
This is a sophisticated part of the script. It uses `var` variables to "remember" where the price was in the past.
* `originPrice`: Remembers if the price was last seen **Above** (1) or **Below** (-1) the tunnel.
* `originEMA`: Remembers if the EMA 12 was last seen **Above** (1) or **Below** (-1) the tunnel.
**Why is this needed?**
To distinguish between a **Breakout** (crossing from Bear to Bull) and a **Pullback** (already Bull, dipped into tunnel, and coming back out).
---
### 3. The Four Entry Triggers
The script looks for four specific scenarios to generate a Buy or Sell signal. You can turn these on/off in the settings.
#### Trigger 1: Price U-Turn (Trend Continuation)
* **Logic:** The Price was *already* above the tunnel (`originPrice == 1`), dipped down, and is now crossing back up (`crossover`).
* **Meaning:** This is a classic "Buy the Dip" signal within an existing trend.
#### Trigger 2: EMA U-Turn (Lagging Confirmation)
* **Logic:** Similar to Trigger 1, but uses the **EMA 12** line instead of the Price candle.
* **Meaning:** This is safer but slower. It waits for the average price to curl back out of the tunnel.
#### Trigger 3: Breakthrough (Momentum Shift)
* **Logic:** The EMA 12 was previously *below* the tunnel (`originEMA == -1`) and has just crossed *above* it (`crossover`).
* **Meaning:** This is a Trend Reversal signal. The market has shifted from Bearish to Bullish.
#### Trigger 4: Wick Rejection (Touch & Go)
* **Logic:**
1. Price is generally above the tunnel.
2. The `Low` of the current candle touches the tunnel.
3. The `Low` of the *previous* candle did NOT touch the tunnel.
4. The candle closes *outside* (above) the tunnel.
* **Meaning:** The price tested the support zone and was immediately rejected (bounced off), leaving a wick.
---
### 4. Trade Management (State Machine)
The script uses a variable called `tradeState` to manage signals so they don't spam your chart.
* `tradeState = 0`: Flat (No position).
* `tradeState = 1`: Long.
* `tradeState = -1`: Short.
**The Rules:**
1. **Entry:** If `validLong` is triggered AND `tradeState` is not already 1 -> Change state to 1 (Long) and plot a **BUY** label.
2. **Holding:** If you are already in State 1, the script ignores new Buy signals.
3. **Exit:** If `tradeState` is 1 AND price closes below EMA 21 -> Change state to 0 (Flat) and plot an **Exit L** label.
---
### 5. Visual Summary
* **Green Label:** Buy Signal (Long Entry).
* **Red Label:** Sell Signal (Short Entry).
* **Grey X:** Exit Signal (Close the position).
* **Blue/Red Tunnel:** The "Hero" tunnel (144/169/200).
* **Grey Background Tunnel:** The "Anchor" tunnel (576/676).
### How to read the signals:
You are looking for the price to interact with the **Hero Tunnel** (the thinner, brighter one).
1. **Trend:** Look at the slope of the Anchor (thick grey) tunnel.
2. **Setup:** Wait for price to come back to the Hero Tunnel.
3. **Trigger:** Wait for a **Green Label**. This means the price dipped into the tunnel and is now blasting out (U-Turn), or has rejected the tunnel (Wick), or has broken through a new trend (Breakthrough).
4. **Exit:** Close the trade when the **Grey X** appears (Price crosses the EMA 21).
Protected script
This script is published as closed-source. However, you can use it freely and without any limitations – learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.
Protected script
This script is published as closed-source. However, you can use it freely and without any limitations – learn more here.
Disclaimer
The information and publications are not meant to be, and do not constitute, financial, investment, trading, or other types of advice or recommendations supplied or endorsed by TradingView. Read more in the Terms of Use.