CMA
Download scriptThis script functions as a visual momentum and trend-tracking tool designed to smooth out market noise and identify clear directional shifts in price action. Instead of simply following the raw price, it uses a mathematical "smoothing" logic to calculate a refined path, helping traders distinguish between significant moves and minor fluctuations.
How the Logic Works
1. Dynamic Smoothing Calculation The core of this tool is its ability to adjust how closely it follows the market price based on volatility. It calculates two distinct values:
- Market Volatility: It looks at the standard deviation (the "spread" or "stretch") of the market over a specific period.
- Price Deviation: It measures how far the current price has moved away from a simple moving average.
By comparing these two values, the script determines a "weighting factor." When volatility is low relative to the price move, the indicator responds more aggressively to the trend. When volatility is high, the indicator becomes smoother and more stable. This prevents the tool from making jagged, erratic movements during choppy market conditions.
2. Trend Detection and Directional Flow Once the smoothed path is calculated, the script monitors the "flow" of that path. It constantly checks whether the current value is higher or lower than the previous one:
- Upward Momentum: If the path is moving higher, the logic identifies an bullish trend.
- Downward Momentum: If the path is moving lower, it identifies a bearish trend.
3. Visual Signaling The script translates these mathematical calculations into three distinct visual elements on your chart:
- The Primary Path: A continuous line representing the smoothed price action described above.
- Trend Highlights: The script uses two separate "buffers" to track upward and downward movements independently. This allows the tool to visually highlight specific segments of the trend, making it easier to see exactly where a shift from "up" to "down" occurred.
Summary for the Trader
Essentially, this tool acts as a sophisticated filter. It takes the raw, messy data of the market and processes it through a volatility-adjusted smoothing formula. The result is a clearer visual representation of the trend, intended to help you identify whether the prevailing momentum is moving higher or lower while filtering out the "noise" that often leads to false signals in standard moving averages.
//+------------------------------------------------------------------+
//| CMA.mq4 |
//| Raff |
//| just_raff1410@yahoo.com |
//+------------------------------------------------------------------+
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 Aqua
#property indicator_color3 Red
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 3
extern int Ma_Period = 2;
extern int Ma_PeriodStd = 5;
extern int Ma_Method = 3;
extern int Ma_Price = 4;
extern int Count_bars = 1500;
double K;
double v1;
double v2;
//---- buffers
double CA[];
double DIR[];
double UpBuffer[];
double DnBuffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
IndicatorBuffers(4);
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(0,CA);
SetIndexBuffer(1,UpBuffer);
SetIndexBuffer(2,DnBuffer);
SetIndexBuffer(3,DIR);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
if (Count_bars+1>Bars-2) Count_bars=Bars-2;
CA[Count_bars+1]=Close[Count_bars+1];
for(int i=Count_bars;i>=0;i--)
{
v1=MathSqrt(iStdDev(NULL,0,Ma_PeriodStd,0,Ma_Method,Ma_Price,i));
v2=MathSqrt(MathAbs(CA[i+1]-iMA(NULL,0,Ma_Period,0,Ma_Method,Ma_Price,i)));
if (v2<v1) {K=0;}
else {K=1-(v1/v2);}
CA[i]=CA[i+1]+K*(iMA(NULL,0,Ma_Period,0,Ma_Method,Ma_Price,i)-CA[i+1]);
DIR[i]=DIR[i+1];
if (CA[i]-CA[i+1] > 0) DIR[i]= 1;
if (CA[i+1]-CA[i] > 0) DIR[i]=-1;
if (DIR[i]>0)
{
UpBuffer[i] = CA[i];
if (DIR[i+1]<0) UpBuffer[i+1]=CA[i+1];
DnBuffer[i] = EMPTY_VALUE;
}
else if (DIR[i]<0)
{
DnBuffer[i] = CA[i];
if (DIR[i+1]>0) DnBuffer[i+1]=CA[i+1];
UpBuffer[i] = EMPTY_VALUE;
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments