Cross Counter MQL4

 

Hello Friends,

I am new to mql4 coding and learning from past 2 months.

I am trying to code MA cross counter. If MA50 cross above MA1000 then BUY CROSS =1 and same for below.

But the issue is Cross Counter increased by 1 on every tick.

Below is code. Please help me with this.

Thanks in Advance.

double MyPoint;

int start()

{

MyPoint=SetPairPipValue(Symbol());





double ma1 = iMA(_Symbol, 0, 50, 0, MODE_EMA, PRICE_CLOSE, 1);

double ma2 = iMA(_Symbol, 0, 1000, 0, MODE_EMA, PRICE_CLOSE, 1);



int Expert1;

int Expert2;



if(Ask>ma1+5*MyPoint && Ask>ma2+5*MyPoint)

{

Expert1=1;

}



if(Bid<ma1-5*MyPoint && Bid<ma2-5*MyPoint)

{

Expert2=1;

}





Comment("Buy  ", Expert1,

"\nSell  ", Expert2);



return(0);

}





double SetPairPipValue(string asPair)

  {

   RefreshRates();

   double mPoint;

   double dDigits=MarketInfo(asPair,MODE_DIGITS);

   if(dDigits<4) mPoint=0.01;

   else mPoint=0.0001;

   return(mPoint);

  }

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
The idea of ​​automated trading is appealing by the fact that the trading robot can work non-stop for 24 hours a day, seven days a week. The robot does not get tired, doubtful or scared, it's is totally free from any psychological problems. It is sufficient enough to clearly formalize the trading rules and implement them in the algorithms, and...
 
Winners786:

Hello Friends,

I am new to mql4 coding and learning from past 2 months.

I am trying to code MA cross counter. If MA50 cross above MA1000 then BUY CROSS =1 and same for below.

But the issue is Cross Counter increased by 1 on every tick.

Below is code. Please help me with this.

Thanks in Advance.


You need to hold back calculation until new bars are formed... add these lines before your "MyPoint=SetPairPipValue(Symbol());":

static datetime lastBarTime = 0;
datetime currBarTime = iTime(Symbol(),Period(),1);

if (currBarTime==lastBarTime)
   return (0);
And 
    lastBarTime = currBarTime;
Before the final "return (0);".
Reason: