9MAMA_NK
Download scriptThis script is a technical trading indicator designed to identify trends in financial markets by calculating two specific moving averages. It uses a sophisticated mathematical approach inspired by signal processing (specifically concepts similar to the MAMA and FAMA systems) to adapt to different market conditions.
Core Logic Overview
The primary goal of this script is to smooth out market "noise" while remaining responsive enough to detect real price movements. It achieves this through several layers of calculation:
1. Price Smoothing and Detrending Instead of looking at raw prices, the script first calculates a weighted average of the high and low prices. It then applies a "detrending" process, which filters out the general direction of the market to focus on the underlying cycles and fluctuations.
2. Signal Processing (Phase and Period Detection) The most complex part of the logic treats price movement like a radio wave or a physical signal. It breaks down the market data into two components:
- In-Phase: The primary direction of the movement.
- Quadrature: The secondary, perpendicular component of the movement.
By analyzing these two components, the script can determine the "Period" (the speed of the cycle) and the "Phase" (the position within that cycle). This allows the indicator to understand whether the market is moving in a fast, volatile manner or a slow, steady trend.
3. Dynamic Adaptation (The Alpha Value) The script uses the calculated phase changes to determine an "Alpha" value. This is a dynamic weight:
- If the market is changing direction rapidly, the indicator adjusts its weight to react more quickly.
- If the market is moving steadily, the indicator slows down to avoid reacting to insignificant price flickers (noise).
4. The Two Moving Averages The final output consists of two lines plotted on your chart:
- MAMA (The Primary Line): This line uses the dynamic Alpha value to follow the price closely while filtering out noise. It is designed to be highly adaptive.
- FAMA (The Secondary Line): This is a further smoothed version of the MAMA line. Because it is derived from the MAMA, it provides a "lagged" but very stable view of the trend, helping traders identify major structural shifts in the market.
Summary for the User
In simple terms, this script doesn't just draw a flat average line. It "listens" to the rhythm of the market. It calculates how fast and in what direction the price is vibrating, then adjusts its own speed to match that rhythm.
- The Blue Line (MAMA) follows the immediate trend dynamics.
- The Red Line (FAMA) acts as a stabilizer, showing the broader, filtered trend.
//+------------------------------------------------------------------+
//| Version July 20, 2006 |
//| Editing Nikolay Kositsin 15.06.2006 farria@mail.redcom.ru |
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| #MAMA_NK.mq4 |
//| |
//| http://forex.kbpauk.ru/ |
//+------------------------------------------------------------------+
#property link "http://forex.kbpauk.ru/"
//---- îòðèñîâêà èíäèêàòîðà â ãëàâíîì îêíå
#property indicator_chart_window
//---- êîëè÷åñòâî èíäèêàòîðíûõ áóôôåðîâ
#property indicator_buffers 2
//---- öâåòà èíäèêàòîðà
#property indicator_color1 Blue
#property indicator_color2 Red
//---- òîëùèíà èíäèêàòîðíûõ ëèíèé
#property indicator_width1 1
#property indicator_width2 1
//---- ÂÕÎÄÍÛÅ ÏÀÐÀÌÅÒÐÛ ÈÍÄÈÊÀÒÎÐÀ
extern double FastLimit = 0.5;
extern double SlowLimit = 0.05;
//---- èíäèêàòîðíûå áóôôåðû
double FABuffer[];
double MABuffer[];
//+------------------------------------------------------------------+
//| #MAMA initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- Ñòèëü èñïîëíåíèÿ ãðàôèêà
SetIndexStyle(0, DRAW_LINE, 0, 2);
SetIndexStyle(1, DRAW_LINE, 0, 2);
//---- 2 èíäèêàòîðíûõ áóôôåðà èñïîëüçîâàíû äëÿ ñ÷¸òà
SetIndexBuffer(0, FABuffer);
SetIndexBuffer(1, MABuffer);
//---- óñòàíîâêà çíà÷åíèé èíäèêàòîðà, êîòîðûå íå áóäóò âèäèìû íà ãðàôèêå
SetIndexEmptyValue(0, 0.0);
SetIndexEmptyValue(1, 0.0);
//---- èìÿ äëÿ îêîí äàííûõ è ëýéáà äëÿ ñóáúîêîí
IndicatorShortName("#MAMA_NK");
SetIndexLabel(0, "#FAMA_NK");
SetIndexLabel(1, "#MAMA_NK");
//---- óñòàíîâêà íîìåðà áàðà, íà÷èíàÿ ñ êîòîðîãî áóäåò îòðèñîâûâàòüñÿ èíäèêàòîð
SetIndexDrawBegin(0, 50);
SetIndexDrawBegin(1, 50);
//---- çàâåðøåíèå èíèöèàëèçàöèè
return(0);
}
//+------------------------------------------------------------------+
//| #MAMA |
//+------------------------------------------------------------------+
int start()
{
//---- ïðîâåðêà êîëè÷åñòâà áàðîâ íà äîñòàòî÷íîñòü äëÿ ðàñ÷¸òà
if(Bars <= 7)
return(0);
//---- ââåäåíèå ïåðåìåííûõ ïàìÿòè
static int time2;
static double Price[4][2], Smooth[7][2], Detrender[7][2], Q1[7][2];
static double I1[7][2], I2[3][2], Q2[3][2];
static double Re[2][2], Im[2][2], SmoothPeriod[2][2], Period_[3][2];
static double Phase[2][2], MAMA[2][2], FAMA[2][2];
//----+ Ââåäåíèå öåëûõ ïåðåìåííûõ è ïîëó÷åíèå óæå ïîäñ÷èòàííûõ áàðîâ
int MaxBar, limit, bar, iii, counted_bars = IndicatorCounted();
//---- ïðîâåðêà íà âîçìîæíûå îøèáêè
if(counted_bars < 0)
return(-1);
//---- ïîñëåäíèé ïîäñ÷èòàííûé áàð äîëæåí áûòü ïåðåñ÷èòàí
if(counted_bars > 0)
counted_bars--;
//----+ Ââåäåíèå ïåðåìåííûõ ñ ïëàâàþùåé òî÷êîé
double jI, jQ, DeltaPhase, alpha;
//---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà, íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçâåä¸í ïîëíûé ïåðåñ÷¸ò âñåõ áàðîâ
MaxBar = Bars - 1 - 7;
//---- îïðåäåëåíèå íîìåðà ñàìîãî ñòàðîãî áàðà, íà÷èíàÿ ñ êîòîðîãî áóäåò ïðîèçåä¸í ïåðåñ÷¸ò òîëüêî íîâûõ áàðîâ
limit = Bars - 1 - counted_bars;
//---- èíèöèàëèçàöèÿ íóëÿ
if(limit >= MaxBar)
{
for(iii = Bars - 1; iii > MaxBar; iii--)
{
FABuffer[bar] = 0;
MABuffer[bar] = 0;
limit = MaxBar;
}
}
//+--- âîññòàíîâëåíèå çíà÷åíèé ïåðåìåííûõ +====+
int Tnew = (int)Time[limit+1];
if(limit < MaxBar)
if(Tnew == time2)
{
for(iii = 0; iii < 4; iii++)
Price[iii][0] = Price[iii][1];
for(iii = 0; iii < 7; iii++)
{
Smooth[iii][0] = Smooth[iii][1];
Detrender[iii][0] = Detrender[iii][1];
Q1[iii][0] = Q1[iii][1];
I1[iii][0] = I1[iii][1];
}
for(iii = 0; iii < 2; iii++)
{
I2[iii][0] = I2[iii][1];
Q2[iii][0] = Q2[iii][1];
Re[iii][0] = Re[iii][1];
Im[iii][0] = Im[iii][1];
SmoothPeriod[iii][0] = SmoothPeriod[iii][1];
Period_[iii][0] = Period_[iii][1];
Phase[iii][0] = Phase[iii][1];
MAMA [iii][0] = MAMA [iii][1];
FAMA [iii][0] = FAMA [iii][1];
}
}
else
{
if(Tnew > time2)
Print("ERROR01");
else
Print("ERROR02");
return(-1);
}
//----
bar = limit;
while(bar >= 0)
{
//+--- Ñîõðàíåíèå çíà÷åíèé ïåðåìåííûõ +==========+
if(bar == 1)
if(((limit == 1) && (time2 == Time[2])) || (limit > 1))
{
for(iii = 0; iii < 4; iii++)
Price[iii][1] = Price[iii][0];
//----
for(iii = 0; iii < 7; iii++)
{
time2 = (int)Time[2];
Smooth[iii][1] = Smooth[iii][0];
Detrender[iii][1] = Detrender[iii][0];
Q1[iii][1] = Q1[iii][0];
I1[iii][1] = I1[iii][0];
}
//----
for(iii = 0; iii < 2; iii++)
{
I2[iii][1] = I2[iii][0];
Q2[iii][1] = Q2[iii][0];
Re[iii][1] = Re[iii][0];
Im[iii][1] = Im[iii][0];
SmoothPeriod[iii][1] = SmoothPeriod[iii][0];
Period_[iii][1] = Period_[iii][0];
Phase[iii][1] = Phase[iii][0];
MAMA [iii][1] = MAMA [iii][0];
FAMA [iii][1] = FAMA [iii][0];
}
}
//+---+==========================================+
for(iii = 0; iii < 4; iii++)
Price[iii][0] = (High[bar+iii] + Low[bar+iii]) / 2;
Smooth[0][0] = (4*Price[0][0] + 3*Price[1][0] + 2*Price[2][0] + Price[3][0]) / 10;
Detrender[0][0] = (0.0962*Smooth[0][0] + 0.5769*Smooth[2][0] - 0.5769*Smooth[4][0] -
0.0962*Smooth[6][0])*(0.075*Period_[2][0] + 0.54);
// {Compute InPhase and Quadrature components}
Q1[0][0] = (0.0962*Detrender[0][0] + 0.5769*Detrender[2][0] - 0.5769*Detrender[4][0] -
0.0962*Detrender[6][0])*(0.075*Period_[2][0] + 0.54);
I1[0][0] = Detrender[3][0];
// {Advance the phase of I1 and Q1 by 90 degrees}
jI = (0.0962*I1[0][0] + 0.5769*I1[2][0] - 0.5769*I1[4][0] -
0.0962*I1[6][0])*(0.075*Period_[1][0] + 0.54);
jQ = (0.0962*Q1[0][0] + 0.5769*Q1[2][0] - 0.5769*Q1[4][0] -
0.0962*Q1[6][0])*(0.075*Period_[1][0] + 0.54);
// {Phasor addition for 3 bar averaging)}
I2[0][0] = I1[0][0] - jQ;
Q2[0][0] = Q1[0][0] + jI;
// {Smooth the I and Q components before applying the discriminator}
I2[1][0] = 0.2*I2[1][0] + 0.8*I2[2][0];
Q2[1][0] = 0.2*Q2[1][0] + 0.8*Q2[2][0];
// {Homodyne Discriminator}
Re[0][0] = I2[0][0]*I2[1][0] + Q2[0][0]*Q2[1][0];
Im[0][0] = I2[0][0]*Q2[1][0] - Q2[0][0]*I2[1][0];
Re[0][0] = 0.2*Re[0][0] + 0.8*Re[1][0];
Im[0][0] = 0.2*Im[0][0] + 0.8*Im[1][0];
//----
if(Im[0][0] != 0 && Re[0][0] != 0)
Period_ [0][0]= 360 / MathArctan(Im[0][0] / Re[0][0]);
//----
if(Period_[0][0] > 1.5*Period_[1][0])
Period_[0][0] = 1.5*Period_[1][0];
//----
if(Period_[0][0] < 0.67*Period_[1][0])
Period_[0][0] = 0.67*Period_[1][0];
//----
if(Period_[0][0] < 6)
Period_[0][0] = 6;
//----
if(Period_[0][0] > 50)
Period_[0][0] = 50;
//----
Period_[0][0] = 0.2*Period_[0][0] + 0.8*Period_[1][0];
SmoothPeriod[0][0] = 0.33*Period_[0][0] + 0.67*SmoothPeriod[1][0];
//----
if(I1[0][0] != 0)
Phase[0][0] = (MathArctan(Q1[0][0] / I1[0][0]));
DeltaPhase = Phase[1][0] - Phase[0][0];
//----
if(DeltaPhase < 1)
DeltaPhase = 1;
alpha = FastLimit / DeltaPhase;
//----
if(alpha < SlowLimit)
alpha = SlowLimit;
MAMA[0][0] = alpha*Price[0][0] + (1 - alpha)*MAMA[1][0];
FAMA[0][0] = 0.5*alpha*MAMA[0][0] + (1 - 0.5*alpha)*FAMA[1][0];
//----
if(bar <= Bars - 50)
{
FABuffer[bar] = MAMA[0][0];
MABuffer[bar] = FAMA[0][0];
}
else
{
FABuffer[bar] = 0;
MABuffer[bar] = 0;
}
//----
bar--;
//----
if(bar < 0)
break;
//----
Smooth[6][0] = Smooth[5][0];
Smooth[5][0] = Smooth[4][0];
Smooth[4][0] = Smooth[3][0];
Smooth[3][0] = Smooth[2][0];
Smooth[2][0] = Smooth[1][0];
Smooth[1][0] = Smooth[0][0];
//----
Detrender[6][0] = Detrender[5][0];
Detrender[5][0] = Detrender[4][0];
Detrender[4][0] = Detrender[3][0];
Detrender[3][0] = Detrender[2][0];
Detrender[2][0] = Detrender[1][0];
Detrender[1][0] = Detrender[0][0];
//----
Q1[6][0] = Q1[5][0];
Q1[5][0] = Q1[4][0];
Q1[4][0] = Q1[3][0];
Q1[3][0] = Q1[2][0];
Q1[2][0] = Q1[1][0];
Q1[1][0] = Q1[0][0];
//----
I1[6][0] = I1[5][0];
I1[5][0] = I1[4][0];
I1[4][0] = I1[3][0];
I1[3][0] = I1[2][0];
I1[2][0] = I1[1][0];
I1[1][0] = I1[0][0];
//----
Q2[1][0] = Q2[0][0];
I2[1][0] = I2[0][0];
Re[1][0] = Re[0][0];
Im[1][0] = Im[0][0];
SmoothPeriod[1][0] = SmoothPeriod[0][0];
Phase[1][0] = Phase[0][0];
Period_[1][0] = Period_[0][0];
MAMA[1][0] = MAMA[0][0];
FAMA[1][0] = FAMA[0][0];
//----
}
return(0);
}
//+------------------------------------------------------------------+
Comments