This stubborn code does not want to do what it is supposed to do!

 

I have some code that are supposed to do two things as soon as the first Buy or Sell order closes at a profit:

1. Reset a list of variables back to zero

2. Delete any outstanding/exisiting buy or sell stop orders

I just cannot get it to do these two functions and i need some help since i cannot find the problem.

Here is the code:

	aOrders = 0;
        
       
       for(i = 0; i <= OrdersTotal()-1; i++)   
        {
         if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
              && OrderSymbol() == Symbol() 
              && OrderMagicNumber() == MagicNumber
              && OrderType() == OP_SELL
              && OrderProfit() > 0)
              
         aOrders++;
              
         else 
        
         if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
              && OrderSymbol() == Symbol() 
              && OrderMagicNumber() == MagicNumber
              && OrderType() == OP_BUY
              && OrderProfit() > 0)
              
          aOrders++;
        } 
                      
        if (aOrders > 0)
      
        {
         Counter = 0;
         BuyTicket = 0;
         SellTicket = 0;
         FirstBuyTicket = 0;
         SecondBuyTicket = 0;
         FirstSellTicket = 0;
         SecondSellTicket = 0;
         
           {
      
            for(i = 0; i <= OrdersTotal()-1; i++)   
        
            if (OrderSelect(i,SELECT_BY_POS)
               && OrderSymbol() == Symbol() 
               && OrderMagicNumber() == MagicNumber
               && OrderType() == OP_SELLSTOP
               || OrderType() == OP_BUYSTOP)
            
               {
       
                  while(IsTradeContextBusy()) Sleep(10);        
                  bool Deleted = OrderDelete(OrderTicket(),Red);
                            
                                                                            
               // Error handling
                  if(Deleted == false)
                     {
                        ErrorCode = GetLastError();
                        ErrDesc = ErrorDescription(ErrorCode);

                        ErrAlert = StringConcatenate("Delete Old Pending Order - Error ",ErrorCode,": ",ErrDesc);
                        Alert(ErrAlert);

                        ErrLog = StringConcatenate("OrderTicket: ",OrderTicket());
                        Print(ErrLog);
                      }
                  }
               }
           }
 


Roger, I had a look at the article you referenced, but it does not really address my own particular problem of initilization of variables and deletion of pending orders. It deals with:

  • Experts that can open only one position at a time
  • Experts that can open one position of each type at a time (for example, one long and one short position)
  • Expert that can open any amount of positions simultaneously

Now i am sure I will be able to learn a lot from studying all the code, but it is not what I need now. It deals with some complex situations (for a beginner like me at least) that do not address my simple requirements. I actually just need a pointer at what is causing the problem in the simple code that I have written.

Is there anybody willing to help me?

 

I want you understand the main rule to control orders. In your case this is very simple

int marketorder=0;
for(i = 0; i <= OrdersTotal()-1; i++)   
        {
         if (OrderSelect(i,SELECT_BY_POS)
              && OrderSymbol() == Symbol() 
              && OrderMagicNumber() == MagicNumber              
              && OrderProfit() > 0)
              {
              if(OrderType() == OP_SELL)marketorder=-1;
              if(OrderType() == OP_BUY)marketorder=1;
              }
        }
if(marketorder!=0)
for(i = 0; i <= OrdersTotal()-1; i++)   
        {
         if (OrderSelect(i,SELECT_BY_POS)
              && OrderSymbol() == Symbol() 
              && OrderMagicNumber() == MagicNumber 
              && ((marketorder==1&& OrderType()==OP_SELLLSTOP)
              ||(marketorder==-1 && OrderType()==OP_BUYSTOP))
              )
         OrderDelete(OrderTicket());
        }
 
Roger:

I want you understand the main rule to control orders. In your case this is very simple


Wonderful! I have learned something new! Thank you so much! I will take some time to study the code you have referenced. Seems that I can learn a lot from it.

Thanks once again for you help!!

 
I made a little update, you have to check orders in the pool.
 
  1. for(i = 0; i <= OrdersTotal()-1; i++)   
    
    if (OrderSelect(i,SELECT_BY_POS)
       && OrderSymbol() == Symbol() 
       && OrderMagicNumber() == MagicNumber
       && OrderType() == OP_SELLSTOP
       || OrderType() == OP_BUYSTOP)
    
       {
    
    Don't trust precedence, don't mix and's and or's
    for(i = 0; i <= OrdersTotal()-1; i++) if (
       OrderSelect(i,SELECT_BY_POS)
    && OrderSymbol()      == Symbol() 
    && OrderMagicNumber() == MagicNumber
    && (  OrderType()     == OP_SELLSTOP
       || OrderType()     == OP_BUYSTOP
       )
    ){

  2.        for(i = 0; i <= OrdersTotal()-1; i++)   
            {
             if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
                  && OrderSymbol() == Symbol() 
                  && OrderMagicNumber() == MagicNumber
                  && OrderType() == OP_SELL
                  && OrderProfit() > 0)
                  
             aOrders++;
                  
             else 
            
             if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)
                  && OrderSymbol() == Symbol() 
                  && OrderMagicNumber() == MagicNumber
                  && OrderType() == OP_BUY
                  && OrderProfit() > 0)
                  
              aOrders++;
            } 
    OrdersTotal is for OPEN orders, not HISTORY orders.
        static datetime lastClose;  datetime lastClosePrev = lastClose;
        for(int pos=0; pos < OrdersHistoryTotal(); pos++) if (
            OrderSelect(pos, SELECT_BY_POS, MODE_HISTORY)   // Only orders w/
        &&  OrderCloseTime()    > lastClosePrev             // not yet processed,
        &&  OrderMagicNumber()  == magic.number             // my magic number
        &&  OrderSymbol()       == Symbol()                 // and my pair.
        &&  OrderType()         <= OP_SELL // Avoid cr/bal forum.mql4.com/32363#325360
        &&  OrderProfit()       > 0
        ){
            lastClose = OrderCloseTime();
            aCount++;
        }

 
WHRoeder:
  1. Don't trust precedence, don't mix and's and or's
  2. OrdersTotal is for OPEN orders, not HISTORY orders.


I just came back to the forum since i found the code from Roger not working - e.g. my aOrders variable NEVER changed from zero.

Thank you for this code. I will apply your suggestions and report back.

 
ernest02:


I just came back to the forum since i found the code from Roger not working - e.g. my aOrders variable NEVER changed from zero..


I think you meant when you put my code into your expert it didn't work properly? :)
 
ernest02:


Thank you for this code. I will apply your suggestions and report back.

I think you may be missing a couple of steps . . . they are in bold . . .

Copy, read, understand, paste

 
RaptorUK:

I think you may be missing a couple of steps . . . they are in bold . . .

Copy, read, understand, paste


Sorry guys (Roger) if i don't always understand the code you suggest. Sometimes I do not study it as well as I should and may not understand fully. Have patience with me! I thought that the first part of Roger's code should have had (OrderSelect(i,SELECT_BY_POS, MODE_HISTORY) since i was looking for trades that CLOSED with a profit.

I am extremely grateful for the help I receive. Without it i would most probably have given up long ago.

The good news is that the problem has been solved with your assistance and the code works wonderfully!

Thanks to all again. Each one of you played a role in helping to solve the problem!

Reason: