MA_MTF_MovingAverage_v1.1_HrzLn+Dev+Lv

Download script
Author: Copyright � 2006, Keris2112
MA_MTF_MovingAverage_v1.1_HrzLn+Dev+Lv

This tool is a Multi-Timeframe (MTF) Moving Average indicator designed for the MetaTrader 4 platform. Its primary purpose is to allow traders to visualize price trends from higher timeframes directly on their current, lower timeframe charts.

Core Functionality

The script calculates a moving average based on data from a specific timeframe that you choose (for example, viewing a Daily trend while looking at an Hourly chart). This helps in identifying the "big picture" market direction without needing to switch back and forth between different tabs or windows.

Key Features and Logic

1. Multi-Timeframe Integration The logic is built to bridge the gap between different time periods. It automatically fetches data from a higher timeframe (like H4, Daily, or Weekly) and projects it onto your active screen. The script ensures that the data remains synchronized so that the trend line reflects the correct historical price action of that larger period.

2. Customizable Moving Averages The tool is highly flexible regarding how it calculates the average. It supports four distinct mathematical methods:

  • Simple Moving Average (SMA)
  • Exponential Moving Average (EMA)
  • Smoothed Moving Average (SMMA)
  • Linear Weighted Moving Average (LWMA)

3. Price Source Selection Traders can choose which specific price point the average should be based on. Rather than just using the closing price, you can configure the logic to calculate based on the High, Low, Open, or even a "Median" or "Typical" price of the candles.

4. Precision Adjustments (Deviation and Level) The script includes a unique mathematical layer that allows for manual "offsets." You can apply a percentage-based deviation or a specific pip level to the moving average line. This is useful for creating "buffer zones" or custom price targets relative to the moving average.

5. Visual Customization In addition to the main moving average line, the script includes a logic to draw a horizontal reference line. This line can be customized by style, color, and length, providing a steady visual anchor on the chart for specific price levels or key milestones.

How it Works for the User

When you load this onto a chart, the script looks at your current view and "looks ahead" into the data of your chosen higher timeframe. It then draws a smooth line representing the average trend. By adjusting the inputs, you can toggle these lines on or off, change their thickness, and determine exactly how much weight is given to past prices versus recent price action.

Price Data Components
Series array that contains open time of each bar
Indicators Used
Moving average indicator
Miscellaneous
Implements a curve of type %1
7 Views
0 Downloads
0 Favorites
MA_MTF_MovingAverage_v1.1_HrzLn+Dev+Lv
//+------------------------------------------------------------------+
//|                                            MTF_MovingAverage.mq4 |
//|                                      Copyright © 2006, Keris2112 |
//|                                     IgorAd    www.forex-tsd.com  |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Keris2112"
#property link      "http://www.forex-tsd.com"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Blue
#property indicator_width1 2

//---- input parameters
/*************************************************************************
PERIOD_M1   1
PERIOD_M5   5
PERIOD_M15  15
PERIOD_M30  30 
PERIOD_H1   60
PERIOD_H4   240
PERIOD_D1   1440
PERIOD_W1   10080
PERIOD_MN1  43200
You must use the numeric value of the timeframe that you want to use
when you set the TimeFrame' value with the indicator inputs.
---------------------------------------
PRICE_CLOSE    0 Close price. 
PRICE_OPEN     1 Open price. 
PRICE_HIGH     2 High price. 
PRICE_LOW      3 Low price. 
PRICE_MEDIAN   4 Median price, (high+low)/2. 
PRICE_TYPICAL  5 Typical price, (high+low+close)/3. 
PRICE_WEIGHTED 6 Weighted close price, (high+low+close+close)/4. 
You must use the numeric value of the Applied Price that you want to use
when you set the 'applied_price' value with the indicator inputs.
---------------------------------------
MODE_SMA    0 Simple moving average, 
MODE_EMA    1 Exponential moving average, 
MODE_SMMA   2 Smoothed moving average, 
MODE_LWMA   3 Linear weighted moving average. 
You must use the numeric value of the MA Method that you want to use
when you set the 'ma_method' value with the indicator inputs.

**************************************************************************/
extern int TimeFrame=0;
extern int MAPeriod=21;
extern int ma_shift=0;
extern int ma_method=1;
extern int applied_price=0;
extern double Deviation=0.00;
extern int Level= 0;//in pips

extern bool    DrawMALine=true;
extern bool    DrawHrzLine=true;
extern color   HrzLineColor=DodgerBlue;
extern int     HrzLineStyle=2;
extern int     HrzLineWidth=1;
extern int     HrzLineLength=24;

extern int     HrzLineOnBar=0; // bar#(from the right); 0-current(open) bar
extern bool    HrzLineOnBackground=true;

extern string   note_Price = "0C 1O 2H 3L 4Md 5Tp 6WghC: Md(HL/2)4,Tp(HLC/3)5,Wgh(HLCC/4)6";
extern string   MA_Method_ = "SMA0 EMA1 SMMA2 LWMA3; ";
extern string TimeFrames = "M1;5,15,30,60H1;240H4;1440D1;10080W1;43200MN";

string short_name;
string HrzLine1;


double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//   string short_name;
//---- indicator line
   SetIndexBuffer(0,ExtMapBuffer1);

   if (DrawMALine)
   SetIndexStyle(0,DRAW_LINE);
   else 
   SetIndexStyle(0,DRAW_NONE);

   if(TimeFrame==0) TimeFrame = Period();
   SetIndexShift(0,ma_shift*TimeFrame/Period());
//---- name for DataWindow and indicator subwindow label   
   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(";
     }
   switch(TimeFrame)
   {
      case 1 : string TimeFrameStr="M1"; break;
      case 5 : TimeFrameStr="M5"; break;
      case 15 : TimeFrameStr="M15"; break;
      case 30 : TimeFrameStr="M30"; break;
      case 60 : TimeFrameStr="H1"; break;
      case 240 : TimeFrameStr="H4"; break;
      case 1440 : TimeFrameStr="D1"; break;
      case 10080 : TimeFrameStr="W1"; break;
      case 43200 : TimeFrameStr="MN1"; break;
      default : TimeFrameStr="TF0";
   } 

   IndicatorShortName(short_name +MAPeriod+") "+TimeFrameStr);  
   HrzLine1= short_name+ MAPeriod+") "+TimeFrameStr+" | "+DoubleToStr(Deviation,2)+", "+Level;
   TimeFrame = MathMax(TimeFrame, Period());

  return(0);
  }

//----
  int deinit()
{
  
   ObjectDelete(HrzLine1);
   return(0);
}

 
//+------------------------------------------------------------------+
//| MTF Moving Average                                   |
//+------------------------------------------------------------------+
int start()
  {
   datetime TimeArray[];
   int    i,shift,limit,y,counted_bars=IndicatorCounted();
    
// Plot defined timeframe on to current timeframe   
   ArrayCopySeries(TimeArray,MODE_TIME,Symbol(),TimeFrame); 
   
limit=Bars-counted_bars;
limit= MathMax(limit,TimeFrame/Period());
   for(i=0,y=0;i<limit;i++)
   {
   if (Time[i]<iTime(NULL,TimeFrame,y)) y++; 
   
 /***********************************************************   
   Add your main indicator loop below.  You can reference an existing
      indicator with its iName  or iCustom.
   Rule 1:  Add extern inputs above for all neccesary values   
   Rule 2:  Use 'TimeFrame' for the indicator timeframe
   Rule 3:  Use 'y' for the indicator's shift value
 **********************************************************/  
    
   ExtMapBuffer1[i]=(1+Deviation/100)*iMA(NULL,TimeFrame,MAPeriod,0,ma_method,applied_price,y)+(Level*Point) ; 
 
   }  
     
//
   
      
      if (ObjectFind(HrzLine1) != 0 && DrawHrzLine)
      ObjectCreate(HrzLine1,OBJ_TREND,0,Time[0],ExtMapBuffer1[HrzLineOnBar],Time[HrzLineLength],ExtMapBuffer1[HrzLineOnBar]);
      ObjectSet(HrzLine1, OBJPROP_COLOR, HrzLineColor);
      ObjectSet(HrzLine1, OBJPROP_STYLE, HrzLineStyle);
      ObjectSet(HrzLine1, OBJPROP_WIDTH, HrzLineWidth);
      ObjectSet(HrzLine1, OBJPROP_BACK,  HrzLineOnBackground);
      ObjectSet(HrzLine1, OBJPROP_RAY,  false);

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

Comments