MA in Color_Hist_001
Download scriptThis technical tool is designed to provide traders with a clear, visual representation of price momentum by analyzing Moving Averages (MA). Instead of cluttering the main price chart with complex lines, this script creates a dedicated window at the bottom of your trading platform, displaying a color-coded histogram that reacts to changes in market trends.
Core Functionality
The primary purpose of this indicator is to track the direction of price movement over a specific period. It calculates various types of Moving Averages—such as Simple (SMA), Exponential (EMA), Smoothed (SMMA), Linear Weighted (LWMA), and Least Squares (LSMA)—to help smooth out "market noise" and reveal the underlying trend.
How the Logic Works
The script constantly compares the current moving average value against the previous one to determine the strength and direction of the trend:
- Trend Identification: The tool looks at where the Moving Average is currently positioned relative to its previous point in time.
- Dynamic Color Coding: The histogram changes color based on these shifts:
- Green: Appears when the current moving average is higher than the previous one, signaling an upward trend or positive momentum.
- Red: Appears when the current moving average is lower than the previous one, signaling a downward trend or negative momentum.
- Yellow: Serves as a transitional or neutral indicator depending on the specific movement logic between the current and previous values.
- Visual Clarity: By using an "Empty Value" command, the script ensures that only the relevant color is visible at any given moment. This prevents overlapping colors from confusing the user, providing a clean, high-contrast visual of whether the market is pushing up or down.
Key Features for the User
- Customizable Smoothing: Users can choose which type of Moving Average they prefer to use, allowing the tool to be adapted to different trading styles (e.g., faster reactions versus smoother, longer-term trends).
- Adjustable Sensitivity: The "Period" setting allows users to define how many past price bars the indicator should consider, making it easy to switch from short-term scalping views to long-term trend analysis.
- Dedicated Visual Space: Because it operates in a separate window (histogram), it allows you to monitor momentum independently of the price candles, providing a clearer "bird's eye view" of market health.
//+------------------------------------------------------------------+
//| MA_In_Color.mq4 |
//| Copyright © 2004, MetaQuotes Software Corp. |
//| Modified from LSMA_In_Color to use any MA by Robert Hill |
//| March 21/2008 - Created separate window/histogram. |
//| GoingForward - Forex-TSD |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, FX Sniper and Robert Hill"
#property link "http://www.metaquotes.net/"
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Yellow
#property indicator_color2 Green
#property indicator_color3 Red
//----
extern int MAPeriod=14;
extern int MAType=1;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
//---- variables
int MAMode;
string strMAType;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
IndicatorBuffers(3);
//---- drawing settings
SetIndexBuffer(2,ExtMapBuffer1);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexBuffer(0,ExtMapBuffer3);
SetIndexStyle(2,DRAW_HISTOGRAM,2);
SetIndexStyle(1,DRAW_HISTOGRAM,2);
SetIndexStyle(0,DRAW_HISTOGRAM,2);
switch(MAType)
{
case 1: strMAType="EMA"; MAMode=MODE_EMA; break;
case 2: strMAType="SMMA"; MAMode=MODE_SMMA; break;
case 3: strMAType="LWMA"; MAMode=MODE_LWMA; break;
case 4: strMAType="LSMA"; break;
default: strMAType="SMA"; MAMode=MODE_SMA; break;
}
IndicatorShortName( strMAType+ " (" +MAPeriod + ") ");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
double LSMA(int Rperiod, int shift)
{
int i;
double sum;
int length;
double lengthvar;
double tmp;
double wt;
//----
length=Rperiod;
//----
sum=0;
for(i=length; i>=1 ;i--)
{
lengthvar=length + 1;
lengthvar/=3;
tmp=0;
tmp =(i - lengthvar)*Close[length-i+shift];
sum+=tmp;
}
wt=sum*6/(length*(length+1));
//----
return(wt);
}
//+------------------------------------------------------------------+
//| |
//+------------------------------------------------------------------+
int start()
{
double MA_Cur, MA_Prev;
int limit;
int counted_bars=IndicatorCounted();
//---- check for possible errors
if (counted_bars<0) return(-1);
//---- last counted bar will be recounted
if (counted_bars>0) counted_bars--;
limit=Bars - counted_bars;
//----
for(int i=limit; i>=0; i--)
{
if (MAType==4)
{
MA_Cur=LSMA(MAPeriod,i);
MA_Prev=LSMA(MAPeriod,i+1);
}
else
{
MA_Cur=iMA(NULL,0,MAPeriod,0,MAMode,PRICE_CLOSE,i);
MA_Prev=iMA(NULL,0,MAPeriod,0,MAMode,PRICE_CLOSE,i+1);
}
//---- COLOR CODING
ExtMapBuffer3[i]=MA_Cur; //red
ExtMapBuffer2[i]=MA_Cur; //green
ExtMapBuffer1[i]=MA_Cur; //yellow
//----
if (MA_Prev > MA_Cur)
{
ExtMapBuffer2[i]=EMPTY_VALUE;
}
else if (MA_Prev < MA_Cur)
{
ExtMapBuffer1[i]=EMPTY_VALUE; //-1 red/greem tight
}
else
{
ExtMapBuffer1[i]=EMPTY_VALUE;//EMPTY_VALUE;
ExtMapBuffer2[i]=EMPTY_VALUE;//EMPTY_VALUE;
}
}
return(0);
}
//+------------------------------------------------------------------+
Comments