Re: How code indicator to count number of crossings of Moving Average

5
pacois wrote: Sat Oct 26, 2019 10:58 pm

Try!!
Thanks so much for your help. Could you share the source of the indicator? I'm learning programming so I need to refer to the source code to better understand programming algorithms. Thank you very much.

I tried to write a piece of code but it seems they didn't work correctly

Code: Select all

int start() {
   int counted_bars=IndicatorCounted();
   int i,limit;
   int countup, countdown;

   if(counted_bars<0) return(-1);
   if(counted_bars>0) counted_bars--;
         limit = MathMin(Bars-counted_bars,Bars-1);
   countup = 0;
   countdown = 0;   
   for(i=limit; i>=0; i--)
   {
      double ima1   = iMA(NULL,0,MA1,0,Ma_Methods,PRICE_CLOSE,i);
      double ima2   = iMA(NULL,0,MA2,0,Ma_Methods,PRICE_CLOSE,i);
      double ima11   = iMA(NULL,0,MA1,0,Ma_Methods,PRICE_CLOSE,i+1);
      double ima21   = iMA(NULL,0,MA2,0,Ma_Methods,PRICE_CLOSE,i+1);
      double ima3   = iMA(NULL,0,MA3,0,Ma_Methods,PRICE_CLOSE,i);
      double ima4   = iMA(NULL,0,MA4,0,Ma_Methods,PRICE_CLOSE,i);
      
      if (ima3 > ima4)
      {
       if ( ima1 > ima2 && ima11 < ima21) 
         {
            countup ++;
            countdown = 0;
         }
      }
      if (ima3 < ima4)
      {
       if ( ima1 < ima2 && ima11 > ima21) 
         {
            countup = 0;
            countdown ++;
         }
      }


    }

Comment (countup, countdown);   
   return(0);
}