OPEN-SOURCE SCRIPT
Updated

4H Swing Trade EMA Trend Strategy

The XAUUSD EMA Trend Strategy indicator is designed to identify market trends and provide buy and sell signals for XAUUSD (gold) based on Exponential Moving Averages (EMAs) and candlestick analysis. Here's how it works, broken down:

1. EMA Setup
Exponential Moving Averages (EMAs) are used to help determine the market trend.
Blue EMA: Calculated using the high prices with a length of 20 periods (emaHigh).
Yellow EMA: Calculated using the low prices with a length of 20 periods (emaLow).
Both EMAs are plotted on the chart in blue (for the high-based EMA) and yellow (for the low-based EMA).

2. Trend Detection
The trend is determined based on the position of the current price relative to the EMAs:
Uptrend: When the closing price is above the blue EMA (high-based EMA).
Downtrend: When the closing price is below the yellow EMA (low-based EMA).
Sideways Trend: If neither the uptrend nor downtrend conditions are met, the market is considered sideways.

3. Buy and Sell Signal Conditions

Buy Signal:
The market is in an uptrend (price is above the blue EMA).
The close price is above the blue EMA, and the candlestick body (close > open) closes above the blue EMA.

Sell Signal:
The market is in a downtrend (price is below the yellow EMA).
The close price is below the yellow EMA, and the candlestick body (close < open) closes below the yellow EMA.
No Signal in Sideways Market: The strategy avoids generating buy or sell signals when the market is in a sideways trend.

4. Stop Loss (SL) and Take Profit (TP) Calculation
Risk-to-Reward Ratio: A ratio of 1.5 is used for setting the take profit (TP) and stop loss (SL).
For Buy Signals:
The stop loss (SL) is set at the previous swing low.
The take profit (TP) is calculated as the entry price plus 1.5 times the distance between the entry price and the previous swing low.
For Sell Signals:
The stop loss (SL) is set at the previous swing high.
The take profit (TP) is calculated as the entry price minus 1.5 times the distance between the entry price and the previous swing high.

5. Signal Labels
Buy and Sell Labels:
When a buy or sell signal is triggered, a label is displayed on the chart, either below or above the candlestick:
Buy label appears below the candlestick with the corresponding stop loss (SL) and take profit (TP) values.
Sell label appears above the candlestick with the corresponding stop loss (SL) and take profit (TP) values.
A line is drawn between the candlestick and the label for clarity.

6. Trend Table
A table is displayed in the upper-right corner of the chart showing the current market trend:
Uptrend: When the price is above the blue EMA.
Downtrend: When the price is below the yellow EMA.
Sideways: When neither of the above conditions are met.

7. Shape Plotting
Buy Signal Shape: A green label is plotted below the candlestick for a buy signal.
Sell Signal Shape: A red label is plotted above the candlestick for a sell signal.


Summary of How the Strategy Works:

Trend Identification: The strategy uses two EMAs (one based on the highs, and the other based on the lows) to identify whether the market is in an uptrend, downtrend, or sideways.
Entry Signals: Buy and sell signals are generated based on the price's position relative to the EMAs and the candlestick's closing price.
SL/TP Calculation: A risk-to-reward ratio of 1:1.5 is applied, with SL and TP set based on previous swings.
Trend Table: The strategy displays the current trend in the upper-right corner for easy tracking.

This setup allows you to track and trade trends effectively on XAUUSD with clear signals and a risk management strategy built-in.
Release Notes
Explanation of Changes:

Alert Conditions:

alertcondition is added for both Buy and Sell signals.

  • Buy Signal: Triggers an alert when the buySignal condition is met. The alert message includes the entry price, Stop Loss (SL), and Take Profit (TP) values.

  • Sell Signal: Triggers an alert when the sellSignal condition is met. The alert message includes the entry price, Stop Loss (SL), and Take Profit (TP) values.

Disclaimer