Order after a certain amount of time

 
Soon after the loss occurs, the order will go further and further losses will occur.

After the loss, I want to place an interval between the following order

Can you tell me where the problem is?

Even if I use it, it does not make any difference when I are not using it.


//+------------------------------------------------------------------+
//|                                                       Module.mq4 |
//|                        Copyright 2017, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2017, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict

extern int MagicNo = 1234;  
extern double Lots = 1.0;   
extern int ShortMA = 30;   
extern int LongMA = 120;   
extern double TakeProfit = 50; 
extern double StopLoss = 50;




//+------------------------------------------------------------------+
//|  variable of take a break after losing the last transaction.                                                    |
//+------------------------------------------------------------------+
extern bool LastOrderCon = False;
extern int Pause_time = 12;
//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
int start()
  {
   double SMACur,SMAPre,LMACur,LMAPre;   
   int i, ticket, total;    
   bool ticketM;  

   SMACur=iMA(NULL,0,ShortMA,0,MODE_SMA,PRICE_OPEN,0);   
   SMAPre=iMA(NULL,0,ShortMA,0,MODE_SMA,PRICE_OPEN,1);   
   LMACur=iMA(NULL,0,LongMA,0,MODE_SMA,PRICE_OPEN,0);   
   LMAPre=iMA(NULL,0,LongMA,0,MODE_SMA,PRICE_OPEN,1);   
   




   total=OrdersTotal();  
   if(total<1)   
     {
         if(SMACur > LMACur && SMAPre < LMAPre && LastOrder()==True )  
           {
            ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,10,0,0,"MA",MagicNo,0,Blue);              
            return(0); 
           }
         if(SMACur < LMACur && SMAPre > LMAPre && LastOrder()==True )  
           {
            ticket=OrderSend(Symbol(),OP_SELL,Lots,Bid,10,0,0,"MA",MagicNo,0,Red);       
            return(0); 
           }
      return(0);
     }


   
return(0);
  }

//+------------------------------------------------------------------+



//+------------------------------------------------------------------+
//| Last time loss of last transaction benefits check module         | 
//+------------------------------------------------------------------+
bool LastOrder()      //LastOrder_Lose_inter
{
   for(int i = OrdersHistoryTotal()-1; i>= 0 ; i--){
   if(!OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))
   continue;
      if(OrderProfit()<0){
       if(TimeCurrent() < OrderCloseTime()+Pause_time*60*60 ){
         LastOrderCon =False;
         }
       if(TimeCurrent() >= OrderCloseTime()+Pause_time*60*60){
         LastOrderCon =True;
         }
         }  
      if(OrderProfit()>0){
         LastOrderCon =True;
         }
         
   }

return(LastOrderCon);
}


 
  1. Don't double post          General rules and best pratices of the Forum. - General - MQL5 programming forum

  2.  total=OrdersTotal();  
       if(total<1)   
    Using OrdersTotal directly and/or no Magic number filtering on your OrderSelect loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 and MetaTrader 4 - MQL4 programming forum

  3.             ticket = OrderSend(Symbol(),OP_BUY,Lots,Ask,10,0,0,"MA",MagicNo,0,Blue);              
                return(0); 
    Check your return codes for errors and report them.
              What are Function return values ? How do I use them ? - MQL4 and MetaTrader 4 - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

  4.    for(int i = OrdersHistoryTotal()-1; i>= 0 ; i--){
       if(!OrderSelect(i, SELECT_BY_POS,MODE_HISTORY))   continue;
    #2 and Do not assume history is ordered by date, it's not.
              Could EA Really Live By Order_History Alone? (ubzen) - MQL4 and MetaTrader 4 - MQL4 programming forum
              Count how many lost orders from the last profit order - MQL4 and MetaTrader 4 - MQL4 programming forum

  5.    SMACur=iMA(NULL,0,ShortMA,0,MODE_SMA,PRICE_OPEN,0);   
       SMAPre=iMA(NULL,0,ShortMA,0,MODE_SMA,PRICE_OPEN,1);
    Are you sure you want to use bar zero? You will intermittently get vanishing crosses.

  6.        if(TimeCurrent() < OrderCloseTime()+Pause_time*60*60 ){
             LastOrderCon =False;
             }
           if(TimeCurrent() >= OrderCloseTime()+Pause_time*60*60){
             LastOrderCon =True;
             }
             }  
          if(OrderProfit()>0){
             LastOrderCon =True;
             }
    Simplify your code. Increase Order after stoploss - MQL4 and MetaTrader 4 - MQL4 programming forum
Reason: