Only One trade per day

 

Hi everybody,

I'm currently working on an EA that use bollinger Bands and would like this EA to only take one trade per day.

In other words, If a position is close on a day T1, I do not want the EA to open a new position (Short or Long).

I tried using the following "function":

int TotalOrder = OrdersHistoryTotal();
if(OrderSelect(TotalOrder-1, SELECT_BY_POS, MODE_HISTORY)==true)
   {
   datetime LastClose = OrderCloseTime();
   datetime CurrentTime = TimeCurrent();
   if(LastClose == CurrentTime)
      {
       double FreeOfLastTrade = 0;
      }
   else
      {
       FreeOfLastTrade = 1;
      }
   }

Using FreeOfLastTrade in my Long and Short condition, the EA doesn't take any position at all.

Could you guys please help me with that ?

Thanks a lot,

Crak

 

You only want the year month and day to match not the time in seconds.

   for(int i=0;i<OrdersHistoryTotal();i++)
     {       
      OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) ;
      if ( (TimeYear(OrderCloseTime())==Year() ) && (TimeMonth(OrderCloseTime())==Month()) && (TimeDay(OrderCloseTime())==Day()) )
        {
         //Found Closed Order for today
        }
      else
        {
        //None closed yet
        }   
     }//end for loop

After thought - putting a loop like this in for every tick woul be unnecessarily time consuming, it would be wise to think of another way of doing it.

 
  1. int TotalOrder = OrdersHistoryTotal();
    if(OrderSelect(TotalOrder-1, SELECT_BY_POS, MODE_HISTORY)==true)
    
    This selects the last closed order, not the last close on this pair by this EA
  2. for(int i=0;i<OrdersHistoryTotal();i++)
         {       
          OrderSelect(i,SELECT_BY_POS,MODE_HISTORY) ;
    
    This also doesn't check pair or EA and fails of the orderSelect does. Always test return codes.

  3. putting a loop like this in for every tick woul be unnecessarily time consumin
    while scanning isn't that intensive, it isn't necessary
    static  datetime    tradingAllowed;
    int newCount = HistoryTotal();  static  int prevCount;
    if (newCount != prevCount){                 prevCount = newCount;
        datetime lastClose;
        for(int pos=newCount-1; pos >= 0; pos--) if (
            OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
        &&  OrderCloseTime()    > lastClose                 // not yet processed,
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        &&  OrderType()         <= OP_SELL){    // Avoid cr/bal https://www.mql5.com/en/forum/126192
            lastClose = OrderCloseTime();
        }
        datetime lastClosePlus24Hrs = lastClose + 24 * 3600;
        datetime lastCloseNextDay   = lastClose % 86400 + 86400
        tradingAllowed = lastcloseNextDay;
    }
    if (TimeCurrent() => tradingAllowed){ ... }
Reason: