False Multiple Order on EA

 

Hello everyone,

I would like to know why my EA send multiple orders (like 2 or 3 with same qty and same price) exactly on the same time ( not always but it appear nearly often ) even if i have some variable to only allow one order at the time.

If someone could help me it would be great thanks 

Here is how looks my EA: 

ps : Etat and nb are variable outside my fonction Open


void CheckForOpen()

//--- go trading only for first tiks of new bar

   if(Volume[0]>1) return;

//--- Buy conditions

   if(  RSI_L > 50 && (Etat == "SELL" || Etat ==" "))

   {

      if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==True && OrderType()== OP_SELL)

      {

         CloseOrders();

         nb = 0;

         Alert("Order Closed");

      }

      if(OrdersTotal()==0 && nb == 0)

      {

         OrderSend(Symbol(),OP_BUY,Lots ,Ask,0,0 ,0 ,"",MAGICMA,0,Blue);

         Alert("Order BUY placed" , nb);

         nb = nb + 1;

         Etat = "BUY";

      }

   }

//--- Sell conditions

   if( ( RSI_L < 50) && (Etat == "BUY" || Etat ==" "))

   {

      if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==True && OrderType()==OP_BUY)

      {

         CloseOrders();

         Alert("Order Closed");

         nb = 0;

      }

      if(OrdersTotal()==0 && nb == 0)

      {

         OrderSend(Symbol(),OP_SELL,Lots ,Bid,0,0,0 ,"",MAGICMA,0,Red);

         Alert("Order  SELL placed" , nb);

         nb = nb + 1;

         Etat = "SELL";

      }

   } 

   return;

   }
 
  1. Why did you post your MT4 question in the MT5 General section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.

  2.    if(Volume[0]>1) return;

    For a new bar test, Bars is unreliable (a refresh/reconnect can change number of bars on chart), volume is unreliable (miss ticks), Price is unreliable (duplicate prices and The == operand. - MQL4 programming forum.) Always use time.
              MT4: New candle - MQL4 programming forum #3 (2014)
              MT5: Accessing variables - MQL4 programming forum #3 (2022)

    I disagree with making a new bar function, because it can only be called once per tick (second call returns false). A variable can be tested multiple times.
              Running EA once at the start of each bar - MQL4 programming forum (2011)

  3.       if(OrderSelect(0,SELECT_BY_POS,MODE_TRADES)==True && OrderType()== OP_SELL)
    

    Magic number only allows an EA to identify its trades from all others. Using OrdersTotal/OrdersHistoryTotal (MT4) or PositionsTotal (MT5), directly and/or no Magic number/symbol filtering on your OrderSelect / Position select loop means your code is incompatible with every EA (including itself on other charts and manual trading.)
              Symbol Doesn't equal Ordersymbol when another currency is added to another seperate chart . - MQL4 programming forum (2013)
              PositionClose is not working - MQL5 programming forum (2020)
              MagicNumber: "Magic" Identifier of the Order - MQL4 Articles (2006)
              Orders, Positions and Deals in MetaTrader 5 - MQL5 Articles (2011)
              Limit one open buy/sell position at a time - General - MQL5 programming forum (2022)

    You need one Magic Number for each symbol/timeframe/strategy. Trade current timeframe, one strategy, and filter by symbol requires one MN.

  4. 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.

  5. OrderSend(Symbol(),OP_SELL,Lots ,Bid,0,0,0 ,"",MAGICMA,0,Red);

    Risk depends on your initial stop loss, lot size, and the value of the symbol. It does not depend on margin and leverage. No SL means you have infinite risk (on leveraged symbols). Never risk more than a small percentage of your trading funds, certainly less than 2% per trade, 6% total.

    1. You place the stop where it needs to be — where the reason for the trade is no longer valid. E.g. trading a support bounce, the stop goes below the support.

    2. AccountBalance * percent/100 = RISK = OrderLots * (|OrderOpenPrice - OrderStopLoss| * DeltaPerLot + CommissionPerLot) (Note OOP-OSL includes the spread, and DeltaPerLot is usually around $10/PIP, but it takes account of the exchange rates of the pair vs. your account currency.)

    3. Do NOT use TickValue by itself - DeltaPerLot and verify that MODE_TICKVALUE is returning a value in your deposit currency, as promised by the documentation, or whether it is returning a value in the instrument's base currency.
                MODE_TICKVALUE is not reliable on non-fx instruments with many brokers - MQL4 programming forum (2017)
                Is there an universal solution for Tick value? - Currency Pairs - General - MQL5 programming forum (2018)
                Lot value calculation off by a factor of 100 - MQL5 programming forum (2019)

    4. You must normalize lots properly and check against min and max.

    5. You must also check Free Margin to avoid stop out

    6. For MT5, see 'Money Fixed Risk' - MQL5 Code Base (2017)

  6. Brendan Prime: like 2 or 3 with same qty and same price) exactly on the same time

    Your code is running on multiple charts, or you are using a VPS and your PC.
 

Thank you so much for your complet reply, and sorry for my mistake on the section.

I will study every details on what you said.

Looks like it will solve lot of my issues