help to use "Indicators: Linear Regression Channel " as a sub program function for mt4 in mql4

 

hi, i want to used "Indicators: Linear Regression Channel " as a sub program function for mt4 in mql4, can you please help.

i used code as below:

.
.
//----------------------
extern bool UseClose = true;
datetime ch_time;
double ch_price;
//----------------------
.
void OnTick(void)
  {
.
LinearRegressionChannel(UseClose,50,ch_time,ch_price); Print(" can not draw LRC: ", GetLastError());
.
  }
// END OF MAIN
//==============================================
void LinearRegressionChannel(bool Use_Close,int barsToCount,datetime q, double w  )
   {
    Use_Close = true;
    barsToCount=50;
    double LR_line[];
    double Sup_line[];
    double Res_line[];
    // variables
    double a,b,c,
           sumy=0.0,
           sumx=0.0,
           sumxy=0.0,
           sumx2=0.0,
           h=0.0,l=0.0;   
    int x;
    // calculate linear regression
    for(int i=0; i<barsToCount; i++)
       {
        sumy+=Close[i];
        sumxy+=Close[i]*i;
        sumx+=i;
        sumx2+=i*i;
       }
    c=sumx2*barsToCount-sumx*sumx; 
    if(c==0.0)
      {
       Alert("Error in linear regression!");
       return;
      }
    // Line equation    
    b=(sumxy*barsToCount-sumx*sumy)/c;
    a=(sumy-sumx*b)/barsToCount;
    // Linear regression line in buffer
    for(x=0;x<barsToCount;x++)
        LR_line[x]=a+b*x;
    // Use PRICE_CLOSE for support-resistance
    if (Use_Close)
     for(x=0;x<barsToCount;x++)
     {
      if(Close[x]-LR_line[x] > h) h = Close[x]-LR_line[x];
      if(LR_line[x] - Close[x]> l) l = LR_line[x] - Close[x];
     }   
    // Use HIGH - LOW
    else
     for(x=0;x<barsToCount;x++)
     {
      if(High[x]-LR_line[x] > h) h = High[x]-LR_line[x];
      if(LR_line[x] - Low[x]> l) l = LR_line[x] - Low[x];
     }
    
    // Drawing support - resistance lines   
   if (h>l)
   {
     for(x=0;x<barsToCount;x++)
     {
       Sup_line[x]=a-h+b*x;
       Res_line[x]=a+h+b*x;
     } 
   }
   else
   {
     for(x=0;x<barsToCount;x++)
     {
       Sup_line[x]=a-l+b*x;
       Res_line[x]=a+l+b*x;
     }
   }
   /*
   LR_line[x]  = 0.0;
   Sup_line[x] = 0.0;
   Res_line[x] = 0.0;
   */
   
   q=Time[49];
   w=LR_line[49];
   
   if(!ObjectCreate(0,"LRC",OBJ_TREND,0,Time[49],LR_line[49],Time[1],LR_line[1]))
        { Print(" can not draw LRC: ", GetLastError());};
   return;
   }
//==============================================
.
.

?


 
Mehrdad Shiri:

hi, i want to used "Indicators: Linear Regression Channel " as a sub program function for mt4 in mql4, can you please help.

i used code as below:

?

Arrays have a null size. Needs set size as

double LR_line[50] = {0};
double Sup_line[50] = {0};
double Res_line[50] = {0};
 
Alexey Volchanskiy:

Arrays have a null size. Needs set size as

thank you to reply. but stil does not work.


 
Mehrdad Shiri:

thank you to reply. but stil does not work.


   /* chart can't have two or more objects with same name
      these are two decisions for resolve this problem
      1. Create LRC in OnInit and set coordinats Time[49],LR_line[49],Time[1],LR_line[1]
      by call ObjectSetDouble() here
      2. Delete LRC  before it create
   */   
   ObjectDelete(0, "LRC"); // method 2    
   if(!ObjectCreate(0,"LRC",OBJ_TREND,0,Time[49],LR_line[49],Time[1],LR_line[1]))
        { Print(" can not draw LRC: ", GetLastError());};
   return;
   }

// also delete line in OnDeinit
void OnDeinit(const int reason)
  {
  ObjectDelete(0, "LRC"); // method 2    
   
  }
 
Alexey Volchanskiy:

i know about that , the main problem is the value for " LR_line[] " is always zero.?!

and it calculated by highlighted row. (see code in above please)

please see below picture:

the price always have zero value.

 
Mehrdad Shiri:

i know about that , the main problem is the value for " LR_line[] " is always zero.?!

please see below picture:

the price always have zero value.

See my screenshot with LRC. I don't see any problems/

LRC 

 
Alexey Volchanskiy:

See my screenshot with LRC. I don't see any problems/

 

And full code

 

extern bool UseClose = true;
datetime ch_time;
double ch_price;
//----------------------

void OnTick(void)
  {

LinearRegressionChannel(UseClose,50,ch_time,ch_price); Print(" can not draw LRC: ", GetLastError());

  }
// END OF MAIN
//==============================================
void LinearRegressionChannel(bool Use_Close,int barsToCount,datetime q, double w  )
   {
    Use_Close = true;
    barsToCount=50;
    double LR_line[50] = {0};
    double Sup_line[50] = {0};
    double Res_line[50] = {0};
    // variables
    double a,b,c,
           sumy=0.0,
           sumx=0.0,
           sumxy=0.0,
           sumx2=0.0,
           h=0.0,l=0.0;   
    int x;
    // calculate linear regression
    for(int i=0; i<barsToCount; i++)
       {
        sumy+=Close[i];
        sumxy+=Close[i]*i;
        sumx+=i;
        sumx2+=i*i;
       }
    c=sumx2*barsToCount-sumx*sumx; 
    if(c==0.0)
      {
       Alert("Error in linear regression!");
       return;
      }
    // Line equation    
    b=(sumxy*barsToCount-sumx*sumy)/c;
    a=(sumy-sumx*b)/barsToCount;
    // Linear regression line in buffer
    for(x=0;x<barsToCount;x++)
        LR_line[x]=a+b*x;
    // Use PRICE_CLOSE for support-resistance
    if (Use_Close)
     for(x=0;x<barsToCount;x++)
     {
      if(Close[x]-LR_line[x] > h) h = Close[x]-LR_line[x];
      if(LR_line[x] - Close[x]> l) l = LR_line[x] - Close[x];
     }   
    // Use HIGH - LOW
    else
     for(x=0;x<barsToCount;x++)
     {
      if(High[x]-LR_line[x] > h) h = High[x]-LR_line[x];
      if(LR_line[x] - Low[x]> l) l = LR_line[x] - Low[x];
     }
    
    // Drawing support - resistance lines   
   if (h>l)
   {
     for(x=0;x<barsToCount;x++)
     {
       Sup_line[x]=a-h+b*x;
       Res_line[x]=a+h+b*x;
     } 
   }
   else
   {
     for(x=0;x<barsToCount;x++)
     {
       Sup_line[x]=a-l+b*x;
       Res_line[x]=a+l+b*x;
     }
   }
   /*
   LR_line[x]  = 0.0;
   Sup_line[x] = 0.0;
   Res_line[x] = 0.0;
   */
   
   q=Time[49];
   w=LR_line[49];

   /* chart can't have two or more objects with same name
      these are two decisions for resolve this problem
      1. Create LRC in OnInit and set coordinats Time[49],LR_line[49],Time[1],LR_line[1]
      by call ObjectSetDouble() here
      2. Delete LRC  before it create
   */   
   ObjectDelete(0, "LRC"); // method 2    
   if(!ObjectCreate(0,"LRC",OBJ_TREND,0,Time[49],LR_line[49],Time[1],LR_line[1]))
        { Print(" can not draw LRC: ", GetLastError());};
   return;
   }

// also delete line in OnDeinit
void OnDeinit(const int reason)
  {
  ObjectDelete(0, "LRC"); // method 2    
   
  }


   
int OnInit()
  {
   return(INIT_SUCCEEDED);
  }
 
Alexey Volchanskiy:

And full code

 

stil same problem.
 

i add all of that line for modification but,i still have zero value;


 
Mehrdad Shiri:
stil same problem.
Hm-m-m-m... Use attached EA pls and write about results here. Do u really have quotes in MT4? What is a build of u terminal? I have last build 950 and 5-digits quotes account.
 
Mehrdad Shiri:

i add all of that line for modification but,i still have zero value;


Where is bars or candles in chart?
Reason: