[MQL4 CODE] How to close all positions when the profit reaches a certain value?

/**
 * How to close all positions when the profit reaches a certain value?
 */
double Profit_to_Close = 0;
bool Cancel_Trading_On_Profit = false;
bool cantrade = false;
void Save_Profit()
{ 
   if (AccountProfit()>=Profit_to_Close) 
   { 
      for(int i=OrdersTotal()-1;i>=0;i--) 
      { 
         OrderSelect(i, SELECT_BY_POS); 
         int type = OrderType(); 
         bool result = false; 
         switch(type) 
         { 
            //Close opened long positions 
            case OP_BUY : result = 
            OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_BID),3,Pink); 
            break; 
            //Close opened short positions 
            case OP_SELL : result = 
            OrderClose(OrderTicket(),OrderLots(),MarketInfo(OrderSymbol(),MODE_ASK),3,Pink); 
         } 
         if(result == false) 
         { 
            Sleep(3000); 
         } 
         else if (Cancel_Trading_On_Profit) cantrade=false;
      } 
      Print ("Account Profit Reached. All Open Trades Have Been Closed"); 
   }
}
#property copyright "Copyright 2021, www.comefx.com"
#property link      "https://www.comefx.com"
#property version   "1.00"
#property strict

Leave a comment

All comments are moderated before being published

Best Sellers

ADVANCED ARTIFICIAL INTELLIGENCE FOREX TRADING ROBOT FOR YOU

Comefx