Problem with orders not appearing

 

Hello,

I'm fairly new to coding and I've run into a problem that I don't know how to fix. I'm trying to make a EA that trades using the Bollinger Bands indicator to then maybe use in a more sophisticated EA later. However during my testing I've noticed that it is not placing any orders. Although it says in the trading history that a total of 4562 orders have been filled, when looking at the operations it says that all of these orders are still pending?


Here the current code:

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

input int BandPeriod                         =  20;
input double BandDeviation                   =  2.0;
ENUM_APPLIED_PRICE BandAppliedPrice          =  PRICE_CLOSE;

input double TPDeviation   =  1.0;
input double SLDeviation   =  1.0;

int OnInit() {

   return(INIT_SUCCEEDED);
}

void OnDeinit(const int reason){ 

   
}

void OnTick() {

   if(!IsNewBar())   return;                                //! = not
   
   double close1  =  iClose(Symbol(),Period(),1);
   double high1   =  iHigh(Symbol(),Period(),1);
   double low1    =  iLow(Symbol(),Period(),1);
   double close2  =  iClose(Symbol(),Period(),2);
   
   double upperArr[2];
   double lowerArr[2];


   CopyBuffer(iBands(Symbol(),Period(),BandPeriod,0,BandDeviation,BandAppliedPrice),1,0,2,upperArr);   
   CopyBuffer(iBands(Symbol(),Period(),BandPeriod,0,BandDeviation,BandAppliedPrice),2,0,2,lowerArr);
   
   double upper1 =  upperArr[0];
   double lower1 =  lowerArr[0];
   double upper2 =  upperArr[1];
   double lower2 =  lowerArr[1];
   
   Comment(upper1,"\n",lower1,"\n",upper2,"\n",lower2);
   
   if(PositionsTotal()==0){
   if (close2>upper2 && close1<upper1) {
      trade.SellStop(0.01,low1,Symbol(),low1+0.001,low1-0.001,0,5,"");
   }
   else if (close2<lower2 && close1>lower1) {
      trade.BuyLimit(0.01,high1,Symbol(),high1-0.001,high1+0.001,0,5,"");
   }
}
}

bool IsNewBar() {

   datetime currentTime       =  iTime(Symbol(),Period(),0);
   
   static datetime prevTime   =  currentTime;
   
   if (prevTime<currentTime) {
      prevTime = currentTime;
      return(true);
   }
   
   return(false);
   
}

Maybe someone with more knowledge about this can help me out and please excuse if it's something very trivial.

 

1. Read and study help

2. The MQL5 style implies that the indicator handle must be created ONCE and must be done in OnInit

3. It is imperative to check the RESULT of the 'CopyBuffer' function