Delay between trades

 
I am trying to accomplish 3 things with this piece of code :

1)  I want to check the order history and identify the last closed trade of a specific signal associated with the chart/EA.

2) I want to be able to correctly identify the symbol associated with the chart/EA, even though other EA's are running and executing trades on different symbols.

3) Once I have identified the last trade closed of said symbol, I am wanting to introduce a 1 hour delay before entering any new trades.

With the code below, I believe that I have requirements (1) and (2) covered, but I simply cannot get the 1 hour delay to work properly (the EA obeys the 1 trade per bar rule in the code, but not the time delay).  Please would someone point out my error as I have spent days and hours on this now, but just cannot get it right :

 

 //code for waiting 1 hour between trades closed by another EA; incl this EA; starts here; 

 

   datetime LastClose = OrderCloseTime();

for(int x=OrdersHistoryTotal()-1;x>=0;x--)

 {

   OrderSelect(x, SELECT_BY_POS,MODE_HISTORY);

   if(OrderSymbol()==Symbol() &&(OrderCloseTime()+3600<TimeCurrent()))

    {

       CountThis();

    if (Buys==0 && Sells==0 && Bars != ThisBarTrade)

         

   

   //code for waiting 1 hour between trades closed by another EA; incl this EA; ends here

   

   {ThisBarTrade = Bars;  // ensure only one trade opportunity per bar;

   CheckForOpen();}

   else{CheckForCloseThis();CheckForCloseAll();}return;

  }

    }//if

    }//if

 

see comments

 

   datetime LastClose=OrderCloseTime();  //Where is the OrderSelect()? Anyway variable LastClose is not used in the code

   for(int x=OrdersHistoryTotal()-1;x>=0;x--)

     {
      OrderSelect(x,SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol()==Symbol() && (OrderCloseTime()+3600<TimeCurrent()))   //This will be true for every trade closed 
        {                                                                    //More than 1 hour ago
         CountThis();  //What does this function do ?

         if(Buys==0 && Sells==0 && Bars!=ThisBarTrade) //Where do the variables Buys & Sells get their values?
                                                       //If conditions are true for 1 closed trade, cannot be true again
                                                       //during this loop
            //code for waiting 1 hour between trades closed by another EA; incl this EA; ends here
           {
            ThisBarTrade=Bars;  // ensure only one trade opportunity per bar;
            CheckForOpen();  //What does this function do ?
           }
         else
           {
            CheckForCloseThis();  //What does this function do ?
            CheckForCloseAll();  //What does this function do ?
           }
         return; //returns after first trade found closed more than 1 hour ago
        }

     }//if
 
  1.             CheckForCloseThis();  //What does this function do ?
                CheckForCloseAll();  //What does this function do ?
    You are looking at closed orders, how can you CloseThis? How can you CloseAll for each closed order?
  2.         return; //returns after first trade found closed more than 1 hour ago
    
    Assumes that history is sorted. It's not. Could EA Really Live By Order_History Alone? - MQL4 forum
  3.       if(OrderSymbol()==Symbol() && (OrderCloseTime()+3600<TimeCurrent()))   //This will be true for every trade closed 
    
    Also assumes that everything in history is closed orders. Not deleted pending. Not cr/bal entries. See above link -> #2 link -> code comment.
  4. Not filtering by magic number makes EA incompatible with all others (including itself on other TFs,) and manual trading  Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 forum
 

Gumrai and WHRoeder - thank you very much for your comments - I will certainly try to address all of them in the corrected code below :

 

datetime LastClose=OrderCloseTime();  //Where is the OrderSelect()? Anyway variable LastClose is not used in the code; 
//I have the OrderSelect()a few lines down and was just describing the variable LastClose here - is that a problem? I have now placed the variable LastClose into the code
   
for(int x=OrdersHistoryTotal()-1;x>=0;x--)

     {
      OrderSelect(x,SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol()==Symbol() && (LastClose+3600<TimeCurrent()&& OrderMagicNumber()==393720))   //This will be true for every trade closed 
      //More than 1 hour ago ; I have now added the specific OrderMagicNumber() as a further filter
//further question: If I only have ONE symbol on this EA, do i need to specifically list the symbol, or is it fine as Symbol()?
{
         CountThis();  //What does this function do ? ; 
//CountThis() is not a part of the code that is concerning me - I will remove it here for the purpose of getting to the bottom of this specific problem that i am having

         if(Buys==0 && Sells==0 && Bars!=ThisBarTrade) //Where do the variables Buys & Sells get their values?
//this is a line of code that i have always had in place before I tried to introduce the 1 Hr delay - 
//it simply ensures that I only trade once per bar and only if no position is already open; the code has always worked well as written, with the variables defined elsewhere
//If conditions are true for 1 closed trade, cannot be true again during this loop
//I will move these conditions elsewhere in my EA as part of the screening criteria for a trade entry
 //code for waiting 1 hour between trades closed by another EA; incl this EA; ends here
           {
            ThisBarTrade=Bars;  // ensure only one trade opportunity per bar;
            CheckForOpen();  //What does this function do ?
/CheckForOpen() is a seperately defined function that is not a part of the code that is concerning me - 
//I will remove it here for the purpose of getting to the bottom of this specific problem that i am having
           }          else            {             CheckForCloseThis();  //What does this function do ?             CheckForCloseAll();  //What does this function do ?
/CheckForCloseThis and CheckForCloseAll() are seperately defined functions that is not a part of the code that is concerning me - 
//I will remove it here for the purpose of getting to the bottom of this specific problem that i am having
           }          return; //returns after first trade found closed more than 1 hour ago
//Thanks for clarifying that
//WHRoeder - you have pointed out a need to sort the OrderHistory for the return to work reliably - I have followed your Forum links, but the code there seems a bit
//too complex for me to be able to do this sorting - I may just have to live with this assumption...
        }

     }//if
So, now here is my cleaned up and corrected code, do you think it would now work and deliver on all 3 of my originally stated intents, or can you still see errors?
{datetime LastClose = OrderCloseTime();
for(int x=OrdersHistoryTotal()-1;x>=0;x--)
 {
   OrderSelect(x, SELECT_BY_POS,MODE_HISTORY);
   if(OrderSymbol()==Symbol() &&(LastClose+3600<TimeCurrent()&& OrderMagicNumber()==393720))
{;//define EA execution if above "if" function returns as TRUE}
else {;//define EA execution if above "if" function returns as FALSE}

    return;
 
see comments
   datetime LastClose=OrderCloseTime();;  //Where is the OrderSelect()?
   for(int x=OrdersHistoryTotal()-1;x>=0;x--)
     {
      OrderSelect(x,SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol()==Symbol() && (LastClose+3600<TimeCurrent() && OrderMagicNumber()==393720)) //Still targets trades 
                                                                                                  //closed more than
                                                                                                  //i hour ago
        {
         ;//define EA execution if above "if" function returns as TRUE
        }
      else
        {
         ;//define EA execution if above "if" function returns as FALSE
        }
      return;//Still returns after first trade found closed more than 1 hour ago
     }
 

Maybe consider something like this

   bool trade_closed_less_than_1_hour_ago=false;
   for(int x=OrdersHistoryTotal()-1;x>=0;x--)
     {
      OrderSelect(x,SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol()==Symbol() && TimeCurrent()-OrderCloseTime<3600 && OrderMagicNumber()==393720)
        {
         ;//define EA execution if above "if" function returns as TRUE
         trade_closed_less_than_1_hour_ago=true;
         break;  //Probably no need to continue with loop once a trade is found that was closed less than 1 hour ago
        }
      else
        {
         ;//define EA execution if above "if" function returns as FALSE
        }
     }
 

Thank you Gumrai.

Just for clarification : this should find a trade that was closed less than 1 hour ago, but will it also introduce a 1 hour delay before entering any new trades i.e. I want to both identify a recently closed trade and then ensure that it does not retrade the same symbol until a full hour has lapsed since the closed trade took place?

 

Yes. I should think that the else is unnecessary

   bool trade_closed_less_than_1_hour_ago=false;
   for(int x=OrdersHistoryTotal()-1;x>=0;x--)
     {
      OrderSelect(x,SELECT_BY_POS,MODE_HISTORY);
      if(OrderSymbol()==Symbol() && (TimeCurrent()-OrderCloseTime<3600 && OrderMagicNumber()==393720))
        {
         trade_closed_less_than_1_hour_ago=true;
         break;  //Probably no need to continue with loop once a trade is found that was closed less than 1 hour ago
        }
     }
   if(trade_closed_less_than_1_hour_ago==false)
     {
      //Code to open new order or whatever
     } 
 

Thank you

I will try that and will post a comment if successful. 

 

working for a better code !!!!

 
nerd-fold_12:

working for a better code !!!!

 

What does that mean?