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 de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.
Script de código aberto
Em verdadeiro espírito do TradingView, o criador deste script o tornou de código aberto, para que os traders possam revisar e verificar sua funcionalidade. Parabéns ao autor! Embora você possa usá-lo gratuitamente, lembre-se de que a republicação do código está sujeita às nossas Regras da Casa.
Aviso legal
As informações e publicações não se destinam a ser, e não constituem, conselhos ou recomendações financeiras, de investimento, comerciais ou de outro tipo fornecidos ou endossados pela TradingView. Leia mais nos Termos de Uso.