Author:
mtf_slope

Overview

The Slope Trend Multi-Timeframe indicator is designed to provide traders with a comprehensive view of market momentum by analyzing price trends across four different timeframes simultaneously. Instead of looking at just the current chart, this tool allows you to see "the big picture" by identifying whether the price is sloping upwards or downwards on various scales (e.g., Daily, Hourly, and Minute charts) all in one place.

Core Logic and Functionality

1. Multi-Timeframe Analysis The script monitors four independent timeframes at once. It calculates the "slope" (the direction of movement) for each timeframe based on specific user-defined periods. This allows a trader to instantly see if the short-term trend is in harmony with the long-term trend or if they are moving in opposite directions.

2. Trend Detection Mechanism For each of the four timeframes, the indicator performs a continuous comparison:

  • Upward Trend: If the current calculated value is higher than the previous value, it identifies an upward slope.
  • Downward Trend: If the current value is lower than the previous value, it identifies a downward slope.
  • Stability Handling: In instances where the price remains flat (no change), the logic is designed to maintain the previous trend direction rather than flickering or resetting, ensuring a smoother visual experience.

3. Visual Representation The results are displayed in a separate window below your main trading chart. The indicator uses a series of distinct symbols (arrows) to represent the status of each timeframe:

  • The four timeframes are assigned different "levels" on this window.
  • Each timeframe has its own dedicated space, making it easy to distinguish between the trend of, for example, a 4-hour chart versus a 30-minute chart.
  • The indicator uses color-coding (typically green and red) to quickly signal "Bullish" vs. "Bearish" movements across all levels.

4. Dynamic Labeling To ensure the user never gets confused about which level corresponds to which timeframe, the script dynamically creates text labels on the screen. These labels identify the specific timeframe (such as M15, H1, D1, etc.) next to their respective trend markers. This allows for a "dashboard" style view where you can see the hierarchy of market trends at a glance.

5. Intelligent Data Synchronization The script includes logic to handle data seamlessly across different chart views. If you are viewing a small timeframe (like 1 minute) but want to see the trend of a larger one (like Daily), the script automatically calculates the correct "bar shift." This ensures that the data remains accurate and synchronized, regardless of which chart you currently have open on your screen.

Miscellaneous
Implements a curve of type %1
3 Views
1 Downloads
0 Favorites
mtf_slope
//+------------------------------------------------------------------+
//|                                              Slope Trend_mtf.mq4 |
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright ""
#property link      ""

#property indicator_separate_window
#property indicator_buffers 8
#property indicator_color1 ForestGreen
#property indicator_color2 Red
#property indicator_color3 ForestGreen
#property indicator_color4 Red
#property indicator_color5 ForestGreen
#property indicator_color6 Red
#property indicator_color7 ForestGreen
#property indicator_color8 Red
#property indicator_minimum 0
#property indicator_maximum 5
//---- input parameters
extern int SlopeTF1=1440;
extern int SlopeTF2=240;
extern int SlopeTF3=60;
extern int SlopeTF4=30;
extern int SlopePeriod=80;
extern int SlopeMethod=3;
extern int SlopePrice=0;
extern int nBars=300;
extern int Code=110;
extern int text_shift=10;
extern color text_col=DodgerBlue;
extern string font_name="Arial";
extern int font_size=4;
extern int Complect=0;
//---- buffers
double ExtMapBuffer1[];
double ExtMapBuffer2[];
double ExtMapBuffer3[];
double ExtMapBuffer4[];
double ExtMapBuffer5[];
double ExtMapBuffer6[];
double ExtMapBuffer7[];
double ExtMapBuffer8[];

bool downloadhistory = false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
   SetIndexStyle(0,DRAW_ARROW);
   SetIndexArrow(0,Code);
   SetIndexBuffer(0,ExtMapBuffer1);
   SetIndexEmptyValue(0,0.0);
   SetIndexStyle(1,DRAW_ARROW);
   SetIndexArrow(1,Code);
   SetIndexBuffer(1,ExtMapBuffer2);
   SetIndexEmptyValue(1,0.0);
   SetIndexStyle(2,DRAW_ARROW);
   SetIndexArrow(2,Code);
   SetIndexBuffer(2,ExtMapBuffer3);
   SetIndexEmptyValue(2,0.0);
   SetIndexStyle(3,DRAW_ARROW);
   SetIndexArrow(3,Code);
   SetIndexBuffer(3,ExtMapBuffer4);
   SetIndexEmptyValue(3,0.0);
   SetIndexStyle(4,DRAW_ARROW);
   SetIndexArrow(4,Code);
   SetIndexBuffer(4,ExtMapBuffer5);
   SetIndexEmptyValue(4,0.0);
   SetIndexStyle(5,DRAW_ARROW);
   SetIndexArrow(5,Code);
   SetIndexBuffer(5,ExtMapBuffer6);
   SetIndexEmptyValue(5,0.0);
   SetIndexStyle(6,DRAW_ARROW);
   SetIndexArrow(6,Code);
   SetIndexBuffer(6,ExtMapBuffer7);
   SetIndexEmptyValue(6,0.0);
   SetIndexStyle(7,DRAW_ARROW);
   SetIndexArrow(7,Code);
   SetIndexBuffer(7,ExtMapBuffer8);
   SetIndexEmptyValue(7,0.0);
   SetIndexLabel(0,NULL);
   SetIndexLabel(1,NULL);
   SetIndexLabel(2,NULL);
   SetIndexLabel(3,NULL);
   SetIndexLabel(4,NULL);
   SetIndexLabel(5,NULL);
   SetIndexLabel(6,NULL);
   SetIndexLabel(7,NULL);
   IndicatorShortName("Slope Trend "+(Complect+1));
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   ObjectDelete("TF1 "+Complect);
   ObjectDelete("TF2 "+Complect);
   ObjectDelete("TF3 "+Complect);
   ObjectDelete("TF4 "+Complect);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int counted_bars=IndicatorCounted();
   int limit, b, b1;
   double ma, ma1;
   string period1, period2, period3, period4;
   if(nBars>Bars) nBars=Bars;   
   if(counted_bars == 0 && downloadhistory)
    {
     ArrayInitialize(ExtMapBuffer1, 0.0);
     ArrayInitialize(ExtMapBuffer2, 0.0);
     ArrayInitialize(ExtMapBuffer3, 0.0);
     ArrayInitialize(ExtMapBuffer4, 0.0);
     ArrayInitialize(ExtMapBuffer5, 0.0);
     ArrayInitialize(ExtMapBuffer6, 0.0);
     ArrayInitialize(ExtMapBuffer7, 0.0);
     ArrayInitialize(ExtMapBuffer8, 0.0);
    }
   if(counted_bars == 0) downloadhistory = true;  
//----
   limit=nBars-1;
//----
   for(int i=limit; i>=0; i--)
    {
     if(Period()>SlopeTF1)
      {
       ma=iCustom(NULL,SlopeTF1,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,i);
       ma1=iCustom(NULL,SlopeTF1,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,i+1);
      }
     else
      {  
       b=iBarShift(NULL,SlopeTF1,iTime(NULL,0,i),false);
       b1=iBarShift(NULL,SlopeTF1,iTime(NULL,0,i+1),false);
       ma=iCustom(NULL,SlopeTF1,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,b);
       ma1=iCustom(NULL,SlopeTF1,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,b1);
      } 
     if(ma>ma1)
      {
       ExtMapBuffer1[i]=4.0;
       ExtMapBuffer2[i]=0.0;
      }
     else 
     if(ma<ma1)
      {
       ExtMapBuffer1[i]=0.0;
       ExtMapBuffer2[i]=4.0;
      }
     else
     if(ma==ma1)
      {
       if(ExtMapBuffer1[i+1]!=0.0)
        {
         ExtMapBuffer1[i]=4.0;
         ExtMapBuffer2[i]=0.0;  
        }
       else
       if(ExtMapBuffer2[i+1]!=0.0)
        {
         ExtMapBuffer1[i]=0.0;
         ExtMapBuffer2[i]=4.0;
        }
      } 
//----
     if(Period()>SlopeTF2)
      {
       ma=iCustom(NULL,SlopeTF2,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,i);
       ma1=iCustom(NULL,SlopeTF2,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,i+1);
      }
     else
      {  
       b=iBarShift(NULL,SlopeTF2,iTime(NULL,0,i),false);
       b1=iBarShift(NULL,SlopeTF2,iTime(NULL,0,i+1),false);           
       ma=iCustom(NULL,SlopeTF2,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,b);
       ma1=iCustom(NULL,SlopeTF2,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,b1);
      } 
     if(ma>ma1)
      {
       ExtMapBuffer3[i]=3.0;
       ExtMapBuffer4[i]=0.0;
      }
     else 
     if(ma<ma1)
      {
       ExtMapBuffer3[i]=0.0;
       ExtMapBuffer4[i]=3.0;
      }
     else
     if(ma==ma1)
      {
       if(ExtMapBuffer3[i+1]!=0.0)
        {
         ExtMapBuffer3[i]=3.0;
         ExtMapBuffer4[i]=0.0;  
        }
       else
       if(ExtMapBuffer4[i+1]!=0.0)
        {
         ExtMapBuffer3[i]=0.0;
         ExtMapBuffer4[i]=3.0;
        }
      }  
//----
     if(Period()>SlopeTF3)
      { 
       ma=iCustom(NULL,SlopeTF3,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,i);
       ma1=iCustom(NULL,SlopeTF3,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,i+1);
      }
     else
      {
       b=iBarShift(NULL,SlopeTF3,iTime(NULL,0,i),false);
       b1=iBarShift(NULL,SlopeTF3,iTime(NULL,0,i+1),false);         
       ma=iCustom(NULL,SlopeTF3,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,b);
       ma1=iCustom(NULL,SlopeTF3,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,b1);
      } 
     if(ma>ma1)
      {
       ExtMapBuffer5[i]=2.0;
       ExtMapBuffer6[i]=0.0;
      }
     else 
     if(ma<ma1)
      {
       ExtMapBuffer5[i]=0.0;
       ExtMapBuffer6[i]=2.0;
      }
     else
     if(ma==ma1)
      {
       if(ExtMapBuffer5[i+1]!=0.0)
        {
         ExtMapBuffer5[i]=2.0;
         ExtMapBuffer6[i]=0.0;  
        }
       else
       if(ExtMapBuffer6[i+1]!=0.0)
        {
         ExtMapBuffer5[i]=0.0;
         ExtMapBuffer6[i]=2.0;
        }
      }
//----
     if(Period()>SlopeTF4)
      { 
       ma=iCustom(NULL,SlopeTF4,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,i);
       ma1=iCustom(NULL,SlopeTF4,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,i+1);
      }
     else
      {
       b=iBarShift(NULL,SlopeTF4,iTime(NULL,0,i),false);
       b1=iBarShift(NULL,SlopeTF4,iTime(NULL,0,i+1),false);            
       ma=iCustom(NULL,SlopeTF4,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,b);
       ma1=iCustom(NULL,SlopeTF4,"Slope Line",SlopePeriod,SlopeMethod,SlopePrice,0,b1);
      } 
     if(ma>ma1)
      {
       ExtMapBuffer7[i]=1.0;
       ExtMapBuffer8[i]=0.0;
      }
     else 
     if(ma<ma1)
      {
       ExtMapBuffer7[i]=0.0;
       ExtMapBuffer8[i]=1.0;
      }
     else
     if(ma==ma1)
      {
       if(ExtMapBuffer7[i+1]!=0.0)
        {
         ExtMapBuffer7[i]=1.0;
         ExtMapBuffer8[i]=0.0;  
        }
       else
       if(ExtMapBuffer8[i+1]!=0.0)
        {
         ExtMapBuffer7[i]=0.0;
         ExtMapBuffer8[i]=1.0;
        }
      }
    }             
//----    
   switch (SlopeTF1)
    {
      case 1:
         period1 = "M1";
         break;
      case 5:
         period1 = "M5";
         break;
      case 15:
         period1 = "M15";
         break;
      case 30:
         period1 = "M30";
         break;      
      case 60:
         period1 = "H1";
         break;
      case 240:
         period1 = "H4";
         break;
      case 1440:
         period1 = "D1";
         break;
      case 10080:
         period1 = "W1";
         break;
      case 43200:
         period1 = "MN";
         break;
    }
//----    
   switch (SlopeTF2)
    {
      case 1:
         period2 = "M1";
         break;
      case 5:
         period2 = "M5";
         break;
      case 15:
         period2 = "M15";
         break;
      case 30:
         period2 = "M30";
         break;      
      case 60:
         period2 = "H1";
         break;
      case 240:
         period2 = "H4";
         break;
      case 1440:
         period2 = "D1";
         break;
      case 10080:
         period2 = "W1";
         break;
      case 43200:
         period2 = "MN";
         break;
    }
//----    
   switch (SlopeTF3)
    {
      case 1:
         period3 = "M1";
         break;
      case 5:
         period3 = "M5";
         break;
      case 15:
         period3 = "M15";
         break;
      case 30:
         period3 = "M30";
         break;      
      case 60:
         period3 = "H1";
         break;
      case 240:
         period3 = "H4";
         break;
      case 1440:
         period3 = "D1";
         break;
      case 10080:
         period3 = "W1";
         break;
      case 43200:
         period3 = "MN";
         break;
    }
//----    
   switch (SlopeTF4)
    {
      case 1:
         period4 = "M1";
         break;
      case 5:
         period4 = "M5";
         break;
      case 15:
         period4 = "M15";
         break;
      case 30:
         period4 = "M30";
         break;      
      case 60:
         period4 = "H1";
         break;
      case 240:
         period4 = "H4";
         break;
      case 1440:
         period4 = "D1";
         break;
      case 10080:
         period4 = "W1";
         break;
      case 43200:
         period4 = "MN";
         break;
    }
//----    
   int indicatorWindow = WindowFind("Slope Trend "+(Complect+1));
   ObjectDelete("TF1 "+Complect);
   ObjectCreate("TF1 "+Complect, OBJ_TEXT, indicatorWindow, Time[0]+text_shift*Period()*60, 4.00);
   ObjectSetText("TF1 "+Complect, period1, font_size, font_name, text_col);
   ObjectDelete("TF2 "+Complect);
   ObjectCreate("TF2 "+Complect, OBJ_TEXT, indicatorWindow, Time[0]+text_shift*Period()*60, 3.00);
   ObjectSetText("TF2 "+Complect, period2, font_size, font_name, text_col);
   ObjectDelete("TF3 "+Complect);
   ObjectCreate("TF3 "+Complect, OBJ_TEXT, indicatorWindow, Time[0]+text_shift*Period()*60, 2.00);
   ObjectSetText("TF3 "+Complect, period3, font_size, font_name, text_col);
   ObjectDelete("TF4 "+Complect);
   ObjectCreate("TF4 "+Complect, OBJ_TEXT, indicatorWindow, Time[0]+text_shift*Period()*60, 1.00);
   ObjectSetText("TF4 "+Complect, period4, font_size, font_name, text_col);
//----
   return(0);
  }
//+------------------------------------------------------------------+

Comments