Flat_Trend
Download scriptThe Flat Trend indicator is designed to provide a visual representation of market momentum and trend direction by analyzing three distinct technical components. Rather than cluttering your main price chart, it operates in a separate window, providing a clear, color-coded summary of current market conditions based on specific mathematical rules.
How the Logic Works
The tool processes historical data across different timeframes to determine which "state" the market is currently in. It evaluates three primary signals to decide what color to display:
- Bullish Momentum (Lime Green): The indicator identifies upward strength by checking if the current price is trading above a specific parabolic stop level while simultaneously confirming that the average directional movement is increasing. When both of these conditions are met, the indicator highlights a positive trend.
- Bearish Momentum (Red): Conversely, the tool looks for downward pressure. It triggers this signal when the price falls below the parabolic stop level and the average directional movement shows a significant decrease. This represents active selling pressure in the market.
- Neutral/Flat Trend (Gold): When neither of the above conditions is satisfied—meaning there is no clear breakout or dominant momentum in either direction—the indicator displays gold. This signifies a "flat" or sideways market where price action lacks a defined trend.
Key Features and Customization
- Dynamic Timeframe Selection: The script allows you to choose specific periods (ranging from 1 minute up to monthly charts). The tool automatically adjusts its calculations to match the timeframe you select, ensuring that the trend data is relevant to your trading style.
- Visual Clarity: By using a histogram format with distinct colors (Red, Lime Green, and Gold), the logic transforms complex moving averages and directional indicators into an easy-to-read visual map.
- Historical Analysis: The script scans back through recent price bars to ensure that the trend signals are consistent and continuous, providing a smoother visual experience of how the market has transitioned from one state to another over time.
//+------------------------------------------------------------------+
//| FlatTrend.mq4 |
//| Kirk Sloan |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Kirk Sloan"
#property link "http://www.metaquotes.net"
//----
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 3
#property indicator_color1 Red
#property indicator_color2 LimeGreen
#property indicator_color3 Gold
//---- input parameters
extern int Minutes=5;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double Ma;
double hhigh, llow;
double Psar;
double PADX,NADX;
string TimeFrameStr;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,1,Red);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,1, LimeGreen);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,1, Gold);
SetIndexBuffer(2,ExtMapBuffer3);
switch(Minutes)
{
case 1 : TimeFrameStr="Period_M1"; break;
case 5 : TimeFrameStr="Period_M5"; break;
case 15 : TimeFrameStr="Period_M15"; break;
case 30 : TimeFrameStr="Period_M30"; break;
case 60 : TimeFrameStr="Period_H1"; break;
case 240 : TimeFrameStr="Period_H4"; break;
case 1440 : TimeFrameStr="Period_D1"; break;
case 10080 : TimeFrameStr="Period_W1"; break;
case 43200 : TimeFrameStr="Period_MN1"; break;
default : TimeFrameStr="Current Timeframe"; Minutes=0;
}
IndicatorShortName("Flat Trend ("+TimeFrameStr+")");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
for(int i=0; i < 300; i++)
{
ExtMapBuffer1[i]=0;
ExtMapBuffer2[i]=0;
ExtMapBuffer3[i]=0;
//----
PADX=iADX(NULL,Minutes,14 ,PRICE_CLOSE,1,i);
NADX=iADX(NULL,Minutes,14 ,PRICE_CLOSE,2,i);
Psar=iSAR(NULL,Minutes,0.02,0.2,i) ;
if (Psar < iClose(NULL, Minutes,i) && PADX > NADX)
{
ExtMapBuffer2[i]=1;
}
if (Psar > iClose(NULL, Minutes,i) && NADX > PADX)
{
ExtMapBuffer1[i]=1;
}
if (ExtMapBuffer1[i]==0 && ExtMapBuffer2[i]==0)
{
ExtMapBuffer3[i]=1;
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments