My EA opens too many trades

 

I'm new to coding so please don't blast my code xD


But basically how my EA works is when 2 trend lines cross is makes a sell/buy trade (depending on which way) but because there is obviously some time when the lines are touching, my EA goes nuts and opens and closes like 100 trades per second, I basically just want help adding in like a buffer where my EA will wait for the next bar before altering/opening/closing new trades.


#define maxBuyOrders 1

#define maxSellOrders 1

void OnTick()

{

   double SlowMovingAverage = iMA(NULL, 0,20,0,MODE_SMA,PRICE_CLOSE,0);

   double SlowMovingAverage1 = iMA(NULL, 0,20,0,MODE_SMA,PRICE_CLOSE,1);

   

   double FastMovingAverage = iMA(NULL, 0,10,0,MODE_SMA,PRICE_CLOSE,0);

   double FastMovingAverage1 = iMA(NULL, 0,10,0,MODE_SMA,PRICE_CLOSE,1);

   

   double minstoplevel=MarketInfo(Symbol(),MODE_STOPLEVEL); 

   Print("Minimum SL / TP Level = ",minstoplevel," points");

   double price=Ask;

  

   if (AccountBalance() > 0) {

   

         if ((FastMovingAverage1 < SlowMovingAverage1) && (FastMovingAverage > SlowMovingAverage)) {

            Comment ("BUY");

            

            closeAllSell();

            

            if(countBuyPositions() < maxBuyOrders){

          

               double BUYSL=(Bid-10000*Point); 

               double BUYTP=(Bid+10000*Point);

               int ticketBuy = OrderSend(Symbol(),OP_BUY,0.1,price,3,BUYSL,BUYTP,"AutoOrder",0,0,clrGreen);

               

               if(ticketBuy<0) { 

                  Print("Buy Order failed with error #",GetLastError()); 

               } 

               else {

                  Print("Buy Order placed successfully");

                  countBuyPositions();

                  

               }}

            

         }

       

         else if ((FastMovingAverage1 > SlowMovingAverage1) && (FastMovingAverage < SlowMovingAverage))

            Comment ("SELL");

            

            closeAllBuy();

            

            if(countSellPositions() < maxSellOrders){

           

               double SELLSL=(Bid+10000*Point); 

               double SELLTP=(Bid-10000*Point);

               int ticketSell = OrderSend(Symbol(),OP_SELL,0.1,Bid,3,SELLSL,SELLTP,"AutoOrder",0,0,clrRed);

                 

               if(ticketSell<0) { 

                  Print("Sell Order failed with error #",GetLastError()); 

               } 

               else {

                  Print("Sell Order placed successfully");

                  countSellPositions();

               }}

            

     }

     Sleep(999999999999);

}



double closeAllBuy()

{

   for (int i = OrdersTotal();i >= 0; i--) {

   

      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

      

         if (OrderSymbol() == Symbol())

         

            if (OrderType()==OP_BUY)

            { 

               OrderClose(OrderTicket(),OrderLots(),Bid,3,NULL);

            }

   }

}



double closeAllSell()

{

   for (int i = OrdersTotal();i >= 0; i--) {

   

      if (OrderSelect(i,SELECT_BY_POS,MODE_TRADES))

      

         if (OrderSymbol() == Symbol())

            

            if (OrderType()==OP_SELL)

            { 

               OrderClose(OrderTicket(),OrderLots(),Ask,3,NULL);

            }

   }

}



int countBuyPositions()

{

   int numberOfBuyPositions = 0;

   for(int i = OrdersTotal()-1; i >= 0 ; i--) {

      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      string currencyPair = OrderSymbol();

      if (_Symbol == currencyPair)

         if(OrderType()==OP_BUY)

         {

            numberOfBuyPositions = numberOfBuyPositions +1;

         }

   }

   return numberOfBuyPositions;

}



int countSellPositions()

{

   int numberOfSellPositions = 0;

   for(int i = OrdersTotal()-1; i >= 0 ; i--) {

      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);

      string currencyPair = OrderSymbol();

      if (_Symbol == currencyPair)

         if(OrderType()==OP_SELL)

         {

            numberOfSellPositions = numberOfSellPositions +1;

         }

   }

   return numberOfSellPositions;

}

Thanks!!

Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Orders in DOM
  • www.mql5.com
For equity securities, the Depth of Market window is available, where you can see the current Buy and Sell orders. Desired direction of a trade operation, required amount and requested price are specified for each order. To obtain information...
 

Please edit your post and

use the code button (Alt+S) when pasting code

 
Harry Ward: when the lines are touching, my EA goes nuts and opens and closes like 100 trades per second,
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12

 
William Roeder:
  1. Please edit your (original) post and use the CODE button (Alt-S)! (For large amounts of code, attach it.)
              General rules and best pratices of the Forum. - General - MQL5 programming forum 2019.05.06
              Messages Editor

  2. You are looking at a signal. Act on a change of signal.
              MQL4 (in Strategy Tester) - double testing of entry conditions - MQL5 programming forum #1 2017.12.12


Thanks! I'll be sure to look at the Forum you linked, and sorry about the coding, this was my first post

 
Keith Watford:

Please edit your post and

use the code button (Alt+S) when pasting code

Done! sorry about that
Reason: