StopOut EA

 
//+------------------------------------------------------------------+
//|                                                      Stopper.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 MaxDailyLoss = 2500;
extern double MaxPermittedLoss = 10000;
extern double Close_all_when_reach_percent = 98;

double Pft, max_loss_result, max_daily_loss_result, max_permitted_loss_result, today_permitted_loss_result, total_profit, today_profit;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---

//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
today_permitted_loss_result = MaxDailyLoss + DailyProfit();
max_permitted_loss_result = MaxPermittedLoss + TotalProfit();


if( (today_permitted_loss_result <= MaxDailyLoss*(100 - Close_all_when_reach_percent)/100) || (max_permitted_loss_result <= MaxPermittedLoss*(100 - Close_all_when_reach_percent)/100) )
{
         CloseAllPos(1); //Close Buys
         CloseAllPos(0); //Close Sells
}
//---
//--- --- Comments --- ---\\
Comment("LET's PROFIT!!! " "\n"
"Today's Permitted Loss: ", today_permitted_loss_result, "\n"
"Max Permitted Loss: ", max_permitted_loss_result, "\n"
"Time CST: ", CurTime() - 3600);

  }
//+------------------------------------------------------------------+

//+------------------------------------------------------------------+
//--- --- Close positions
//+------------------------------------------------------------------+
int CloseAllPos(int type)
  {
   int buy=1; int sell=1;
   int i,b=0;

   if(type==1)
     {
      while(buy==1)
        {
         buy=0;
         for(i=0;i<OrdersTotal();i++)
           {
            if( (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true))
              {
               if(OrderType()==OP_BUY)
                 {buy=1;if(OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet)){};}
                 }else{buy=0;
              }
           }
         if(buy==0){return(0);}
        }
     }
   if(type==0)
     {
      while(sell==1)
        {
         sell=0;
         for(i=0;i<OrdersTotal();i++)
           {
            if( (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==true))
              {
               if(OrderType()==OP_SELL)
                 {sell=1;if(OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet)){};}
                 }else{sell=0;
              }
           }
         if(sell==0){return(0);}
        }
     }
   return(0);
  }
  
//+------------------------------------------------------------------+
//--- --- Total Closed Profit.
//+------------------------------------------------------------------+           
double TotalClosedProfit()
  {
   Pft=0;
   int HT=HistoryTotal();
  
   for (int cnt=0 ; cnt<HT ; cnt++)
   {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY)==true)
         if( (OrderType()==OP_BUY || OrderType()==OP_SELL) )
         {
            Pft = Pft + (OrderProfit() + OrderCommission() + OrderSwap());
         }
   }
   return(Pft);
}

//+------------------------------------------------------------------+
//--- --- Total Openned Profit.
//+------------------------------------------------------------------+           
double TotalOpennedProfit()
  {
   Pft=0;
   int OT=OrdersTotal();
  
   for (int cnt=0 ; cnt<OT ; cnt++)
   {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
         if( (OrderType()==OP_BUY || OrderType()==OP_SELL) )
         {
            Pft = Pft + (OrderProfit() + OrderCommission() + OrderSwap());
         }
   }
   return(Pft);
}

//+------------------------------------------------------------------+
//--- --- Total Profit.
//+------------------------------------------------------------------+           
double TotalProfit()
  {
   return ( TotalClosedProfit() + TotalOpennedProfit() );
  }


//+------------------------------------------------------------------+
//--- --- Daily Closed Profit.
//+------------------------------------------------------------------+   
double DailyClosedProfit()
  {
   Pft=0;
   int HT=HistoryTotal();
   datetime CST = CurTime() - 3600;
  
   for (int cnt=0 ; cnt<HT ; cnt++)
   {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY)==true)
         if( (OrderType()==OP_BUY || OrderType()==OP_SELL) 
         && (TimeYear(OrderCloseTime()) == Year())
         && (TimeMonth(OrderCloseTime()) == Month())
         && (TimeDay(OrderCloseTime()) == TimeDay(CST))         
         )
         {
            Pft = Pft + (OrderProfit() + OrderCommission() + OrderSwap());
         }
   }
   return(Pft);
}

//+------------------------------------------------------------------+
//--- --- Daily Openned Profit.
//+------------------------------------------------------------------+   
double DailyOpennedProfit()
  {
   Pft=0;
   int OT=OrdersTotal();
  
   for (int cnt=0 ; cnt<OT ; cnt++)
   {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES)==true)
         if( (OrderType()==OP_BUY || OrderType()==OP_SELL) )
         {
            Pft = Pft + (OrderProfit() + OrderCommission() + OrderSwap());
         }
   }
   return(Pft);
}

//+------------------------------------------------------------------+
//--- --- Daily Profit.
//+------------------------------------------------------------------+   
double DailyProfit()
  {
   return ( DailyClosedProfit() + DailyOpennedProfit() );
  }

I created this EA to close all orders after a given daily DD or total DD. Everything I know about programming I learned only here on this forum. So it is possible that this EA is not well written. I'm finding it too slow. It seems that looking at history at each tick, when you have many many orders, consumes a lot of computational resources. Any ideas to resolve this? I will be very grateful if someone can improve the code to make it faster and more efficient and publish it here! Thank you!
 
  1. Paulo Martins Barbosa Junior: It seems that looking at history at each tick, when you have many many orders, consumes a lot of computational resources. Any ideas to resolve this?

    Stop doing that. If history count hasn't changed, there is nothing to do.

  2.         for(i=0;i<OrdersTotal();i++)
               if( (OrderSelect(i,SELECT_BY_POS,MODE_TRADES)
               && OrderType()==OP_SELL){
                  if(OrderClose( … @Paulo Martins Barbosa Junior 
    In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
    2. For In First Out (FIFO rules — US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1 № 11 ACCOUNT_FIFO_CLOSE

    3. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

  3. if(OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY)==true)

    You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.

 
William Roeder:
  1. Stop doing that. If history count hasn't changed, there is nothing to do.

  2. In the presence of multiple orders (one EA multiple charts, multiple EAs, manual trading,) while you are waiting for the current operation (closing, deleting, modifying) to complete, any number of other operations on other orders could have concurrently happened and changed the position indexing:
    1. For non-FIFO (non-US brokers), (or the EA only opens one order per symbol,) you can simply count down, in a position loop, and you won't miss orders. Get in the habit of always counting down.
                Loops and Closing or Deleting Orders - MQL4 programming forum
    2. For In First Out (FIFO rules — US brokers,) and you (potentially) process multiple orders per symbol, you must find the earliest order (count up,) close it, and on a successful operation, reprocess all positions.
                CloseOrders by FIFO Rules - Strategy Tester - MQL4 programming forum - Page 2 #16
                MetaTrader 5 platform beta build 2155: MQL5 scope, global Strategy Tester and built-in Virtual Hosting updates - Best Expert Advisors - General - MQL5 programming forum #1 № 11 ACCOUNT_FIFO_CLOSE

    3. and check OrderSelect in case earlier positions were deleted.
                What are Function return values ? How do I use them ? - MQL4 programming forum
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles
    4. and if you (potentially) process multiple orders, must call RefreshRates() after server calls if you want to use, on the next order / server call, the Predefined Variables (Bid/Ask.) Or instead, be direction independent and just use OrderClosePrice().

  3. You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don't write if(bool == true), just use if(bool) or if(!bool). Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and "if long entry" is an incomplete sentence.


Thank you! your tips were very useful! :)
 
//+------------------------------------------------------------------+
//|                                                      Stopper.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   "2.00"
#property strict

extern double MaxDailyLoss = 2500;
extern double MaxPermittedLoss = 5000;
extern double Close_all_when_reach_percent = 98;

double profit, max_permitted_loss_result, today_permitted_loss_result, closed_profit_total, closed_profit_daily;
int history_count;

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
history_count = HistoryTotal();
closed_profit_total = ClosedProfitTotal();
closed_profit_daily = ClosedProfitDaily();        

//---
   return(INIT_SUCCEEDED);
  }
  
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
  
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   if(HistoryTotal() > history_count)
   {
      history_count = HistoryTotal();
      closed_profit_total = ClosedProfitTotal();
      closed_profit_daily = ClosedProfitDaily();
   }
  
   today_permitted_loss_result = MaxDailyLoss + OpennedProfitDaily() + closed_profit_daily;
   max_permitted_loss_result = MaxPermittedLoss + OpennedProfitTotal() + closed_profit_total;

   
   if( OrdersTotal() > 0 
   && ( (today_permitted_loss_result <= MaxDailyLoss*(100 - Close_all_when_reach_percent)/100) 
   || (max_permitted_loss_result <= MaxPermittedLoss*(100 - Close_all_when_reach_percent)/100)) )
   {
   CloseAllPos();
   Print("You reached the loss limit and was stopped out!");
   } //Close Buys/Sells and Delete Pending orders

//--- --- Comments --- ---\\
Comment(
"Today's Permitted Loss: ",NormalizeDouble(today_permitted_loss_result,2), "\n"
"Max Permitted Loss: ", NormalizeDouble(max_permitted_loss_result,2));

} // end tick function

//+------------------------------------------------------------------+
//--- --- Close positions                                                              |
//+------------------------------------------------------------------+
void CloseAllPos()
  {
   int slippage=0;
   int max_attempts=14;
   double pause_if_trade_buzy=0.2;
   double order_profit;
   bool ticket;

  for(int i=OrdersTotal()-1; i>=0; i--)
     {
      if(OrderSelect(i,SELECT_BY_POS))
        {
               ticket=false;
               order_profit=OrderProfit()+OrderSwap()+OrderCommission();
               for(int cnt=0; cnt < max_attempts; cnt++)
                 {
                  while(IsTradeContextBusy()) Sleep(int(pause_if_trade_buzy*1000));
                  RefreshRates();
                  //---
                  if(OrderType()==OP_BUY) 
                  ticket=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_BID),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,clrNONE);
                  //---
                  else
                     if(OrderType()==OP_SELL) 
                     ticket=OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(MarketInfo(OrderSymbol(),MODE_ASK),int(MarketInfo(OrderSymbol(),MODE_DIGITS))),slippage,clrNONE);
                  //---
                  else
                     if((OrderType()==OP_SELLSTOP || OrderType()==OP_BUYSTOP || OrderType()==OP_SELLLIMIT || OrderType()==OP_BUYLIMIT)) 
                     ticket=OrderDelete(OrderTicket(),clrNONE);
                  //--- no closing
                  else
                     ticket=true;

                  if(ticket)break;
                 }//end for2
        }//end order select
     }//end for1
  }//end close_all function
  
//+------------------------------------------------------------------+
//--- --- Total Closed Profit.
//+------------------------------------------------------------------+           
double ClosedProfitTotal()
 {
   profit=0;
   int HT=HistoryTotal();
  
   for (int cnt=0 ; cnt<HT ; cnt++)
   {
      if( OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY) )
         if( (OrderType()==OP_BUY || OrderType()==OP_SELL) )
         {
            profit = profit + (OrderProfit() + OrderCommission() + OrderSwap());
         }
   }
   return(profit);
}

//+------------------------------------------------------------------+
//--- --- Total Openned Profit.
//+------------------------------------------------------------------+           
double OpennedProfitTotal()
  {
   profit=0;
   int OT=OrdersTotal();
  
   for (int cnt=0 ; cnt<OT ; cnt++)
   {
      if( OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) )
         if( (OrderType()==OP_BUY || OrderType()==OP_SELL) )
         {
            profit = profit + (OrderProfit() + OrderCommission() + OrderSwap());
         }
   }
   return(profit);
}

//+------------------------------------------------------------------+
//--- --- Daily Closed Profit.
//+------------------------------------------------------------------+   
double ClosedProfitDaily()
  {
   profit=0;
   int HT=HistoryTotal();
   datetime CST = CurTime() - 3600;
  
   for (int cnt=0 ; cnt<HT ; cnt++)
   {
      if( OrderSelect(cnt,SELECT_BY_POS,MODE_HISTORY) )
         if( (OrderType()==OP_BUY || OrderType()==OP_SELL) 
         && (TimeYear(OrderCloseTime()) == Year())
         && (TimeMonth(OrderCloseTime()) == Month())
         && (TimeDay(OrderCloseTime()) == TimeDay(CST))         
         )
         {
            profit = profit + (OrderProfit() + OrderCommission() + OrderSwap());
         }
   }
   return(profit);
}

//+------------------------------------------------------------------+
//--- --- Daily Openned Profit.
//+------------------------------------------------------------------+   
double OpennedProfitDaily()
  {
   profit=0;
   int OT=OrdersTotal();
  
   for (int cnt=0 ; cnt<OT ; cnt++)
   {
      if(OrderSelect(cnt,SELECT_BY_POS,MODE_TRADES) )
         if( (OrderType()==OP_BUY || OrderType()==OP_SELL) )
         {
            profit = profit + (OrderProfit() + OrderCommission() + OrderSwap());
         }
   }
   return(profit);
}

//+------------------------------------------------------------------+
//--- --- The Glourious End --- ---
//+------------------------------------------------------------------+ 

  






I hope it's a little better now. I believe I solved the issue of unnecessary checks on the history. I rewrote the entire function of closing all orders. I tried to improve the name of some functions and variables. Thanks!