OPEN-SOURCE SCRIPT
EMA 20/50 Optimal Distance (M15)

EMA 20/50 Optimal Distance (M15) is a quantitative context filter designed to evaluate market direction and trading conditions in XAUUSD using exponential moving averages.
This indicator uses the 20 EMA and 50 EMA on the 15-minute timeframe to define two key components:
1. Market bias (direction)
2. Risk level based on EMA separation
--- DIRECTION LOGIC ---
- BUY:
EMA 20 > EMA 50 and price is above both averages.
- SELL:
EMA 20 < EMA 50 and price is below both averages.
- NO BIAS:
When price is between EMAs or alignment is unclear.
--- EMA DISTANCE (NORMALIZED) ---
The distance is calculated as the absolute difference between EMA 20 and EMA 50, normalized for XAUUSD volatility.
Classification:
- High Risk:
Distance < 0.8
→ Weak or ranging market conditions.
- Medium Risk:
Distance between 0.8 and 1.5
→ Acceptable but not optimal conditions.
- Low Risk (Optimal):
Distance > 1.5
→ Strong trend with higher probability of continuation.
---
--- WHAT MAKES THIS INDICATOR DIFFERENT ---
Unlike standard EMA crossover tools, this script introduces a quantitative layer by measuring the distance between moving averages to classify market conditions.
It is not designed to generate entry signals, but to provide a structured and measurable framework for:
- Filtering trades based on market quality
- Categorizing setups by risk conditions
- Supporting data-driven decision making
---
--- PURPOSE ---
This indicator is specifically built as a base tool for backtesting and quantitative strategy development.
It is intended to be used in combination with market structure models (such as BOS/CHOCH) and lower timeframe execution strategies.
The goal is to help traders:
- Collect structured data
- Identify statistical edge
- Optimize entries, trade management, and sessions based on real performance metrics
---
Designed for intraday trading in Gold (XAUUSD).
EMA 20/50 – Distancia Óptima (M15) es un indicador diseñado para evaluar el contexto direccional y la calidad del entorno de mercado en XAUUSD mediante medias móviles exponenciales.
Utiliza la EMA 20 y EMA 50 en temporalidad de 15 minutos para definir el sesgo de mercado y el nivel de riesgo según la distancia entre ambas.
Este indicador no genera entradas, sino que funciona como filtro cuantitativo para backtesting y desarrollo de estrategias basadas en estructura de mercado (BOS/CHOCH).
Permite analizar condiciones de mercado, clasificar operaciones y optimizar decisiones en base a datos reales.
This indicator uses the 20 EMA and 50 EMA on the 15-minute timeframe to define two key components:
1. Market bias (direction)
2. Risk level based on EMA separation
--- DIRECTION LOGIC ---
- BUY:
EMA 20 > EMA 50 and price is above both averages.
- SELL:
EMA 20 < EMA 50 and price is below both averages.
- NO BIAS:
When price is between EMAs or alignment is unclear.
--- EMA DISTANCE (NORMALIZED) ---
The distance is calculated as the absolute difference between EMA 20 and EMA 50, normalized for XAUUSD volatility.
Classification:
- High Risk:
Distance < 0.8
→ Weak or ranging market conditions.
- Medium Risk:
Distance between 0.8 and 1.5
→ Acceptable but not optimal conditions.
- Low Risk (Optimal):
Distance > 1.5
→ Strong trend with higher probability of continuation.
---
--- WHAT MAKES THIS INDICATOR DIFFERENT ---
Unlike standard EMA crossover tools, this script introduces a quantitative layer by measuring the distance between moving averages to classify market conditions.
It is not designed to generate entry signals, but to provide a structured and measurable framework for:
- Filtering trades based on market quality
- Categorizing setups by risk conditions
- Supporting data-driven decision making
---
--- PURPOSE ---
This indicator is specifically built as a base tool for backtesting and quantitative strategy development.
It is intended to be used in combination with market structure models (such as BOS/CHOCH) and lower timeframe execution strategies.
The goal is to help traders:
- Collect structured data
- Identify statistical edge
- Optimize entries, trade management, and sessions based on real performance metrics
---
Designed for intraday trading in Gold (XAUUSD).
EMA 20/50 – Distancia Óptima (M15) es un indicador diseñado para evaluar el contexto direccional y la calidad del entorno de mercado en XAUUSD mediante medias móviles exponenciales.
Utiliza la EMA 20 y EMA 50 en temporalidad de 15 minutos para definir el sesgo de mercado y el nivel de riesgo según la distancia entre ambas.
Este indicador no genera entradas, sino que funciona como filtro cuantitativo para backtesting y desarrollo de estrategias basadas en estructura de mercado (BOS/CHOCH).
Permite analizar condiciones de mercado, clasificar operaciones y optimizar decisiones en base a datos reales.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.
Script open-source
Nello spirito di TradingView, l'autore di questo script lo ha reso open source, in modo che i trader possano esaminarne e verificarne la funzionalità. Complimenti all'autore! Sebbene sia possibile utilizzarlo gratuitamente, ricordiamo che la ripubblicazione del codice è soggetta al nostro Regolamento.
Declinazione di responsabilità
Le informazioni e le pubblicazioni non sono intese come, e non costituiscono, consulenza o raccomandazioni finanziarie, di investimento, di trading o di altro tipo fornite o approvate da TradingView. Per ulteriori informazioni, consultare i Termini di utilizzo.