This script is designed as a multi-timeframe analysis tool for the MetaTrader platform. Its primary purpose is to allow a trader to view data from four different timeframes simultaneously on a single chart, providing a comprehensive overview of market trends without needing to switch between different windows.
Core Functionality
The tool focuses on retrieving information from four specific intervals: 5-minute (M5), 15-minute (M15), 30-minute (M30), and 60-minute (H1). It calculates and displays the values for a secondary custom indicator (specifically set to use a TMA or similar calculation method) across all these timeframes at once.
How the Logic Works
To achieve this, the script follows a structured process:
- Data Synchronization: Since every timeframe moves at a different speed, the script creates a bridge between the current chart and the higher/lower timeframes. It tracks the specific "time coordinates" for each interval to ensure that the data being pulled matches the correct point in history.
- Dynamic Mapping: As you move back through historical price bars on your chart, the script automatically calculates which corresponding bar belongs to the 5-minute, 15-minute, 30-minute, and hourly periods. This ensures that even if the timeframes don't align perfectly, the lines drawn on your chart represent the most accurate relative data available.
- Multi-Layered Visualization: The script creates four distinct "buffers" (visual layers). Each layer represents one of the four timeframes. By plotting these together, the user can see how a short-term trend (like the 5-minute view) interacts with longer-term trends (like the hourly view).
- Customizable Inputs: The logic allows users to adjust specific parameters such as "Periods" and "ApplyTo." These settings dictate how the underlying calculation is weighted, allowing the user to smooth out the lines or make them more sensitive to recent price action.
User Benefit
Instead of manually checking four different charts to see if the market is moving in harmony across various timeframes, this script consolidates that information into a single visual display. It allows a trader to identify "confluence"—where multiple timeframes agree on a trend direction—making it easier to spot high-probability trading opportunities at a glance.
//+------------------------------------------------------------------+
//| #MTF_4_CoeffoLine.mq4 |
//| Copyright © 2005, MetaQuotes Software Corp. |
//| http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2005, MetaQuotes Software Corp."
#property link "http://www.metaquotes.net"
#property indicator_chart_window
#property indicator_buffers 4
#property indicator_color1 Blue
#property indicator_color2 Blue
#property indicator_color3 Blue
#property indicator_color4 Blue
//---- input parameters
//---- buffers
double Buffer_M5[];
double Buffer_M15[];
double Buffer_M30[];
double Buffer_H1[];
datetime dt5[200], dt15[200], dt30[200], dth1[200];
extern string CustomIndicator = "TMA";
extern int CustomIndIndex = 0;
extern int PRD_1 = 5;
extern int PRD_2 = 15;
extern int PRD_3 = 30;
extern int PRD_4 = 60;
//-----------------------
extern int Periods=30;
extern int ApplyTo=0;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_LINE);
SetIndexBuffer(0,Buffer_M5);
SetIndexStyle(1,DRAW_LINE);
SetIndexBuffer(1,Buffer_M15);
SetIndexStyle(2,DRAW_LINE);
SetIndexBuffer(2,Buffer_M30);
SetIndexStyle(3,DRAW_LINE);
SetIndexBuffer(3,Buffer_H1);
IndicatorShortName("MTF_4_TMA M5,M15,M30,M60");
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int limit, shift;
if(counted_bars<0) return(-1);
if (counted_bars==0) limit=Bars-1; // limit = Bars-MinBars-1;
//---- last counted bar will be recounted
if(counted_bars>0) limit=Bars-counted_bars;
limit--;
int i5 = 0, i15 = 0, i30 = 0, ih1 = 0;
static bool fft = true;
//if (NewBar()) // Update array every 15 minutes
{
ArrayCopySeries(dt5,MODE_TIME,Symbol(),PRD_1);
ArrayCopySeries(dt15,MODE_TIME,Symbol(),PRD_2);
ArrayCopySeries(dt30,MODE_TIME,Symbol(),PRD_3);
ArrayCopySeries(dth1,MODE_TIME,Symbol(),PRD_4);
}
if ((fft) && (Symbol() == "EURUSD"))
{
fft = false;
datetime d = iTime(NULL, PERIOD_D1, 0);
for (int j=0; j<80; j++)
{
Print(j,") ",dt5[j]-d," ",dt15[j]-d, " ", dt30[j]-d," ", dth1[j]-d);
}
}
for(int i=0; i<limit; i++)
{
datetime ddt = iTime(NULL, 0, i);
if (ddt<dt5[i5]) i5++;
if (ddt<dt15[i15]) i15++;
if (ddt<dt30[i30]) i30++;
if (ddt<dth1[ih1]) ih1++;
double COG5=iCustom(NULL,PERIOD_M5,CustomIndicator,Periods,ApplyTo,CustomIndIndex,i5);
double COG5S=0;
double COG15=iCustom(NULL,PERIOD_M15,CustomIndicator,Periods,ApplyTo,CustomIndIndex,i15);
double COG15S=0;
double COG30=iCustom(NULL,PERIOD_M30,CustomIndicator,Periods,ApplyTo,CustomIndIndex,i30);
double COG30S=0;
double COGH1=iCustom(NULL,PERIOD_H1,CustomIndicator,Periods,ApplyTo,CustomIndIndex,ih1);
double COGH1S=0;
Buffer_M5[i] = COG5;
Buffer_M15[i] = COG15;
Buffer_M30[i] = COG30;
Buffer_H1[i] = COGH1;
}
return(0);
}
//+------------------------------------------------------------------+
/*
bool NewBar()
{
static datetime dt = 0;
if (dt != iTime(NULL, PERIOD_M15, 0))
{
dt = iTime(NULL, PERIOD_M15, 0);
}
return(false);
}
*/
Comments