AMA_for_Expert2

Download script
Author: Copyright � 2006, MetaQuotes Software Corp.
AMA_for_Expert2

This script is designed as a sophisticated technical analysis tool for MetaTrader, specifically created to help traders identify market trends and potential turning points by analyzing price movement patterns. It operates by calculating a dynamic moving average that adapts to market volatility rather than just following the price blindly.

Here is a breakdown of how the logic functions:

Core Price Selection

The script begins by allowing the user to define which type of price data it should "watch." Instead of only looking at where a candle closes, it can be configured to track opening prices, highs, lows, or various averages (like the median or weighted average). This flexibility allows the tool to be tuned to different trading styles.

Adaptive Smoothing Logic

The primary engine of this script is its ability to distinguish between "noise" and actual market movement:

  • Volatility Tracking: The script calculates the absolute change in price from one candle to the next. It then measures the "noise" (the average intensity of these changes) over a specific period.
  • Error Rate Calculation: It determines how much the current price deviates from the expected trend relative to that noise level.
  • Dynamic Weighting: Based on this deviation, the script calculates a smoothing factor. When the market is stable and moving predictably, the average stays smooth. When the market becomes volatile or experiences sharp shifts, the script adjusts its sensitivity automatically.

Trend and Signal Generation

Beyond just drawing a single line, the logic performs complex secondary calculations to identify significant "breakouts":

  • Standard Deviation Analysis: The script maintains a running calculation of the standard deviation of the moving average itself. This tells the tool how much the trend is "wobbling."
  • Breakout Detection: It compares the current change in the moving average against the calculated standard deviation. If the movement exceeds a certain threshold (set by the user), it interprets this as a significant momentum shift.
  • Visual Signaling: When these significant shifts occur, the script identifies them as "Up" or "Down" movements. On the trading chart, these are represented by specific visual markers (arrows) to alert the trader to potential trend reversals or strong momentum entries.

Summary for the User

In simple terms, this tool doesn't just tell you what the price is doing; it tries to filter out the "jitter" of the market to show you the underlying direction. It is designed to provide a smoother trend line that reacts intelligently to volatility, providing visual alerts when the price makes a move significant enough to potentially signal a change in market direction.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
14 Views
0 Downloads
0 Favorites
AMA_for_Expert2
//+------------------------------------------------------------------+
//|                                              AMA for Expert2.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                Copyright © 2004, by konKop,wellx |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "optimized by Rosh"

#property indicator_chart_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Aqua
#property indicator_color3 Magenta
//---- input parameters
extern int       periodAMA=10;
extern double    nfast=2.0;
extern double    nslow=30.0;
extern double    G=2.0;
extern double    dK=2.0;
extern int       PriceType=0; // öåíà, îò êîòîðîé ñòðîèòñÿ 
//PRICE_CLOSE 0 Öåíà çàêðûòèÿ 
//PRICE_OPEN 1 Öåíà îòêðûòèÿ 
//PRICE_HIGH 2 Ìàêñèìàëüíàÿ öåíà 
//PRICE_LOW 3 Ìèíèìàëüíàÿ öåíà 
//PRICE_MEDIAN 4 Ñðåäíÿÿ öåíà, (high+low)/2 
//PRICE_TYPICAL 5 Òèïè÷íàÿ öåíà, (high+low+close)/3 
//PRICE_WEIGHTED 6 Âçâåøåííàÿ öåíà çàêðûòèÿ, (high+low+close+close)/4 

//---- buffers
double AMAbuffer[];
double upAMA[];
double downAMA[];
double AbsBuffer[];

double AMA2Buffer[];
double SumAMABuffer[];
double StdAMA[];

double slowSC,fastSC,dFS;

//+------------------------------------------------------------------+
//| âîçâðàùàåò öåíó                                                  |
//+------------------------------------------------------------------+
double Price(int shift)
  {
//----
   double res;
//----
   switch (PriceType)
      {
      case PRICE_OPEN: res=Open[shift]; break;
      case PRICE_HIGH: res=High[shift]; break;
      case PRICE_LOW: res=Low[shift]; break;
      case PRICE_MEDIAN: res=(High[shift]+Low[shift])/2.0; break;
      case PRICE_TYPICAL: res=(High[shift]+Low[shift]+Close[shift])/3.0; break;
      case PRICE_WEIGHTED: res=(High[shift]+Low[shift]+2*Close[shift])/4.0; break;
      default: res=Close[shift];break;
      }
   return(res);
  }
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(7);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,AMAbuffer);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,159);
   SetIndexBuffer(1,upAMA);
   SetIndexEmptyValue(1,0.0);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,159);
   SetIndexBuffer(2,downAMA);
   SetIndexEmptyValue(2,0.0);
   
   SetIndexBuffer(3,AbsBuffer);
   SetIndexBuffer(4,AMA2Buffer);
   SetIndexBuffer(5,SumAMABuffer);
   SetIndexBuffer(6,StdAMA);
   
   slowSC=(2.0 /(nslow+1));
   fastSC=(2.0 /(nfast+1));
   dFS=fastSC-slowSC;
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//----
   int i,limit,limit2;
   double Noise,ER,SSC;
   double SredneeAMA,SumKvadratAMA;
   double val1,val2;
   if (counted_bars>0) 
      {
      limit=Bars-counted_bars; 
      limit2=limit;
      }
   if (counted_bars==0)
      {
      limit=Bars-1;
      /*
      for (i=limit;i>=0;i--)
         {
         AMAbuffer[i]=0;
         upAMA[i]=0;
         downAMA[i]=0;
         AbsBuffer[i]=0;
         NoiseBuffer[i]=0;
         ERBuffer[i]=0;
         SSCBuffer[i]=0;
         StdAMA[i]=0;
         }
      ArrayInitialize(,)
      */
      limit2=Bars-periodAMA-1;
      }
   limit--;
   limit2--;
   //Print("limit=",limit);
   //Print("Ïîøåë Abs");
   for (i=limit;i>=0;i--)
      {
      AbsBuffer[i]=MathAbs(Price(i)-Price(i+1));
      }   
   //Print("Ïîøåë Noise");
   for (i=limit2;i>=0;i--)
      {
      Noise=iMAOnArray(AbsBuffer,0,periodAMA,0,MODE_SMA,i)*periodAMA;
      if (Noise!=0) ER=MathAbs(Price(i)-Price(i+periodAMA))/Noise; else ER=0;
      SSC=MathPow(ER*dFS+slowSC,G);
      AMAbuffer[i]=Price(i)*SSC+AMAbuffer[i+1]*(1-SSC);
      AMA2Buffer[i]=AMAbuffer[i]*AMAbuffer[i]+AMA2Buffer[i+1];// íàêàïëèâàåì ñóììó êâàäðàòîâ ÀÌû
      SumAMABuffer[i]=SumAMABuffer[i+1]+AMAbuffer[i];
      }   
   //Print("Ïîøåë Std");
   for (i=limit2;i>=0;i--)
      {
      val1=0;
      val2=0;
      SredneeAMA=(SumAMABuffer[i]-SumAMABuffer[i+periodAMA])/periodAMA;
      SumKvadratAMA=AMA2Buffer[i]-AMA2Buffer[i+periodAMA];
      StdAMA[i]=MathSqrt(SumKvadratAMA/periodAMA-SredneeAMA*SredneeAMA);
      if (MathAbs(AMAbuffer[i]-AMAbuffer[i+1])>dK*StdAMA[i])
         {
         if (AMAbuffer[i]-AMAbuffer[i+1]>0) val1=AMAbuffer[i];
         else val2=AMAbuffer[i];
         } 
      upAMA[i]=val1;
      downAMA[i]=val2;
      }
   //Print("Çàêîí÷èëè");

//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments