Supertrend line1
Download scriptOverview
This script is a multi-timeframe analysis tool designed to provide traders with a comprehensive view of market trends. Instead of looking at just one timeframe (like the current chart you are viewing), it simultaneously tracks and displays the "SuperTrend" status across four different timeframes at once. This allows for a holistic perspective on price action, helping users identify whether the broader trend is in alignment with the shorter-term movements.
Key Functionalities
1. Multi-Timeframe Synchronization The core logic of this script involves pulling data from multiple timeframes simultaneously. It identifies four distinct periods (which can be customized, but are often set to intervals like 30 minutes, 1 hour, 4 hours, and Daily). It calculates the SuperTrend value for each of these periods and projects them onto your current chart.
2. Automated Intelligence The script features an "AutoDisplay" logic. When enabled, it detects which timeframe you are currently viewing and automatically adjusts the visible indicators to show the most relevant surrounding timeframes. For example, if you are looking at a 15-minute chart, it will intelligently select the most useful neighboring periods to display to ensure the information remains contextually relevant.
3. Visual Indicators The script generates visual lines on your chart representing the trend for each of the four periods. By seeing these multiple lines together, a user can quickly spot "trend confluence"—where multiple timeframes agree on a direction—or "divergence," where short-term price action might be moving against a stronger long-term trend.
4. Real-Time Alerts and Notifications Beyond just visual lines, the script includes an active monitoring system:
- Directional Alerts: It monitors for "flips" in the trend. When the SuperTrend changes direction (from upward to downward or vice versa) on any of the monitored timeframes, the script triggers a pop-up alert.
- Audio Cues: The script includes a sound alarm system. It can be configured to play different sounds depending on which timeframe triggered the change, providing an immediate audible signal to the trader without needing to watch the screen constantly.
- Smart Filtering: To prevent "alert fatigue," the script is programmed to only trigger these notifications once per new bar, ensuring you aren't overwhelmed by multiple pings for a single trend change.
Summary of User Experience
For a non-technical user, this tool acts as a "trend dashboard." It simplifies complex market analysis by gathering data from various time horizons and consolidating it into one view. It tells you not just what is happening right now on your current screen, but also what the "big picture" looks like across the broader market landscape, notifying you instantly whenever those major trends shift.
//+------------------------------------------------------------------+
//| #MTF Supertrend line.mq4 |
//| Copyright © 2006, Eli hayun |
//| http://www.elihayun.com |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, Eli hayun"
#property link "http://www.elihayun.com"
#property indicator_chart_window
#property indicator_buffers 8
#property indicator_color1 MistyRose
#property indicator_color2 Aqua
#property indicator_color3 MistyRose
#property indicator_color4 Aqua
#property indicator_color5 MistyRose
#property indicator_color6 Aqua
#property indicator_color7 MistyRose
#property indicator_color8 Aqua
//---- buffers
double buf_up1D[];
double buf_down1D[];
double buf_up4H[];
double buf_down4H[];
double buf_up1H[];
double buf_down1H[];
double buf_up30M[];
double buf_down30M[];
extern int Period_1 = PERIOD_M15;
extern int Period_2 = PERIOD_M30;
extern int Period_3 = PERIOD_H1;
extern int Period_4 = PERIOD_H4;
extern bool AutoDisplay = true;
extern bool display_Period_1 = true;
extern bool display_Period_2 = true;
extern bool display_Period_3 = true;
extern bool display_Period_4 = true;
extern bool Play_Sound = true;
string lastplayup="UP";
string lastplaydn="DN";
string playup="UP";
string playdn="DN";
bool TurnedUp = false;
bool TurnedDown = false;
double alertTag;
double control=2147483647;
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init()
{
//---- indicators
if (AutoDisplay)
{
display_Period_1 = false; display_Period_2 = false; display_Period_3 = false; display_Period_4 = false;
switch (Period())
{
case PERIOD_M1 : SetValues(PERIOD_M1, PERIOD_M5, PERIOD_M15,PERIOD_M30, true, true, false, false); break;
case PERIOD_M5 : SetValues(PERIOD_M5, PERIOD_M15,PERIOD_M30,PERIOD_H1, true, true, false, false); break;
case PERIOD_M15 : SetValues(PERIOD_M5, PERIOD_M15,PERIOD_M30,PERIOD_H1, true, true, true, false); break;
case PERIOD_M30 : SetValues(PERIOD_M5, PERIOD_M15,PERIOD_M30,PERIOD_H1, false, true, true, false); break;
case PERIOD_H1 : SetValues(PERIOD_M15, PERIOD_M30,PERIOD_H1, PERIOD_H4, false, false, true, true); break;
case PERIOD_H4 : SetValues(PERIOD_M30, PERIOD_H1, PERIOD_H4, PERIOD_D1, false, true, true, true); break;
case PERIOD_D1 : SetValues(PERIOD_H1, PERIOD_H4, PERIOD_D1, PERIOD_W1, true, true, true, false); break;
case PERIOD_W1 : SetValues(PERIOD_H4, PERIOD_D1, PERIOD_W1, PERIOD_MN1, false, true, true, false); break;
case PERIOD_MN1 : SetValues(PERIOD_H4, PERIOD_D1, PERIOD_W1, PERIOD_MN1, false, false, true, true); break;
}
}
int draw = DRAW_LINE; if (!display_Period_4) draw = DRAW_NONE;
SetIndexStyle(0,draw, STYLE_SOLID, 4);
SetIndexBuffer(0,buf_up1D);
SetIndexStyle(1,draw, STYLE_SOLID, 4);
SetIndexBuffer(1,buf_down1D);
SetIndexLabel(0, tf2txt(Period_4)); SetIndexLabel(1, tf2txt(Period_4));
draw = DRAW_LINE; if (!display_Period_3) draw = DRAW_NONE;
SetIndexStyle(2,draw, STYLE_SOLID, 4);
SetIndexBuffer(2,buf_up4H);
SetIndexStyle(3,draw, STYLE_SOLID, 4);
SetIndexBuffer(3,buf_down4H);
SetIndexLabel(2, tf2txt(Period_3)); SetIndexLabel(3, tf2txt(Period_3));
draw = DRAW_LINE; if (!display_Period_2) draw = DRAW_NONE;
SetIndexStyle(4,draw, STYLE_SOLID, 4);
SetIndexBuffer(4,buf_up1H);
SetIndexStyle(5,draw, STYLE_SOLID, 4);
SetIndexBuffer(5,buf_down1H);
SetIndexLabel(4, tf2txt(Period_2)); SetIndexLabel(5, tf2txt(Period_2));
draw = DRAW_LINE; if (!display_Period_1) draw = DRAW_NONE;
SetIndexStyle(6,draw, STYLE_SOLID, 4);
SetIndexBuffer(6,buf_up30M);
SetIndexStyle(7,draw, STYLE_SOLID, 4);
SetIndexBuffer(7,buf_down30M);
SetIndexLabel(6, tf2txt(Period_1)); SetIndexLabel(7, tf2txt(Period_1));
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function |
//+------------------------------------------------------------------+
int deinit()
{
//----
//----
return(0);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
int start()
{
int counted_bars=IndicatorCounted();
int i=0, y1d=0, y4h=0, y1h=0, y30m=0;
int limit=Bars-counted_bars;
datetime TimeArray_1D[] ,TimeArray_4H[], TimeArray_1H[], TimeArray_30M[];
//----
ArrayCopySeries(TimeArray_1D,MODE_TIME,Symbol(),Period_4);
ArrayCopySeries(TimeArray_4H,MODE_TIME,Symbol(),Period_3);
ArrayCopySeries(TimeArray_1H,MODE_TIME,Symbol(),Period_2);
ArrayCopySeries(TimeArray_30M,MODE_TIME,Symbol(),Period_1);
for(i=0, y1d=0, y4h=0, y1h=0, y30m=0;i<limit;i++)
{
if (Time[i]<TimeArray_1D[y1d]) y1d++;
if (Time[i]<TimeArray_4H[y4h]) y4h++;
if (Time[i]<TimeArray_1H[y1h]) y1h++;
if (Time[i]<TimeArray_30M[y30m]) y30m++;
buf_up1D[i] = iCustom(NULL, Period_4, "SuperTrend", false, 1, y1d);
buf_down1D[i] = iCustom(NULL, Period_4, "SuperTrend", false, 0, y1d);
if (i < 2)
{
SoundAlarm(buf_up1D[i] , iCustom(NULL, Period_4, "SuperTrend", false, 1, y1d+1), 4, "Down");
SoundAlarm(buf_down1D[i] , iCustom(NULL, Period_4, "SuperTrend", false, 0, y1d+1), 4, "Up");
// if (buf_up1D[i]< buf_down1D[i] && buf_down1D[i] == control && i==0 && &alertTag!=Time[0])
if (Play_Sound==true && buf_up1D[i] > buf_down1D[i] && buf_up1D[i] == control && i==0 && Time[i]<TimeArray_1D[y1d]&& alertTag!=Time[0])
{
Alert("SuperTrend changed direction going Up on ", Symbol()," at ",Period()," bu= ",buf_up1D[i]," bd= ",buf_down1D[i]," i= ",i," tim= ",Time[i]);
lastplayup=playup;
alertTag = Time[0];
}
if (Play_Sound==true && buf_up1D[i] < buf_down1D[i] && buf_down1D[i] == control && i==0 && Time[i]<TimeArray_1D[y1d] && alertTag!=Time[0])
// if (buf_up1D[i] > buf_down1D[i] && buf_up1D[i] == control && i==0 && playdn != lastplaydn && alertTag!=Time[0])
{
Alert("SuperTrend changed direction going Down on ", Symbol()," at ",Period()," bu= ",buf_up1D[i]," bd= ",buf_down1D[i]," i= ",i," tim= ",Time[i]);
lastplaydn=playdn;
alertTag = Time[0];
}
}
buf_up4H[i] = iCustom(NULL, Period_3, "SuperTrend", false, 1, y4h);
buf_down4H[i] = iCustom(NULL, Period_3, "SuperTrend", false, 0, y4h);
if (i < 2)
{
SoundAlarm(buf_up4H[i] , iCustom(NULL, Period_3, "SuperTrend", false, 1, y4h+1), 3, "Down");
SoundAlarm(buf_down4H[i] , iCustom(NULL, Period_3, "SuperTrend", false, 0, y4h+1), 3, "Up");
}
buf_up1H[i] = iCustom(NULL, Period_2, "SuperTrend", false, 1, y1h);
buf_down1H[i] = iCustom(NULL, Period_2, "SuperTrend", false, 0, y1h);
if (i < 2)
{
SoundAlarm(buf_up1H[i] , iCustom(NULL, Period_2, "SuperTrend", false, 1, y1h+1), 2, "Down");
SoundAlarm(buf_down1H[i] , iCustom(NULL, Period_2, "SuperTrend", false, 0, y1h+1), 2, "Up");
}
buf_up30M[i] = iCustom(NULL, Period_1, "SuperTrend", false, 1, y30m);
buf_down30M[i] = iCustom(NULL, Period_1, "SuperTrend", false, 0, y30m);
if (i < 2)
{
SoundAlarm(buf_up30M[i] , iCustom(NULL, Period_1, "SuperTrend", false, 1, y30m+1), 1, "Down");
SoundAlarm(buf_down30M[i] , iCustom(NULL, Period_1, "SuperTrend", false, 0, y30m+1), 1, "Up");
}
}
//----
return(0);
}
//+------------------------------------------------------------------+
void SoundAlarm(double v1, double v2, int prd, string sDir)
{
bool bDisplay = true;
string sPrd = "";
static bool bAlreadyAlarm = true;
if (NewBar())
bAlreadyAlarm = false;
switch ( prd )
{
case 4 : bDisplay = display_Period_4; sPrd = tf2txt(Period_3); break;
case 3 : bDisplay = display_Period_3; sPrd = tf2txt(Period_3); break;
case 2 : bDisplay = display_Period_2; sPrd = tf2txt(Period_2); break;
case 1 : bDisplay = display_Period_1; sPrd = tf2txt(Period_1); break;
}
if ((!Play_Sound) || (!bDisplay) || bAlreadyAlarm)
return;
// if (v1 != EMPTY_VALUE && v2 != EMPTY_VALUE)
// {
// Alert("SuperTrend changed direction going " ,sDir, " on ", Symbol()," at ",Period()," v1= ",v1," v2= ",v2," inum= ",inumber," p= ",prd);
// bAlreadyAlarm = true;
// }
}
bool NewBar()
{
static datetime dt = 0;
if (Time[0] != dt)
{
dt = Time[0];
return(true);
}
return(false);
}
string tf2txt(int tf)
{
if (tf == PERIOD_M1) return("M1");
if (tf == PERIOD_M5) return("M5");
if (tf == PERIOD_M15) return("M15");
if (tf == PERIOD_M30) return("M30");
if (tf == PERIOD_H1) return("H1");
if (tf == PERIOD_H4) return("H4");
if (tf == PERIOD_D1) return("D1");
if (tf == PERIOD_W1) return("W1");
if (tf == PERIOD_MN1) return("MN1");
return("??");
}
void SetValues(int p1, int p2, int p3, int p4, bool d1, bool d2, bool d3, bool d4)
{
display_Period_1 = d1; display_Period_2 = d2; display_Period_3 = d3; display_Period_4 = d4;
Period_1 = p1; Period_2 = p2; Period_3 = p3; Period_4 = p4;
}
Comments