Need help with a possible EA bug.

 

Hi all,

I am using MT4 for automated trading. The situation goes like this.

The EA I am using is placing multiple orders per bar. See the image below where it placed 10 orders in a 30M bar.

EA BUG


How can I modify an expert advisor in MQL4 so that it places only one order per bar?


Any help would be highly appreciated.

Cheers!

 

Include a flag which will turn on once a order is placed and should turn off at new bar. If this flag is on then no further trades. Let me know if you have difficult in implementing this. 

Regards, 

 

Hello, there are many caused of this problem.  Maybe because of logic issue, or other issues.  We cannot give any suggestion unless we could see and review the codes.
 

MQLDesigner:

Hello, there are many caused of this problem.  Maybe because of logic issue, or other issues.  We cannot give any suggestion unless we could see and review the codes.

Hello many thanks for your answers.


I tried to include a flag as sineshydv suggested. I am not quite sure if this will work. So far there have been no multiple orders.


Here is the order's dispatch part of the code. In red fonts are the modifications I did. Do you think this is going to work?


int OpenOrder(int Typ, double Lots, int OrderSlippage, string Orderkommentar, int Magicnumer, color FareVonOrderPfeil)

{
if(IsTradeContextBusy()==true)return;
//----
   int Ticketnummer= 0,Fehlermeldung = 0;
   //----
   switch (Typ) {
   case 0:
      for (int i = 0; i < 100; i++)
      {
         RefreshRates();
         static datetime lastOrderSend;
   if (Time[0] == lastOrderSend) return(0); // Only send one per bar
         Ticketnummer= OrderSend(Symbol(), OP_BUY, Lots, NormalizeDouble(MarketInfo(Symbol(),MODE_ASK),Digits), OrderSlippage, 0, 0, Orderkommentar, Magicnumer, 0, FareVonOrderPfeil);
         if (Ticketnummer < 0) Alert("OrderSend Failed: ", GetLastError());
             else            lastOrderSend = Time[0]; // Send no more this bar

         Fehlermeldung = GetLastError();
         if (Fehlermeldung == 0) break;
         if (Fehlermeldung == 130){Print("Open Buy Order Error takeprofit or stoploss to small.");break;}
         if (Fehlermeldung == 4){Print("Open Buy Order Error SERVER_BUSY.");break;}
         if (Fehlermeldung == 137){Print("Open Buy Order Error BROKER_BUSY.");break;}
         if (Fehlermeldung == 146){Print("Open Buy Order Error TRADE_CONTEXT_BUSY.");break;}
         if (Fehlermeldung == 136){Print("Open Buy Order Error OFF_QUOTES.");break;}
         if (Fehlermeldung > 0){Print("Open Buy Order Error #",Fehlermeldung);break;}
         Sleep(1000);
      }
      break;
   case 1:
      for (i = 0; i < 100; i++)
      { 
         RefreshRates();
        
   if (Time[0] == lastOrderSend) return(0); // Only send one per bar
         Ticketnummer= OrderSend(Symbol(), OP_SELL, Lots, NormalizeDouble(MarketInfo(Symbol(),MODE_BID),Digits), OrderSlippage, 0, 0, Orderkommentar, Magicnumer, 0, FareVonOrderPfeil);
                  if (Ticketnummer < 0) Alert("OrderSend Failed: ", GetLastError());
             else            lastOrderSend = Time[0]; // Send no more this bar


        
         Fehlermeldung = GetLastError();
         if (Fehlermeldung == 0) break;
         if (Fehlermeldung == 130){Print("Open Sell Order Error takeprofit or stoploss to small.");break;}
         if (Fehlermeldung == 4){Print("Open Sell Order Error SERVER_BUSY.");break;}
         if (Fehlermeldung == 137){Print("Open Sell Order Error BROKER_BUSY.");break;}
         if (Fehlermeldung == 146){Print("Open Sell Order Error TRADE_CONTEXT_BUSY.");break;}
         if (Fehlermeldung == 136){Print("Open Sell Order Error OFF_QUOTES.");break;}
         if (Fehlermeldung > 0){Print("Open Sell Order Error #",Fehlermeldung);break;}
         Sleep(1000);
      }
     break;
   }
 
   //----
   return (Ticketnummer);
//----
}
 

I think you should move the position of "static datetime lastOrderSend;" like this:

int OpenOrder(int Typ, double Lots, int OrderSlippage, string Orderkommentar, int Magicnumer, color FareVonOrderPfeil)

{

    static datetime lastOrderSend;

.....

 
Hello there, personally i feel this solution is good.  The way for comparing the current chart time to the last order chart time should work well. The only thing you need to aware is when you use faster time frame like M5 or M1, by doing so you may have quite a lot of order just in 1 hour (of course this wont be an issue if you want it works this way).
 

Many thanks for your input. I also moved the declaration of lastordersend as biantoro suggested. Seems to works fine uptill now.
 

SCINVESTOR1:

Many thanks for your input. I also moved the declaration of lastordersend as biantoro suggested. Seems to works fine uptill now.
ok
 

Since this is MQl4 you may get more help if you post it in www.mql4.com

and use src button to post code

src 

Reason: