#MTF Supertrend line

Download script
Author: Copyright � 2006, Eli hayun
#MTF Supertrend line

This script is designed to provide a comprehensive overview of market trends by overlaying multiple timeframe analyses onto a single chart. Instead of making you switch back and forth between different time periods (like looking at 15-minute, hourly, and daily charts separately), this tool aggregates that information into one visual display.

Core Functionality

The primary purpose of this script is to track the "SuperTrend"—a popular technical indicator used to identify the direction of market momentum—across four different timeframes simultaneously. It allows you to see at a glance whether the broader market trend aligns with the smaller, shorter-term price movements.

Key Features

Multi-Timeframe Synchronization The script pulls data from four distinct periods (which can be customized by the user). It calculates the trend for each and draws corresponding lines on your current chart. This helps in identifying "confluence," where multiple timeframes agree on a direction, providing a more reliable signal for potential trades.

Smart Automatic Display The tool includes an intelligent "AutoDisplay" logic. Depending on which timeframe you are currently viewing (e.g., if you are looking at a 5-minute chart vs. a Daily chart), the script automatically selects the most relevant surrounding timeframes to display. This ensures that your screen remains clean and only shows the data that makes sense for your current zoom level.

Visual Hierarchy To help distinguish between the different data sets, the script applies different visual styles (such as varying line thicknesses) to each timeframe's trend line. This allows you to easily differentiate between a fast-moving short-term signal and a slow-moving long-term trend.

Real-Time Alerts The script features an integrated notification system. When the SuperTrend changes direction on any of the tracked timeframes, the tool can trigger an audible sound alert. It includes built-in logic to ensure you only hear one alert per new candle, preventing repetitive noises during high volatility.

Contextual Logging Whenever a trend shift occurs, the script logs the event internally. This provides a record of when a direction changed and on which timeframe it happened, allowing for better post-trade analysis of how different time periods reacted to market moves.

Miscellaneous
Implements a curve of type %1It plays sound alerts
12 Views
0 Downloads
0 Favorites
#MTF Supertrend line
//+------------------------------------------------------------------+
//|                                         #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 Red
#property indicator_color2 Blue
#property indicator_color3 Red
#property indicator_color4 Blue
#property indicator_color5 Red
#property indicator_color6 Blue
#property indicator_color7 Red
#property indicator_color8 Blue
//---- 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;


//+------------------------------------------------------------------+
//| 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, 3);
   SetIndexBuffer(2,buf_up4H);
   SetIndexStyle(3,draw, STYLE_SOLID, 3);
   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, 2);
   SetIndexBuffer(4,buf_up1H);
   SetIndexStyle(5,draw, STYLE_SOLID, 2);
   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, 1);
   SetIndexBuffer(6,buf_up30M);
   SetIndexStyle(7,draw, STYLE_SOLID, 1);
   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");
      }
      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))   
   {
      Print("SuperTrend changed direction to " ,sDir, " at ", sPrd); // , " ",v1," ",v2," ",prd);
      PlaySound("alert.wav");
      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