Author: Copyright � 2007, MetaQuotes Software Corp.
MAMA_FAMA

This script is designed as a specialized technical analysis tool for the MetaTrader platform. Its primary purpose is to identify market trends and price momentum by calculating two distinct smoothed lines on your trading chart: the MAMA (Mixed Adaptive Moving Average) and the FAMA (Fractal Adaptive Moving Average).

Core Logic Overview

Unlike standard moving averages that treat all past data with a fixed weight, this script is "adaptive." This means it automatically adjusts its sensitivity based on the current behavior of the market. It identifies whether the price is trending strongly, moving sideways (ranging), or experiencing rapid volatility, and changes its calculation style accordingly to reduce "noise" while staying responsive to new price movements.

How the Calculation Works

The script processes data through several layers of logic:

  • Price Averaging: The foundation begins by taking the midpoint of the high and low prices for each candle. This creates a balanced baseline for the calculations.
  • Smoothing & Detrending: The script applies a weighted smoothing formula to the price data. It then performs a "detrending" process, which essentially filters out long-term trends to isolate the immediate cycles and oscillations of the market.
  • Phase Analysis: A significant part of the logic involves calculating the "Phase" of the market. By analyzing the relationship between different smoothed components, the script determines the speed at which the price is moving. It looks for changes in the cycle's direction and velocity.
  • Adaptive Weighting (Alpha): The core intelligence lies in the calculation of a variable called "alpha." Depending on the detected phase shift, the script adjusts this alpha value.
    • In fast-moving markets, the script adapts to react quickly.
    • In slow or choppy markets, it increases smoothing to prevent false signals.
  • Final Output (MAMA and FAMA):
    • MAMA: This is the primary adaptive line. It uses the calculated alpha to blend the current price with previous values.
    • FAMA: This acts as a secondary, even smoother version of the MAMA. By applying an additional layer of averaging to the MAMA results, it provides a "buffer" that helps traders see the broader trend more clearly.

Practical Application

The relationship between these two lines provides visual cues for traders. Because they adapt to different speeds of market movement, the interaction between the MAMA and FAMA—such as when they cross each other or spread apart—serves as a dynamic indicator of momentum shifts and potential trend reversals.

Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
MAMA_FAMA
//+------------------------------------------------------------------+
//|                                                    MAMA_FAMA.mq4 |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.ru/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.ru/"

#property indicator_chart_window
#property indicator_buffers 2
#property indicator_color1 Red
#property indicator_color2 Blue

extern double    FastLimit=0.5;
extern double    SlowLimit=0.05;

//---- buffers
double MAMA[];
double FAMA[];
double SmoothPeriod[];
double Phase[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   IndicatorBuffers(4);
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,MAMA);
   SetIndexLabel(0,"MAMA");
   
   SetIndexStyle(1,DRAW_LINE);
   SetIndexBuffer(1,FAMA);
   SetIndexLabel(1,"FAMA");

   SetIndexBuffer(2,SmoothPeriod);
   SetIndexBuffer(3,Phase);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }

//+------------------------------------------------------------------+
//|  âû÷èñëèòü ìåäèàíó äëÿ áàðà ñ èíäåêñîì index                     |
//+------------------------------------------------------------------+
double Price(int index)
   {
   double res;
//----
   res=(High[index]+Low[index])/2.0;
//----
   return(res);   
   }

//+------------------------------------------------------------------+
//|  âû÷èñëèòü Smooth äëÿ áàðà ñ  èíäåêñîì i                         |
//+------------------------------------------------------------------+
double Smooth(int i)
   {
   double res;
//----
   res=(4*Price(i)+3*Price(i+1)+2*Price(i+2)+Price(i+3))/10.0;   
//----
   return(res);   
   }

//+------------------------------------------------------------------+
//|  âûñèëèòü Detrender æäÿ äàííîãî áàðà ñ çàäàííûì                  |
//+------------------------------------------------------------------+
double Detrender(int i, double ThatPer)
   {
   double res;
//----
   res=(0.0962*Smooth(i)+0.5769*Smooth(i+2)-0.5769*Smooth(i+4)-0.0962*Smooth(i+6))*(0.075*ThatPer+0.54);
//----
   return(res);   
   }
  
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int i,limit,counted_bars=IndicatorCounted();
   double periodD,prev_period,DeltaPhase,I1,Q1,thatPeriod;
   double alpha;
//----
   if (counted_bars==0) limit=Bars-27;
   if (counted_bars>0) limit=Bars-counted_bars;

   for (i=limit;i>=0;i--)
      {
      periodD=iCustom(NULL,0,"MAMA_Slave2",7,i);
      prev_period=iCustom(NULL,0,"MAMA_Slave2",7,i+1);
      thatPeriod=prev_period;
      Q1=iCustom(NULL,0,"MAMA_Slave2",6,i);
      I1=Detrender(i,thatPeriod);
      SmoothPeriod[i]=0.33*periodD+0.67*prev_period;
      if (I1!=0) Phase[i]=360*MathArctan(Q1/I1)/2*3.1415;
      DeltaPhase=Phase[i+1]-Phase[i];
      if (DeltaPhase<1) DeltaPhase=1;
      alpha=FastLimit/DeltaPhase;
      if (alpha<SlowLimit)  alpha=SlowLimit;
      MAMA[i]=alpha*Price(i)+(1-alpha)*MAMA[i+1];
      FAMA[i]=0.5*alpha*MAMA[i]+(1-0.5*alpha)*FAMA[i+1];
      }
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments