MTF_BH_Ergodic_4TFBars
Download scriptThis script is designed as a multi-timeframe visual analysis tool for MetaTrader. Its primary purpose is to track and display trend signals across four different timeframes simultaneously, allowing a trader to see how higher-level market trends interact with lower-level price action at a single glance.
Core Logic and Functionality
1. Multi-Timeframe Integration The core of this script is its ability to "reach out" and pull data from four distinct timeframes (specifically defined as Hourly, 4-Hour, Daily, and Weekly). Instead of forcing the user to switch charts manually to see what is happening on different scales, the script synchronizes this information into a single window.
2. Signal Detection via Nested Indicators The script operates by communicating with another underlying indicator (the "BH-Ergodic"). It looks for specific directional signals—essentially identifying whether the price movement on those higher timeframes is currently "up" or "down."
3. Visual Mapping Once the script identifies these signals from the various timeframes, it maps them onto the chart using a series of visual markers (arrows).
- It organizes these signals into eight distinct data streams: four representing upward trends and four representing downward trends across the different timeframes.
- By displaying these together, the user can see an immediate "consensus" of the market. For example, if multiple timeframes are showing the same directional signal simultaneously, it suggests a stronger, more aligned market trend.
4. Dynamic Time Synchronization Because different timeframes have different numbers of candles per unit of time (e.g., one Daily candle covers many Hourly candles), the script includes logic to calculate "time shifts." This ensures that the data pulled from a Weekly chart is correctly aligned with the current moment on an Hourly chart, preventing the visual markers from appearing out of sync or incorrectly placed in time.
5. Data Smoothing and Persistence The script includes a mechanism to ensure that once a signal is identified, it remains visible and consistent across the relevant price bars. It "refreshes" its buffers to make sure that as new data comes in, the historical visual markers stay correctly positioned, providing a clean and continuous history of market sentiment across all selected timeframes.
Summary for the User
In simple terms, this script acts as a Trend Dashboard. It scans four different "horizons" of the market at once and places markers on your screen to tell you whether each of those horizons is trending up or down. This helps you avoid making trades that go against the broader trend of the market.
//+------------------------------------------------------------------+
//|MTF_BH_Ergodic_4TFBars basedonMTF_Supertrend2_Bars.mq4 |
//|2007, Bruce Hellstrom 2006, MetaQuotes Software Corp. |
//|4TFBars Forex-TSD.com ki 2006 Elihayun http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Eli hayun."
#property link "http://www.elihayun.com"
#property indicator_separate_window
#property indicator_minimum 0
#property indicator_maximum 6
#property indicator_buffers 8
#property indicator_color1 MediumOrchid
#property indicator_color2 MediumPurple
#property indicator_color3 MediumOrchid
#property indicator_color4 MediumPurple
#property indicator_color5 Aqua
#property indicator_color6 DarkTurquoise
#property indicator_color7 Aqua
#property indicator_color8 DarkTurquoise
// IndicatorDigits(2);
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];
extern string CustomIndicator = "BH-Ergodic";
extern int BarDiff = 1; // calculations are taken between current bar and current bar + BarDiff
extern int r = 2; // First moving average on mean values
extern int s = 10; // Second moving average applied to first
extern int u = 5; // Third moving average applied to division
extern int trigger = 3; // Final moving average or smoothing
extern int PriceType = 0; // 0=Close, 1=Open, 2=High, 3=Low, 4=Median, 5=Typical, 6=Weighted
extern string note____TF = "1,5,15,30,60H1,240H4,1440D1,10080W1,43200MN1";
extern string _Pprice_Type = "C0,O1,H2,L3,Mdn4,Tpcl5,WghtCl_6";
extern int Maxbars=1000;
//extern int NbPeriods = 10;
//extern int LSMA_Period = 5;
extern int TF4 = PERIOD_H1;
extern int TF3 = PERIOD_H4;
extern int TF2 = PERIOD_D1;
extern int TF1 = PERIOD_W1;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
SetIndexStyle(0,DRAW_ARROW);
SetIndexArrow(0,110);
SetIndexBuffer(0,ExtMapBuffer1);
SetIndexEmptyValue(0,0.0);
SetIndexStyle(1,DRAW_ARROW);
SetIndexArrow(1,110);
SetIndexBuffer(1,ExtMapBuffer2);
SetIndexEmptyValue(1,0.0);
SetIndexStyle(2,DRAW_ARROW);
SetIndexArrow(2,110);
SetIndexBuffer(2,ExtMapBuffer3);
SetIndexEmptyValue(2,0.0);
SetIndexStyle(3,DRAW_ARROW);
SetIndexArrow(3,110);
SetIndexBuffer(3,ExtMapBuffer4);
SetIndexEmptyValue(3,0.0);
SetIndexStyle(4,DRAW_ARROW);
SetIndexArrow(4,110);
SetIndexBuffer(4,ExtMapBuffer5);
SetIndexEmptyValue(4,0.0);
SetIndexStyle(5,DRAW_ARROW);
SetIndexArrow(5,110);
SetIndexBuffer(5,ExtMapBuffer6);
SetIndexEmptyValue(5,0.0);
SetIndexStyle(6,DRAW_ARROW);
SetIndexArrow(6,110);
SetIndexBuffer(6,ExtMapBuffer7);
SetIndexEmptyValue(6,0.0);
SetIndexStyle(7,DRAW_ARROW);
SetIndexArrow(7,110);
SetIndexBuffer(7,ExtMapBuffer8);
SetIndexEmptyValue(7,0.0);
//----
SetIndexLabel(0,"BH-Ergodic("+TF4+")");
SetIndexLabel(1,"BH-Ergodic("+TF3+")");
SetIndexLabel(2,"BH-Ergodic("+TF2+")");
SetIndexLabel(3,"BH-Ergodic("+TF1+")");
SetIndexLabel(4,"BH-Ergodic("+TF4+")");
SetIndexLabel(5,"BH-Ergodic("+TF3+")");
SetIndexLabel(6,"BH-Ergodic("+TF2+")");
SetIndexLabel(7,"BH-Ergodic("+TF1+")");
IndicatorDigits(1);
//---
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
//----
int limit;
int TF[] = {0,0,0,0};
TF[0] = TF1;
TF[1] = TF2;
TF[2] = TF3;
TF[3] = TF4;
// limit = MathMin(counted_bars,Maxbars);
// if (limit ==0)
// limit = Maxbars;
limit=Bars-counted_bars;
limit=MathMax(limit,TF1/Period());
limit=MathMax(limit,TF2/Period());
limit=MathMax(limit,TF3/Period());
limit=MathMax(limit,TF4/Period());
limit=MathMin(limit,Maxbars);
for (int ii=0; ii<limit; ii++)
{
ExtMapBuffer1[ii] = 0;
ExtMapBuffer2[ii] = 0;
ExtMapBuffer3[ii] = 0;
ExtMapBuffer4[ii] = 0;
ExtMapBuffer5[ii] = 0;
ExtMapBuffer6[ii] = 0;
ExtMapBuffer7[ii] = 0;
ExtMapBuffer8[ii] = 0;
for (int jj=0; jj<4; jj++)
{
int y = iBarShift(NULL, TF[jj], Time[ii]);
SetIndicator(GetCustomBars(TF[jj], y), jj, ii);
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
bool GetCustomBars(int TF, int shift)
{
double h = iCustom(Symbol(), TF, CustomIndicator, BarDiff,r,s,u,trigger,PriceType,1, shift);
double l = iCustom(Symbol(), TF, CustomIndicator, BarDiff,r,s,u,trigger,PriceType,0, shift);
return(h<l);
}
void SetIndicator(bool isUP, int ind, int shift)
{
if (isUP)
{
switch (ind)
{
case 3: ExtMapBuffer1[shift] = ind+1; break;
case 2: ExtMapBuffer2[shift] = ind+1; break;
case 1: ExtMapBuffer3[shift] = ind+1; break;
case 0: ExtMapBuffer4[shift] = ind+1; break;
}
} else {
switch (ind)
{
case 3: ExtMapBuffer5[shift] = ind+1; break;
case 2: ExtMapBuffer6[shift] = ind+1; break;
case 1: ExtMapBuffer7[shift] = ind+1; break;
case 0: ExtMapBuffer8[shift] = ind+1; break;
}
}
//---- Refresh buffers ++++++++++++++++++++ upgrade by Raff
int TimeFrame,TimeArray[],i; if (TimeFrame>Period()) {
int PerINT=TimeFrame/Period()+1;
datetime TimeArr[]; ArrayResize(TimeArr,PerINT);
ArrayCopySeries(TimeArr,MODE_TIME,Symbol(),Period());
for(i=0;i<PerINT+1;i++) {if (TimeArr[i]>=TimeArray[0]) {
//----
/********************************************************
Refresh buffers: buffer[i] = buffer[0];
********************************************************/
ExtMapBuffer1[i] = ExtMapBuffer1[0];
ExtMapBuffer2[i] = ExtMapBuffer2[0];
ExtMapBuffer3[i] = ExtMapBuffer3[0];
ExtMapBuffer4[i] = ExtMapBuffer4[0];
ExtMapBuffer5[i] = ExtMapBuffer5[0];
ExtMapBuffer6[i] = ExtMapBuffer6[0];
ExtMapBuffer7[i] = ExtMapBuffer7[0];
ExtMapBuffer8[i] = ExtMapBuffer8[0];
//----
} } }
//+++++++++++++++++++++++++++++++++++++++++++++++ Raff
return(0);
}
Comments