1 new trade per new candle on EA mql4. How?

 
I'm still learning how to code an EA but I face a challenge while doing it. Here's what I'm trying to achieve...

I want the code to open a trade only if there is no open trade during that specific candle. I do not mind having a new trade opened on every candle (multiple trades running at the same time) but there should not be more than 1 new trade opened per new candle. I tried looking into magic number but a magic number only prevents a double entry so the code below does not solve my problem.


{
   int openOrders = OrdersTotal();
   
   for(int i = 0; i < openOrders; i++)
   {
      if(OrderSelect(i,SELECT_BY_POS)==true)
      {
         if(OrderMagicNumber() == magicNBforbuy) 
         {
            return true;
         }  
      }
   }
   return false;
}

I also tried time based restriction but this code only allow the EA to execute the code once per candle so this code also does not solve my problem.


   {
   static datetime saved_candle_time;
   if(Time[0]==saved_candle_time)
      return false;
   else
      saved_candle_time=Time[0];
   return true;
   }

Can someone help me with this problem? Please.


Thank you!

 
Don_xyZ:
I'm still learning how to code an EA but I face a challenge while doing it. Here's what I'm trying to achieve...

I want the code to open a trade only if there is no open trade during that specific candle. I do not mind having a new trade opened on every candle (multiple trades running at the same time) but there should not be more than 1 new trade opened per new candle. I tried looking into magic number but a magic number only prevents a double entry so the code below does not solve my problem.


I also tried time based restriction but this code only allow the EA to execute the code once per candle so this code also does not solve my problem.


Can someone help me with this problem? Please.


Thank you!

Solved it!

Here's what I did (with the help of someone over at another forum)

bool CheckOpenOrders()
{
   int openOrders = OrdersTotal();
   
   for(int i = 0; i < openOrders; i++)
   {
      if(OrderSelect(i,SELECT_BY_POS)==true)
      {
         if (OrderOpenTime()>= Time[0])
         {
            return true;
         }  
      }
   }
   return false;
}


and then employ !CheckOpenOrders() in the EA.

 
Don_xyZ #:

Solved it!

Here's what I did (with the help of someone over at another forum)


and then employ !CheckOpenOrders() in the EA.


If you changed the chart timeframe to a higher timeframe then you may have problems. 


 if (OrderOpenTime()>= Time[0])


Use iTime(...) function instead of Time[0] to be more specific about which timeframe candle you mean.

 
Daniel Mou #:


If you changed the chart timeframe to a higher timeframe then you may have problems. 



Use iTime(...) function instead of Time[0] to be more specific about which timeframe candle you mean.

Thank you for the suggestion. I am aware of that too.

The EA is not final and that is why I use OHCL and Time with [] instead of the ones with "i". Once everything works, I will change it.