MQL4 trend line - regression

post your indicators here

Moderator: moderators

davidf
rank: <50 posts
rank: <50 posts
Posts: 24
Joined: Fri May 19, 2006 6:10 pm
Reputation: 0
Gender: Male
Contact:

MQL4 trend line - regression

Postby davidf » Sun May 28, 2006 2:05 pm

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
Attachments
trendregrline.gif
Regress Line indicator
trendregrline.gif (20.82 KiB) Viewed 2713 times
DavidF.zip
(1.06 KiB) Downloaded 362 times

Please add www.kreslik.com to your ad blocker white list.
Thank you for your support.

Return to “Tradestation indicators”