Page 1 of 1

MQL4 trend line - regression

Posted: Sun May 28, 2006 2:05 pm
by davidf
Hi,

I create indicator based on rgression line. It is draw line fo last 500 bars. See in examle below.

Code: Select all

//+------------------------------------------------------------------+
//|                                                       DavidF.mq4 |
//|                      Copyright © 2006, MetaQuotes Software Corp. |
//|                                        http://www.metaquotes.net |
//+------------------------------------------------------------------+
#property copyright "Copyright © 2006, MetaQuotes Software Corp."
#property link      "http://www.metaquotes.net"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_color1 Red
//---- buffers
double ExtMapBuffer1[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  { 
//---- indicators
   SetIndexStyle(0,DRAW_LINE);
   SetIndexBuffer(0,ExtMapBuffer1);
   string short_name = "DaviF Linear regression indicator is running!";
   IndicatorShortName(short_name);
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
  {
   int    counted_bars=IndicatorCounted();
//---- check for possible errors
   if (counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if (counted_bars>0) counted_bars--;
   int i, countBars;
   double dClose, avgClose, avgBars, m1Line, m2Line, mLine, aLine, dResult;
   
   countBars = 500; // here set nuber of loocback preriod for counting bars fo liner regres formula
   i = 0;
   avgBars = countBars/2; // averange of all bars in chart = x
   avgClose = 0;
   
   while(i<=countBars)
    {
     avgClose = avgClose + Close[i];
     i++;
    }
   
   avgClose = avgClose/countBars; // averange of close price of all bars in chart = y
     
   m1Line =0 ; m2Line = 0; //par of mLine > it is parts param of line
   
   i = 0;
   while(i<=countBars)
    {
     m1Line = ((i - avgBars) * (Close[i] - avgClose)) + m1Line;
     m2Line = (MathPow((i - avgBars),2)) + m2Line;
     i++;
    }
   
   // it is param for formula of regress line ...
   // ... m = sum(x - avg(y))* sum(y - avg(y)) / sum((x - avg(x))*(x - avg(x)))
   mLine = m1Line / m2Line; 
   // it is param for formula of regress line > a = avg(y) - m*avg(x)
   aLine = avgClose - (mLine * avgBars);
   
   // it is formula of line y = a*m - x
   for (i = 0; i<countBars; i++)
    {
      dResult = aLine + (mLine * i);
      ExtMapBuffer1[i]= dResult ;
    }
 
  Comment("a: ", DoubleToStr(aLine,4),
          " = avgClose(", DoubleToStr(avgClose,4),
          ") - m(", DoubleToStr(mLine,6),
          ") * avgBars(", DoubleToStr(avgBars,0), ") ", m1Line, " ", m2Line);

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


DavidF