MA_color

This script is designed to provide a clear, visual representation of market trends by using a Moving Average—a common tool used to smooth out price fluctuations and identify the general direction of the market. Unlike standard indicators that simply draw a single line, this tool is designed to change colors dynamically to signal shifts in momentum.

How the Logic Works

The core functionality of the script revolves around tracking the direction of the Moving Average from one point in time to the next. Here is the step-by-step breakdown of its logic:

1. Calculating the Movement The tool constantly compares the current value of the Moving Average with its previous value. By looking at these two consecutive points, it determines whether the average is currently "climbing" (moving upward) or "falling" (moving downward).

2. Dynamic Color Switching The script uses different visual layers to represent these movements:

  • Upward Trend: When the current Moving Average value is higher than the previous one, the tool identifies this as an upward movement. It then chooses a specific color (Green) to highlight that section of the chart.
  • Downward Trend: Conversely, when the current Moving Average value is lower than the previous one, it identifies a downward movement and uses a different color (Red) to represent this phase.

3. Visual Continuity To ensure the chart remains clean and readable, the script includes logic to manage how these colors are drawn. It checks the state of the previous data point before drawing a new line. This prevents visual "overlap" and ensures that when the trend changes—for example, from moving up to moving down—the transition is clearly defined on your screen.

Summary for the User

In simple terms, this script acts as a trend-following visual aid. Instead of you having to manually interpret whether the price momentum is shifting, the script does the math automatically. It "paints" the trend:

  • Green segments indicate that the average is currently trending upward.
  • Red segments indicate that the average is currently trending downward.

This allows you to quickly identify the prevailing market sentiment at a glance without the visual clutter of multiple overlapping lines.

Indicators Used
Moving average indicator
16 Views
0 Downloads
0 Favorites
MA_color
//+------------------------------------------------------------------+
//|                                                MA_In_Color.mq4   |
//|                      Copyright © 2004, MetaQuotes Software Corp. |
//+------------------------------------------------------------------+
//mod2009fxtsd
#property  copyright "Copyright © 2006, FX Sniper and Robert Hill"
#property  link      "http://www.metaquotes.net/"

//---- indicator settings

#property  indicator_chart_window
#property  indicator_buffers 4
#property indicator_color1 Green
#property indicator_color2 Red
#property indicator_color3 Red
#property indicator_color4 Green

extern int       MAPeriod=5;
extern int       MAMode =1;
extern int       MAPrice=0;

//---- buffers

double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];


//+------------------------

int init()
  {
   
   SetIndexBuffer(0,ExtMapBuffer2);
   SetIndexBuffer(1,ExtMapBuffer3);
   SetIndexBuffer(2,ExtMapBuffer4);
   SetIndexBuffer(3,ExtMapBuffer5);
   

   IndicatorShortName( "MA "+MAPeriod );
//---- 
   return(0);
  }

//-------------------------------
int start()
 {
  
   int limit;
   int counted_bars = IndicatorCounted();
   if (counted_bars<0) return(-1);
   if (counted_bars>0) counted_bars--;
   limit = Bars - counted_bars;


   for(int i=limit; i>=0; i--)
   {
      double ma0=iMA(NULL,0,MAPeriod,0,MAMode,MAPrice,i);
      double ma1=iMA(NULL,0,MAPeriod,0,MAMode,MAPrice,i+1);
         
//=========          

       ExtMapBuffer2[i] = EMPTY_VALUE;       ExtMapBuffer3[i] = EMPTY_VALUE; 
       ExtMapBuffer4[i] = EMPTY_VALUE;       ExtMapBuffer5[i] = EMPTY_VALUE; 

       
       if (ma0 > ma1 ) 
            {                          
               if (  ExtMapBuffer4[i+1]!=EMPTY_VALUE) 
                    
                  {  ExtMapBuffer5[i] = ma0;     ExtMapBuffer5[i+1]= ma1;
                        }
               else   
                  {  ExtMapBuffer2[i] = ma0;    ExtMapBuffer2[i+1]= ma1;
                        }
            }

        else 
            {        
               if ( ExtMapBuffer2[i+1]!=EMPTY_VALUE) 

                  { ExtMapBuffer4[i] = ma0;     ExtMapBuffer4[i+1]= ma1;
                        }
               else        
                  { ExtMapBuffer3[i] = ma0;     ExtMapBuffer3[i+1]= ma1;
                        }

            }
        
        
        
     }
    
      return(0);
 }
//+------------------------------------------------------------------+



Comments