Help to find Distances to a certain indicator value - page 2

 
strutch:

My thought is:

 ( Before below zero ) & ( Present above zero ) => There was a zero cross!

 ( Before above zero ) & ( Present below zero ) => There was a zero cross! 

What about the condition where the values match on the present bar (index bar) ?
 

.

Maybe the problem is elsewhere.. Here is the complete code I'm using to get those Distances:


 

//+------------------------------------------------------------------+
//|                                                   Trendlines.mq4 |
//|                                                          StRuTcH |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "StRuTcH"
#property link      ""

#property indicator_chart_window


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
//
// Input parameters
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

extern int       Fast_Period=4;
extern int       Slow_Period=150;



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
//
// Buffers
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

double   DIF_MA[];


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
//
// Custom indicator initialization function
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int init()
  {

   SetIndexBuffer(0,DIF_MA);   
   
   //---- indicator names
   Comment("Trendlines("+Fast_Period+","+Slow_Period+")");
   

   return(0);
  }

  
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Custom indicator iteration function
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


int start()
{

   int Barras;
   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--;

   Barras=Bars-counted_bars;   

  

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Calculate distances A,B,C,D,E 
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


int index=0, CrossAt[5], ArrayIndex = 0;

while(ArrayIndex <= 5)
   {
   
 // code to find cross at bar number index
     
      DIF_MA[index]=(iMA(NULL,0, Fast_Period, 0, 0, 0, index) - iMA(NULL,0, Slow_Period, 0, 0, 0, index));

 // if cross found
   
      if( (DIF_MA[index]>0 && DIF_MA[index+1]<=0) || (DIF_MA[index]<0 && DIF_MA[index+1]>=0) )      
         {
         CrossAt[ArrayIndex] = index;
         ArrayIndex++;
         }
      index++;
   }


Comment("DistA = ", CrossAt[0], "\nDistB = ", CrossAt[1],  "\nDistC = ", CrossAt[2], "\nDistD = ", CrossAt[3], "\nDistE = ", CrossAt[4] );


return(0);
}

//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


 
strutch:

.

Maybe the problem is elsewhere.. Here is the complete code I'm using to get those Distances:

What is the problem ?  what isn't working correctly ?  I have to pop out for 30mins,  I'll try you code when I come back but it would be good to know what the current issue is ?
 
RaptorUK:
What about the condition where the values match on the present bar (index bar) ?


I'm pretty sure you're right about that RaptorUK...

But I simply cannot see the need of a third condition!

 

In my (humble) point of view, a zero cross always implies:

X (n)>0  &&   X (n+1)<0  and vice versa!

.

 

.

 

The issue is that I cannot get these distances! :(

 

 Thank you soo much for your help RaptorUK! :)

 
strutch:


I'm pretty sure you're right about that RaptorUK...

But I simply cannot see the need of a third condition!

 

In my (humble) point of view, a zero cross always implies:

X (n)>0  &&   X (n+1)<0  and vice versa!

Two points,  if you are comparing double values you need to be careful . . .   Can price != price ?   and if for one of the indexes x(n) = 0 it won't be greater or less than 0
 
strutch:

.

 The issue is that I cannot get these distances! :(

Your array has a size of zero elements . . .

double   DIF_MA[];

  . . .  and even if it was correct,  you can't use DIF_MA[index+1]  as you haven't calculated it . . .

 

Try this . . . .

double   DIF_MA[2];   //  2 elements



///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Calculate distances A,B,C,D,E 
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


int index=0, CrossAt[5], ArrayIndex = 0;

while(ArrayIndex <= 5)
   {
   
 // code to find cross at bar number index
     
      DIF_MA[0] = (iMA(NULL,0, Fast_Period, 0, 0, 0, index) - iMA(NULL,0, Slow_Period, 0, 0, 0, index));           // change this line
      DIF_MA[1] = (iMA(NULL,0, Fast_Period, 0, 0, 0, index + 1) - iMA(NULL,0, Slow_Period, 0, 0, 0, index + 1));   // add this line  

 // if cross found
   
      if( (DIF_MA[0]>0 && DIF_MA[1]<=0) || (DIF_MA[0]<0 && DIF_MA[1]>=0) )      // change this line
         {
 

.

It works! :)

And now, it makes so much sense!!

I'm very newbie about the arrays. This was a great lesson to me!

I'm starting to get in the concept of the Arrays.. 

Thank you so much RaptorUK!! :)

 

Here is the correct code, in case someone needs it..

 

//+------------------------------------------------------------------+
//|                                                   Trendlines.mq4 |
//|                                                          StRuTcH |
//|                                                                  |
//+------------------------------------------------------------------+
#property copyright "StRuTcH"
#property link      ""

#property indicator_chart_window


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
//
// Input parameters
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

extern int       Fast_Period=4;
extern int       Slow_Period=150;



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
//
// Buffers
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
//
// Custom indicator initialization function
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

int init()
  {

 
   
   //---- indicator names
   Comment("Trendlines("+Fast_Period+","+Slow_Period+")");
   

   return(0);
  }

  
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Custom indicator iteration function
//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


int start()
{

   int Barras;
   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--;

   Barras=Bars-counted_bars;   

  

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Calculate distances A,B,C,D,E 
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


int index=0, CrossAt[5], ArrayIndex = 0;

double   DIF_MA[2];

while(ArrayIndex <= 5)
   {
   
 // code to find cross at bar number index
     
      DIF_MA[0] = (iMA(NULL,0, Fast_Period, 0, 0, 0, index) - iMA(NULL,0, Slow_Period, 0, 0, 0, index));
      DIF_MA[1] = (iMA(NULL,0, Fast_Period, 0, 0, 0, index + 1) - iMA(NULL,0, Slow_Period, 0, 0, 0, index + 1));

 // if cross found
   
      if( (DIF_MA[0]>0 && DIF_MA[1]<=0) || (DIF_MA[0]<0 && DIF_MA[1]>=0) )
  
         {
         CrossAt[ArrayIndex] = index;
         ArrayIndex++;
         }
      index++;
   }


Comment("DistA = ", CrossAt[0], "\nDistB = ", CrossAt[1],  "\nDistC = ", CrossAt[2], "\nDistD = ", CrossAt[3], "\nDistE = ", CrossAt[4] );


return(0);
}

//
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


 
strutch:

.

It works! :)

And now, it makes so much sence!!

I'm very newbie about the arrays. This was a great lesson to me!

I'm starting to get in the concept of the Arrays.. 

Thank you so much RaptorUK!! :)


:-)  you are very welcome.
Reason: