Check if open trade in Bar - MT4

 

Dear all, 

I wrote an EA that allows a total of 5 trades to be opened at one time. However, I only want one trade to open per bar although not necessarily at the beginning of the bar. Therefore, the code written here (https://www.mql5.com/en/forum/216567) or similar variations does not work for me. 

I used Day trading, so the EA will run on every tick until the conditions are met to open 1 trade, and then it will stop. It will only check again when a new bar is formed. 

I wrote the code below. However, i'm getting an error: '}' - not all control paths return a value 

Please assist me. I'm not sure how to update the code so it works properly. 

bool  CheckTradeInBar()
{ 
   if(OrdersTotal()>0)                
   { for(int t=0; t<=OrdersTotal(); t++)                                                             
     { if(OrderSelect(t,SELECT_BY_POS,MODE_TRADES)==true)
       { if(OrderSymbol() != Symbol()) continue;
       
         if(OrderSymbol() == Symbol())             
         { if( (TimeDayOfYear(OrderOpenTime()) == TimeDayOfYear(TimeCurrent())) && (OrderOpenTime() > Time[0]) )
           { return false; Print("Trade already opened in current bar"); t++; } 
           else                                                                       //if( OrderOpenTime() < Time[0] )
           { return true; }}            
   }}}
}



How to have only one trade per bar?
How to have only one trade per bar?
  • 2017.09.29
  • www.mql5.com
Hi guys, What is the easiest way to enforce just one trade per bar on close of the bar? Best Regards, Ashish...
 
Please post in the correct section in future.
I will move this to the MQL4 and Metatrader 4 section.
 
Keith Watford:
Please post in the correct section in future.
I will move this to the MQL4 and Metatrader 4 section.

sorry... and thanks

 
bool  CheckTradeInBar()
{
   if(OrdersTotal()>0)
     {
      for(int t=0; t<=OrdersTotal(); t++)
        {
         if(OrderSelect(t,SELECT_BY_POS,MODE_TRADES)==true)
           {
            //if(OrderSymbol() != Symbol()) continue;  //No need for this line as you check the symbol in the following line.

            if(OrderSymbol() == Symbol())
              {
               if( (TimeDayOfYear(OrderOpenTime()) == TimeDayOfYear(TimeCurrent())) && (OrderOpenTime() > Time[0]) )
                 {
                  return false;  //You return false when there is a trade opened during the lifetime of the bar ???????
                  Print("Trade already opened in current bar");  //This won't print as you have already returned
                  t++; //WHY????
                 }
               else                                                                       //if( OrderOpenTime() < Time[0] )
                 {
                  return true;  //You are returning true if a checked trade did not open during the bar, you need to check the remaining trades.
                                //You should return nothing here and return where I have indicated lower down.
                 }
              }
           }
        }
     }
    // You need to return something here probably true
}

You don't need all the TimeDayOfYear stuff.

Sorry, I re-formatted your code to how I prefer, personal choice, I know.

 
Keith Watford:

You don't need all the TimeDayOfYear stuff.

Sorry, I re-formatted your code to how I prefer, personal choice, I know.

Perfect. It worked. Thank you. 

 
Just typed, not compiled, not tested.
bool  CheckTradeInBar(){
   for(int t=OrdersTotal()-1; t>=0; --t) if(
   && OrderSelect(t,SELECT_BY_POS)
// && OrderMagicNumber == MN
   && OrderSymbol() == Symbol()
   ) if( OrderOpenTime() >= Time[0]) ) return true; // Order opened this bar.
   return false;
}
Just typed, not compiled, not tested.
 
Keith Watford:

You don't need all the TimeDayOfYear stuff.

Sorry, I re-formatted your code to how I prefer, personal choice, I know.

this is what i landed on. I tested it already and it works. Thanks again


bool CheckTradeInBar()
{
   if(OrdersTotal()>0)
   {  
      for(int t=0; t<=OrdersTotal(); t++)
      {  
         if(OrderSelect(t,SELECT_BY_POS,MODE_TRADES)==true)
         {  
            if(OrderSymbol() == Symbol())
            {   if( OrderOpenTime() > Time[0] )
                
                { Print("Trade already opened in current bar"); 
                  return false; }                                       //Return false when there is a trade opened during the lifetime of the bar
             }
          }
       }
   }
   return true;                                                    //Return true when there is no trade opened during the lifetime of the bar
}
Reason: