trend_v2_v1

Download script
Author: Copyright � 2008, MetaQuotes Software Corp.
trend_v2_v1

This script functions as a technical analysis tool designed to help traders visualize price trends by identifying the underlying mathematical relationship between time and price. Rather than simply tracking where the price sits at any given moment, this logic attempts to determine the "velocity" or the specific direction of the current market movement over a defined period.

How the Logic Works

1. Defining the Observation Window The tool operates based on a user-defined timeframe (the "period"). It looks back at a specific number of recent candles to gather data points. Instead of looking at just one price point, it examines how the price has moved across this entire window to understand the broader context of the current trend.

2. Mathematical Modeling (Linear Regression) At its core, the script uses a process similar to "Linear Regression." It treats the historical closing prices as a series of coordinates on a graph. The logic performs complex calculations to find the "Line of Best Fit"—a straight line that passes through these price points in the way that minimizes the distance between the line and the actual market data.

3. Calculating the Slope The primary goal of this calculation is to determine the slope of that best-fit line.

  • If the slope is positive, it indicates an upward trend.
  • If the slope is negative, it indicates a downward trend.
  • The steepness of the slope tells the user how aggressively the price is moving in that direction.

4. Visual Output The final result of these calculations is plotted as a continuous line in a separate window on your trading platform. By observing this line, you can see the smoothed trajectory of the market. It filters out "noise" (small, insignificant price fluctuations) to show you the prevailing direction of the price action based on the mathematical average of the recent past.

Summary for the Trader

This tool is designed for traders who prefer to see the "big picture." By calculating the trend slope over a set period, it provides a smoothed visual representation of market momentum, helping you identify whether the current price movement is structurally trending up, down, or moving sideways.

Miscellaneous
Implements a curve of type %1
9 Views
0 Downloads
0 Favorites
trend_v2_v1
//+------------------------------------------------------------------+
//|                                                    27sen2007.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_separate_window
#property  indicator_buffers 1
#property  indicator_color1  Blue


extern int period =20;

double A[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
//---- indicators
 SetIndexStyle(0,DRAW_LINE,EMPTY,2,Blue);
   SetIndexBuffer(0,A);
   SetIndexDrawBegin(0,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;
  
  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[1];
   }


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

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

Comments