Reset/break code after an if statement to limit the amount of open positions within a buy/sell zone

 

Hi everyone

I've just got a little stuck trying to run this code contiguously. I only want to open  3 positions when in a buy or sell zone but because I'm using HistoryDealsTotal it only works the first time!

I'm pretty new to MQL5 and guessing I might need to use a loop but am getting really stuck and would really appreciate some help!!!

Thanks in advance

Will

//+------------------------------------------------------------------+
//|                   BUY TRADE CONDITIONS                           |
//+------------------------------------------------------------------+
 
            
     if(iMA1Buffer[0]>=iMA2Buffer[0])
     {
     if((HistorySelect(0,TimeCurrent())) &&
        (iQMPBuffer[0]!=EMPTY_VALUE)     && 
        (PositionsTotal()==0)            &&
        (HistoryDealsTotal()<7))
        
     {
     trade.Buy(1.0,NULL,Ask,SL_buy,TP_buy,NULL);
     }
     }

  
//+------------------------------------------------------------------+
//|                   SELL TRADE CONDITIONS                          |
//+------------------------------------------------------------------+   
   
   
   if(iMA1Buffer[0]<=iMA2Buffer[0])
   {
   if((HistorySelect(0,TimeCurrent())) &&
      (iQMPBuffer[1]!=EMPTY_VALUE)     && 
      (PositionsTotal()==0)            &&
      (HistoryDealsTotal()<7))
    
   {
   trade.Sell(1.0,NULL,Bid,SL_Sell,TP_sell,"Sell Order");
   }
   }
Documentation on MQL5: Trade Functions / HistorySelect
Documentation on MQL5: Trade Functions / HistorySelect
  • www.mql5.com
HistorySelect() creates a list of orders and a list of trades in a mql5-program, for further referring to the list elements using corresponding functions. The deals list size can be returned using the HistoryDealsTotal() function; the size of the list of orders in the history can be obtained using HistoryOrdersTotal(). Selection in the list of...
 
Please edit your post and
use the code button (Alt+S) when pasting code
 

Sorry about that. I'm just looking for a way to code the EA to open 3 positions when in each zone, then when price enter the next zone, I wait it to reset its memory and start the process again. Using HistoryDealsTotal it remembering the first 3 positions forever. Not sure what else to use?! 

Thanks

Will