Need assistance checking if the last closed order was a profit and the day before

 

Hi,

After analysis of my backtest results it seems apparent that my EA would perform better if orders were not raised if the previous day an order was closed in profit. I've tried adding a function.

 

bool CheckLastOrder()
{ 
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);  
      if ( OrderSymbol()==Symbol() && OrderMagicNumber() == MagicNumber )
      {
         if ( TimeDay(OrderCloseTime())+1 < TimeDay(TimeCurrent()) && OrderProfit() > 0 )
         {
            Print ("Not last day", " ",  TimeDay(OrderCloseTime())+1 , " ", TimeDay(TimeCurrent()) );
            return(True);
         }
         
         if ( TimeDay(OrderCloseTime())+1 >= TimeDay(TimeCurrent()) && OrderProfit > 0 ) 
         {
            return(False);
            Print ("last day", " ",  TimeDay(OrderCloseTime())+1 , " ", TimeDay(TimeCurrent()) );
         }
      }
      return(True);
   }
}

 

Which I call before all my trade logic with

   if ( CheckLastOrder());
   {  
...

...

 

 However it's not preventing orders being raised on the following day as expected. Can anyone spot any obvious mistakes with my logic please?

 

Best regards Steve 

 

You are in trouble every month: 31 => 1!

Either use TimDayOfYear (= once-a-year-trouble) or use:

...
if ( OrderCloseTime() > iTime(_Symbol,PERIOD_D1,1) ) { // closed after yesterday's open..
...
 

Hmm, hadn't thought about that issue. That's not the current issue with the function though as I'm getting orders on for example the 16th opening after a trade closed on the 15th.

I shall however modify the code to take this into account and then move forward from there.

Thanks!

Best regards Steve 

 

Okay I now have this and it still allows orders to open if an order closed the previous day in profit.

 

bool CheckLastOrder()
{ 
   for(int i=OrdersHistoryTotal()-1;i>=0;i--)
   {
      OrderSelect(i, SELECT_BY_POS,MODE_HISTORY);  
      if ( OrderSymbol()==Symbol() && OrderMagicNumber() == Magic )
      {
         Print (OrderCloseTime(), " ",iTime(_Symbol,PERIOD_D1,1));
         if ( OrderCloseTime() < iTime(_Symbol,PERIOD_D1,1) && OrderProfit() > 0 )
         {
//            Print ("Not last day", " ",  TimeDay(OrderCloseTime())+1 , " ", TimeDay(TimeCurrent()) );
            return(True);
         }
         
         if ( OrderCloseTime() >= iTime(_Symbol,PERIOD_D1,1) && OrderProfit() > 0 ) 
         {
            return(False);
  //          Print ("last day", " ",  TimeDay(OrderCloseTime())+1 , " ", TimeDay(TimeCurrent()) );
         }
      }
      return(True);
   }
}

 

Now if I change the function to setting a variable rather than returning a boolean and test this, it works. Go figure!

 Best regards Steve 

Reason: