Problem with OrderClose()

 

Hello guys,


I am quite new, and i have one very unusual problem with funciton OrderClose. When i test my strategy with simulation, everything works OK, but if i try to strat EA on real time trading(DEMO trading of course :)), the expert opens new and new position and doesn't colse previous orders, very strange for me. I read a lot of documents about this function, but can't remove this problem.


Thank for the help.:)

I paste here part of my code.


for(int i=0;i<OrdersTotal();i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false)
      {
      Print("OrderSelect Problem!!!");
       break;
       }
      //---- check order type 
      if(OrderType()==OP_BUY && OrderSymbol()==Symbol() )
        {
         OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
         break;
        }
      if(OrderType()==OP_SELL && OrderSymbol()==Symbol() )
        {
         OrderClose(OrderTicket(),OrderLots(),Ask,5,White);
         break;
        }
 
HLA:

When i test my strategy with simulation, everything works OK, but if i try to strat EA on real time trading(DEMO trading of course :)), the expert opens new and new position and doesn't colse previous orders, very strange for me.

There is at least a latent bug in your code which assumes - roughly speaking - that there will only be one open order for the symbol you are trading. This assumption holds in the backtester, provided that your EA is only opening one order at a time, but may not hold elsewhere. Your code may be breaking because, outside of the backtesting environment, there are open orders from other EAs or from manual trading. There have been many discussions on this forum about the need to loop down from OrdersTotal() - 1 to zero, rather than looping up, if you are doing something which alters the list of open orders. Taking one of the many examples, see my post at the end of the first page of https://www.mql5.com/en/forum/119511.

 
jjc:

Your code may be breaking because, outside of the backtesting environment, there are open orders from other EAs or from manual trading.

Thank you for the help guys, but this is my first and only EA. I doesn't start any other expert, so i doesn't have any open oreders. Also this expert opens only one position in any direction and after some time close this order and opens other in the opposite direction. This is one very simple logic, i just try to uderstand MQL.

Last night after i wrote my post, the EA is mysteriously started works. I just put this " OrderSymbol()==Symbol()" in this row "if(OrderType()==OP_BUY && OrderSymbol()==Symbol() )" end expert strated closed the orders. Can you explain me why did EA do this.

 
Could it be that you've hard-coded a specific pair into your order open, and then you are placing your EA on the chart of a different pair? CB
 
HLA wrote >>

Hello guys,

I am quite new, and i have one very unusual problem with funciton OrderClose. When i test my strategy with simulation, everything works OK, but if i try to strat EA on real time trading(DEMO trading of course :)), the expert opens new and new position and doesn't colse previous orders, very strange for me. I read a lot of documents about this function, but can't remove this problem.

Thank for the help.:)

I paste here part of my code.

Place some Print statemente in your code and watch the journal... Its trying to close, you see the entry in the log-file?

look for GetLastError() in Documentation, will help for shure :-)

if(OrderType()==OP_BUY && OrderSymbol()==Symbol() )
{

>>HERE Print("Close Buy");
OrderClose(OrderTicket(),OrderLots(),Bid,5,White);
break;
}

 
cloudbreaker:
Could it be that you've hard-coded a specific pair into your order open, and then you are placing your EA on the chart of a different pair? CB

No i use Symbol() like a first parameter in functin OrderSend, i am not in home now,and i can't paste my original code but i am sure that i doesn't use a specific pair in my code.

 
HLA:

Last night after i wrote my post, the EA is mysteriously started works. I just put this " OrderSymbol()==Symbol()" in this row "if(OrderType()==OP_BUY && OrderSymbol()==Symbol() )" end expert strated closed the orders. Can you explain me why did EA do this.

I can't see a reason other than there being more than one open order, including things which aren't from your EA. Therefore, until you add the "OrderSymbol() == Symbol()" check, your EA is attempting to close orders for a different symbol, originating either from another EA or from manual trading.

 
HLA:

Hello guys,


I am quite new, and i have one very unusual problem with funciton OrderClose. When i test my strategy with simulation, everything works OK, but if i try to strat EA on real time trading(DEMO trading of course :)), the expert opens new and new position and doesn't colse previous orders, very strange for me. I read a lot of documents about this function, but can't remove this problem.


Thank for the help.:)

I paste here part of my code.


I am also new in EA, but the way you try to close an order in a loop is incorrect, I think.

Problem 1

in for loop the value of i increments by one, but the TotalCout() also decrements,... so the i<TotalCount() is incorrect (you will miss some of your orders)

Solution

save the value of TotalCount into a variable before loop and use "i<myTotalCountVariable"


Problem 2

in the loop you are using the SelectOrder( BY_POS), and the CloseOrder(), when you closing the orders, the position values of the orders are recalculated by the broker (imho), so you probably select an other order, then you want to select

Solution

you collect all the tickets you need in first loop, into array, then in second loop you close the orders, using those tickets


I am not 100% sure in it because I am new in MQL4, but by feeling the problem is this.

Please correct me if I am wrong.

 
drkenyi:

I am also new in EA, but the way you try to close an order in a loop is incorrect, I think.

Problem 1

in for loop the value of i increments by one, but the TotalCout() also decrements,... so the i<TotalCount() is incorrect (you will miss some of your orders)

Solution

save the value of TotalCount into a variable before loop and use "i<myTotalCountVariable"


Problem 2

in the loop you are using the SelectOrder( BY_POS), and the CloseOrder(), when you closing the orders, the position values of the orders are recalculated by the broker (imho), so you probably select an other order, then you want to select

Solution

you collect all the tickets you need in first loop, into array, then in second loop you close the orders, using those tickets


I am not 100% sure in it because I am new in MQL4, but by feeling the problem is this.

Please correct me if I am wrong.

Both problems are solved by using a decrementing loop counter. See here -> https://www.mql5.com/en/forum/119840

 
gordon:

Both problems are solved by using a decrementing loop counter. See here -> https://www.mql5.com/en/forum/119840

Yes you're right, but only in that case if the position, recalculated by the broker is works, the way you think it works(it is not documented). If the developers of MQL4 made some recalculation somehow else way, then it's a little bit questionable.


[...]


By the way you might can help me in my problem in https://forum.mql4.com/29652 this post. O:)

Reason: