Help to close OP_BUYLIMIT

 

Hello,

Sorry for my bad English, I'm Frenchy...., I'll try to be clear

In my code, when conditions replies, the OP_BUYLIMIT (Achat_1) is open and a I place a new OP_BUYLIMIT pending. If Achat_2 is active, a new OP_BUYLIMIT (Achat_3) is pending. There is additional code between these lines, useless for understanding I suppose.

Prix = (MarketInfo (Symbol(), MODE_ASK)) - (MACD_Sécurité * Point * MyPoint); 
Achat_1 = OrderSend(Symbol(), OP_BUYLIMIT, Lots, Prix, 3, 0, 0, "Colombanaise MACD : Ordre Achat_1 envoyé", Colombanaise_MACD_Magic_Number, 0, Blue);
Achat_2 = OrderSend(Symbol(), OP_BUYLIMIT, Lots, Prix_Achat_1-(MACD_Montante * Point * MyPoint), 3, 0, 0, "Colombanaise MACD : Ordre Achat_2 envoyé", 
   Colombanaise_MACD_Magic_Number, 0, Blue);
OrderModify(Achat_1, OrderOpenPrice(), Stop_Loss, Prix_Vente, 0, White); 

When the gain is reached, the pending (Achat_x) OP_BUYLIMIT is delete

Total_Orders = OrdersTotal();   
   if(OrderSelect(Achat_1, SELECT_BY_TICKET) == true)
      {                                                                                                                                      
      if (
         ((OrderCloseTime()!=0) && (Total_Orders > 0)) || 
         ((OrderCloseTime()==0) && (OrderType() == OP_BUYLIMIT) && (MACD_Current < MACD_Signal_Current) && (Bars != BarsCount))
         )
         {                       
         for(int i = Total_Orders - 1; i >= 0 ; i --)          
            {                                                                                                                                   
            if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
               {                                                                                                               
               Cmd = OrderType();
               if(Cmd != OP_BUY && Cmd != OP_SELL && Colombanaise_MACD_Magic_Number == OrderMagicNumber())
                  {                                                                                                 
                  Result = OrderDelete(OrderTicket());
                  if(Result == TRUE) Print("Suppression du BUY Limit effectué : ", OrderTicket());
                  if(Result != TRUE) Print("Erreur lors de la Suppression du BUY Limit : ", OrderTicket());
                  break;
                  }                                                                                                      
               }                                                                                                                 
               else { Print( "Erreur lors de la sélection du Buy Limit à supprimer ", GetLastError()); break; }
            }                                                                                                         
      } 


This all works fine, however, I am running several EA simultaneously with the same principle.

I would like to have more active EA, but when a Buy is active, all OP_BUYLIMIT of all EA are delete to have only one at a time active trade open

I thought monitor all open and pending orders, but I can not find the method to detect if Achat_1 with Magic Number X or Achat_1 with Magic Number Y is close

Below the way I try but if someone can guide me a little, thanks

 Total_Orders = OrdersTotal();

 for(Position_Index = Total_Orders - 1; Position_Index >= 0 ; Position_Index --)
      {
      if ( ! OrderSelect(Position_Index, SELECT_BY_POS, MODE_TRADES)) continue;
   
      if (
         (OrderCloseTime()!=0) && ???


Emmanuel





 
arsouille:

Hello,

I thought monitor all open and pending orders, but I can not find the method to detect if Achat_1 with Magic Number X or Achat_1 with Magic Number Y is close

Below the way I try but if someone can guide me a little, thanks

Select the order using OrderSelect()  then check it's Magic number using OrderMagicNumber()  
 

Not sure I get you correctly, but do you have to know Achat_1 with MagicX or Achat_2 with MagicY is closed?
Could you not just filter for open/market orders and if any is active delete whatever order you need to delete?

      if (
         (OrderCloseTime()!=0)

      //could be
      if(OrderType()==OP_BUY){DeleteAllPendingBuyOrders();} //OrderType OP_BUY out of pool MODE_TRADES is a market order.

 You may want to include a filter for the given magicnumbers too.

 
  1.     if ( ! OrderSelect(Position_Index, SELECT_BY_POS, MODE_TRADES)) continue;
           if (
             (OrderCloseTime()!=0) && ???
    If you are selecting by TICKET, the MODE is ignored. You get the ticket which may be open, pending or closed, so you need to check OrderCloseTime. When you are selecting by POSITION/ mode_TRADES, OrderCloseTime will always be zero.
  2. if(OrderSelect(Achat_1, SELECT_BY_TICKET) == true)
    {           
      if (
        ((OrderCloseTime()!=0) && (Total_Orders > 0)) 
        || 
        ((OrderCloseTime()==0) && (OrderType() == OP_BUYLIMIT) 
            && (MACD_Current < MACD_Signal_Current) && (Bars != BarsCount))
        )
    
    This is almost unreadable and therefor not understandable.
  3. Always move unchanging things outside loops. In this case macd and barsCount have nothing do with the order selected.
    if (MACD_Current < MACD_Signal_Current
    && Bars != BarsCount
    && OrderSelect(Achat_1, SELECT_BY_TICKET)
    ){           
      if (
        (OrderCloseTime()!=0 && Total_Orders > 0)
        || 
        (OrderCloseTime()==0 && OrderType() == OP_BUYLIMIT)
        )

  4. Use Self documentating code, Simplify using functions.
    if (MACD_Current < MACD_Signal_Current
    && Bars != BarsCount
    && OrderSelect(Achat_1, SELECT_BY_TICKET)
    ){   
      bool hasClosedWithOtherPending = OrderCloseTime()!=0 && Total_Orders > 0,
            isStillPending           = OrderCloseTime()==0 && OrderType() == OP_BUYLIMIT;
      if (hasClosedWithOtherPending || isStillPending) DeleteAllPendingBuyOrders();
    

  5. You would never write if ( (2+2==4) == true) would you? Then don't write if (bool == true) it's redundant. if (bool) and if(!bool) is sufficient especially if you use readable var names.
    Redundant tests
    Result = OrderDelete(OrderTicket());
    if(Result == TRUE) Print("Suppression du BUY Limit effectué : ", OrderTicket());
    if(Result != TRUE) Print("Erreur lors de la Suppression du BUY Limit : ", OrderTicket());
    Simplified
    if (OrderDelete(OrderTicket()) 
            Print("Suppression du BUY Limit effectué : ", OrderTicket());
    else    Print("Erreur lors de la Suppression du BUY Limit : ", OrderTicket(), "?",GetLastError());

  6. What are Function return values ? How do I use them ? - MQL4 forum
    Check your return codes AND print out the error code so you find out WHY.
  7. else { Print( "Erreur lors de la sélection du Buy Limit à supprimer ", GetLastError()); break; }
    
    You have not called a function since the orderSelect, so what use is GetLastError here?
  8. Don't use bars or volume, unreliable. Always use time[]
 

Hello,

Thanks for your answers.

What do you think of this code?

If Achat_1 was closed in this EA or if Achat_1 is open in other EA (1, 2, 3 or 4), all pending orders are closed

Total_Orders = OrdersTotal();
   
      if (
         (OrderSelect(Achat_1, SELECT_BY_TICKET) == true) && (OrderCloseTime()!=0) && (OrderMagicNumber() == Colombanaise_MACD_Magic_Number) && (Total_Orders > 0) ||
         (OrderSelect(Achat_1, SELECT_BY_TICKET) == true) && (OrderCloseTime()==0) && (OrderMagicNumber() == Other_EA_Magic_Number_1) && (Total_Orders > 0) ||
         (OrderSelect(Achat_1, SELECT_BY_TICKET) == true) && (OrderCloseTime()==0) && (OrderMagicNumber() == Other_EA_Magic_Number_2) && (Total_Orders > 0) ||
         (OrderSelect(Achat_1, SELECT_BY_TICKET) == true) && (OrderCloseTime()==0) && (OrderMagicNumber() == Other_EA_Magic_Number_3) && (Total_Orders > 0) ||
         (OrderSelect(Achat_1, SELECT_BY_TICKET) == true) && (OrderCloseTime()==0) && (OrderMagicNumber() == Other_EA_Magic_Number_4) && (Total_Orders > 0)
         )
            {                       
            for(int i = Total_Orders - 1; i >= 0 ; i --)          
               {                                                                                                                                   
               if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
                  {                                                                                                               
                  Cmd = OrderType();
                  if(Cmd != OP_BUY && Cmd != OP_SELL && OrderMagicNumber()==Colombanaise_MACD_Magic_Number)
                     {
                     Result = OrderDelete(OrderTicket());
                     if(Result == TRUE) Print("Suppression du BUY Limit effectué : ", OrderTicket());
                     if(Result != TRUE) Print("Erreur lors de la Suppression du BUY Limit : ", OrderTicket());
                     break;
                     }                                                                                                      
                  }                                                                                                                 
                  else { Print( "Erreur lors de la sélection du Buy Limit à supprimer ", GetLastError()); break; }
               }
            } 


Other question, a EA open and close orders automatically.

In my example, Achat_1 is open, then close, and a other Achat_1 is open and close......

How the system differentiates Achat_1 closed at 15pm with a other Achat_1 closed at 16pm.

When I asked whith this code, my question is of course the last order closed

(OrderSelect(Achat_1, SELECT_BY_TICKET) == true) && (OrderCloseTime()!=0)


Last question, is it a problem if several EA have the same name Achat_1 ?


Thanks, Emmanuel


 

WHRoeder,

sorry, I post without see your comment.

I'll read it


Emmanuel

 
arsouille:

Last question, is it a problem if several EA have the same name Achat_1 ?

An EA's variables,  even if they have the same name as variables in another running EA, are all kept separate . . .   all EAs have a start() function and they do not interfere with each other,  the same is true for variables.