MQL5 EA opens multiple trade instead of One

 

Hello,

I coded an EA which runs perfectly but places multiple trades instead of one. 

Using the most currently opened order as reference point, the EA is meant to open 1 Buy order when price increases by 100 pips and open a Sell order when it decreases by 100 pips. However, if the most currently opened order is due to close by take profit (set at 100 pips) then EA allows it to close before opening new order in price direction and a Stop loss of zero is currently used. But instead of opening just One order, it opens multiple unending orders.

This code is pasted below: 

int OnInit()

  {  

  trade.SetExpertMagicNumber(MagicNumber);

  return(INIT_SUCCEEDED);  

  }

  

void OnDeinit()

  {

//---

  }



void OnTick()

  {

// Get the Ask and Bid price

double Ask=NormalizeDouble (SymbolInfoDouble (_Symbol,SYMBOL_ASK),_Digits);

double Bid=NormalizeDouble (SymbolInfoDouble (_Symbol,SYMBOL_BID),_Digits);



// Buy and Sell when there is no Open Order

if (PositionsTotal()==0)

trade.Sell (InpLot,NULL,Bid,(Bid+SL * _Point), (Bid-TP * _Point),NULL) &&

trade.Buy (InpLot,NULL,Ask,(Ask-SL * _Point),  (Ask+TP * _Point),NULL); 



for(int i=PositionsTotal()-1; i>=0; i--) 

   if(m_position.SelectByIndex(i+1)) // selects the position by index for further access to its properties

      {

         if(m_position.Magic()==MagicNumber)

           for(int k=0; k<50; k++)

              {

                 if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_SELL)

                     {    

                        if (m_position.PriceOpen() < m_position.PriceCurrent() - (k+1)*(100 * _Point))       

                           trade.Buy (InpLot,NULL,Ask,(Ask-SL * _Point),  (Ask+TP * _Point),NULL) ;                          

                              }

                                else

                                   {                        

                                      if (m_position.PriceOpen() > m_position.PriceCurrent() +(k+1)* (100 * _Point))       

                                         trade.Sell (InpLot,NULL,Bid,(Bid+SL * _Point), (Bid-TP * _Point),NULL);                         

                                            }

                                         if((ENUM_POSITION_TYPE)PositionGetInteger(POSITION_TYPE)==POSITION_TYPE_BUY)

                                     {                  

                                 if (m_position.PriceOpen() < m_position.PriceCurrent() -(k+1)* (100 * _Point))       

                             trade.Buy (InpLot,NULL,Ask,(Ask-SL * _Point),  (Ask+TP * _Point),NULL) ;

                         }

                     else

                 {                        

              if (m_position.PriceOpen() > m_position.PriceCurrent() + (k+1)*(100 * _Point))       

           trade.Sell (InpLot,NULL,Bid,(Bid+SL * _Point), (Bid-TP * _Point),NULL);                         

       }

     }

  }

}
 

Is your account a netting account or a hedging account? In case of a hedging account this code does not seem to close the open position, but it adds a second position, of opposite type. The original position remains open and continues to be beyond your target levels.

For example: if you opened a long position by buying trade.Buy(). This position reaches your profit target and you want to close it. In that case you should not use trade.Sell(), but trade.PositionClose().

 
WindmillMQL:

Is your account a netting account or a hedging account? In case of a hedging account this code does not seem to close the open position, but it adds a second position, of opposite type. The original position remains open and continues to be beyond your target levels.

For example: if you opened a long position by buying trade.Buy(). This position reaches your profit target and you want to close it. In that case you should not use trade.Sell(), but trade.PositionClose().

Thanks for your advice, additional knowledge has been imparted through your post.

 
if (m_position.PriceOpen() < m_position.PriceCurrent() - (k+1)*(100 * _Point))       

                           trade.Buy (InpLot,NULL,Ask,(Ask-SL * _Point),  (Ask+TP * _Point),NULL) ;    



there are many orders that may reach this condition,so your ea will open positions on every tick...
i think you should check the highest or lowest positon in this “if”
 
Hao Tan:
there are many orders that may reach this condition,so your ea will open positions on every tick...
i think you should check the highest or lowest positon in this “if”

Thanks, you are absolutely correct. The code currently continues to open orders without stopping (over 100 opened orders and still counting) so I made an improvement thus:  

for(int k=0; k<4; k++) ...

if (m_position.PriceOpen() < m_position.PriceCurrent() - (k+1)*(100 * _Point) && PositionsTotal()==k)       

                           trade.Buy (InpLot,NULL,Ask,(Ask-SL * _Point),  (Ask+TP * _Point),NULL) ;   

However, this improvement only limits total number of opened orders to 3 instead of just 1 when each condition is reached. Furthermore, one out of the 4 conditions does not open new position when its condition is reached. Any idea/solution, from anyone, to achieve this will be highly appreciated.  

 
if(PositionsTotal()==0)
{
if (m_position.PriceOpen() < m_position.PriceCurrent() - (k+1)*(100 * _Point) && PositionsTotal()==k)       

                           trade.Buy (InpLot,NULL,Ask,(Ask-SL * _Point),  (Ask+TP * _Point),NULL) ;   
}

You can expand on this by filtering trades for each instrument and or magic number.

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool SymbolIsTradeCheck(string symbol)
{
   int total=PositionsTotal(); // number of open positions
//--- iterate over all open positions
   for(int i=total-1; i>=0; i--)
      {
         if(symbol==PositionGetString(POSITION_SYMBOL))
            {
               return(true);
            }
         //can add if(magic == magicnumber) etc...
      }
   return(false);
}
//+------------------------------------------------------------------+
 
Marco vd Heijden:

You can expand on this by filtering trades for each instrument and or magic number.

Do these codes let you choose the pairs you want to trade and look within them or is it a set parameter?