Better_H4_diff_1

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

Technical Overview

The Better_MA script is a specialized financial analysis tool designed for the MetaTrader platform. Its primary purpose is to provide traders with a clearer view of market momentum by calculating and displaying specific differences between various moving averages across different timeframes. Instead of showing you the price directly, this indicator focuses on the "spread" or "distance" between trend lines, which can help in identifying the strength and direction of market movements.

How the Logic Works

1. Multi-Timeframe Analysis The script does not look at the chart in isolation. It scans data from three distinct timeframes simultaneously:

  • H1 (1-Hour)
  • H4 (4-Hour)
  • D1 (Daily) (Note: The code specifically targets the 240-minute/4-hour interval for its primary calculations).

By looking at these larger timeframes, the tool aims to filter out "noise" from smaller, more volatile price movements, providing a more stable perspective on the overall trend.

2. Moving Average Comparisons For each timeframe, the script calculates three different Simple Moving Averages (SMAs) based on their closing prices. It specifically looks at periods of:

  • 5 units
  • 10 units
  • 15 units

The core logic involves subtracting these averages from one another. For example, it calculates the difference between the 5-period average and the 10-period average, as well as the 10-period and 15-period averages, and the total spread between the 5 and 15-period averages.

3. Dynamic Options (User Customization) The script includes a selection system (Option 0, 1, or 2) that allows the user to choose which specific "spread" data they want to visualize on their chart:

  • Option 0: Displays the spread values associated with the H1 timeframe.
  • Option 1: Displays the spread values associated with the H4 timeframe.
  • Option 2: Displays the spread values associated with the Daily/longer-term timeframe.

Visual Representation

The results are plotted as three distinct lines on a separate window below the main price chart. These lines represent:

  1. The difference between the shortest and medium moving averages.
  2. The difference between the medium and longest moving averages.
  3. The total difference between the shortest and longest moving averages.

Summary for the Trader

By monitoring these three lines, a trader can observe how much "tension" exists between different trend speeds. When these lines expand, it often suggests increasing momentum; when they contract or cross, it may indicate a flattening of the trend or a potential shift in market direction. This tool is designed to help you see the "skeleton" of the market's movement across multiple timeframes at a single glance.

Price Data Components
Series array that contains close prices for each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
5 Views
0 Downloads
0 Favorites
Better_H4_diff_1
//+------------------------------------------------------------------+
//|                                                   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=1000;
extern int Option=1;//0,1,2

string SymbolName;
int PeriodMinutes1=0;
int PeriodMinutes5=PERIOD_M5;
int PeriodMinutes60=PERIOD_H1;
int PeriodMinutes240=PERIOD_H4;


//---- buffers
double VolBuffer1[];
double VolBuffer2[];
double VolBuffer3[];
double x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
double y0,y1,y2,y3,y4,y5,y6,y7,y8,y9;
double z0,z1,z2,z3,z4,z5,z6,z7,z8,z9;

double at0,at1,at2,at3,at4,at5,at6,at7,at8;
double at9,at10,at11,at12,at13,at14,at15,at16,at17;
double at18,at19,at20,at21,at22,at23,at24,at25,at26;

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

int init()
  {


    SymbolName = Symbol();


   IndicatorBuffers(3);
   
   SetIndexStyle(0,DRAW_LINE,0,1);
   SetIndexBuffer(0, VolBuffer1);

   SetIndexEmptyValue(0,EMPTY_VALUE);
   
   SetIndexStyle(1,DRAW_LINE,0,1);
   SetIndexBuffer(1, VolBuffer2);

   SetIndexEmptyValue(1,EMPTY_VALUE);
   
   SetIndexStyle(2,DRAW_LINE,0,1);
   SetIndexBuffer(2, VolBuffer3);

   SetIndexEmptyValue(2,EMPTY_VALUE);

   return(0);
  }


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

  }

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

   for (int i=NrBars-1;i>=0;i--)
      {
      datetime time1=iTime(SymbolName,PeriodMinutes1,i);
      
//      int b5=iBarShift(SymbolName,PeriodMinutes5,time1,true);
//      int b60=iBarShift(SymbolName,PeriodMinutes60,time1,true);
      int b240=iBarShift(SymbolName,PeriodMinutes240,time1,true);
    
/*
      if(b5>=0)
      {
        x0=iClose(SymbolName,PeriodMinutes5,b5);   
           
        x1=iMA(SymbolName,PeriodMinutes5,5,0,MODE_SMA,PRICE_CLOSE,b5);
        x2=iMA(SymbolName,PeriodMinutes5,10,0,MODE_SMA,PRICE_CLOSE,b5);
        x3=iMA(SymbolName,PeriodMinutes5,15,0,MODE_SMA,PRICE_CLOSE,b5);
        
        at0=x1-x2;
        at1=x2-x3;
        at2=x1-x3;

        y1=iMA(SymbolName,PeriodMinutes5,5,0,MODE_SMA,PRICE_CLOSE,b5+1);
        y2=iMA(SymbolName,PeriodMinutes5,10,0,MODE_SMA,PRICE_CLOSE,b5+1);
        y3=iMA(SymbolName,PeriodMinutes5,15,0,MODE_SMA,PRICE_CLOSE,b5+1);

        at9=y1-y2;
        at10=y2-y3;
        at11=y1-y3;

        z1=iMA(SymbolName,PeriodMinutes5,5,0,MODE_SMA,PRICE_CLOSE,b5+2);
        z2=iMA(SymbolName,PeriodMinutes5,10,0,MODE_SMA,PRICE_CLOSE,b5+2);
        z3=iMA(SymbolName,PeriodMinutes5,15,0,MODE_SMA,PRICE_CLOSE,b5+2);

        at18=z1-z2;
        at19=z2-z3;
        at20=z1-z3;        
      }
*/
      
/*     
      if(b60>=0)
      {        
        x4=iMA(SymbolName,PeriodMinutes60,5,0,MODE_SMA,PRICE_CLOSE,b60);
        x5=iMA(SymbolName,PeriodMinutes60,10,0,MODE_SMA,PRICE_CLOSE,b60);
        x6=iMA(SymbolName,PeriodMinutes60,15,0,MODE_SMA,PRICE_CLOSE,b60);

        at3=x4-x5;
        at4=x5-x6;
        at5=x4-x6;
        
        y4=iMA(SymbolName,PeriodMinutes60,5,0,MODE_SMA,PRICE_CLOSE,b60+1);
        y5=iMA(SymbolName,PeriodMinutes60,10,0,MODE_SMA,PRICE_CLOSE,b60+1);
        y6=iMA(SymbolName,PeriodMinutes60,15,0,MODE_SMA,PRICE_CLOSE,b60+1);

        at12=y4-y5;
        at13=y5-y6;
        at14=y4-y6;
        
        z4=iMA(SymbolName,PeriodMinutes60,5,0,MODE_SMA,PRICE_CLOSE,b60+2);
        z5=iMA(SymbolName,PeriodMinutes60,10,0,MODE_SMA,PRICE_CLOSE,b60+2);
        z6=iMA(SymbolName,PeriodMinutes60,15,0,MODE_SMA,PRICE_CLOSE,b60+2);

        at21=z4-z5;
        at22=z5-z6;
        at23=z4-z6;        
      }       
*/      
      
      if(b240>=0)
      {             
        x7=iMA(SymbolName,PeriodMinutes240,5,0,MODE_SMA,PRICE_CLOSE,b240);
        x8=iMA(SymbolName,PeriodMinutes240,10,0,MODE_SMA,PRICE_CLOSE,b240);
        x9=iMA(SymbolName,PeriodMinutes240,15,0,MODE_SMA,PRICE_CLOSE,b240);
        at6=x7-x8;
        at7=x8-x9;
        at8=x7-x9;
        y7=iMA(SymbolName,PeriodMinutes240,5,0,MODE_SMA,PRICE_CLOSE,b240+1);
        y8=iMA(SymbolName,PeriodMinutes240,10,0,MODE_SMA,PRICE_CLOSE,b240+1);
        y9=iMA(SymbolName,PeriodMinutes240,15,0,MODE_SMA,PRICE_CLOSE,b240+1);

        at15=y7-y8;
        at16=y8-y9;
        at17=y7-y9;
        z7=iMA(SymbolName,PeriodMinutes240,5,0,MODE_SMA,PRICE_CLOSE,b240+2);
        z8=iMA(SymbolName,PeriodMinutes240,10,0,MODE_SMA,PRICE_CLOSE,b240+2);
        z9=iMA(SymbolName,PeriodMinutes240,15,0,MODE_SMA,PRICE_CLOSE,b240+2);
        at24=z7-z8;
        at25=z8-z9;
        at26=z7-z9;
      }
  
   if(Option==0)
   {  
	  VolBuffer1[i]=at6; 
 	  VolBuffer2[i]=at7;   
 	  VolBuffer3[i]=at8;      
 	}
 	else if(Option==1)
   {   
	  VolBuffer1[i]=at15; 
 	  VolBuffer2[i]=at16;   
 	  VolBuffer3[i]=at17;      
 	}
 	else if(Option==2)
   {    
	  VolBuffer1[i]=at24; 
 	  VolBuffer2[i]=at25;   
 	  VolBuffer3[i]=at26; 
 	}
 	  
  
  }
  
 
   
		

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

Comments