One trade per day NOT working

 
Hi all, I would like the EA to open only one trade per 24 hours (every trading day one trade at specified time). The code below has no errors from the compiler, but won't open any trades. Could be a logic error or the return value of the function TradePlacedToday() Can PLEASE somebody help me to fix this? THANKS!!! File attached...
Files:
test3.mq4  3 kb
 
huntrader:
Hi all, I would like the EA to open only one trade per 24 hours (every trading day one trade at specified time). The code below has no errors from the compiler, but won't open any trades. Could be a logic error or the return value of the function TradePlacedToday() Can PLEASE somebody help me to fix this? THANKS!!! File attached...

    if((OrdersTotal() == 0) && (TradePlacedToday() == false))

If OrdersTotal==0

Then this must return true

bool TradePlacedToday()
    {
     datetime now = TimeCurrent();
     datetime bod = now-now%86400; 
     for (int k=OrdersTotal()-1;k>=0;k--)       //if OrdersTotal==0, this for loop will be ignored
      {
         if (OrderSelect(k,SELECT_BY_POS,MODE_HISTORY))
         {                     
            datetime opp=OrderOpenTime();
            datetime cur=opp-opp%86400;           
          if (opp<cur)return(0);
         }
           return(false);      
      }
      return(true);                             //So this will always return true if OrdersTotal==0
    }   
 
GumRai:

 

 

 


Thank you for the help! I'm still confused how to fix the code. I would like this function to check in history for a trade, if there was a trade, then to prevent opening any other today till tomorrow.

And max trade is only one per day.

What changes I need to make or something else all together?

Thans! 

 

If you want to check open trades use OrdersTotal() for your loop

If you want to check closed trades then use OrdersHistoryTotal() for your loop

You want to check both, so use 2 loops 

 
if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)!=1)Print(GetLastError);
Try something like that.
 
huntrader:

 

Thanks for your help!

But I just can't figure it out... 

Trying a different approach, but this gets me a warning: "return value of "OrderSelect" should be checked" 

1. How would I solve this?

2. Will this code prevent opening more then one order per day? 

Thank you so much!!! 

 I refer you to my earlier response
GumRai:

If you want to check open trades use OrdersTotal() for your loop

If you want to check closed trades then use OrdersHistoryTotal() for your loop

You want to check both, so use 2 loops 

You are only checking closed trades so if there are open trades, they won't be counted.

The warning is so that you are sure that an order has been selected, or the remainder of the code may be working with the wrong trade, maybe one will be counted twice.

if(OrderSelect(i, SELECT_BY_POS,MODE_HISTORY)==false)
   {
   //Print an error report or whatever you want to do in case of the select failing
   continue;
   }

 can also be written

if(!OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
   {
   //Print an error report or whatever you want to do in case of the select failing
   continue;
   }
 
Thanks very much for your help! I will solve this now.