EA is working perfectly on Strategy Tester(Every tick based on real ticks) but does not work properly while using it on a demo account.

 

My EA is programmed to open a trade and close the trades in exactly 3 minutes time. The EA works perfectly on the strategy tester(Every tick based on real ticks) however it didn't work while testing it on demo, it keeps closing the trades after about a minute. I tried removing the iBars , but the EA would just go on to open multiple trades, instead of the supposed amount. The code is attached below. I'd really appreciate if anyone can help. Thank you!!!

#include <Trade/Trade.mqh>
CTrade trade;

//---external adjustable variable
input int numberOfPositions = 3;
input double lotSize = 0.20;
input int takeProfit = 1000000;
input int stopLoss = 10000000;

int handle;
int barsTotal;

int OnInit(){

   handle = iCustom(_Symbol,PERIOD_CURRENT,"Market//Boom and crash smasher",8,1);
  

   return(INIT_SUCCEEDED);
}



void OnTick(){
  
  
      //--- Ask and Balance
       double Ask = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_ASK),_Digits);
       double Bid = NormalizeDouble(SymbolInfoDouble(_Symbol, SYMBOL_BID),_Digits);
       double Balance = AccountInfoDouble(ACCOUNT_BALANCE);
       double Equity = AccountInfoDouble(ACCOUNT_EQUITY);
  
  int bars = iBars(_Symbol,PERIOD_CURRENT);
  if(barsTotal !=bars){
  barsTotal = bars;
   double arrSellSignals[];  
   CopyBuffer(handle,0,1,1,arrSellSignals);
      checkCloseTimer();

   
   if(arrSellSignals[0] > 0){
      if(PositionsTotal()<numberOfPositions)
      {
           trade.Sell(lotSize,NULL,Bid,(Bid+(stopLoss)* _Point),(Bid-(takeProfit) * _Point),NULL);
      }
          
      
   }
  }
}
void checkCloseTimer(){

      //---count down all open position
      for(int i = PositionsTotal()-1; i>=0; i--){          
            //---Get the current position ticket
            ulong ticket = PositionGetTicket(i);
            //---Get the Position opening time
            long  positionOpenTime=PositionGetInteger(POSITION_TIME);
            //---get the local time
            long localTime = TimeLocal();
            //---calculate the difference
            long difference = localTime - positionOpenTime;
               
               Comment("\nLocal Time: ",localTime,
                 "\nPosition Open Time: ",positionOpenTime );
               
      if(difference > 180){
         Print("Trade closed due to time limit!!!!!!!!!!!!!!!!!!!!!!!!");
            trade.PositionClose(ticket);
      }      
      
      }
}
 
Seanny234:

My EA is programmed to open a trade and close the trades in exactly 3 minutes time. The EA works perfectly on the strategy tester(Every tick based on real ticks) however it didn't work while testing it on demo, it keeps closing the trades after about a minute. I tried removing the iBars , but the EA would just go on to open multiple trades, instead of the supposed amount. The code is attached below. I'd really appreciate if anyone can help. Thank you!!!

PositionOpenTime and TimeLocal are not in the same time zone.

You should use TimeCurrent instead of TimeLocal.

 
Thank you so much Dominik, I will try that now and give you feedback. 
 
Thank you so much Dominik, it's working perfectly!!!
Reason: