Trade Execution code not working

 
Hi, may I ask why my code is not working :(, the error keeps getting the same code 4756, I have printed the debug printed log it seems nothing wrong with it but it still wont open any trades at all. I started some researches on official website about the OrderSend function but I still couldnt find out what problems it has with the code.

// Function to open a trade with specified TP
void OpenTrade(double tpPrice)
{
    MqlTradeRequest request={};
    MqlTradeResult result={};
    MqlTick Latest_Price; // Structure to get the latest prices      
    
    double Ask = SymbolInfoDouble(_Symbol,SYMBOL_ASK);
    double Bid = SymbolInfoDouble(_Symbol,SYMBOL_BID);
    
    // Retrieve latest price info
    if (!SymbolInfoTick(_Symbol, Latest_Price))
    {
        Print("Failed to get latest prices. Error: ", GetLastError());
        return;
    }

    // Current Bid and Ask prices
    double dBid_Price = Latest_Price.bid;  
    double dAsk_Price = Latest_Price.ask;  
    double dbLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN);
    ZeroMemory(request);
    request.action = TRADE_ACTION_DEAL;
    request.symbol = _Symbol;

    // Set price based on order type
    request.price = isBuy ? Ask : Bid;

    // Normalize SL and TP
    request.sl = NormalizeDouble(stopLossPrice, _Digits);
    request.tp = NormalizeDouble(tpPrice, _Digits);
    request.volume = NormalizeDouble(lotSize,2);
    request.type = isBuy ? ORDER_TYPE_BUY : ORDER_TYPE_SELL;
    request.deviation = 5;
    request.magic = 123456;
    request.comment = "TriosFX";
    
    // Print for debugging
    Print("Symbol: ", request.symbol);
    Print("Digits: ", _Digits);
    Print("SL: ", request.sl);
    Print("TP: ", request.tp);
    Print("Lot: ", request.volume);
    Print("Min Lot: ",dbLotsMinimum);
    Print("TYPE: ", request.type);
    Print("Current Ask: ", Ask);
    Print("Current Bid: ", Bid);
    
    // Send the trade request
    if (!OrderSend(request, result))
    {
        Print("Error opening trade: ", GetLastError());
        PrintFormat("retcode=%u  deal=%I64u  order=%I64u",result.retcode,result.deal,result.order);
        return;  // Exit if there's an error
    }
    
    // Log success
    Print("Trade opened successfully: ", _Symbol, " ", (isBuy ? "BUY" : "SELL"), " Lot Size: ", lotSize, " TP: ", tpPrice);
    
    // Store ticket number
    ticket = result.order;

    // Reset last error before selecting the order
    ResetLastError();
    if (!OrderSelect(ticket))
    {
        PrintFormat("OrderSelect(%I64u) failed. Error %d", ticket, GetLastError());
        return;  // Exit if selection fails
    }
    
    // Calculate BE prices based on order type
    if (request.type == ORDER_TYPE_BUY)
    {
        BEPriceTriggered = OrderGetDouble(ORDER_PRICE_OPEN) + BEPipsPrice;
        BEPrice = OrderGetDouble(ORDER_PRICE_OPEN) + setBEPricePipsPrice;
    }
    else if (request.type == ORDER_TYPE_SELL)
    {
        BEPriceTriggered = OrderGetDouble(ORDER_PRICE_OPEN) - BEPipsPrice;
        BEPrice = OrderGetDouble(ORDER_PRICE_OPEN) - setBEPricePipsPrice;
    }
}
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
  • Did you look up the meaning of error 4756?
  • Did you follow up and see which return code is reported in the trade result?
  • Did you lookup the meaning of that return code?
 

Your code alone does not help. Also show the log output.

Also have a look at the following and implement it in your EA. It is important that you carry out these checks even if your EA is not meant for the Market.

Articles

The checks a trading robot must pass before publication in the Market

MetaQuotes, 2016.08.01 09:30

Before any product is published in the Market, it must undergo compulsory preliminary checks in order to ensure a uniform quality standard. This article considers the most frequent errors made by developers in their technical indicators and trading robots. An also shows how to self-test a product before sending it to the Market.
 
Fernando Carreiro #:
  • Did you look up the meaning of error 4756?
  • Did you follow up and see which return code is reported in the trade result?
  • Did you lookup the meaning of that return code?
Im getting these logs from the debug outputs, I checked all the price and lot value, still not having any ideas on what it is wrong with it
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   Symbol: XAUUSD
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   Digits: 2
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   SL: 2740.0
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   TP: 2710.0
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   Lot: 1.0
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   Min Lot: 0.01
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   TYPE: 1
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   Current Ask: 2737.41
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   Current Bid: 2737.19
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   Error opening trade: 4756
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   Error: 10011
2024.11.05 23:18:41.130 ICTFollow (XAUUSD,H1)   retcode=10030  deal=0  order=0
 
Jason Goh #: Im getting these logs from the debug outputs, I checked all the price and lot value, still not having any ideas on what it is wrong with it

So, I ask again ...

  • Did you look up the meaning of 4756?
  • Did you look up the meaning of 10011?
  • Did you look up the meaning of 10030?

ERR_TRADE_SEND_FAILED

4756

Trade request sending failed

10011

TRADE_RETCODE_ERROR

Request processing error

10030

TRADE_RETCODE_INVALID_FILL

Invalid order filling type


    Based on the final return code about the filling type, what do you think may be the problem?

    Read the following too ...

    Filly Policy - Basic Principles - Trading Operations - MetaTrader 5 Help
     
    Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
    struct MqlTradeRequest
      {
       ENUM_TRADE_REQUEST_ACTIONS    action;           // Trade operation type
       ulong                         magic;            // Expert Advisor ID (magic number)
       ulong                         order;            // Order ticket
       string                        symbol;           // Trade symbol
       double                        volume;           // Requested volume for a deal in lots
       double                        price;            // Price
       double                        stoplimit;        // StopLimit level of the order
       double                        sl;               // Stop Loss level of the order
       double                        tp;               // Take Profit level of the order
       ulong                         deviation;        // Maximal possible deviation from the requested price
       ENUM_ORDER_TYPE               type;             // Order type
       ENUM_ORDER_TYPE_FILLING       type_filling;     // Order execution type
       ENUM_ORDER_TYPE_TIME          type_time;        // Order expiration type
       datetime                      expiration;       // Order expiration time (for the orders of ORDER_TIME_SPECIFIED type)
       string                        comment;          // Order comment
       ulong                         position;         // Position ticket
       ulong                         position_by;      // The ticket of an opposite position
      };

    type_filling

    Order execution type. Can be one of the enumeration ENUM_ORDER_TYPE_FILLING values.


     
    the type of filling is broker specific just so you also know -> Market watch - right click the symbol - click Specification - look for Filling
     
    Fernando Carreiro #:
    Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure

    type_filling

    Order execution type. Can be one of the enumeration ENUM_ORDER_TYPE_FILLING values.


    Oh...IC...I thought the type and the type_filling both are the same thing ...The problem has been solved and it is working now , Thanks a lot for the helps !