Stack Overflow problem associated with OrderClose command

 

Hi,


I got a stack overflow problem running EA.

The problem was traced to the portion of EA codes below:  


if (..............)

(

Print Statement A

               bool OCcheck = OrderClose(OrderTicket(),OrderLots(),Bid,3,Magenta); // close position

                

               icount = 0;

              

               while(OCcheck != TRUE)

               {         

               RefreshRates();

               Sleep(1);

               OCcheck = OrderClose(OrderTicket(),OrderLots(),Bid,3,Magenta);

               icount++;

               }


Print Statement B

-----------------------

-----------------------


The symptom of the problem is shown below from EA log:

2022.02.09 12:15:02.240               sm3b_4 EURUSD,H4: close #161478286 buy 0.20 EURUSD at 1.14397 at price 1.14212

2022.02.09 12:15:01.822               Stack overflow in 'C:\Users\Wally\AppData\Roaming\MetaQuotes\Terminal\3294B546D50FEEDA6BF3CFC7CF858DB7\MQL4\Experts\sm3b_4.ex4'

2022.02.09 12:15:01.762               sm3b_4 EURUSD,H4: sell close bid  1.1421 (This is Print Statement A)

...

...

...


The above shows it to be a stack overflow problem. The order got executed and the EA still continues running but stop printing print statements.  The last printing recorded in EA log is Print Statement A. Print Statement B is not printed.

I thought the problem occurring at the while loop where looping goes forever. So I commented off the while loop as shown below.  

               //while(OCcheck != TRUE)

              // {         

              //RefreshRates();

              // Sleep(1);

              // OCcheck = OrderClose(OrderTicket(),OrderLots(),Bid,3,Magenta);

              // icount++;

              // }


However, I still get exactly same result.  It looks like the problem is associated with the OrderClose command. This stack overflow problem repeatedly happened every time. In the past (more than several months ago) I never had such problem in this portion of the EA codes which I never changed. It shows up now every time. I could not find a solution to fix it. Would very much appreciate anyone who can shed the light on this problem and help find the solution.


Many Thanks.   

 


Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 

Hi Everyone,


I would like to make more clarification to my posting yesterday as follows:

(1) Correction to the statement yesterday - The EA actually stop running although the smiley face of EA still on. As shown in the EA log which I post again in below, even when stack overflow happened, the order still got executed (filled).


2022.02.09 12:15:02.240               sm3b_4 EURUSD,H4: close #161478286 buy 0.20 EURUSD at 1.14397 at price 1.14212

2022.02.09 12:15:01.822               Stack overflow in 'C:\Users\Wally\AppData\Roaming\MetaQuotes\Terminal\3294B546D50FEEDA6BF3CFC7CF858DB7\MQL4\Experts\sm3b_4.ex4'

2022.02.09 12:15:01.762               sm3b_4 EURUSD,H4: sell close bid  1.1421 (This is Print Statement A)


(2) The stack overflow seems to be associated with OrderClose command in the above. This same stack overflow problem also occurred to OrderSend command. They were also verified to occur when EA runs in either Oanda or Pepperstone MT4 platform


(3) This problem never happened in the past even several months ago when I last ran this EA.


This is a weird problem and has dragged me for more than a week. I could not solve it. Would very much appreciate if any of the experts in this forum can provide an insight. 


Thanks a lot in advance!

 
wlinpepper #:

Hi Everyone,


I would like to make more clarification to my posting yesterday as follows:

(1) Correction to the statement yesterday - The EA actually stop running although the smiley face of EA still on. As shown in the EA log which I post again in below, even when stack overflow happened, the order still got executed (filled).


2022.02.09 12:15:02.240               sm3b_4 EURUSD,H4: close #161478286 buy 0.20 EURUSD at 1.14397 at price 1.14212

2022.02.09 12:15:01.822               Stack overflow in 'C:\Users\Wally\AppData\Roaming\MetaQuotes\Terminal\3294B546D50FEEDA6BF3CFC7CF858DB7\MQL4\Experts\sm3b_4.ex4'

2022.02.09 12:15:01.762               sm3b_4 EURUSD,H4: sell close bid  1.1421 (This is Print Statement A)


(2) The stack overflow seems to be associated with OrderClose command in the above. This same stack overflow problem also occurred to OrderSend command. They were also verified to occur when EA runs in either Oanda or Pepperstone MT4 platform


(3) This problem never happened in the past even several months ago when I last ran this EA.


This is a weird problem and has dragged me for more than a week. I could not solve it. Would very much appreciate if any of the experts in this forum can provide an insight. 


Thanks a lot in advance!

You could try using a for loop instead of a while loop. 
I'm not sure if the ''icount++'' is an iteration limiter, but if it is, it does not limit the number of iterations since it is not checked inside the while loop.

As your code is right now, if an error occurs in OrderClose, it will never stop trying.. 500 times per second**.. maybe causing the Overflow.
Make sure to always use IsStopped() in your while loop if you do use one. And be REALLY careful with while loops.... I only use for loops for this very reason.

**Instead of sleeping 1ms between attemps, just wait for the next tick or terminate the program if the error is critical for your strategy.

PS. You should use the Code Button to post your code, it makes it easier for people to help you out.

Cheers!

 
Jeremie Courchesne #:

You could try using a for loop instead of a while loop. 
I'm not sure if the ''icount++'' is an iteration limiter, but if it is, it does not limit the number of iterations since it is not checked inside the while loop.

As your code is right now, if an error occurs in OrderClose, it will never stop trying.. 500 times per second**.. maybe causing the Overflow.
Make sure to always use IsStopped() in your while loop if you do use one. And be REALLY careful with while loops.... I only use for loops for this very reason.

**Instead of sleeping 1ms between attemps, just wait for the next tick or terminate the program if the error is critical for your strategy.

PS. You should use the Code Button to post your code, it makes it easier for people to help you out.

Cheers!

Thanks a lot for your suggestion!

My apology that I am new in this forum and not familiar with the functional buttons here. I just clicked the reply button and hopefully it replies my message to you.

As mentioned in the earlier posting, I commented off the while loop and the result was still the same. The stack overflow occurred as soon as OrderClose is executed. This was verified with one experiment in that I put one print statement right after the OrderClose statement and it didn't print.

I actually just found out what was causing this problem and fixed it. The way I approched is that I find one different version of EA that uses exactly same block of codes involving OrderClose command but does not exhibit the stackoverflow problem here. I modify this EA into a new EA by moving the codes that implements all the strategies in the problematic EA. Then I run it and it produces the same stackoverflow problem. Then I diff the two EA's to find out what are the key difference between the two EA codes. I find the most suspicious culprit may be the about 10 new 1D arrays of large size (40000) in the problematic EA. This gives the clue. Once these array sizes are reduced to 4000, the EA stops producing stackoverflow problem.


So, it looks like this problem is linked to array memory usage. However, it is still unknown to me how this memory usage affects the OederClose command operation. Anyway, the issue is solved here! Many thanks for your dedicated support to your fellow forum member!