Better_MA_H1

Download script
Author: Copyright � 2007, MetaQuotes Software Corp.
Better_MA_H1

This tool is designed to provide a visual representation of moving average data for three different parameters simultaneously, displayed in a single dedicated window on your trading platform. It allows you to monitor different trends and price averages at a glance to help identify market dynamics.

Core Logic Overview

The script functions by scanning through a specific number of historical price bars (defined by the user) and calculating three distinct moving average lines based on the closing prices of the asset.

Key Features and Behavior:

  • Multi-Line Tracking: The indicator creates three separate visual lines (colored in Lime, Aqua, and White). Each line corresponds to a different moving average length:
    • The first line tracks a 5-period Simple Moving Average.
    • The second line tracks a 10-period Simple Moving Average.
    • The third line tracks a 15-period Simple Moving Average.
  • Time Synchronization: A primary objective of this script is to ensure that the data points for all three averages are synchronized correctly with the current chart's timeframe. It uses a "shift" logic to align historical data, ensuring that the values plotted for each line correspond to the exact same moment in time across the different calculation periods.
  • Data Processing: The script iterates backward through the history of the selected asset. For every point in time within the specified range, it retrieves the closing price and calculates the respective moving average based on the predefined lengths (5, 10, and 15).
  • Visual Output: By plotting these three different averages together, the tool allows you to observe how short-term trends (the 5-period) compare against slightly longer-term trends (the 10 and 15-period) in real-time.

Summary for the User

Essentially, this indicator acts as a multi-layered trend tracker. Instead of cluttering your main chart with multiple overlapping lines, it isolates these specific moving averages into one separate window, providing a clean look at how different price sensitivities are behaving simultaneously on the market.

Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
13 Views
0 Downloads
0 Favorites
Better_MA_H1
//+------------------------------------------------------------------+
//|                                                   Better_MA.mq4  |
//|                      Copyright © 2007, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2007, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Lime
#property indicator_color2 Aqua
#property indicator_color3 White



extern int NrBars=500;

string     Symbol1;
string     Symbol2;
string     Symbol3;
int        Period1= PERIOD_H1;

//---- buffers
double VolBuffer1[];
double VolBuffer2[];
double VolBuffer3[];
 double x1,x2,x3;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|

int init()
  {
/*
   if(StringFind(Symbol(),"m",0)>0)
   {
    Symbol1 = Symbol1+"m";
    Symbol2 = Symbol2+"m";
    Symbol3 = Symbol3+"m";
   }   
*/   

    Symbol1 = Symbol();
    Symbol2 = Symbol();
    Symbol3 = Symbol();

   IndicatorBuffers(3);
   
   SetIndexStyle(0,DRAW_LINE,0,1);
   SetIndexBuffer(0, VolBuffer1);
   SetIndexLabel(0,Symbol1+" "+Period1);   
   SetIndexEmptyValue(0,EMPTY_VALUE);
   
   SetIndexStyle(1,DRAW_LINE,0,1);
   SetIndexBuffer(1, VolBuffer2);
   SetIndexLabel(1,Symbol2+" "+Period1);
   SetIndexEmptyValue(1,EMPTY_VALUE);
   
   SetIndexStyle(2,DRAW_LINE,0,1);
   SetIndexBuffer(2, VolBuffer3);
   SetIndexLabel(2,Symbol3+" "+Period1);
   SetIndexEmptyValue(2,EMPTY_VALUE);

   return(0);
  }


//+------------------------------------------------------------------+
//| Custor indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {

  }

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {


  

  
  int pos=0;
  pos=NrBars;
  while(pos>=0)
  //while(pos<=NrBars)
     {  
    
      int pos1 = iBarShift(Symbol1, Period1, iTime(NULL,0,pos), false);
	   x1=iMA(Symbol1,Period1,5,0,MODE_SMA,PRICE_CLOSE ,pos1);
		//if(x1==0)
		//  x1=EMPTY_VALUE;      
		if (iTime(Symbol1, Period1, pos1) == iTime(NULL,0,pos))  // have to be sure the times match.
		{ 
 
		  VolBuffer1[pos]=x1; 
		}
		else
		{ 
			VolBuffer1[pos]=x1;   
		}

      int pos2 = iBarShift(Symbol2, Period1, iTime(NULL,0,pos), false);
      x2=iMA(Symbol2,Period1,10,0,MODE_SMA,PRICE_CLOSE ,pos2);
		//if(x2==0)
		//  x2=EMPTY_VALUE;      
		if (iTime(Symbol2, Period1, pos2) == iTime(NULL,0,pos))  // have to be sure the times match.
		{   			    
		 	 VolBuffer2[pos]=x2;   
		}
		else
		{
			VolBuffer2[pos]=x2;   
		}

      int pos3 = iBarShift(Symbol3, Period1, iTime(NULL,0,pos), false);
      x3=iMA(Symbol3,Period1,15,0,MODE_SMA,PRICE_CLOSE,pos3);
		//if(x3==0)
		//  x3=EMPTY_VALUE;      
		if (iTime(Symbol3, Period1, pos3) == iTime(NULL,0,pos))  // have to be sure the times match.
		{ 	 		    
		 	 VolBuffer3[pos]=x3;   
		}
		else
		{
			VolBuffer3[pos]=x3;   
		}
		
 	   //pos++;
 	   pos--;
     }

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

Comments