Single Execution of a code

 

Hello everyone! I have this code in my EA. Problem is, the code lays out numerous orders in my chart when I only intended to run the code once until the next event occurs. Any advice?

if (OrderType==ORDER_TYPE_SELL && DealMagicNumber==MAGICMA)   // Buy Trade
                           {
                              m_trade.BuyStop(0.01,m_symbol.Ask()+GridPoints*_Point,_Symbol,0,m_symbol.Ask()+(GridPoints+GridPoints)*_Point);
                              m_trade.BuyLimit(0.01,m_symbol.Bid()-GridPoints*_Point,_Symbol,0,m_symbol.Bid());
                              m_trade.SellLimit(0.01,m_symbol.Ask()+GridPoints*_Point,_Symbol,0,m_symbol.Ask());
                           }
                           return;
 

Before issuing a trade order, search: "if there are pending orders in the market?"

//+------------------------------------------------------------------+
//| Is pending orders exists                                         |
//+------------------------------------------------------------------+
bool IsPendingOrdersExists(void)
  {
   for(int i=OrdersTotal()-1; i>=0; i--) // returns the number of current orders
      if(m_order.SelectByIndex(i))     // selects the pending order by index for further access to its properties
         if(m_order.Symbol()==Symbol() && m_order.Magic()==MAGICMA)
            return(true);
//---
   return(false);
  }

more or less like this:

if(!IsPendingOrdersExists())
  {
   if(OrderType==ORDER_TYPE_SELL && DealMagicNumber==MAGICMA) // Buy Trade
      ***
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
Documentation on MQL5: Constants, Enumerations and Structures / Trade Constants / Trade Operation Types
  • www.mql5.com
Trading is done by sending orders to open positions using the OrderSend() function, as well as to place, modify or delete pending orders. Each trade order refers to the type of the requested operation. Trading operations are described in the ENUM_TRADE_REQUEST_ACTIONS enumeration...
 

Correct "And sign" in your code then  test again...

if (OrderType==ORDER_TYPE_SELL && DealMagicNumber==MAGICMA)   // Buy Trade
 
Thank you all
 
Aliakbar Kavosi :

Correct "And sign" in your code then  test again...

 if (OrderType==ORDER_TYPE_SELL && DealMagicNumber==MAGICMA)   // Buy Trade 

I, too, was not attentive (I corrected my code above)

 
Vladimir Karputov:

I, too, was not attentive (I corrected my code above)

Thank you 
Reason: