This script is designed to provide a more consistent and stable visual perspective on market trends by "locking" a Moving Average (MA) to a specific timeframe, regardless of which smaller timeframe you are currently viewing.
The Core Concept: Timeframe Synchronization
In standard trading, if you use an 8-period Moving Average on a 15-minute chart, the line reacts very quickly to price changes. If you switch to a 1-hour chart, that same 8-period indicator will look completely different because it is now calculating based on much larger blocks of time.
This tool solves that problem by synchronizing the data. It allows you to choose a "Master Timeframe" and a "Base Period." The script then automatically calculates the necessary math to ensure that the moving average line represents the exact same amount of historical price action across all your charts.
How it Works in Practice
Instead of simply showing you what is happening on the current screen, the script performs a behind-the-scenes conversion:
- The Master Reference: You select a target timeframe (for example, the 1-hour chart) and a base period (such as 8).
- Automatic Scaling: When you move to a smaller timeframe (like the 5-minute chart), the script calculates how many bars are needed to equal that 1-hour/8-period value. It then draws a line that reflects that larger data set on your small chart.
- Visual Consistency: This means if you are looking at a 15-minute chart and then switch to a 30-minute chart, the moving average will remain "locked." The line won't shift or change its behavior based on your zoom level; it will stay visually consistent with the larger timeframe you chose as your anchor.
Key Features
- Customizable Smoothing: You can choose different types of moving averages (Simple, Exponential, etc.) to suit your preferred trading style.
- Price Source Selection: You can decide whether the average is calculated based on the closing price, opening price, highs, lows, or other weighted variations.
- Unified Analysis: It provides a way for traders to see "the big picture" while still doing their detailed analysis on smaller, more active timeframes. This helps in identifying major trend directions without getting distracted by minor price fluctuations on the lower charts.
//+------------------------------------------------------------------+
//| MALock.mq4 |
//| Copyright © tembox, 27 March 2015 |
//| ------------ |
//| |
//| Moving Average bound to spesific Time Frame |
//| example: Locked MA(8) on H1 --> MA(32) on M15 --> MA(16) on M30 |
//| Locked MA(32) on M15 --> MA(8) on H1 --> MA(16) on M30 |
//+------------------------------------------------------------------+
#property copyright "Copyright © tembox"
#property link ""
//---
#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
#property indicator_width1 1
//+------------------------------------------------------------------+
//| Input parameters |
//+------------------------------------------------------------------+
extern string noteMATF = "5=M5; 15=M15; 30=M30; 60=H1; 240=H4; 1440=D1; 10080=W1; 43200=MN";
extern int MATF = 60;
extern int MAPeriod = 8;
extern string noteMAMethod = "0=MODE_SMA; 1=MODE_EMA; 2=MODE_SMMA; 3=MODE_LWMA";
extern int MAMethod = 0;
extern string noteMAPrice = "0=CLOSE; 1=OPEN; 2=HIGH; 3=LOW; 4=MEDIAN; 5=TYPICAL; 6=WIGHTED";
extern int MAPrice = 0;
extern int MAShift = 0;
//+------------------------------------------------------------------+
//| Buffers |
//+------------------------------------------------------------------+
double Buff1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,Buff1);
SetIndexLabel(0,"MALock("+MAPeriod+"->"+(MATF*MAPeriod/Period())+")");
SetIndexDrawBegin(0,(MATF*MAPeriod)/Period());
// Data window
IndicatorShortName("MALock("+MAPeriod+")");
return(0);
}
//+------------------------------------------------------------------+
//| Custor indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int limit;
int counted_bars = IndicatorCounted();
double NewPeriod = (MATF*MAPeriod/Period());
//---
if(counted_bars < 0) return(-1);
limit=Bars-1 -counted_bars;
//---
for(int i=limit; i>=0; i--)
{
Buff1[i]=iMA(NULL,0,NewPeriod,MAShift,MAMethod,MAPrice,i);
}
return(0);
}
//+------------------------------------------------------------------+
Comments