need some help

[Deleted]  

Hi Fellas

seems as if i have written some bad code... I would like to measure the slope of a linear regression curve in dependence of the relative shift to the current bar, the function actually works but only for i=0, for every other value of i the function returns 2147483647.00 (empty value). What have i done wrong? i would be pleased someone to help me...

//+------------------------------------------------------------------+
//| Start function                          
//+------------------------------------------------------------------+
int start()
  {int i;
   slope[i]=slope(i, SymbolY, SymbolX);    
//---- done
   return(0);
  }
  
//+------------------------------------------------------------------+
// Calculating slope
//+------------------------------------------------------------------+
double slope(int i, string symbolY, string symbolX)
   {double meanrY, meanrX, ssYX, ssXX, b;
    int k, limit=i+MA_Period;
    
    for(k=i; k<limit; k++)
   {
    rY[k]       = iClose(symbolY,0,k)/iOpen(symbolY,0,k)-1;
    rX[k]       = iClose(symbolX,0,k)/iOpen(symbolX,0,k)-1; 
   }
    meanrY       = iMAOnArray(rY,0,MA_Period,0,MODE_SMA,i);
    meanrX       = iMAOnArray(rX,0,MA_Period,0,MODE_SMA,i);
    
    for(k=i; k<limit; k++)
   {  
    sqerryYxX[k]= (rX[k]-meanrX)*(rY[k]-meanrY);
    sqerrxX[k]  = MathPow(rX[k]-meanrX,2);
   } 
    ssYX        = iMAOnArray(sqerryYxX,0,MA_Period,0,MODE_SMA,i)*MA_Period;
    ssXX        = iMAOnArray(sqerrxX,0,MA_Period,0,MODE_SMA,i)*MA_Period;
                            
    b           = ssYX/ssXX;                  
    return(b);
   }
Files:
slopeyx.mq4  3 kb
 
76vier:

Hi Fellas

seems as if i have written some bad code... I would like to measure the slope of a linear regression curve in dependence of the relative shift to the current bar, the function actually works but only for i=0, for every other value of i the function returns 2147483647.00 (empty value). What have i done wrong? i would be pleased someone to help me...

<CODE REMOVED>

Please edit your post . . . 

 

Use SRC 

 
76vier:

Hi Fellas

seems as if i have written some bad code... I would like to measure the slope of a linear regression curve in dependence of the relative shift to the current bar, the function actually works but only for i=0, for every other value of i the function returns 2147483647.00 (empty value). What have i done wrong? i would be pleased someone to help me...

I'm not sure why but your mean values are getting messed up.
 
76vier: the function actually works but only for i=0, for every other value of i the function returns 2147483647.00 (empty value). What have i done wrong?
  1. Because you are never populating any other element but the zero.

    Updating slope[0] only


    int start()
      {int i;
       slope[i]=slope(i, SymbolY, SymbolX);
    In an indicator
    int start(){
      int counted = IndicatorCounted();
      if(counted < MA_Period) counted = MA_Period; // Don't look past end
      for(int i = Bars-1-counted; i>=0; i--)
       slope[i]=slope(i, SymbolY, SymbolX);
    In a EA
    datetime lastComputed;
    int init(){ lastComputed = 0; }
    int start(){
      int i = iBarShift(NULL,0, lastComputed); lastComputed = Time[0];
      if (i > Bars - MA_Period) i = Bars - MA_Period; // Don't look past end
      for(; i>=0; i--)
       slope[i]=slope(i, SymbolY, SymbolX);

  2. It is also bad style to name your variables the same as your functions.
[Deleted]  
ty for your effort. its working now.
[Deleted]  
there seems to be another problem... the indicator displays wrong values if not compiled three times. I opened another topic for this issue, see: https://forum.mql4.com/54368 Again I would be pleased for some help...