failed market buy [Order to close this position already exists]

 

Hello ,

can someone explain this error to me

2021.02.17 03:33:46.033 Trades  '160048826': failed market buy 0.1 EURUSD, close #2238727196 sell 0.1 EURUSD 1.20938 [Order to close this position already exists]
2021.02.17 03:33:46.033 Trades  '160048826': failed market sell 0.1 EURGBP, close #2238727195 buy 0.1 EURGBP 0.87127 [Order to close this position already exists]
2021.02.17 03:33:46.034 Trades  '160048826': failed market sell 0.09 GBPUSD, close #2238727194 buy 0.09 GBPUSD 1.38816 [Order to close this position already exists]

I try to close all positions but every time the robot returns this error and crashes. This is the code that I use, it is a HEDGING account. thanks,


void Close_MIO(stThree &MxSmb[], int accounttype)
  {
   int i;
   
   for(i = PositionsTotal() - 1; i >= 0; i--)
     {
      switch(accounttype)
        {
         case  ACCOUNT_MARGIN_MODE_RETAIL_HEDGING:
            ctrade.PositionClose(PositionGetTicket(i));
           
            break;
         default:
            break;
        }

     }

  }
 

Uninstall software and reinstall

Hopefully it ll be solved 

 

it is very strange in visual testing mode it works well, try uninstall and re-install but still the same
How to quickly develop and debug a trading strategy in MetaTrader 5
How to quickly develop and debug a trading strategy in MetaTrader 5
  • www.mql5.com
Scalping automatic systems are rightfully regarded the pinnacle of algorithmic trading, but at the same time their code is the most difficult to write. In this article we will show how to build strategies based on analysis of incoming ticks using the built-in debugging tools and visual testing. Developing rules for entry and exit often require years of manual trading. But with the help of MetaTrader 5, you can quickly test any such strategy on real history.
 
try it on another broker and see if the error is still happening
 

I already did that, but I realized that with latency 0 it works fine but with latency greater than that, things get complicated with errors.

thanks guys

 
mohanx:

I already did that, but I realized that with latency 0 it works fine but with latency greater than that, things get complicated with errors.

thanks guys

Is your function called on every tick? It so, it might try to close it again in next tick, after an order was sent to the broker, but before it was actually closed.
 
La_patates:
Is your function called on every tick? It so, it might try to close it again in next tick, after an order was sent to the broker, but before it was actually closed.

yes  called on every tick.

It may be that

The Fundamentals of Testing in MetaTrader 5
The Fundamentals of Testing in MetaTrader 5
  • www.mql5.com
What are the differences between the three modes of testing in MetaTrader 5, and what should be particularly looked for? How does the testing of an EA, trading simultaneously on multiple instruments, take place? When and how are the indicator values calculated during testing, and how are the events handled? How to synchronize the bars from different instruments during testing in an "open prices only" mode? This article aims to provide answers to these and many other questions.
Reason: