1 trade per candle, up to a max number of trades?

 

Hi,

I am new to programming in MQL and have had a good look on the site to try and answer this question but to no avail.  I only want to enter one trade per candle but if, on the next candle, the signal still holds the EA should make another trade up to a maximum number of lots.  I think I understand the first part of my problem but don't seem to be able to get the second part to work.  Any help much appreciated or suggestion on where to look.  Thanks  Here is simple code:

 

 extern double lotsize = 0.01;

  

   extern double maxlots = 0.03;

  

   extern int MacdFastEma = 12;

   

   extern int MacdSlowEma = 26;



int OnInit()

  {

//---

    

  return(INIT_SUCCEEDED);

  }

   

//---

   

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   

  }

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

//| Expert tick function                                             |

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

void OnTick()

{

  static datetime candletime;

  if(candletime != Time[0]) 

 {

   CheckForSignal(); 



  candletime=Time[0];

   }

}



void CheckForSignal()



{

   double atrh = iATR(NULL,60,120,0);



   double spread_broker = MarketInfo(Symbol(),MODE_SPREAD);

  

   double spread = spread_broker/100000;

   

   double stoploss = atrh;

  

   double takeprofit = 2*atrh;

   

   double EntryPrice = Close[2];

   

   double CurrentMacd = iMACD(Symbol(),0,MacdFastEma,MacdSlowEma,9,PRICE_CLOSE,MODE_MAIN,1);

   double PreviousMacd = iMACD(Symbol(),0,MacdFastEma,MacdSlowEma,9,PRICE_CLOSE,MODE_MAIN,2);

   

   if(OrdersTotal()==0)  //This line works as expected

   //if(OrdersTotal()<=maxlots)  // This line works as if only 0.01 lot can ever be held?

      if(Low[1]<Close[2] && CurrentMacd<PreviousMacd)

         OrderSend(Symbol(),OP_BUYLIMIT,lotsize,EntryPrice,2,EntryPrice-stoploss,EntryPrice+takeprofit,NULL,0,TimeCurrent()+3*60*60,Green);

      

      else if(High[1]>Close[2] && CurrentMacd>PreviousMacd)

         OrderSend(Symbol(),OP_SELLLIMIT,lotsize,EntryPrice,2,EntryPrice+stoploss,EntryPrice-takeprofit,NULL,0,TimeCurrent()+3*60*60,Red);

   

      else MessageBox("NO SIGNAL","Info"); 

   

   }
Reason: