Simplest Steps for MQL4 by Rethu Alex Mathew

 
//THESE STEPS TO BE PLACED UNDER EXPERT ADVISOR
//---------------------------------------------
#include <TRADE EXECUTE.mqh>
#include <CLOSE ALL.mqh>
#include <TO CLOSE ALL PENDING ORDERS.mqh>

extern double TOTAL_AMOUNT_INCLUDING_PROFIT;
extern double EQUAL_INTERVALS_DISPLAY,

MAX_LOCK_PROFIT,
MINI_LOCK_PROFIT=0.0,

MINI_GREATER_EQUATION,
MINI_UP_DOWN;

extern double BVOLUME,SVOLUME;

int start()
  {

    if (TOTAL_AMOUNT_INCLUDING_PROFIT>AccountEquity() && TOTAL_AMOUNT_INCLUDING_PROFIT-AccountEquity()>=1.00)
       {
  
       //-->CASE 1
        STARTPOINT(BVOLUME,SVOLUME,MAX_LOCK_PROFIT,EQUAL_INTERVALS_DISPLAY);
        EQUATIONONE(BVOLUME,SVOLUME,MAX_LOCK_PROFIT,EQUAL_INTERVALS_DISPLAY);
        EQUATIONTWO(BVOLUME,SVOLUME,MAX_LOCK_PROFIT,EQUAL_INTERVALS_DISPLAY);
        EQUATIONTHREE(BVOLUME,SVOLUME,MINI_LOCK_PROFIT,MINI_UP_DOWN);
        EQUATIONFOUR(BVOLUME,SVOLUME,MINI_LOCK_PROFIT,MINI_UP_DOWN);
        FINALONE(BVOLUME,SVOLUME,MAX_LOCK_PROFIT,EQUAL_INTERVALS_DISPLAY);
    
       //-->END CASE 1
        

       } 
if(TOTAL_AMOUNT_INCLUDING_PROFIT!=0) // Pending Orders wont get deleted if TOTAL_AMOUNT_INCLUDING_PROFIT=0.
{
if(TOTAL_AMOUNT_INCLUDING_PROFIT < AccountEquity() &&  AccountEquity()-TOTAL_AMOUNT_INCLUDING_PROFIT > MINI_GREATER_EQUATION)
     {
     if (MINI_GREATER_EQUATION!=0.0)
        {
         ToCloseAllPendingOrders();
        } 
     }
}
     
if(TOTAL_AMOUNT_INCLUDING_PROFIT!=0) // Instant orders wont get deleted if TOTAL_AMOUNT_INCLUDING_PROFIT=0.
{
if(TOTAL_AMOUNT_INCLUDING_PROFIT < AccountEquity() &&  AccountEquity()-TOTAL_AMOUNT_INCLUDING_PROFIT > MINI_GREATER_EQUATION)
     {
      ToCloseAllTrades();
     } 
}

}
All of the users will be happy with this.
Files:
mql4_1.zip  3 kb
 
13111981:
All of the users will be happy with this.

You are indeed need to learn again :(

1. When executing some trading function, make sure to check the return of OrderSelect against OrderSymbol() and OrderMagicNumber() .

2. Check if there is error when executing some trade function (OrderSend, OrderClose, OrderDelete, OrderModify), and act accordingly to the error - retry the trade function or just forget it. 

3. When closing position, close it with correct price using OrderClosePrice(), not MarketInfo(OrderSymbol(),MODE_BID) or MarketInfo(OrderSymbol(),MODE_ASK) .

4. When deleting position with ticket number, use the real ticket number, don't count up the ticket number from zero - because that is waste of resources. Or using SELECT_BY_POS, check if the position is indeed a pending order. Check also MarketInfo's MODE_FREEZELEVEL, just in case the pending is within the freeze level - which cannot be deleted.

And please don show off your programming skill saying superlative if you still learning https://www.mql5.com/en/forum/8982

 
  1. Agreed 1-3.
  2. #4 You can close pending or active orders when the market is closer than the maximum of FreezeLevel and MarketInfo(chart.symbol, MODE_STOPLEVEL)*Point
Reason: