How to make sure MQL4 EA opens one trade only when condition is met

I am new to mql4 programming. I want want to understand some fundamental. I have this testing EA whose code is displayed below. How do I stop it from multiple entry. Like when the condition for buy is true, it enters buy on every tick, the same with sell.

My question is how to i stop it from entering several buy or sell as the case may be. I want it to enter single buy on a particular pair and when the sell condition is true, it will close the buy and enter sell. Please help me out.

Thanks in advance.



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

//|                                                        test2.mq4 |

//|                        Copyright 2019, MetaQuotes Software Corp. |

//|                                             https://www.mql5.com |

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

#property copyright "Copyright 2019, MetaQuotes Software Corp."

#property link      "https://www.mql5.com"

#property version   "1.00"

#property strict

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

//| Expert initialization function                                   |

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

int OnInit()

  {

//---

   

//---

   return(INIT_SUCCEEDED);

  }

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

//| Expert deinitialization function                                 |

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

void OnDeinit(const int reason)

  {

//---

   

  }

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

//| Expert tick function                                             |

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

void OnTick()

  {

//---

   double f1 =  iMA(NULL,0,14,0,MODE_EMA,PRICE_CLOSE,0);

double f2 = iMA(NULL,0,3,0,MODE_EMA,PRICE_CLOSE,0);

   if(f2 > f1) 

   {

      OrderSend(Symbol(),OP_BUY,0.01,Ask,3,NULL,NULL);

   }

   else if(f2 < f1)

   {

  OrderSend(Symbol(),OP_SELL,0.01,Bid,3,NULL,NULL);

  }

  }

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

You need to define a boolean flag:

example:

bool buy=false;

if( buy condition && buy==false)

{

OrderSend…

buy=true; ( here it becomes true)

}

or… you can use a for loop and count positions like: for( int i=0; i<OrdersTotal(); i++) and then check if (OrdersTotal()==0) OrderSend…

and other case for sell

boll sell=false;

if(conditional statement && buy==false)

OrderSend…

sell=true // here it becomes true

//---
   if(!iBot)
     {
      int iBuy=OrderSend();
      if(iBuy<0)
        {
         Alert("OrderSend failed with error #",ErrorDescription(GetLastError()));
        }
      else
        {
         Alert("some order details");
         iBot=true;
        }

      //---
     }
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      //filter orders by magic,symbol type
      //if no open order is found set iBot = false
     }

OrderSend does not return a boolean. Your test fails.

You should be able to read your code out loud and have it make sense. You would never write if( (2+2 == 4) == true) would you? if(2+2 == 4) is sufficient. So don’t write if(bool == true) , just use if(bool) or if( ! bool) . Code becomes self documenting when you use meaningful variable names, like bool isLongEnabled where as Long_Entry sounds like a trigger price or a ticket number and “if long entry” is an incomplete sentence.

Sure you are write but I wanted to be more clear with ==false

Generally speaking we do not have to consider that a function cannot return false so yes NOT ! is used without check again equals false or true.

Your post is incoherent.

If you want to check if an OrderSend() was successful

if(OrderSend()>0)

the correct error check is if(OrderSend()<0) - so that you will not be surprised when the order opens on zero return))

Please note that my post was

“If you want to check if an OrderSend() was successful”

Yours is about if the order is NOT successful so neither is more correct than the other.