Page 1 of 1

How code indicator to count number of crossings of Moving Average

Posted: Sat Oct 26, 2019 4:24 am
by tradevn001
Hi everyone, I am in the process of learning how to coding. Could you help me with the code problem ount number of crossings of Moving Average.
Thank you very much!

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

Posted: Sat Oct 26, 2019 6:20 am
by pacois
tradevn001 wrote: Sat Oct 26, 2019 4:24 am Hi everyone, I am in the process of learning how to coding. Could you help me with the code problem ount number of crossings of Moving Average.
Thank you very much!
Hi, explain better, use mt4 to illustrate what you think and want.

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

Posted: Sat Oct 26, 2019 12:48 pm
by tradevn001
pacois wrote: Sat Oct 26, 2019 6:20 am
Hi, explain better, use mt4 to illustrate what you think and want.
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!

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

Posted: Sat Oct 26, 2019 10:58 pm
by pacois
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!!

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

Posted: Sun Oct 27, 2019 5:54 pm
by tradevn001
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);
}

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

Posted: Mon Oct 28, 2019 11:22 am
by pacois
[quote=tradevn001 post_id=1295397438 time=1572159293 user_id=4935909]


Try!!

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

Posted: Mon Oct 28, 2019 4:29 pm
by tradevn001
[quote=pacois post_id=1295397453 time=1572222159 user_id=4868669]



So sorry for bothering you so much. But it seems that the code is incorrect, the return value is not correct

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

Posted: Mon Oct 28, 2019 8:00 pm
by pacois
tradevn001 wrote: Mon Oct 28, 2019 4:29 pm
Hi, on 2000 Bar of 15 minutes only 3 crossings?

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

Posted: Mon Oct 28, 2019 8:18 pm
by tradevn001
pacois wrote: Mon Oct 28, 2019 8:00 pm
Yes, Only 3 crossing EMA 10 x EMA 50 when EMA 50 x EMA 200.
When the EMA 50 with the 200 EMA cross again, it will count back from the beginning