//+------------------------------------------------------------------+
//|                                                    ProfitDog.mq4 |
//|                                                  Baskan Kalezade |
//|                                      http://www.signalscreen.com |
//+------------------------------------------------------------------+
#property copyright "Baskan Kalezade"
#property link      "http://www.signalscreen.com"

extern string Explain1="Closes All Positions When Profit is 5%";
extern double ProfitPercentage=5;
extern string Explain2="OR Closes All Positions When Profit is 1000USD";
extern double ProfitUSD=1000;

//+------------------------------------------------------------------+
//| expert initialization function                                   |
//+------------------------------------------------------------------+
int init()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert deinitialization function                                 |
//+------------------------------------------------------------------+
int deinit()
  {
//----
   
//----
   return(0);
  }
//+------------------------------------------------------------------+
//| expert start function                                            |
//+------------------------------------------------------------------+
int start()
  {
//----
check_profit();
//----
   return(0);
  }
  
  void check_profit()
  {
 double profit=0,totalprofit=0,profitpercentage=0;
 double tobid=0,toask=0;
 int   slippage = 4;
  for (int i = 0; i < OrdersTotal(); i++)
  {    
     RefreshRates();
     OrderSelect(i,SELECT_BY_POS,MODE_TRADES);       
          profit=OrderProfit();
          totalprofit=totalprofit+profit;     
   }
  
 profitpercentage=(100*totalprofit)/AccountBalance();      
  
  DoubleToStr(profitpercentage,2) ;
       
 Comment("Profit=",DoubleToStr(profitpercentage,2),"%  ", "(",DoubleToStr(totalprofit,2),"USD)\n","Target:",ProfitPercentage,"%  OR  ",ProfitUSD,"USD\nsignalscreen.com");
  
  if((totalprofit>=ProfitUSD)||profitpercentage>=ProfitPercentage)
  {
 Print("Profit %=",profitpercentage," Profit=",totalprofit);
 Alert("All Position are Closed with Profit=",profitpercentage,"%  Profit=",totalprofit,"USD");
 
   
   for (i = 0; i < 100; i++)
   if(OrdersTotal()>0)
   {    
     OrderSelect(0,SELECT_BY_POS,MODE_TRADES);       
      if (OrderType() == OP_BUY)
        { 
         tobid=MarketInfo(OrderSymbol(),MODE_BID);
         if(OrderClose(OrderTicket(),OrderLots(),tobid,slippage,Red)!=0)
         Print("Orderclose Error:", GetLastError()); 
         continue;            
      }  
     if (OrderType() == OP_SELL)
      { 
        toask=MarketInfo(OrderSymbol(),MODE_ASK);
        if(OrderClose(OrderTicket(),OrderLots(),toask,slippage,Red)!=0)
        Print("Orderclose Error:", GetLastError()); 
        continue;            
      }  
  }
 }

  }
  
//+------------------------------------------------------------------+