MA_Filter_4MA_FlatTrend_direct_iCust
Download scriptThe 4MAs Trend indicator is designed to provide traders with a clear, visual representation of market momentum and trend direction by analyzing the relationship between four different Moving Averages simultaneously. Instead of cluttering your main price chart with multiple lines, this tool aggregates that data into a clean histogram in a separate window, making it easier to identify whether the market is trending upwards, downwards, or moving sideways (flat).
Core Logic and Functionality
The script operates by calculating four distinct Moving Averages. The user can customize the length (period) and the specific mathematical calculation (mode) for each of these averages. By comparing these four values against one another, the indicator determines the prevailing market sentiment through three primary states:
1. Bullish Trend (Lime Green) When the Moving Averages indicate strong upward momentum, the indicator displays a Lime Green histogram. The script identifies this state in two ways depending on your settings:
- Direction Filter Mode: If enabled, the script confirms an uptrend by checking if all four averages are currently higher than they were in the previous candle.
- Standard Mode: If the filter is off, it simply checks if the shorter-term averages are consistently positioned above the longer-term averages.
2. Bearish Trend (Red) Conversely, when the data suggests a consistent downward move, the indicator displays a Red histogram. Similar to the bullish logic:
- Direction Filter Mode: It confirms a downtrend if all four averages have dropped compared to their previous values.
- Standard Mode: It identifies a downtrend when the shorter-term averages are positioned consistently below the longer-term averages.
3. Neutral / Flat Trend (Gold) When the market is indecisive, consolidating, or moving horizontally, the indicator displays a Gold histogram. This occurs when the Moving Averages do not meet the specific criteria for a clear upward or downward trend. This visual cue helps traders avoid "choppy" markets where trend-following strategies might otherwise result in false signals.
Key User Features
- Customizable Timeframes: The indicator can be configured to display data from specific time periods (e.g., M1, H1, D1), allowing for multi-timeframe analysis.
- Flexible Moving Average Types: Because it utilizes a secondary "All Averages" engine, you are not limited to simple averages; you can choose from various complex calculations (such as EMA, Wilders, Hull MA, etc.) to suit your specific trading style.
- Directional Filtering: Users can toggle a filter that compares current data against previous history or simply compare the relative order of the four moving averages to decide on a trend signal.
//+------------------------------------------------------------------+
//| 4MAs Trend.mq4|
//| Flat Trend |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
//2009 fxtsd mod icust
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 1
#property indicator_buffers 3
#property indicator_color1 LimeGreen
#property indicator_color2 Red
#property indicator_color3 Gold
//---- input parameters
int Minutes=0;
extern int MA1_Period=7;
extern int MA1_Mode=8;
extern int MA2_Period=9;
extern int MA2_Mode=8;
extern int MA3_Period=13;
extern int MA3_Mode=8;
extern int MA4_Period=21;
extern int MA4_Mode=8;
extern string _ = "iCustom ind parameters";
int TimeFrame = 0;
// int MA_Period = 14;
extern int Price = 0;
extern int MA_Shift = 0;
//extern int Color_Mode = 0;
//extern int MA_Method = 9;
extern string note_MA_Method ="0SMA 1EMA 2WilderSMMA 3LWMA ";
extern string ______ ="4SineWMA 5TriMA 6LSMA 7SMMA ";
extern string _____ ="8HMA 9ZeroLagEMA 10DEMA 11T3";
extern string ____ ="12iTrendJEhlers 13Median ";
extern string ___ ="14GeoMean 15REMA 16ILRS";
extern string __ ="17IE/2 18TriMAgen 19VWMA ";
//extern int MA5_Period=15;
//extern int MA5_Mode=3;
//extern int MA6_Period=21;
//extern int MA6_Mode=3;
extern bool DirectionFilterOn = true;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
string TimeFrameStr;
double MA1,MA2,MA3,MA4,MA5,MA6;
double MA1p,MA2p,MA3p,MA4p,MA5p,MA6p;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,5,LimeGreen);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,5, Red);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexStyle(2,DRAW_HISTOGRAM,STYLE_SOLID,5, Gold);
SetIndexBuffer(2,ExtMapBuffer3);
for(int i=0; i<=indicator_buffers; i++) SetIndexLabel(i,NULL);
switch(Minutes)
{
case 1 : TimeFrameStr="M1"; break;
case 5 : TimeFrameStr="M5"; break;
case 15 : TimeFrameStr="M15"; break;
case 30 : TimeFrameStr="M30"; break;
case 60 : TimeFrameStr="H1"; break;
case 240 : TimeFrameStr="H4"; break;
case 1440 : TimeFrameStr="D1"; break;
case 10080 : TimeFrameStr="W1"; break;
case 43200 : TimeFrameStr="MN"; break;
default : TimeFrameStr="TF0"; Minutes=0;
}
if (DirectionFilterOn)string name = " Direction Filter On "; else name = "";
IndicatorShortName("Flat Trend w 4MA ["+TimeFrameStr+"] "+name);
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int i,limit, counted_bars=IndicatorCounted();
if(counted_bars<0) return(-1);
if(counted_bars>0) counted_bars--;
limit= Bars-counted_bars;
for(i=limit; i>=0; i--)
{
MA1=iCustom(NULL,0,"MA_AllAverages_v2.2_mtf",0,MA1_Period,Price,MA_Shift,0,MA1_Mode,0,i);
MA2=iCustom(NULL,0,"MA_AllAverages_v2.2_mtf",0,MA2_Period,Price,MA_Shift,0,MA2_Mode,0,i);
MA3=iCustom(NULL,0,"MA_AllAverages_v2.2_mtf",0,MA3_Period,Price,MA_Shift,0,MA3_Mode,0,i);
MA4=iCustom(NULL,0,"MA_AllAverages_v2.2_mtf",0,MA4_Period,Price,MA_Shift,0,MA4_Mode,0,i);
ExtMapBuffer1[i]=0.0; ExtMapBuffer2[i]=0.0; ExtMapBuffer3[i]=0.0;
if (DirectionFilterOn)
{
MA1p=iCustom(NULL,0,"MA_AllAverages_v2.2_mtf",0,MA1_Period,Price,MA_Shift,0,MA1_Mode,0,i+1);
MA2p=iCustom(NULL,0,"MA_AllAverages_v2.2_mtf",0,MA2_Period,Price,MA_Shift,0,MA2_Mode,0,i+1);
MA3p=iCustom(NULL,0,"MA_AllAverages_v2.2_mtf",0,MA3_Period,Price,MA_Shift,0,MA3_Mode,0,i+1);
MA4p=iCustom(NULL,0,"MA_AllAverages_v2.2_mtf",0,MA4_Period,Price,MA_Shift,0,MA4_Mode,0,i+1);
if(MA1>MA1p&&MA2>MA2p&&MA3>MA3p&&MA4>MA4p) { ExtMapBuffer1[i]=1; ExtMapBuffer2[i]=0.0; ExtMapBuffer3[i]=0.0; }
else
if(MA1<MA1p&&MA2<MA2p&&MA3<MA3p&&MA4<MA4p) { ExtMapBuffer2[i]=1; ExtMapBuffer3[i]=0.0; ExtMapBuffer1[i]=0.0; }
else { ExtMapBuffer3[i]=1; ExtMapBuffer1[i]=0.0; ExtMapBuffer2[i]=0.0; }
}
else
{
if (MA1>MA2 &&MA2>MA3 &&MA3>MA4) { ExtMapBuffer1[i]=1; ExtMapBuffer2[i]=0.0; ExtMapBuffer3[i]=0.0; }
else
if (MA1<MA2 &&MA2<MA3 &&MA3<MA4) { ExtMapBuffer2[i]=1; ExtMapBuffer3[i]=0.0; ExtMapBuffer1[i]=0.0; }
else { ExtMapBuffer3[i]=1; ExtMapBuffer1[i]=0.0; ExtMapBuffer2[i]=0.0; }
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
Comments