AMA_Bands_v1
Download scriptThis script is designed as a technical analysis tool for trading platforms, specifically created to visualize price trends and potential momentum shifts using an "Adaptive Moving Average" (AMA) system combined with dynamic volatility bands.
Core Logic Overview
The primary goal of this tool is to smooth out market noise to reveal the underlying trend. Unlike standard moving averages that treat all data points equally, this script adapts itself based on the current market conditions—becoming faster during volatile trends and smoother when the market is consolidating.
Key Components of the Analysis
1. Adaptive Smoothing (The Main Line) The heart of the script is its calculation of the "Main Line." It determines how much to weight recent price action versus historical data by calculating an Efficiency Ratio.
- If the market is moving strongly in one direction, the indicator reacts more quickly.
- If the price is "choppy" or sideways, the indicator slows down to avoid giving false signals.
2. Dynamic Directional Signals (Arrows) The script analyzes the rate of change between the previous and current values of the Adaptive Moving Average. It generates three distinct visual cues:
- Upward Arrows: Triggered when the average is rising significantly enough to cross a specific momentum threshold.
- Downward Arrows: Triggered when the average is falling significantly enough to cross that same threshold.
- Stop Symbols: Appear when the price movement is too small to be considered a definitive trend change, indicating a period of stability or lack of momentum.
3. Volatility Bands (Inner and Outer) In addition to the main line and arrows, the script can display "Bollinger-style" bands. These represent the price's volatility:
- Standard Deviation: The script calculates how far the current price is deviating from the average over a specific window of time.
- Inner Bands: These provide a closer look at immediate price fluctuations.
- Outer Bands: These define wider boundaries, often used to identify "overextended" prices where the market might be reaching an extreme high or low.
How it Functions for the User
When applied to a chart, the tool provides a comprehensive visual environment:
- The Main Line shows you the overall direction of the market.
- The Arrows act as immediate highlights for when the trend strength shifts.
- The Bands create a "corridor" that helps you visualize the boundaries of normal price behavior, making it easier to spot potential breakouts or reversals.
//+------------------------------------------------------------------+
//| AMA_Bands.mq4 |
//| Copyright © 2006, Alexandre |
//| mailto: a_g_j_@hotmail.com |
//+------------------------------------------------------------------+
//---- general properties
#property copyright "Copyright © 2006, Alexandre"
#property link "mailto: a_g_j_@hotmail.com"
//---- indicator properties
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 DarkViolet // 'line' (main)
#property indicator_color2 DodgerBlue // 'arrow' (up)
#property indicator_color3 OrangeRed // 'arrow' (down)
#property indicator_color4 Silver // 'arrow' (stop-symbol)
#property indicator_color5 Aqua // 'line' (inner BB upper band)
#property indicator_color6 Aqua // 'line' (inner BB lower band)
#property indicator_color7 Magenta // 'line' (outer BB upper band)
#property indicator_color8 Magenta // 'line' (outer BB lower band)
//---- defines
//---- input parameters
extern int Range = 9;
extern int SlowAMA = 30;
extern int FastAMA = 2;
extern double P_G = 2.0;
extern double K_F = 2.0;
extern bool InnerBandsOn = true;
extern double BandsDevInn = 1.0;
extern bool OuterBandsOn = true;
extern double BandsDevOut = 2.0;
extern bool LastBarOnly = false;
//---- common buffers
//---- indicator buffers
double AMA_Buffer[];
double AMA_UpSig[];
double AMA_DwSig[];
double AMA_NlSig[];
double AMA_UppBufferL[];
double AMA_LowBufferL[];
double AMA_UppBufferH[];
double AMA_LowBufferH[];
//---- variables
double A = 0.45; // quotation coefficient - value close to optimal
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0, DRAW_LINE);
SetIndexLabel(0, "Main Line");
SetIndexBuffer(0, AMA_Buffer); // main line
SetIndexDrawBegin(0, Range);
//
SetIndexStyle(1, DRAW_ARROW);
SetIndexLabel(1, "Signal Up");
SetIndexArrow(1, 225); // small arrow up
SetIndexBuffer(1, AMA_UpSig);
SetIndexDrawBegin(1, Range);
//
SetIndexStyle(2, DRAW_ARROW);
SetIndexLabel(2, "Signal Down");
SetIndexArrow(2, 226); // small arrow down
SetIndexBuffer(2, AMA_DwSig);
SetIndexDrawBegin(2, Range);
//
SetIndexStyle(3, DRAW_ARROW);
SetIndexLabel(3, "Signal Stop");
SetIndexArrow(3, 251); // small stop-symbol
SetIndexBuffer(3, AMA_NlSig);
SetIndexDrawBegin(3, Range);
//
if (InnerBandsOn == true)
{
SetIndexStyle(4, DRAW_LINE);
SetIndexLabel(4, "Inner BB Upper Line");
SetIndexBuffer(4, AMA_UppBufferL); // inner BB upper line
SetIndexDrawBegin(4, Range);
//
SetIndexStyle(5, DRAW_LINE);
SetIndexLabel(5, "Inner BB Lower Line");
SetIndexBuffer(5, AMA_LowBufferL); // inner BB lower line
SetIndexDrawBegin(5, Range);
}
//
if (OuterBandsOn == true)
{
SetIndexStyle(6, DRAW_LINE);
SetIndexLabel(6, "Outer BB Upper Line");
SetIndexBuffer(6, AMA_UppBufferH); // outer BB upper line
SetIndexDrawBegin(6, Range);
//
SetIndexStyle(7, DRAW_LINE);
SetIndexLabel(7, "Outer BB Lower Line");
SetIndexBuffer(7, AMA_LowBufferH); // outer BB lower line
SetIndexDrawBegin(7, Range);
}
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars = IndicatorCounted();
int Limit;
int i, j, k;
double d_f, old_val, new_val, sum, std_dev;
static double v_prev, v_curr, ER_Low, ER_Upp;
//----
if (counted_bars < 0) { return(-1); }
Limit = Bars - counted_bars;
//
if (Limit > Range) // run once when getting started
{
k = Limit - Range;
v_prev = Close[Limit-Range];
ER_Low = 2.0 / (SlowAMA + 1); // lower Efficiency Ratio threshold
ER_Upp = 2.0 / (FastAMA + 1); // upper Efficiency Ratio threshold
}
else // run elsewhere
{
if (LastBarOnly == false)
{ k = Limit; v_prev = AMA_Buffer[Limit+1]; }
else
{ k = 0; v_prev = AMA_Buffer[1]; }
}
//
for (i=k; i>=0; i--)
{
v_curr = GetAMAOneStep(v_prev, i, Range, ER_Low, ER_Upp, P_G, A);
d_f = v_curr - v_prev;
v_prev = v_curr;
AMA_Buffer[i] = v_curr; // line
// signals - 1st derivative
if ((MathAbs(d_f) > (K_F * Point)) && (d_f > 0.0)) // signal up
{
AMA_UpSig[i] = AMA_Buffer[i];
AMA_DwSig[i] = EMPTY_VALUE;
AMA_NlSig[i] = EMPTY_VALUE;
}
else if ((MathAbs(d_f) > (K_F * Point)) && (d_f < 0.0)) // signal down
{
AMA_UpSig[i] = EMPTY_VALUE;
AMA_DwSig[i] = AMA_Buffer[i];
AMA_NlSig[i] = EMPTY_VALUE;
}
else // signal stop
{
AMA_UpSig[i] = EMPTY_VALUE;
AMA_DwSig[i] = EMPTY_VALUE;
AMA_NlSig[i] = AMA_Buffer[i];
}
}
if ((InnerBandsOn == true) || (OuterBandsOn == true))
{
// calculate standard deviation
i = Bars - Range;
if (counted_bars > (Range - 1)) { i = Bars - counted_bars - 1; }
while (i >= 0)
{
sum = 0.0;
k = i + Range - 1;
old_val = AMA_Buffer[i];
while (k >= i)
{
new_val = Close[k] - old_val;
sum += new_val * new_val;
k--;
}
std_dev = MathSqrt(sum / Range);
// complete BB's buffers
if (InnerBandsOn == true)
{
AMA_UppBufferL[i] = old_val + BandsDevInn * std_dev;
AMA_LowBufferL[i] = old_val - BandsDevInn * std_dev;
}
if (OuterBandsOn == true)
{
AMA_UppBufferH[i] = old_val + BandsDevOut * std_dev;
AMA_LowBufferH[i] = old_val - BandsDevOut * std_dev;
}
i--;
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
//+------------------------------------------------------------------+
//| Close Prices Adaptive Smoothing - AMA One Step |
//+------------------------------------------------------------------+
double GetAMAOneStep(double v_prev, int n_bar, int range, double er_low,
double er_upp, double pow, double a)
{
double v_curr, noise, er, ssc;
//----
// calculate efficiency ratio & ssc
noise = 0.000000001;
for (int i=n_bar+range-1; i>=n_bar; i--)
{ noise += MathAbs(Close[i] - Close[i+1]); }
er = MathAbs(Close[n_bar] - Close[n_bar+range]) / noise;
ssc = er * er_upp + er_low;
ssc = MathPow(ssc, pow);
// calculate AMA one step, using explicit/implicit Euler Schema
v_curr = (v_prev + ssc * (a * (Close[n_bar+1] - v_prev) +
(1.0 - a) * Close[n_bar])) / (1.0 + (1.0 - a) * ssc);
//----
return(v_curr);
}
Comments