Stuck on a history loop - can anyone help?

 
Hi there
I have spent so many hours on this problem it's not funny. I seem so close but yet so far away.

If B1 > B3, I'd like a sell order to execute ONLY if there hasn't been a closed trade today with the same magic number.
I've tried countless variations for the braces etc but keep going round in circles.

Testing the code below by using both a magic number that has or hasn't closed today, I get "Wahooo!" printed every time B1 is > than B3 but no sell order triggers. Why don't I get "No cigar!" if the magic number has had a closed trade today?????


This is driving me crazy folks and I would so appreciate your help...
Cheers
Darrel



if(B1 > B3)

{
for(int k=0; k<OrdersHistoryTotal(); k--)
{OrderSelect(k,SELECT_BY_POS,MODE_HISTORY);

if(OrderMagicNumber()==MagicNumber &&
TimeDayOfYear(OrderCloseTime())==TimeDayOfYear(TimeCurrent()) )

{ Print("No Cigar! = ", TimeDayOfYear(TimeCurrent()));
//continue;

}else{

Print("Wahooo! = ", TimeDayOfYear(TimeCurrent()));
Order = SIGNAL_SELL; }

}
}
 

Try it this way (untested)

if( B1 > B3 ){
   for( int k=OrdersHistoryTotal()-1; k>=0; k-- ){
      if( !OrderSelect(k,SELECT_BY_POS,MODE_HISTORY) )
         continue;

      if( OrderMagicNumber()!=MagicNumber )
         continue;
      
      if( TimeDayOfYear(OrderCloseTime())==TimeDayOfYear(TimeCurrent()) ){
         Print("No Cigar! = ", TimeDayOfYear(TimeCurrent()));
         //continue;
      }
      else{
         Print("Wahooo! = ", TimeDayOfYear(TimeCurrent()));
         Order = SIGNAL_SELL;
      }
} 

Notice that you were decrementing the position (k) starting at zero and not checking if the OrderSelect actually worked.

Also notice the use of the SRC button to post source code.

 

Thank you dabbler.

Unfortunately it didn't work for me. I've tried another way and I really thought I'd cracked it. My testing was working perfectly for a long time. Until today.
It was a sell order which shouldn't have triggered as this morning a different sell order (with the same magic number) closed. For some reason my journal is getting a mixture of "Wahooo's" and "No Cigar's".

Why is my loop not seemingly checking all of the history????

Thanks for any help.

     {
     for( int k=OrdersHistoryTotal()-1; k>=0; k-- )
     {
        if (
             OrderSelect(k, SELECT_BY_POS, MODE_HISTORY)

             &&  OrderMagicNumber()  == MagicNumber             
             &&  OrderSymbol()       == Symbol()                 
             &&  OrderType()         <= OP_SELL                     
             && TimeDayOfYear(OrderCloseTime()) == TimeDayOfYear(TimeCurrent())
           )
        
                {       
                 Print("No cigar! = ", TimeDayOfYear(TimeCurrent()));   
                break; 
                }
                
                {       
                Print("Wahoooo! = ", TimeDayOfYear(TimeCurrent()));
                     Order = SIGNAL_SELL;  
                     }
     }
     }
 
darrelf:

Why is my loop not seemingly checking all of the history????

Thanks for any help.

Do you have ALL of your history showing ? What gets checked as the History is only what is shown in the Account History.
 

Also . . . I think you need to move some of your code outside of the loop . . . and add a bool

bool OrderClosedToday = false;


     {
     for( int k=OrdersHistoryTotal()-1; k>=0; k-- )
        {
        if ( OrderSelect(k, SELECT_BY_POS, MODE_HISTORY)

             &&  OrderMagicNumber()  == MagicNumber             
             &&  OrderSymbol()       == Symbol()                 
             &&  OrderType()         <= OP_SELL                     
             && TimeDayOfYear(OrderCloseTime()) == TimeDayOfYear(TimeCurrent()) )   
        
           {       
           Print("No cigar! = ", TimeDayOfYear(TimeCurrent()));   
           OrderClosedToday = true;
           break; 
           }
        }  // end of for

     if(!OrderClosedToday)
        {
        Print("Wahoooo! = ", TimeDayOfYear(TimeCurrent()));
        Order = SIGNAL_SELL;
        }  
     }
 


Thank you for your comments RaptorUK - I so appreciate your suggestion. I think however I've fixed it. I will take a look at your code as well though even if it just helps me learn.

Anyway, I think my error was:

&&  OrderType()         <= OP_SELL   

I'm pretty certain I should have used '==' and not '<='.

I run one MT4 instance with two EA's on it for the same instrument. One EA is my Buy strategy, the other is for my Sell strategy (they have different magic numbers).

In my Buy strategy, I will use:

&&  OrderType()         == OP_BUY

Do my thoughts ring true as to the cause of the error?
Cheers!


 
darrelf:


I'm pretty certain I should have used '==' and not '<='.

You definitely DID want <=. You don't want deleted pending orders or credit/balance entries. Only Buys and Sells.

    for(int iPos=OrdersHistoryTotal()-1; iPos >= 0; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS, MODE_HISTORY)  // Only orders w/
    &&  OrderMagicNumber()  == Magic.Number             // my magic number
    &&  OrderSymbol()       == chart.symbol             // and my pair.
    &&  OrderType()         <= OP_SELL//Avoid cr/bal https://www.mql5.com/en/forum/126192
    ){
        OrderClosedToday = true; break; }

Once you find the order, then you may want to remember that order's direction.

See also Order History sort by closing date - MQL4 forum



 
darrelf:


Thank you for your comments RaptorUK - I so appreciate your suggestion. I think however I've fixed it. I will take a look at your code as well though even if it just helps me learn.

Anyway, I think my error was:

I'm pretty certain I should have used '==' and not '<='.

I run one MT4 instance with two EA's on it for the same instrument. One EA is my Buy strategy, the other is for my Sell strategy (they have different magic numbers).

In my Buy strategy, I will use:

Do my thoughts ring true as to the cause of the error?
Cheers!

This isn't what you said in your first post . . . <= OP_SELL is OP_BUY or OP_SELL see here . . . but yes, if you have 2 EAs one for buys and one for sells then yes, use == OP_BUY and == OP_SELL . . .
Reason: