Exit Criteria randomly not working on Live Trade

 

I create an EA that works well on strategy tester, but when I tried on Live account, randomly my EA not closing the trade.

My EA open a correct position at 10 March 12:50 and it should be closed somewhere around 13:46

Picture 2 is my EA when running on Tester.

Picture 1 is my live account.

Is there any clue why the thing such this happen?

I try to check my own code but seems everything works well.

Files:
1.jpeg  188 kb
2.jpeg  214 kb
 
Oscar Utomo: I try to check my own code but seems everything works well.

Obviously not or you wouldn't be posting.

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button).
     How To Ask Questions The Smart Way. 2004
          Be precise and informative about your problem

We can't see your broken code.

Fix your broken code.

With the information you've provided — we can only guess. And you haven't provided any useful information for that.

 
William Roeder:

Obviously not or you wouldn't be posting.

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button).
     How To Ask Questions The Smart Way. 2004
          Be precise and informative about your problem

We can't see your broken code.

Fix your broken code.

With the information you've provided — we can only guess. And you haven't provided any useful information for that.

Oh.... i'm new to this forum. Ok, here my exit criteria code:
double ma1 = iMA(NULL,0,MA1,0,MAmethod1,MAprice1,_bar);
double ma2 = iMA(NULL,0,MA2,0,MAmethod2,MAprice2,_bar);
   double ma3 = iMA(NULL,0,MA3,0,MAmethod3,MAprice3,_bar);
   double c=Close[_bar];
if(buys > 0){
         if(mode == 1){  
            bool valid = true;
            string trade = "";
               if(c>= ma1 && ma1 >= ma2 && ma2 >= ma3){
                  valid = true;
                  trade = "buy";
               }else if(c<= ma1 && ma1 <= ma2 && ma2 <= ma3){
                  valid = true;
                  trade = "sell";
               }else{
                  valid = false;
               }
            
            if(valid){  
               if(trade == "sell"){                  
                  return(-1);
               }
            }
         }
   };
   if(sells > 0){            
         if(mode == 1){  
            bool valid = true;
            string trade = "";
               if(c>= ma1 && ma1 >= ma2 && ma2 >= ma3){
               
                  Print("c:" + c + ";ma1:" + ma1 + ";ma2:" + ma2 + ";ma3:" + ma3);
                  valid = true;
                  trade = "buy";
               }else if(c<= ma1 && ma1 <= ma2 && ma2 <= ma3){
                  valid = true;
                  trade = "sell";
               }else{
                  valid = false;
               }
               
            
            if(valid){  
               if(trade == "buy"){                  
                  return(1);
               }
            }
         }
   };
   
And here my Start() code
int start()
  {
//----
   if(IsNewBar() && !InitRun)
     {
      int ticket=0; int i;

      int buys=CalcTrades(MagicNumber,OP_BUY);
      int sells=CalcTrades(MagicNumber,OP_SELL);

      int entry=EntrySignal(1);
      
      bool BuySignal=entry>0 ;
      bool SellSignal=entry<0 ;
      
      i=0;
      int exit = ExitSignal(1);
       while(exit>0 && sells>0 && i<RUNS)
        {
         if(i>0) Sleep(SLEEP);
         CloseOrders(MagicNumber,OP_SELL);
         sells=CalcTrades(MagicNumber,OP_SELL);
         i++;
        }
      while(exit<0 && buys>0 && i<RUNS)
        {
         if(i>0) Sleep(SLEEP);
         CloseOrders(MagicNumber,OP_BUY);
         buys=CalcTrades(MagicNumber,OP_BUY);
         i++;
        }
           
      i=0;
      while(BuySignal && ticket<=0 && i<RUNS && (buys==0 && sells==0) && flagTrend == 0 && IsTradeAllowed())
        {
         flagTrend = 1;
         ticket=BuyMkt(MagicNumber,Lot,Comm);
         if(ticket<=0){Sleep(SLEEP); i++;}
        }
      while(SellSignal && ticket<=0 && i<RUNS && (buys==0 && sells==0) && flagTrend == 0 && IsTradeAllowed())
        {
         flagTrend = -1;
         ticket=SellMkt(MagicNumber,Lot,Comm);
         if(ticket<=0){Sleep(SLEEP); i++;}
        }

      SLTP(MagicNumber,StopLoss,TakeProfit);
     }
   InitRun=false;
//----
   return(0);
  }



 
  1. Don't copy and paste code — write it once. (Are they the same? Why or why not?) Here is your “exit criteria code” simplified:

    double ma1 = iMA(NULL,0,MA1,0,MAmethod1,MAprice1,_bar);
    double ma2 = iMA(NULL,0,MA2,0,MAmethod2,MAprice2,_bar);
    double ma3 = iMA(NULL,0,MA3,0,MAmethod3,MAprice3,_bar);
    double c=Close[_bar];
       if(mode == 1){
                bool valid = true;
                string trade = "";
                   if(c>= ma1 && ma1 >= ma2 && ma2 >= ma3){
                      valid = true;
                      trade = "buy";
                   }else if(c<= ma1 && ma1 <= ma2 && ma2 <= ma3){
                      valid = true;
                      trade = "sell";
                   }else{
                      valid = false;
                   }
       if(valid){  
          if(buys > 0 && trade == "sell"){                  
             return(-1);
          };
          if(sells > 0 && trade == "buy"){                  
             return(1);
          };
       }
    }
    What are you returning here?
    
  2. If mode is not one, your code does nothing.

  3. Otherwise your code does whatever you don't show. There are no mind readers here. You asked “Is there any clue why the thing such this happen?” But don't show the code that does anything.

  4. if(i>0) Sleep(SLEEP);

    After Sleep and between server calls the market will have changed. You must update your Predefined Variables or series arrays before using any of them — you must call RefreshRates.

  5.          if(ticket<=0){Sleep(SLEEP); i++;}

    № 4. Nothing has changed, infinite loop.

  6. Oscar Utomo: And here my Start() code

    You should stop using the old event handlers and IndicatorCounted() and start using new event handlers.
              Event Handling Functions - MQL4 Reference
              How to do your lookbacks correctly - MQL4 programming forum #9-14 & #19 2016.05.11

 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.