Error 4107 XAUUSD

 

Hi,


In my code, i am trying to close order when Profit is less then 5 USD, it works well on many pairs but XAUUSD. I couldnt solve the problem, when i try to get error code, i noticed that it is error 4107, any idea why is it happening ? Only for XAUUSD.


Thanks for help.


for(int i = 0; i < OrdersTotal(); i++)

     {

      if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))    // İlgili işlemi seç

        {

         if(OrderProfit() < - 5)

           {

            if(OrderClose(OrderTicket(), OrderLots(), Bid, 30,clrNONE))   // İşlemi kapat        }

              {               

Alert("EXIT   ", GetLastError();

              }

           }

        }

 

Please use the code button if you post code:  (Alt+S) or 

Beide that you check whether the profit is less than -5:  if(OrderProfit() < - 5)

If you code doesn't do what it should use the debugger: https://www.metatrader5.com/en/metaeditor/help/development/debug

Code debugging - Developing programs - MetaEditor Help
  • www.metatrader5.com
MetaEditor has a built-in debugger allowing you to check a program execution step by step (by individual functions). Place breakpoints in the code...
 
            if(OrderClose(OrderTicket(), OrderLots(), Bid, 30,clrNONE))   // İşlemi kapat        }
  1. You can only use Bid, if the selected order is the current chart symbol, and it is a buy order.

    MT4: You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.

  2. 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.
         If trading multiple timeframes, and filter by symbol requires use a range of MN (base plus timeframe).
              Why are MT5 ENUM_TIMEFRAMES strange? - General - MQL5 programming forum - Page 2 #11 (2020)

 
Thanks a lot William, fixed the problem now.