Author: Copyright � 2006, Keris2112
MTFMA

This tool is designed to help traders visualize price trends by calculating Moving Averages from higher timeframes directly onto their current chart. In traditional trading, looking at a high timeframe (like a Daily or Weekly chart) provides a clearer picture of the "big picture" trend, but it often requires switching back and forth between different windows. This script solves that problem by projecting that larger perspective onto your active screen.

How the Logic Works

1. Multi-Timeframe Synchronization The core logic of this script is its ability to bridge different time scales. It looks at a higher timeframe (e.g., Hourly or Daily) and identifies the corresponding price data for your current view. Even if you are looking at a 5-minute chart, the script "reaches out" to pull the necessary data from the larger timeframe to ensure the moving average remains accurate to that broader trend.

2. Customizable Smoothing Methods The script provides different ways to calculate the "average" price, allowing users to choose how much weight they want to give to recent price action:

  • Simple Moving Average (SMA): Calculates a straightforward average of prices over a specific period.
  • Exponential Moving Average (EMA): Gives more importance to the most recent prices, making it react faster to sudden market changes.

3. Dynamic Price Selection Not all data points are equal in trading. The script allows you to choose which specific price point from each candle should be used for the calculation. You can set it to track the Open, High, Low, or Close prices. Most traders use the "Close" price, but choosing others can help identify different types of price behavior.

4. Automatic Labeling and Visibility To ensure clarity, the script automatically generates a descriptive label on your chart. This tells you exactly what parameters are currently active—such as the specific timeframe being tracked, the period length, and the calculation method used. It also ensures that the visual line is drawn correctly across your screen without cluttering the workspace.

Summary of Benefits

  • Seamless Context: See the "Big Picture" trend without leaving your current chart.
  • Flexibility: Adjust the timeframe, period, and calculation style to match your specific trading strategy.
  • Precision: Automatically synchronizes data between different timeframes so that the moving average stays true to the higher timeframe's movements.
Miscellaneous
Implements a curve of type %1
6 Views
0 Downloads
0 Favorites
MTFMA
//+------------------------------------------------------------------+
//|                                                        MTFMA.mq4 |
//|                                      Copyright © 2006, Keris2112 |
//|                             fixed for lower timeframes by Renato |
//|                                     http://www.reniza.com/forex/ |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Keris2112"
#property link      "http://www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 DodgerBlue

//---- Name for DataWindow and indicator subwindow label
string short_name;

//---- indicator parameters
extern int TimeFrame=0;
extern int MA_Period=13;
extern int MA_Shift=0;
extern int MA_Method=MODE_SMA;
extern int Applied_Price=3;
extern int Shift=0;

//----
int ExtCountedBars;
int limit;
int HTFlimit;
datetime HTFTimeArray[];
double HTFPriceArray[];

//---- indicator buffers
double ExtMapBuffer[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init() {

//---- drawing settings
   if ( MA_Period<2 ) MA_Period=2;
   int draw_begin=MA_Period;
   SetIndexStyle(0,DRAW_LINE);
   SetIndexShift(0,MA_Shift);
   IndicatorDigits(MarketInfo(Symbol(),MODE_DIGITS));

//---- indicator short name
   switch(MA_Method) {
      case 1 : short_name="MTF_EMA("; break;
      case 2 : short_name="MTF_SMMA("; break;
      case 3 : short_name="MTF_LWMA("; break;
      default : short_name="MTF_SMA("; MA_Method=0;
     }

	short_name=short_name+MA_Period+") (";

   switch(Applied_Price) {
      case 0 : short_name=short_name+"Open"; break;
      case 1 : short_name=short_name+"Low"; break;
      case 2 : short_name=short_name+"High"; break;
      case 3 : short_name=short_name+"Close"; break;
      default : short_name=short_name+"Close"; Applied_Price=3;
     }

	short_name=short_name+":"+Symbol()+",";

	if ( TimeFrame==0 )
		TimeFrame=Period();
   switch(TimeFrame) {
      case 1 : short_name=short_name+"M1"; break;
      case 5 : short_name=short_name+"M5"; break;
      case 15 : short_name=short_name+"M15"; break;
      case 30 : short_name=short_name+"M30"; break;
      case 60 : short_name=short_name+"H1"; break;
      case 240 : short_name=short_name+"H4"; break;
      case 1440 : short_name=short_name+"D1"; break;
      case 10080 : short_name=short_name+"W1"; break;
      case 43200 : short_name=short_name+"MN1"; break;
   } 

	short_name=short_name+")";

   IndicatorShortName(short_name);  
   SetIndexDrawBegin(0,draw_begin);
   
   Comment(short_name);
   
//---- indicator buffers mapping
   SetIndexBuffer(0,ExtMapBuffer);

//----
   return(0);
  }
 
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit() {

//----

	Comment("");

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

//+------------------------------------------------------------------+
//| MTF Moving Average                                   |
//+------------------------------------------------------------------+
int start()
  {
   if( Bars<=MA_Period ) return(0);
   ExtCountedBars=IndicatorCounted();

//---- check for possible errors
   if ( ExtCountedBars<0 ) return(-1);
   if ( ExtCountedBars>0 ) ExtCountedBars=0; 

	limit=Bars-MA_Period-1;
	HTFlimit=ArrayCopySeries(HTFTimeArray,MODE_TIME,Symbol(),TimeFrame)-1;
   ArrayCopySeries(HTFPriceArray,Applied_Price,Symbol(),TimeFrame); 

	int i,y;

  	for(i=0,y=0;i<limit;i++) {
		if ( TimeFrame>Period() )
	   	if (Time[i]<HTFTimeArray[y]) y++;   
		if ( TimeFrame<=Period() )
	   	while(Time[i]<HTFTimeArray[y]) y++;
	   switch(MA_Method) {
      	case 0 : ExtMapBuffer[i]=SMA(HTFPriceArray,MA_Period,y); break;
      	case 1 : ExtMapBuffer[i]=EMA(HTFPriceArray,MA_Period,y); break;
		}
	}

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

//+------------------------------------------------------------------+
//| Simple Moving Average                                            |
//+------------------------------------------------------------------+
double SMA(double PriceArray[], int MA_Period, int shift) {
   double sum=0;
   for(int i=shift+MA_Period-1;i>=shift;i--)
      sum+=PriceArray[i];
	return(sum/MA_Period);
}

double EMA(double PriceArray[], int MA_Period, int shift) {
   double pr=2.0/(MA_Period+1);
   double sum=PriceArray[shift+2*MA_Period-2];
   for(int i=shift+2*MA_Period-1;i>=shift;i--)
      sum=PriceArray[i]*pr+sum*(1-pr);
	return(sum);
}


Comments