ExpertRemove() Help needed please

 
void OnDeinit(const int reason)
  {
//---
   
  }

#include "M_CloseAllOrdersThisPair_1.mq4"

extern double    Lots            = 0.1;
extern double    TP_Dist         = 150;
extern double    SL_Dist         = 150;
extern int       Sell_Stop_Dist   = 00;  // distance from horizontal upper line
      
       int     Digits();
       double  Sell_Stop_Entry;
  

           
void OnTick()
  {
   
   int    ticket = 0;   
   double Take_Profit_Line   = NormalizeDouble(ObjectGetDouble(0,"TPL",OBJPROP_PRICE,0),Digits);  // Take Profit price value
   double Entry_Set_Line     = NormalizeDouble(ObjectGetDouble(0,"ENT",OBJPROP_PRICE,0),Digits);  // Entry price value
   double Stop_Loss_Line     = NormalizeDouble(ObjectGetDouble(0,"SLL",OBJPROP_PRICE,0),Digits); // Stoploss Price value 
     
   double Price_A            = NormalizeDouble(MarketInfo(Symbol(),MODE_ASK),Digits);              // current Ask price
   double Price_B            = NormalizeDouble(MarketInfo(Symbol(),MODE_BID),Digits);             // current Bid price
   double Min_Dist           = NormalizeDouble(MarketInfo(Symbol(), MODE_STOPLEVEL),Digits);      // Closest price for order
   
  
   Sell_Stop_Entry = NormalizeDouble(Entry_Set_Line - ((Min_Dist+Sell_Stop_Dist)*Point),Digits); // When price reaches here BS set above
      
   double SL = NormalizeDouble((Stop_Loss_Line-(SL_Dist*Point)),Digits);   // SLL set on chart above entry line
   double TP = NormalizeDouble((Take_Profit_Line+(TP_Dist*Point)),Digits); // TPL set on chart below entry line
   
   if(OrdersTotal()>=1)
   { 
   Comment("SELL STOP: ");
   }  
   else Comment("SELL STOP EA: ","\n","Min Dist below Ent line:  ",Min_Dist,"\n","SLL above Entry Line. ","\n", "TPL below Entry Line.");
   
   
//------------------------------------------------------------------------------------------------
         
 
  
      if(OrdersTotal()<1)
      {  
      if(Price_A >= Entry_Set_Line && Price_A<=SL)  // Price in zone between entry line and sl line                  
      ticket = OrderSend(Symbol(),OP_SELLSTOP,Lots,Sell_Stop_Entry,30,SL,TP," ");    
      if(ticket < 0)
      Alert("Error Sending Order!", GetLastError()); 
                               
      }  
   
//--------------------------------------------------------------------------------------
         
      else if (OrdersTotal()>=1 && Price_B<=TP)  // Bid moves down to TP level (Bid below Ask)
         {   
         M_CloseAllOrdersThisPair_1();             
         Comment("TP REACHED EA REMOVED:");
         ExpertRemove();
         } 
//------------------------------------------------------------------------------------  
   
    else if (OrdersTotal()>=1  && Price_A >= SL)     // Ask moves up to SL level (Ask above Bid)                                                            
         {                                                                                   
         M_CloseAllOrdersThisPair_1();
         Comment("BS NOT ENTERED AND/OR SL EA REMOVED.");    
         ExpertRemove();                                                                                            
         }
   
   
  
   
} // end on tick

Hello All,

I am learning mql4 and am struggling with one particular issue.

I have written some code that does basically the following,

i) When price reaches a zone, defined by a horizontal upper line and horizontal lower line  a pending buy order is set.

ii) if price continues down to the lower line I want to delete the buy order and remove the ea

iii) if price goes above the upper line I want to remove the ea and leave the order.


The issue I am having is that when using ExpertRemove() it seems to remove all the orders.

I cannot seem to remove the ea and still leave any order on the chart,

The code Iines are attached. The rest of the code works it just when I attempt to remove the ea the issue starts.

Any advice would be greatly appreciated.

Thank you


    

    

 
Cobh63:

I have written some code that does basically the following,

i) When price reaches a zone, defined by a horizontal upper line and horizontal lower line  a pending buy order is set.

ii) if price continues down to the lower line I want to delete the buy order and remove the ea

iii) if price goes above the upper line I want to remove the ea and leave the order.

The issue I am having is that when using ExpertRemove() it seems to remove all the orders.

I cannot seem to remove the ea and still leave any order on the chart,

The code Iines are attached. The rest of the code works it just when I attempt to remove the ea the issue starts.

I tried this crazy code:

void OnTick()
  {
   static int cnt = 0;
   if (cnt==0OrderSend(_Symbol,OP_BUYSTOP,0.01,Ask*2,1,0,0);
   
   if (cnt==20) ExpertRemove();
   else Print (cnt++);
  }

and my buystop still remains...

so it's probably time you show your whole code :).

 
Seng Joo Thio:

I tried this crazy code:

and my buystop still remains...

so it's probably time you show your whole code :).

Hi Seng Joo Thio,


Thanks for the reply, I really appreciate it.

I had been working on different solutions and eventually found one that worked out well.

I'ts a bit of a work around and removes the expert at the SL and TP levels. 


If a trade is not entered it is removed at SL

If trade is entered it is either removed at SL or TP, code attached.


I will have a look again and try the code you gave.


Thanks again

Files:
 

Cobh63: If a trade is not entered it is removed at SL

The issue I am having is that when using ExpertRemove() it seems to remove all the orders.

  1. False. The pending order remains unless your delete it.

  2. Please don't post image of code. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3. Removing the EA does nothing to the order. Your code must be doing that.

  4. OrdersTotal() >= 1 && Price_A>=TP will never be true if Price_A is the Bid. The order will already have closed, and OrdersTotal() would be zero.

  5. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

 
William Roeder:
  1. False. The pending order remains unless your delete it.

  2. Please don't post image of code. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  3. Removing the EA does nothing to the order. Your code must be doing that.

  4. OrdersTotal() >= 1 && Price_A>=TP will never be true if Price_A is the Bid. The order will already have closed, and OrdersTotal() would be zero.

  5. Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles

Hi William,


Thanks for the advice, I have edited and added all the code.

I tried Seng Joo Thio's but the pending are still deleted. 


The basic idea is to place one pending sell order, when price goes up into the zone between the Entry line and the Stop Loss Line


If price  keeps going and passes the  Stop_Loss_Line   Then remove the Sell Stop and remove the EA

If price turns around and the sell stop is entered to remove the ea at the Take_Profit_Line and leave the order in place with SL and TP


I tried this but it didn't work and eventually I came up with the workaround in the above code.


The work around removes the expert at the SL and TP levels. 

If a trade is not entered it is removed at SL

If trade is entered it is either removed at SL or TP, code attached.


The workaround is ok and allows the ea to be removed but it's not ideal.


Thanks for taking the time to comment.

Reason: