Problem with simple sell deal

 

Hello,

I want to make a simple sell but I always receive error. This is my code:

bool cTrades::Sell(string iSymbol, double iVolume, string iLabel = "")
{
    MqlTradeResult Result = {0};
    MqlTradeRequest Request;
    
    Request.action = TRADE_ACTION_DEAL;
    Request.symbol = iSymbol;
    Request.volume = NormalizeDouble(iVolume, 2);
    Request.sl = 0;
    Request.tp = 0;
    Request.deviation = 1000;
    Request.type = ORDER_TYPE_SELL;
    Request.comment = iLabel;

    if(OrderSend(Request, Result))
    {
        TradeData PosData;
        PosData.Position = Result;
        iTrade.SetPoint(PosData);
        ActivePositions++;

        return true;
    }
    else
    {
        int Error = GetLastError();
        return false;
    }
}

Every time Error have a value 4756!

Every time retcode in the result structure is 10013.

What is my mistake??

 
I found the solution. Just add
ZeroMemory(request);

before modifying the request!

 
one_monk #: I found the solution. Just add before modifying the request!

No, that is not the solution. The correct solution is for you to fill in every field correctly.

You have left many fields unfilled that are vital for a correct order placement. For example, you have not a filling type, nor a magic number, nor an opening price.

You were just lucky that that it worked with the missing fields set to zero, but that will not always be the case.

All the fields are necessary and need to be properly set according to a specific need.

 
Fernando Carreiro #:

No, that is not the solution. The correct solution is for you to fill in every field correctly.

You have left many fields unfilled that are vital for a correct order placement. For example, you have not a filling type, nor a magic number, nor an opening price.

You were just lucky that that it worked with the missing fields set to zero, but that will not always be the case.

All the fields are necessary and need to be properly set according to a specific need.

not all fields are mandatory look at the documentation, filling type is mandatory


https://www.mql5.com/en/docs/constants/structures/mqltraderequest

Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
Documentation on MQL5: Constants, Enumerations and Structures / Data Structures / Trade Request Structure
  • www.mql5.com
Trade Request Structure - Data Structures - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
amando #:

not all fields are mandatory look at the documentation, filling type is mandatory


https://www.mql5.com/en/docs/constants/structures/mqltraderequest

Don't you find it odd that not one of the example orders include request.type_filling ?

It is no wonder that so many people have this problem.

 
amando #: not all fields are mandatory look at the documentation, filling type is mandatory https://www.mql5.com/en/docs/constants/structures/mqltraderequest

Even when certain fields are not mandatory, they still should be set to prevent unexpected actions, given that their initial values are not known.

For example, imagine a normal "TRADE_ACTION_DEAL" operation where the uninitialised "position ticket" value just so happens to be "correct" and be a valid position in the opposite direction, causing a partial close of that position when in fact you wanted to place a new hedged order instead.

So, even if the documentation does not define it as mandatory, it is a better practice to set all the fields explicitly to prevent any unpredictable mishaps.

Reason: