//+------------------------------------------------------------------+
//|                              Close when profit level reached.mq4 |
//|                        Copyright 2020, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2020, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
extern double ProfitMultiplier = 1.2;
int Profit;
      
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---   
   ObjectCreate("BALANCE", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("BALANCE"," BALANCE =     "+ round (AccountBalance()) ,20 , "Arial BOLD", clrFloralWhite);       
   ObjectSet("BALANCE", OBJPROP_CORNER, 1);       
   ObjectSet("BALANCE", OBJPROP_XDISTANCE,  10);       
   ObjectSet("BALANCE", OBJPROP_YDISTANCE, 50); 
   ObjectCreate("EQUITY", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("EQUITY"," EQUITY =     "+ round (AccountEquity()) ,20 , "Arial BOLD", clrFloralWhite);       
   ObjectSet("EQUITY", OBJPROP_CORNER, 1);       
   ObjectSet("EQUITY", OBJPROP_XDISTANCE,  10);       
   ObjectSet("EQUITY", OBJPROP_YDISTANCE, 100); 
   ObjectCreate("PROFIT", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("PROFIT"," PROFIT =     "+ round (AccountProfit()) ,20 , "Arial BOLD", clrFloralWhite);       
   ObjectSet("PROFIT", OBJPROP_CORNER, 1);       
   ObjectSet("PROFIT", OBJPROP_XDISTANCE,  10);       
   ObjectSet("PROFIT", OBJPROP_YDISTANCE, 150); 
   ObjectCreate("TARGET", OBJ_LABEL, 0, 0, 0);
   ObjectSetText("TARGET"," TARGET =     "+ round ( AccountBalance() * ProfitMultiplier) ,20 , "Arial BOLD", clrFloralWhite);       
   ObjectSet("TARGET", OBJPROP_CORNER, 1);       
   ObjectSet("TARGET", OBJPROP_XDISTANCE,  10);       
   ObjectSet("TARGET", OBJPROP_YDISTANCE, 200); 

Profit = ( AccountBalance() * ProfitMultiplier);

if (AccountEquity() >= Profit)

 

{
   
   //Update the exchange rates before closing the orders
   RefreshRates();
      //Log in the terminal the total of orders, current and past
      Print(OrdersTotal());
      
   //Start a loop to scan all the orders
   //The loop starts from the last otherwise it would miss orders
   for(int i=(OrdersTotal()-1);i>=0;i--){
      
      //If the order cannot be selected throw and log an error
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false){
         Print("ERROR - Unable to select the order - ",GetLastError());
         break;
      } 
 
      //Create the required variables
      //Result variable, to check if the operation is successful or not
      bool res=false;
      
      //Allowed Slippage, which is the difference between current price and close price
      int Slippage=0;
      
      //Bid and Ask Price for the Instrument of the order
      double BidPrice=MarketInfo(OrderSymbol(),MODE_BID);
      double AskPrice=MarketInfo(OrderSymbol(),MODE_ASK);
 
      //Closing the order using the correct price depending on the type of order
      if(OrderType()==OP_BUY){
         res=OrderClose(OrderTicket(),OrderLots(),BidPrice,Slippage);
      }
      if(OrderType()==OP_SELL){
         res=OrderClose(OrderTicket(),OrderLots(),AskPrice,Slippage);
      }
      
      //If there was an error log it
      if(res==false) Print("ERROR - Unable to close the order - ",OrderTicket()," - ",GetLastError());
   }
}
return;
}