Help to find Distances to a certain indicator value

 

.

Hi,

Im making an indicator that needs this distances:

 

The indicator in the picture is the difference between two Moving Averages (MA_4 and MA_150).

And the idea was to retrieve the distances to the last 5 "zeros" in the indicator..  :(

 I've wrote this piece of code but it gives me DistA = DistB = DistC = DistD = DistE = 0 !! 

 Can someone help me here please!

 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 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;

  

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Calculo das distancias A,B,C,D,E 
//
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

for(int a=1; a<Barras; a++)
  { 
   DIF_T3[a]=(iMA(NULL,0, Periodo_Rapida, 0, Simples0_Exponencial1, 0, a)) - (iMA(NULL,0, Periodo_Lenta, 0, Simples0_Exponencial1, 0, a));
   int DistA=0;
   if( (DIF_T3[a]>0 && DIF_T3[a+1]<0) || (DIF_T3[a]<0 && DIF_T3[a+1]>0) )      
      {
      DistA=a;
      break;   
      }
   }


for(int b=(DistA+1); b<Barras; b++)
  { 
   DIF_T3[b]=(iMA(NULL,0, Periodo_Rapida, 0, Simples0_Exponencial1, 0, b)) - (iMA(NULL,0, Periodo_Lenta, 0, Simples0_Exponencial1, 0, b));
   int DistB=0;   
   if( (DIF_T3[b]>0 && DIF_T3[b+1]<0) || (DIF_T3[b]<0 && DIF_T3[b+1]>0) )      
      {
      DistB=b;
      break;
      }
   }


for(int c=(DistB+1); c<Barras; c++)
  { 
   DIF_T3[c]=(iMA(NULL,0, Periodo_Rapida, 0, Simples0_Exponencial1, 0, c)) - (iMA(NULL,0, Periodo_Lenta, 0, Simples0_Exponencial1, 0, c));
   int DistC=0;   
   if( (DIF_T3[c]>0 && DIF_T3[c+1]<0) || (DIF_T3[c]<0 && DIF_T3[c+1]>0) )      
      {
      DistC=c;
      break;
      }
   }


for(int d=(DistC+1); d<Barras; d++)
  { 
   DIF_T3[d]=(iMA(NULL,0, Periodo_Rapida, 0, Simples0_Exponencial1, 0, d)) - (iMA(NULL,0, Periodo_Lenta, 0, Simples0_Exponencial1, 0, d));
   int DistD=0;   
   if( (DIF_T3[d]>0 && DIF_T3[d+1]<0) || (DIF_T3[d]<0 && DIF_T3[d+1]>0) )      
      {
      DistD=d;
      break;
      }
   }


for(int e=(DistD+1); e<Barras; e++)
  { 
   DIF_T3[e]=(iMA(NULL,0, Periodo_Rapida, 0, Simples0_Exponencial1, 0, e)) - (iMA(NULL,0, Periodo_Lenta, 0, Simples0_Exponencial1, 0, e));
   int DistE=0;   
   if( (DIF_T3[e]>0 && DIF_T3[e+1]<0) || (DIF_T3[e]<0 && DIF_T3[e+1]>0) )      
      {
      DistE=e;
      break;
      }
   }
    
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
 

.

Anyone?

 Pleeeassseee... I'm stuck in this...  :(

 

.

 Still doesnt know how I get this.. :(

 Anyone please... ?

 . 

 

Use iCustom

code below is not tested nor compiled. 

int number = 5, count = 0, pos;
double data_1, data_2;

for (pos = 0 ; pos < Bars ; pos ++)
   {
   data_1 = iCustom (NULL, 0, "..." , ... , buffer, pos); 
   data_2 = iCustom (NULL, 0, "..." , ... , buffer, pos + 1);

   if (data_1 > 0 && data_2 <= 0)
      {
      count ++;
      // draw object Vertical line on Time [pos + 1]
      }
      else
      {
      if (data_1 < 0 && data_2 >= 0)
        {
        count ++;
        // draw object Vertical line on Time [pos + 1]
        }
      }
   
   if (count == number) break;

   }

 

.

Thank you very much phi.nuts! :)

 

But.. I'm afraid that your code doesn't help me because I need the values of the last five crosses to use after.. 

In my code I have 5 FOR cylcles in order to get the 5 values (Distances A, B, C, D and E).

 
strutch:

.

Thank you very much phi.nuts! :)

 

But.. I'm afraid that your code doesn't help me because I need the values of the last five crosses to use after.. 

In my code I have 5 FOR cylcles in order to get the 5 values (Distances A, B, C, D and E).

Just use one while loop until you have all 5 values . . .
 

.

Sory RaptorUK but I don't know how can a While loop help me in here!

After the cycle I need to have those 5 distances (see the big blue arrows in 1st post picture) like this:

DIST_A = xx

DIST_B = xx

DIST_C = xx

DIST_D = xx  

DIST_E = xx 

The only way I see to get it is with those for loops:

1st FOR (starting in actual candle) 

2nd FOR (starting in actual minus DIST_A)

3th FOR (starting in actual minus DIST_B)

and so on...

  

But this is my newbie programmer brain working... :(

 

 
strutch:

Sory RaptorUK but I don't know how can a While loop help me in here!

After the cycle I need to have those 5 distances (see the big blue arrows in 1st post picture) like this:

DIST_A = xx

DIST_B = xx

DIST_C = xx

DIST_D = xx  

DIST_E = xx 

The only way I see to get it is with those for loops:

1st FOR (starting in actual candle) 

2nd FOR (starting in actual minus DIST_A)

3th FOR (starting in actual minus DIST_B)

and so on...

  

But this is my newbie programmer brain working... :(

Just thinking aloud . . . .

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

while(ArrayIndex <= 5)
   {
   
   // code to find cross at bar number index

   // if cross found
   CrossAt[ArrayIndex] = index;
   ArrayIndex++;

   index++;
   }
   

 

 

.

I think I understand your idea.. less code lines indeed..  : ) 

But I Still can't get those distance values...  : (

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// 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, CrossAt[5], ArrayIndex = 0;


while(ArrayIndex <= 5)
   {
   
   // code to find cross at bar number index
   
   for(index=0; index<Barras; 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++;
         }
      }
   }

// after that... 

int DIST_A = CrossAt[1];
int DIST_B = CrossAt[2];
int DIST_C = CrossAt[3];
int DIST_D = CrossAt[4];
int DIST_E = CrossAt[5];


    
 
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////  
 
strutch:

.

I think I understand your idea.. less code lines indeed..  : ) 

But I Still can't get those distance values...  : (

I don't do much with Indicators so I might be very rusty . . .  but,  to identify a cross don't you need to check the situation at 3 positions,  the index bar,  the bar before and the bar after ? to look for the change of state ?

There are several problems in your code . . .  index++;  is not dependant of there being a cross . . .  take it out of that if statement,    what is the for loop for ?  and why are you using index in the for loop,  you need to use a different variable . . .

 
RaptorUK:

 to identify a cross don't you need to check the situation at 3 positions,  the index bar,  the bar before and the bar after ? to look for the change of state ?

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! 

 

RaptorUK:
 

index++; is not dependant of there being a cross . . . take it out of that if statement

Absolutely right!

 

RaptorUK:

what is the for loop for ?

I can only say that I might be "FOR loop adicted"!!!  :P

 

RaptorUK:

and why are you using index in the for loop, you need to use a different variable . . .

I'm not used to the WHILE loop. Sory about the confusion...