SlopeDirectionLine

Download script
Author: MT4 release WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104
SlopeDirectionLine

This script is designed as a visual trading tool to help identify the current direction of market trends by analyzing price movements over time. It simplifies complex mathematical calculations into two distinct visual lines on your chart, allowing you to see whether the market is currently leaning toward an upward or downward trajectory.

How the Logic Works

The tool operates through a three-step processing sequence to filter out "market noise" and highlight clear trends:

1. Data Smoothing and Vector Calculation Instead of looking at raw price jumps—which can be erratic—the script first calculates a series of weighted moving averages. It compares a shorter-term average against a longer-term average to create a "vector." Essentially, it measures the momentum of the price; if the short-term movement is pulling away from the long-term average, it indicates significant energy in that direction.

2. Trend Refinement The script then takes those raw vectors and applies a secondary layer of smoothing (using a square root calculation based on your chosen period). This step is crucial because it prevents the indicator from reacting too quickly to minor price fluctuations. It ensures that the lines you see on the chart represent sustained trends rather than temporary spikes.

3. Trend Identification (The Visual Output) Finally, the script evaluates whether the refined data is moving up or down compared to the previous point in time:

  • Uptrend: When the logic detects a consistent upward move, it plots a Light Blue line on your chart.
  • Downtrend: When the logic detects a consistent downward move, it plots a Tomato (Red) line.

Key Features for the User

  • Visual Clarity: By separating the trends into two different colors and lines, the script removes the confusion of "choppy" markets. You can easily see when one trend ends and another begins.
  • Transition Detection: The logic is designed to "bridge" the gap at transition points. When the market shifts from an uptrend to a downtrend, the script ensures that the final point of the old trend and the starting point of the new trend are connected, providing a continuous visual history of the market's path.
  • Customizable Sensitivity: Through the settings (Period and Method), you can decide how "sensitive" the script is. A higher period will make the lines smoother and slower to react (better for long-term trends), while a lower period will make it more responsive to immediate price changes.
Indicators Used
Moving average indicatorMoving average indicator
Miscellaneous
Implements a curve of type %1
18 Views
0 Downloads
0 Favorites
SlopeDirectionLine
//+------------------------------------------------------------------+ 
//| HMA.mq4 
//| Copyright © 2006 WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104 
//| wizardserg@mail.ru 
//| Revised by IgorAD,igorad2003@yahoo.co.uk |   
//| Personalized by iGoR AKA FXiGoR for the Trend Slope Trading method (T_S_T) 
//| Link: 
//| contact: thefuturemaster@hotmail.com                                                                         
//+------------------------------------------------------------------+
#property copyright "MT4 release WizardSerg <wizardserg@mail.ru>, ?? ??????? ForexMagazine #104" 
#property link      "wizardserg@mail.ru" 

#property indicator_chart_window 
#property indicator_buffers 2 
#property indicator_color1 LightBlue 
#property indicator_color2 Tomato 
//---- input parameters 
extern int       period=55; 
extern int       method=3;                         // MODE_SMA 
extern int       price=0;                          // PRICE_CLOSE 
//---- buffers 
double Uptrend[];
double Dntrend[];
double ExtMapBuffer[]; 


//+------------------------------------------------------------------+ 
//| Custom indicator initialization function                         | 
//+------------------------------------------------------------------+ 
int init() 
{ 
    IndicatorBuffers(3);  
    SetIndexBuffer(0, Uptrend); 
    //ArraySetAsSeries(Uptrend, true); 
    SetIndexBuffer(1, Dntrend); 
    //ArraySetAsSeries(Dntrend, true); 
    SetIndexBuffer(2, ExtMapBuffer); 
    ArraySetAsSeries(ExtMapBuffer, true); 
    
    SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,2);
    SetIndexStyle(1,DRAW_LINE,STYLE_SOLID,2);
    
    IndicatorShortName("Slope Direction Line("+period+")"); 
    return(0); 
} 

//+------------------------------------------------------------------+ 
//| Custor indicator deinitialization function                       | 
//+------------------------------------------------------------------+ 
int deinit() 
{ 
    // ???? ????? ?????? ?????? 
    return(0); 
} 

//+------------------------------------------------------------------+ 
//| ?????????? ???????                                               | 
//+------------------------------------------------------------------+ 
double WMA(int x, int p) 
{ 
    return(iMA(NULL, 0, p, 0, method, price, x));    
} 

//+------------------------------------------------------------------+ 
//| Custom indicator iteration function                              | 
//+------------------------------------------------------------------+ 
int start() 
{ 
    int counted_bars = IndicatorCounted(); 
    
    if(counted_bars < 0) 
        return(-1); 
                  
    int x = 0; 
    int p = MathSqrt(period);              
    int e = Bars - counted_bars + period + 1; 
    
    double vect[], trend[]; 
    
    if(e > Bars) 
        e = Bars;    

    ArrayResize(vect, e); 
    ArraySetAsSeries(vect, true);
    ArrayResize(trend, e); 
    ArraySetAsSeries(trend, true); 
    
    for(x = 0; x < e; x++) 
    { 
        vect[x] = 2*WMA(x, period/2) - WMA(x, period);        
 //       Print("Bar date/time: ", TimeToStr(Time[x]), " close: ", Close[x], " vect[", x, "] = ", vect[x], " 2*WMA(p/2) = ", 2*WMA(x, period/2), " WMA(p) = ",  WMA(x, period)); 
    } 

    for(x = 0; x < e-period; x++)
     
        ExtMapBuffer[x] = iMAOnArray(vect, 0, p, 0, method, x);        
    
    for(x = e-period; x >= 0; x--)
    {     
        trend[x] = trend[x+1];
        if (ExtMapBuffer[x]> ExtMapBuffer[x+1]) trend[x] =1;
        if (ExtMapBuffer[x]< ExtMapBuffer[x+1]) trend[x] =-1;
    
    if (trend[x]>0)
    { Uptrend[x] = ExtMapBuffer[x]; 
      if (trend[x+1]<0) Uptrend[x+1]=ExtMapBuffer[x+1];
      Dntrend[x] = EMPTY_VALUE;
    }
    else              
    if (trend[x]<0)
    { 
      Dntrend[x] = ExtMapBuffer[x]; 
      if (trend[x+1]>0) Dntrend[x+1]=ExtMapBuffer[x+1];
      Uptrend[x] = EMPTY_VALUE;
    }              
    
    //Print( " trend=",trend[x]);
    }
    
    return(0); 
} 
//+------------------------------------------------------------------+ 

Comments