Stopping multiple EA entry

To add comments, please log in or register
pekele4u
25
pekele4u  

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);

  }

  }

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


Discover new MetaTrader 5 opportunities with MQL5 community and services
Discover new MetaTrader 5 opportunities with MQL5 community and services
  • www.mql5.com
Ask questions on technical analysis, discuss trading systems and improve your MQL5 programming skills to develop your own trading strategies. Communicate and share your experience with traders from anywhere in the world, answer questions and help beginners — MQL5.community is developing along with you. Signal Systems I was affraid to open...
Marius Ovidiu Sunzuiana
1503
Marius Ovidiu Sunzuiana  

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

Alois Mwirichia
180
Alois Mwirichia  
//---
   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
     }
Correction. Thank you  @William Roeder
William Roeder
19170
William Roeder  
pekele4u: I am new to mql4 programming. How do I stop it from multiple entry.
  1. Why did you post your MT4 question in the Root / MT5 EA section  instead of the MQL4 section, (bottom of the Root page?)
              General rules and best pratices of the Forum. - General - MQL5 programming forum
    Next time post in the correct place. The moderators will likely move this thread there soon.

  2. 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
              Messages Editor

  3. You need to add tests for that.
William Roeder
19170
William Roeder  
Alois Mwirichia:
      bool iBuy=OrderSend();
      if(iBuy)//true

OrderSend does not return a boolean. Your test fails.

William Roeder
19170
William Roeder  
Marius Ovidiu Sunzuiana:     if( buy condition && buy==false)
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.
Marius Ovidiu Sunzuiana
1503
Marius Ovidiu Sunzuiana  
William Roeder:
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.
Keith Watford
Moderator
17220
Keith Watford  
Marius Ovidiu Sunzuiana:
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)

Ivan Nehrishnyi
72
Ivan Nehrishnyi  
Keith Watford:

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))
Keith Watford
Moderator
17220
Keith Watford  
Ivan Nehrishnyi:
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.

To add comments, please log in or register