OrderClose error 4108 I cant see why?

 

Im testing the EA ive attached when on strategy tester I test over a short period of between 01/09/2015 to 15/09/2015 there are no error codes and the EA appears to work okay.

But, when i test on longer periods from between 04/05/2015 to  15/09/2015 i get both the following errors unkown ticket 63 for OrderClose function followed by OrderClose error 4108?

 I cant see why it appears to work on a small test but not on a larger test when its the exact same code?

Files:
testx8.mq4  25 kb
testj8.ex4  27 kb
testu8.txt  22 kb
 
            if(highestHigh > R3) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);
            if(highestHigh > midR2 && Bid < R2 + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);                                         
            if(highestHigh > R2 && Bid < midR1 + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);            
            if(highestHigh > midR1 && Bid < R1 + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);                             
            if(highestHigh > R1 && Bid < midPivot + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);                
            if(highestHigh > midPivot && Bid < pivot + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);                                         
            if(highestHigh > pivot && Bid < midS1 + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen); 
            if(highestHigh > midS1 && Bid < S1 + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen); 
            if(highestHigh > S1 && Bid < midS2 + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen); 
            if(highestHigh > midS2 && Bid < S2 + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);                                                                                                        
            if(highestHigh > S2 && Bid < midS3 + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);
            if(highestHigh > midS3 && Bid < S3 + 30 * _Point) bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);               
       

Probably, there are occasions where more than one of the conditions is true in a block of code like this. If so, it will try to close the same order multiple times

Try changing it to

            if((highestHigh > R3)
            || (highestHigh > midR2 && Bid < R2 + 30 * _Point) 
            || (highestHigh > R2 && Bid < midR1 + 30 * _Point)            
            || (highestHigh > midR1 && Bid < R1 + 30 * _Point)                            
            || (highestHigh > R1 && Bid < midPivot + 30 * _Point)                
            || (highestHigh > midPivot && Bid < pivot + 30 * _Point)                                        
            || (highestHigh > pivot && Bid < midS1 + 30 * _Point) 
            || (highestHigh > midS1 && Bid < S1 + 30 * _Point) 
            || (highestHigh > S1 && Bid < midS2 + 30 * _Point) 
            || (highestHigh > midS2 && Bid < S2 + 30 * _Point)                                                                                                       
            || (highestHigh > S2 && Bid < midS3 + 30 * _Point)
            || (highestHigh > midS3 && Bid < S3 + 30 * _Point))
                  bool orderclose = OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,clrGreen);         

and do the same for the other similar block 

Not tested

 
renzbub:

Im testing the EA ive attached when on strategy tester I test over a short period of between 01/09/2015 to 15/09/2015 there are no error codes and the EA appears to work okay.

But, when i test on longer periods from between 04/05/2015 to  15/09/2015 i get both the following errors unkown ticket 63 for OrderClose function followed by OrderClose error 4108?

 I cant see why it appears to work on a small test but not on a larger test when its the exact same code?

  

Yes great! I have tested this and it does stop the 2 errors appearing in journal but, unfortunately it still continues to close orders straight way for no apparent reason.

 Again when I test for short periods within a month it doesn't have any problems. But, when I test for longer periods it often closes my order straight away.

 Im sure the problem lies within the if statement now edited? Somehow its as if im overloading the EA?  

 
renzbub:

Yes great! I have tested this and it does stop the 2 errors appearing in journal but, unfortunately it still continues to close orders straight way for no apparent reason.

 Again when I test for short periods within a month it doesn't have any problems. But, when I test for longer periods it often closes my order straight away.

 Im sure the problem lies within the if statement now edited? Somehow its as if im overloading the EA?  

You didn't mention this in your original post.

There is always a reason.

With so many possible conditions to exit a trade, it is possible that one of them at least coincides with conditions to open a trade. If such a case occurs the order will be closed immediately.

Write in your code a print statement stating conditions to open a trade when the trade is opened 


Write in your code a print statement stating conditions to close a trade when the trade is closed

See if they coincide 

 
GumRai:

You didn't mention this in your original post.

There is always a reason.

With so many possible conditions to exit a trade, it is possible that one of them at least coincides with conditions to open a trade. If such a case occurs the order will be closed immediately.

Write in your code a print statement stating conditions to open a trade when the trade is opened 


Write in your code a print statement stating conditions to close a trade when the trade is closed

See if they coincide 

Not sure how I would write a print statement stating conditions to open a trade when the trade is opened.

I've tried the code below but to no avail?

There's two things I don't understand in what your saying.

1) How can I open a another trade when one trade is already opened? This would cause errors?

2) How can a Print statement open a trade? 

            // Pivots to close on Sell Order            
            if((lowestLow < midR2 && Ask > R3 - 30 * _Point) 
            ||(lowestLow < R2 && Ask > midR2 - 30 * _Point) 
            ||(lowestLow < midR1 && Ask > R2 - 30 * _Point)
            ||(lowestLow < R1 && Ask > midR1 - 30 * _Point)               
            ||(lowestLow < midPivot && Ask > R1 - 30 * _Point)                            
            ||(lowestLow < pivot && Ask > midPivot - 30 * _Point)
            ||(lowestLow < midS1 && Ask > pivot - 30 * _Point)            
            ||(lowestLow < S1 && Ask > midS1 - 30 * _Point) 
            ||(lowestLow < midS2 && Ask > S1 - 30 * _Point)              
            ||(lowestLow < S2 && Ask > midS2 - 30 * _Point) 
            ||(lowestLow < midS3 && Ask > S2 - 30 * _Point) 
            ||(lowestLow < S3)) 
            bool orderclose = OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrRed);    

            if(gSellTicket > 0) 
            {
               Print(gSellTicket);  
            }
            else
            {
               Print(orderclose);
            }  
 
renzbub:

Not sure how I would write a print statement stating conditions to open a trade when the trade is opened.

I've tried the code below but to no avail?

There's two things I don't understand in what your saying.

1) How can I open a another trade when one trade is already opened? This would cause errors?

2) How can a Print statement open a trade? 

Possibly resolved problem? Ive found that I forgot to reset my barCount object back to 0 when one of the conditions are found to be true. Seen here :

 

         // Check sell order for stop placement
         if(OrderType() == OP_SELL)
         {
            static int barCount = TimeMinute(TimeCurrent()); // Creating this static variable I can use this to count each bar from order open
            barCount++;  
            
            int lowest = iLowest(NULL,0,MODE_LOW,barCount,1);   
            double lowestLow = Low[lowest]; 
                       
            // Pivots to close on Sell Order            
            if((lowestLow < midR2 && Ask > R3 - 30 * _Point) 
            ||(lowestLow < R2 && Ask > midR2 - 30 * _Point) 
            ||(lowestLow < midR1 && Ask > R2 - 30 * _Point)
            ||(lowestLow < R1 && Ask > midR1 - 30 * _Point)               
            ||(lowestLow < midPivot && Ask > R1 - 30 * _Point)                            
            ||(lowestLow < pivot && Ask > midPivot - 30 * _Point)
            ||(lowestLow < midS1 && Ask > pivot - 30 * _Point)            
            ||(lowestLow < S1 && Ask > midS1 - 30 * _Point) 
            ||(lowestLow < midS2 && Ask > S1 - 30 * _Point)              
            ||(lowestLow < S2 && Ask > midS2 - 30 * _Point) 
            ||(lowestLow < midS3 && Ask > S2 - 30 * _Point) 
            ||(lowestLow < S3))                                    
            { 
               bool orderclose = OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,clrRed);
               barCount = 0;
            }  
          }  

 

Without this being reset it messes up the lowest and so will incorrectly close an order because the 4th parameter is telling the function to search for the lowest to far back. Instead of only searching for lowest from when the OrderType() is found.

But, for all other trades it appears to be working fine. I see there is only once its closing an order to soon.

Reason: