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

4
tradevn001 wrote: Sat Oct 26, 2019 12:48 pm

Thanks for the reply. I want to code an indicator with the following content:
For example, I use 3 MA10, 50 and 200 lines. When MA50 crosses MA200, count the number of cross of MA 10 with MA50.
Thank you very much!
Try!!
These users thanked the author pacois for the post:
tradevn001

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);
}
Attachments




Who is online

Users browsing this forum: No registered users and 22 guests