Too many orders sent!

 

Hello,

In OnTick() function of my EA I have if statement to check an entry condition such as

if(iMA(_Symbol,PERIOD_CURRENT,ii_MAPeriod,0,MODE_SMA,PRICE_CLOSE,1) < iClose(_Symbol,PERIOD_CURRENT,1)
&& iMA(_Symbol,PERIOD_CURRENT,ii_MAPeriod,0,MODE_SMA,PRICE_CLOSE,2) >= iClose(_Symbol,PERIOD_CURRENT,2)
{
      OrderSend(_Symbol,OP_BUY,ii_LotSize,Ask,5,Ask-(Point*ii_StopLoss),Ask+(Point*ii_TakeProfit),"Mine_Buy",0,0,Green);
}

This condition deals with closed candles therefore as soon as a candle expiries and the entry condition is valid an order has to be sent. However, not only one order is sent I have punch of orders sent around 10 orders!! Why is that?

Sometimes few orders are sent then few seconds another punch are sent again

Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
Documentation on MQL5: Integration / MetaTrader for Python / order_calc_margin
  • www.mql5.com
order_calc_margin - MetaTrader for Python - Integration - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
matrixyb:

Hello,

In OnTick() function of my EA I have if statement to check an entry condition such as

This condition deals with closed candles therefore as soon as a candle expiries and the entry condition is valid an order has to be sent. However, not only one order is sent I have punch of orders sent around 10 orders!! Why is that?

Sometimes few orders are sent then few seconds another punch are sent again

There is nothing in your code to suggest that it only checks conditions once per bar.

 
Keith Watford #:

There is nothing in your code to suggest that it only checks conditions once per bar.

How to achive that?

 
matrixyb #:

How to achive that?

   static datetime barTime=0;
   datetime barTimeNow=iTime(_Symbol,PERIOD_CURRENT,0);
   if(barTime!=barTimeNow)
     {
      barTime=barTimeNow;
      //Do stuff that is only done on the open of a new bar
     }
 
Keith Watford #:

Perfect! Regards

 
Keith Watford #:
So if you are in the middle of the 1Hr candle when you start that code..  what would that code say at the first arriving tick ? Is it a new bar? 
 
Daniel Cioca #: So if you are in the middle of the 1Hr candle when you start that code..  what would that code say at the first arriving tick ? Is it a new bar? 

Please study the following in the CodeBase. It caters for that exact situation.

Code Base

Detecting the start of a new bar or candle

Fernando Carreiro, 2022.04.24 00:46

Detecting the start of a new bar or candle, in the OnTick() event handler of an expert advisor.

Reason: