KG Flat Detector Overview
The KG MA BAR is a specialized technical analysis tool designed to identify specific market conditions—primarily "flat" markets or periods of low volatility. Instead of just showing you where the price is going, this script analyzes the relationship between moving averages and Bollinger-style bands to highlight when the market is consolidating or struggling to establish a clear trend.
How the Logic Works
The tool operates by processing several layers of data simultaneously to determine the current state of the market:
1. Boundary Identification (Bands) The script first calculates two dynamic boundaries based on historical price data (specifically using a Weighted Price Moving Average). These boundaries represent the upper and lower limits of "normal" price action over a set period. Think of these as the outer walls of the market's current range.
2. Trend Interaction Analysis Once the boundaries are established, the script looks at a shorter-term moving average. It constantly monitors how this shorter trend interacts with the upper and lower boundaries:
- It checks if the trend is pushing toward or breaking through these limits.
- It detects "crosses"—moments where the trend switches from being influenced by the upper boundary to the lower boundary (or vice versa).
3. The Detection Mechanism (The "Flat" Signal) The core logic uses a series of "if/then" rules to categorize the market's behavior into different visual states:
- Trend Confirmation: If the price action is moving consistently in one direction relative to the boundaries, the script identifies this as active movement.
- Flat Market Detection: When the price and the trend averages become trapped between the boundaries or fail to break decisively, the script flags this as a "Flat" state.
Visual Representation
The indicator translates these complex calculations into a visual histogram at the bottom of your trading screen:
- Color Coding: The script uses different colors (White, Lime, and Red) to represent different states of the market trend and the strength of the current price movement.
- Dynamic Labeling: The tool automatically updates its display name to show which specific period settings (e.g., the Band Moving Average length) are currently being used, allowing you to keep track of your analysis parameters at a glance.
Summary for the Trader
In short, this script acts as a filter. It strips away the "noise" of constant price fluctuations and highlights periods where the market is sideways or "flat." By identifying these zones, it helps traders avoid entering positions in stagnant markets where there may be insufficient momentum to reach a profit target.
//+------------------------------------------------------------------+
//| KG MA BAR.mq4 |
//| Copyright © 2007, MetaQuotes Software Corp. |
//| http://www.metaquotes.net/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net/"
//---- indicator settings
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 3
#property indicator_color1 White
#property indicator_color2 Lime
#property indicator_color3 Red
//---- indicator buffers
double ExtBuffer0[];
double ExtBuffer1[];
double ExtBuffer2[];
double ExtBuffer3[];
double ExtBuffer4[];
double ExtBuffer5[];
double ExtBuffer6[];
double ExtBuffer7[];
int TF=0;
extern int MA=1;
extern int MA_Mode=0;
extern int Price_Type=6;
extern int Band_MA=96;
extern int SD=1;
string tanda;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- 2 additional buffers are used for counting.
IndicatorBuffers(5);
//---- drawing settings
SetIndexStyle(0,DRAW_HISTOGRAM);
SetIndexStyle(1,DRAW_HISTOGRAM);
SetIndexStyle(2,DRAW_HISTOGRAM);
IndicatorDigits(Digits+2);
SetIndexDrawBegin(0,38);
SetIndexDrawBegin(1,38);
SetIndexDrawBegin(2,38);
//---- 4 indicator buffers mapping
SetIndexBuffer(0,ExtBuffer0);
SetIndexBuffer(1,ExtBuffer1);
SetIndexBuffer(2,ExtBuffer2);
SetIndexBuffer(3,ExtBuffer3);
SetIndexBuffer(4,ExtBuffer4);
//---- name for DataWindow and indicator subwindow label
SetIndexLabel(1,NULL);
SetIndexLabel(2,NULL);
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Accelerator/Decelerator Oscillator |
//+------------------------------------------------------------------+
int start()
{
int i,limit;
int counted_bars=IndicatorCounted();
double prev,current,MACurr;
double stoch1,stoch2;
//---- last counted bar will be recounted
if(counted_bars>0) counted_bars--;
limit=Bars-counted_bars;
//---- macd counted in the 1-st additional buffer
for(i=0; i<limit; i++)
ExtBuffer3[i]=iBands(NULL,0,Band_MA,SD,0,PRICE_WEIGHTED,MODE_UPPER,i);//iCustom(NULL, 0, "KG Bands",Band_MA,SD,Price_Type,1,i);//
//---- signal line counted in the 2-nd additional buffer
for(i=0; i<limit; i++)
ExtBuffer4[i]=iBands(NULL,0,Band_MA,SD,0,PRICE_WEIGHTED,MODE_LOWER,i);//iCustom(NULL, 0, "KG Bands",Band_MA,SD,Price_Type,2,i);//
//---- dispatch values between 2 buffers
bool up=true;
for(i=limit-1; i>=0; i--)
{
current=ExtBuffer3[i];
prev=ExtBuffer4[i];
MACurr=ExtBuffer5[i];
if(iMA(NULL,0,MA,0,MA_Mode,PRICE_WEIGHTED,i)>current || iMA(NULL,0,MA,0,MA_Mode,PRICE_WEIGHTED,i)<prev) up=true;
if(iMA(NULL,0,MA,0,MA_Mode,PRICE_WEIGHTED,i)<current && iMA(NULL,0,MA,0,MA_Mode,PRICE_WEIGHTED,i)>prev) up=false;
if(!up)
{
ExtBuffer2[i]=0.0;
ExtBuffer1[i]=0.0;
ExtBuffer0[i]=1;
}
if(up)
{
if (iMA(NULL,0,MA,0,MA_Mode,PRICE_WEIGHTED,i)>current)
{
ExtBuffer1[i]=1;
ExtBuffer2[i]=0.0;
ExtBuffer0[i]=0.0;
}
else
{
ExtBuffer1[i]=0.0;
ExtBuffer2[i]=1;
ExtBuffer0[i]=0.0;
}
}
else
ExtBuffer0[i]=1;
//ExtBuffer1[i]=0.0;
//ExtBuffer2[i]=0.0;
if(TF == 0)
{tanda = Period();}
if(TF == 1)
{tanda = "M1";}
if(TF == 5)
{tanda = "M5";}
if(TF == 15)
{tanda = "M15";}
if(TF == 30)
{tanda = "M30";}
if(TF == 60)
{tanda = "H1";}
if(TF == 240)
{tanda = "H4";}
if(TF == 1440)
{tanda = "D1";}
if(TF == 10080)
{tanda = "W1";}
if(TF == 43200)
{tanda = "M1";}
IndicatorShortName("KG FLAT_DETECTOR: ("+Band_MA+")");
}
//---- done
return(0);
}
//+------------------------------------------------------------------+
Comments