CTrade::PositionClose() — The Return Worth Contract and What It Prices in Manufacturing – Buying and selling Programs – 3 June 2026


If you’re migrating from MQL4 or debugging a place administration bug you can not reproduce in Technique Tester, learn this earlier than altering anything.

What the Documentation Truly Says

The MQL5 reference for `CTrade::PositionClose()` is technically correct. Each overloads are listed. The return worth is documented as a boolean. What the documentation doesn’t state plainly is what `true` truly means by way of account state: the request handed inside validation and was despatched to the dealer. Nothing extra.

That is distinct from the outdated MQL4 contract. `OrderClose()` in MQL4 was synchronous — when it returned `true`, the order was settled and the following line of code ran in opposition to a constant account state. Builders who constructed programs on that contract and are actually porting to MT5 carry the belief ahead, typically with out realizing it.

The Async Hole in Stay Situations

When `PositionClose()` returns `TRADE_RETCODE_DONE`, the shut request has been acknowledged. The dealer affirmation is pending. On ECN brokers beneath regular load, the time between `TRADE_RETCODE_DONE` and the place truly disappearing from `PositionSelect()` is roughly 300–800ms.

Technique Tester doesn’t replicate this hole. Each backtest will move cleanly. The failure surfaces at market open on the primary stay day.

The sensible consequence: if the following line of code after `PositionClose()` calls `PositionSelect()` to examine account state earlier than operating entry logic, it should discover the outdated place nonetheless current. If entry logic is condition-gated on “no open place,” it should both skip the entry or open a second place alongside the one being closed. The account briefly holds double publicity with no error within the log.

Right here is the naive sample that produces this:

bool closed = m_trade.PositionClose(Image());
if(closed)
{
    
    if(!PositionSelect(Image()))
    {
        
        
    }
}

The Deferred Verification Sample

The right sample defers any logic that is dependent upon a closed account state till place elimination is confirmed on the following tick:

bool m_closePending = false;

bool ClosePosition(const string image)
{
    if(!m_trade.PositionClose(image))
     Image=%s",
               __FUNCTION__, m_trade.ResultRetcode(), image);
        return false;
    
    m_closePending = true;
    return true;
}

void OnTick()
{
    if(m_closePending)
    {
        if(!PositionSelect(_Symbol))
        {
            m_closePending = false;
            
        }
        return;    
    }

    
}

The flag blocks all dependent logic till `PositionSelect()` returns `false`, confirming precise elimination. No sleep, no arbitrary delays — only a state examine on the following tick.

OnTradeTransaction Timing

`OnTradeTransaction` fires as a part of the affirmation sequence, nevertheless it fires earlier than the place registry has totally up to date. Studying `PositionSelect()` inside `OnTradeTransaction` will return stale state within the hole interval. Use `OnTradeTransaction` to set flag state solely — by no means to learn place state.

void OnTradeTransaction(const MqlTradeTransaction &trans,
                        const MqlTradeRequest &request,
                        const MqlTradeResult &outcome)
{
    if(trans.kind == TRADE_TRANSACTION_DEAL_ADD)
    {
        
        m_transactionReceived = true;
    }
}

Dealing with 4 Return Codes Appropriately

Not all retcodes from `PositionClose()` are equal:

  • `TRADE_RETCODE_DONE` — Request accepted. Apply deferred verification.
  • `TRADE_RETCODE_REQUOTE` — Value modified throughout request. Retry instantly utilizing the present bid or ask.
  • `TRADE_RETCODE_REJECTED` — Request was invalid. Examine image, margin necessities, and cease ranges earlier than retrying.
  • `TRADE_RETCODE_TIMEOUT` — No affirmation obtained. That is the harmful case: the order might have stuffed and the affirmation was misplaced in transit. At all times confirm place state independently earlier than retrying. A blind retry on a `TIMEOUT` that stuffed will open a second place.
uint retcode = m_trade.ResultRetcode();

if(retcode == TRADE_RETCODE_DONE)
{
    m_closePending = true;
}
else if(retcode == TRADE_RETCODE_REQUOTE)
{
    
    m_retryClose = true;
}
else if(retcode == TRADE_RETCODE_REJECTED)
 Image=%s",
           __FUNCTION__, _Symbol);

else if(retcode == TRADE_RETCODE_TIMEOUT)
{
    
    if(!PositionSelect(_Symbol))
    {
        m_closePending = false;  
    }
    else
    {
        m_retryClose = true;  
    }
}

What This Seems Like in a Manufacturing EA

The total sample combines the entire above: `ClosePosition()` units the pending flag, `OnTick()` checks and clears it after confirming elimination, and retcode dealing with branches to the proper response for every case.

This sample isn’t advanced. The documentation describes the return contract precisely. The hole between documentation and manufacturing actuality is in how the migration from MQL4 carries a synchronous assumption into an asynchronous execution surroundings.

We see this sample inflicting account state bugs in roughly a 3rd of the MT5 EA rescue work we deal with every year. It passes each backtest. It fails at market open. The repair is fewer than 20 strains.



Source link

Related articles

Treas Sec Bessent: “I want job market had come out at this time”.

Treasury Secretary Bessent stated: I want job market had come out at this timeThen added that he has no prior data of tomorrow's job knowledge.Then why say that?The Trump administration, his household, and...

Normal Chartered Holds $100,000 Bitcoin Worth Prediction, Says “Backside Is Almost In”

$920 billion Wall Avenue big Normal Chartered maintained its $100,000 Bitcoin worth prediction regardless of the crash to $61K lows at the moment. The funding financial institution claimed Bitcoin backside is nearly...

10 Tech Shares Providing a Compelling Different as Bitcoin Plummets

Bitcoin has been underperforming in current months, each by way of general efficiency and volatility Conversely, double-digit swings will not be unusual amongst US tech shares, which have soared in current months Crypto traders in...

A truck-mounted nuclear reactor that runs for many years with out refueling is being examined in China

The challenge is led by Wu Yican on the Institute of Nuclear Vitality Security Know-how, who has described the system as a first-of-its-kind cell nuclear unit. "Our group has constructed the world's first...

iFi’s new moveable headphone DAC seems to be much more like a hip flask, and it will serve a swift a dram of hi-res...

iFi unveils the iDSD GR2 at Excessive Finish ViennaNew moveable DAC, following on from xDSD GryphonEnhancements in some departments, together with amplificationWe're seeing swathes of aspirational new hi-fi equipment at Excessive Finish Vienna...
spot_img

Latest articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

WP2Social Auto Publish Powered By : XYZScripts.com