Author: Copyright � 2008, MetaQuotes Software Corp.
trend_v3_5

This script functions as a sophisticated technical analysis tool designed to identify and visualize market trends by calculating the underlying mathematical "path" of price movements. Rather than simply following the current price, the logic seeks to establish a foundational trend line based on historical data over a specific user-defined timeframe (the "period").

Core Methodology

The script utilizes a linear regression approach to analyze the relationship between time and price. It looks back at a set number of previous candles to determine the most accurate "line of best fit." By calculating this mathematical average, the tool filters out the daily "noise" and minor fluctuations of the market to reveal the broader direction of the trend.

Key Logic Components

  • Trend Identification: The script calculates two primary coefficients: the starting point of the trend and its slope (the rate at which it rises or falls). This allows the tool to project where the price "should" be based on its historical trajectory.
  • Dynamic Support and Resistance Buffers: One of the most unique aspects of this logic is how it calculates boundaries. It measures the average distance between the actual high and low prices and the calculated trend line over the specified period.
  • Volatility Adjustment: It uses these average distances to create "envelopes" or buffers. By adding and subtracting these calculated distances from the main trend line, the script produces upper and lower boundaries. These boundaries represent a zone of equilibrium; as long as the price stays within these limits, it is considered to be following the established trend.

Visual Output

The logic translates into several distinct visual elements on your chart:

  1. The Primary Trend Line: A clear indicator of the current trajectory (upward, downward, or flat).
  2. Dynamic Boundaries: These act as automated "ceilings" and "floors." They expand or contract based on market volatility—widening when the market is erratic and narrowing when the market is stable.
  3. Trend Classification: The logic differentiates between various trend types (such as falling trends versus flat markets) to help distinguish between a price drop and a period of stagnation.

In summary, this tool helps traders see past the immediate chaos of every tick. It provides a mathematical "spine" for the market, showing you the underlying direction while automatically adjusting its boundaries to account for how much the market typically swings away from that spine.

Miscellaneous
Implements a curve of type %1
12 Views
0 Downloads
0 Favorites
trend_v3_5
//+------------------------------------------------------------------+
//|                                                     trend_v3.mq4 |
//|                      Copyright © 2008, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2008, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property  indicator_buffers 5
#property  indicator_color1  Blue
#property  indicator_color2  Black
#property  indicator_color3  Black
#property  indicator_color4  Red
#property  indicator_color5  Black

extern int period =20;
double A[],C[],D[],C1[],C2[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators

  SetIndexStyle(0,DRAW_LINE);
  SetIndexBuffer(0,A);
  SetIndexDrawBegin(0,0);

  SetIndexStyle(1,DRAW_LINE);
  SetIndexBuffer(1,D);
  SetIndexDrawBegin(1,0);
  
    SetIndexStyle(2,DRAW_LINE);
  SetIndexBuffer(2,C);
  SetIndexDrawBegin(2,0);
      SetIndexStyle(3,DRAW_LINE);
  SetIndexBuffer(3,C1);
  SetIndexDrawBegin(3,0);
      SetIndexStyle(4,DRAW_LINE);
  SetIndexBuffer(4,C2);
  SetIndexDrawBegin(4,0);

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

int start()
  {

   int    counted_bars=IndicatorCounted();
   int   i, j, k, q, limit;
   double X[2][2], Y[2], B[2], x[2][2000], y[2000], a,sr,sr1,sr2;
  
  limit=Bars-counted_bars;
  for(q=0; q<limit; q++)
   {
     
   ArrayInitialize(X,0);
   ArrayInitialize(Y,0);   
   ArrayInitialize(B,0);   
   ArrayInitialize(x,0);
   ArrayInitialize(y,0);  
//===========================================
for (i=0; i<=period; i++) {
x[0][i]=1;
x[1][i]=i+q;
y[i]=   Close[i+q];
}
//===========================================
  for (k=1; k<=period; k++) {
  for (j=0; j<=1; j++) {
  for (i=0; i<=1; i++) { 
   X[i][j]=x[j][k]*x[i][k]+X[i][j];   
   }
   Y[j]=y[k]*x[j][k]+Y[j];
   }}  
//===========================================  
k=0; 
 j=1; 
  a=X[k][j]/X[k][k];
  for (i=0; i<=3; i++){
  X[i][j]=X[i][j]-X[i][k]*a; 
  }
  Y[j]=Y[j]-Y[k]*a;

//==========================================  
for (k=2; k>=1; k--){
a=0;
for (i=1; i>=k; i--){
a=X[i][k-1]*B[i]+a;
}
B[k-1]=(Y[k-1]-a)/X[k-1][k-1];
}
//=========================================


   A[q]=B[0]+B[1]*q;
  A[q]=B[0]+B[1]*q;
   if(B[1]<0){C1[q]=B[0]+B[1]*q;}
  if(B[1]==0){C2[q]=B[0]+B[1]*q;}
  
  for(j=q; j<=q+period; j++){
sr1=MathAbs(High[j]-(B[0]+B[1]*(j)))+sr1;
sr2=MathAbs(Low[j]-(B[0]+B[1]*(j)))+sr2;
}
sr1=sr1/period;
sr2=sr2/period;
 D[q]=B[0]+2*sr1+B[1]*(q);
C[q]=B[0]-2*sr2+B[1]*(q);


   }


//}
//=========================================

   return(0);
  }
//+------------------------------------------------------------------+

Comments