This script is a specialized technical analysis tool designed to help traders identify trends and potential entry points in financial markets. It works by calculating a sophisticated moving average that adapts to market conditions, specifically focusing on distinguishing between actual price trends and "market noise."
How the Logic Works
1. Intelligent Smoothing (The AMA) Unlike standard moving averages that simply look at past prices, this script uses an algorithm to measure the relationship between current price movement and the volatility of the recent past. It calculates a "signal-to-noise" ratio. If the market is moving steadily in one direction, the indicator identifies it as a trend; if the price is jumping around randomly (noise), the indicator reacts differently to prevent giving false signals.
2. Dynamic Adaptation The script uses two different speed settings (Fast and Slow) to balance responsiveness. It calculates how quickly the indicator should react to new information. By comparing these speeds, the tool creates a smoothed line that follows the price closely enough to be relevant but smoothly enough to ignore minor, insignificant fluctuations.
3. Trend Identification & Signal Generation The core logic tracks the direction of this adaptive moving average:
- Buy Signals: When the average moves upward significantly past a specific threshold, the script identifies a potential "Buy" opportunity.
- Sell Signals: When the average moves downward significantly past that same threshold, it identifies a potential "Sell" opportunity.
Visual and Audible Alerts
To ensure traders don't have to stare at the charts constantly, the script includes an automated notification system:
- On-Screen Markers: It places visual arrows on the chart to highlight exactly where the trend shift occurs.
- Audio & Text Alerts: When a new signal is triggered, the script can play an audible alert and display a text comment on the screen showing the current asset name, timeframe, and the specific price at which the signal appeared.
- Smart Filtering: The script includes logic to ensure it only alerts you once per trend change, preventing your screen from being flooded with repetitive notifications during a single move.
//+------------------------------------------------------------------+
//| AMA&AMAsig.mq4 |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2004, by GOODMAN & Mstera è AF "
#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Sienna
#property indicator_color2 Green
#property indicator_color3 Red
#property indicator_width1 2
#property indicator_width2 1
#property indicator_width3 1
//---- input parameters
extern int periodAMA=10;
extern int nfast=2;
extern int nslow=30;
extern double G=2.0;
extern double dK=2.0;
//---- buffers
double kAMAbuffer[];
double kAMAupsig[];
double kAMAdownsig[];
extern bool UseSound = True;
extern string SoundFile = "expert.wav";
//+------------------------------------------------------------------+
int cbars=0, prevbars=0, prevtime=0;
double slowSC,fastSC;
bool SoundBuy = False;
bool SoundSell = False;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,159);
SetIndexStyle(2,DRAW_ARROW);
SetIndexArrow(2,159);
//SetIndexDrawBegin(0,nslow+nfast);
SetIndexBuffer(0,kAMAbuffer);
SetIndexBuffer(1,kAMAupsig);
SetIndexBuffer(2,kAMAdownsig);
IndicatorDigits(4);
//slowSC=0.064516;
//fastSC=0.2;
//cbars=IndicatorCounted();
//----
return(0);
}
int last_dir = 0;
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,pos=0;
double noise=0.000000001,AMA,AMA0,signal,ER;
double dSC,ERSC,SSC,ddK;
if (prevbars==Bars) return(0);
//---- TODO: add your code here
slowSC=(2.0 /(nslow+1));
fastSC=(2.0 /(nfast+1));
cbars=IndicatorCounted();
if (Bars<=(periodAMA+2)) return(0);
//---- check for possible errors
if (cbars<0) return(-1);
//---- last counted bar will be recounted
if (cbars>0) cbars--;
pos=Bars-periodAMA-2;
AMA0=Close[pos+1];
int dir = 0;
while (pos>=0)
{
if(pos==Bars-periodAMA-2) AMA0=Close[pos+1];
signal=MathAbs(Close[pos]-Close[pos+periodAMA]);
noise=0.000000001;
for(i=0;i<periodAMA;i++)
{
noise=noise+MathAbs(Close[pos+i]-Close[pos+i+1]);
}
ER =signal/noise;
dSC=(fastSC-slowSC);
ERSC=ER*dSC;
SSC=ERSC+slowSC;
AMA=AMA0+(MathPow(SSC,G)*(Close[pos]-AMA0));
kAMAbuffer[pos]=AMA;
ddK=(AMA-AMA0);
if ((MathAbs(ddK)) > (dK*Point) && (ddK > 0)) kAMAupsig[pos] =AMA; else kAMAupsig[pos]=0;
if ((MathAbs(ddK)) > (dK*Point) && (ddK < 0)) kAMAdownsig[pos]=AMA; else kAMAdownsig[pos]=0;
AMA0=AMA;
pos--;
}
if (kAMAupsig[0] != EMPTY_VALUE && kAMAupsig[0] != 0 && SoundBuy)
{
SoundBuy = False;
if (UseSound) PlaySound (SoundFile);
Comment (Symbol()," ",Period()," hellkas Buy @ ",Ask);
Alert (Symbol()," ",Period()," hellkas Buy @ ",Ask);
}
if (!SoundBuy && (kAMAupsig[0] == EMPTY_VALUE || kAMAupsig[0] == 0)) SoundBuy = True;
if (kAMAdownsig[0] != EMPTY_VALUE && kAMAdownsig[0] != 0 && SoundSell)
{
SoundSell = False;
if (UseSound) PlaySound (SoundFile);
Comment (Symbol()," ",Period(), " hellkas Sell @",Bid);
Alert (Symbol()," ",Period(), " hellkas Sell @",Bid);
}
if (!SoundSell && (kAMAdownsig[0] == EMPTY_VALUE || kAMAdownsig[0] == 0)) SoundSell = True;
prevbars=Bars;
return(0);
}
Comments