Function which allow 1 trade per candle not at beginning of candle only

 
How can I make a function that only allow 1 trade or open position per candle not only at the beginning of the candle but until new candle forms. The newbar function only allows at beginning of new candle only 
 
Capture the count of opened trade in a variable and use it as an additional condition to avoid entering new trade. Reset it to zero on new bar. This may help 
 
Youg Chee The newbar function only allows at beginning of new candle only 

You code it exactly the same. When you open a new trade, remember the current candle's time. If they are the same, don't open a new one.

Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help (2017.04.21)

 
William Roeder:

You code it exactly the same. When you open a new trade, remember the current candle's time. If they are the same, don't open a new one.

Show us your attempt (using the CODE button) and state the nature of your problem.
          No free help (2017.04.21)

Code:

//+------------------------------------------------------------------+
//| Tradeallowed                                                     |
//+------------------------------------------------------------------+
bool Tradeallowed()
  {
   bool hastraded;
   for(int a =PositionsTotal()-1; a>=0; a--)
     {
      string symbol1 = PositionGetSymbol(a);
      if(Symbol() == symbol1)
        {
         ulong pticket1 = PositionGetTicket(a);
         double csl1 = PositionGetDouble(POSITION_SL);
         int posT = PositionGetInteger(POSITION_TYPE);
         if(NewBar() == true)
           {
            if(posT == POSITION_TYPE_BUY)
              {
               hastraded = true;
              }
            if(posT == POSITION_TYPE_SELL)
              {
               hastraded = true;
              }
           }
        }
     }
   return false;
  }

 
Youg Chee: bool Tradeallowed(){
  1. Please edit your post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum (2019.05.06)
              Messages Editor

  2. Your code has no checks for #2

 
Youg Chee:

Code:


Please insert the code correctly: when editing a message, press the button     Codeand paste your code into the pop-up window. (The first time I corrected your message)
 
Youg Chee:

Code:


Even if this is your approach the function should return proper value

return(hastraded);

and also select the position by index before you start working with them

 
bool Tradeallowed(){

      bool hastraded = false;
      
    for(int a =PositionsTotal()-1;a>=0;a--){
         
         string symbol1 = PositionGetSymbol(a);  
         
         if(Symbol() == symbol1){
                  
            ulong pticket1 = PositionGetTicket(a);
            double csl1 = PositionGetDouble(POSITION_SL);
            int posT = PositionGetInteger(POSITION_TYPE);
          
               if(posT == POSITION_TYPE_BUY){
            
                       hastraded = true;
                        
                  
                  }
                 
               
               
               if(posT == POSITION_TYPE_SELL){
            
                       hastraded = true;
                       
                  
                }
               
             }   
         }
          
     
     
     return hastraded;    

}
Vladimir Karputov:
Please insert the code correctly: when editing a message, press the button     and paste your code into the pop-up window. (The first time I corrected your message)
Reason: