EA is not carrying out the function I'm telling it to.

 

Morning folks! I have a TFJ here. 

I am literally incensed right now. I am absolutely fuming.

The stupidity of the way in which MT4 handles my EA is beyond me, and how on earth anyone could release an application onto the market is nothing short of amazing.

I have been coding for 4 years now and I have never come across anything as stupid, idiotic, moronic and as pathetic as what I am witnessing here, and I'm disgusted at the developers of MT4 and I am sick sick sick to the back teeth of ripping my hair out just because MT4 cannot be othered to do as it's told.

Please do not tell me that the problem is with me, or some smarmy line about how "the problem is in the space between the chair and the terminal" because quite frankly, I don't particularly want to hear it - if you're not going to say anything constructive then please scroll on. If the issue was with me I would not be here typing this post - so therefore, we can safely say that the issue is not with me. 

If I tell an algorithm to work, it works. If I tell an algorithm to close trades when I tell it to, it does just that: it closes trades. If I tell an algorithm to send a confirmation email, it does it. If I tell an algorithm to jump up and down with one hand on it's stomach and one hand on it's head - it does it.

It's an algorithm, not a disobedient child. 

So why the hell, and in what universe, does my EA think it's appropriate and correct for my EA to the following stupid and moronic things: 

  • furnished me with 4105 (Invalid Parameter) Errors in replacement of a MessageBox confirming the opening on the position?
  • refuses to close additional opened positions when there are two or more positions that have simultaneously been opened. 

See the code below, here is a "for" loop, going over the order ledger if multiple orders over different symbols have been placed. I only want one order to be opened so I have build a fail-safe feature that if multiple orders have been opened (int total = OrdersTotal();

No you would have thought that it would close ALL of the order except the fist one in the ledger - but oh no no no no no no no, of course that doesn't work, because why would it? I mean why would something work and therefore would make my life easier for the first time in 35 years? Why would be able to feel some tranquillity for just 5 minutes? Eh?

 int total = OrdersTotal();
            
            if(total >= 2){
            
               for(int i = total - 1; i >= 1; total --){
               
                  if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)){
                  
                     if(OrderType() == OP_BUY && OrderSymbol() == Symbol()){
                     
                        bool excessOrderClose = OrderClose(OrderTicket(),LotSize,MarketInfo(Symbol(),MODE_BID),300,clrAqua); i--; 
                     
                     }
                     
                     else if (OrderType() == OP_SELL && OrderSymbol() == Symbol()){
                     
                        bool excessOrderClose = OrderClose(OrderTicket(),LotSize,MarketInfo(Symbol(),MODE_ASK),300,clrAqua); i--; 
                     
                     }     /*OrderType() == OP_SELL*/
                  
                  }     /*(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))*/
               
               }     /*int i = total - 1; i >= 1; total -*/
            
            }     /*total >= 2*/

TFJ = Total F*****g Joke.

 
Todd Peter Gilbey:

Morning folks! I have a TFJ here. 

I am literally incensed right now. I am absolutely fuming.

for somebody that does not want unconstructive comments, you've gone about asking for help the wrong way with your rant...

first up if you don't want multiple orders open, check before you open them - much cheaper that way

as you are so certain that it is not you, tell us what the highlighted code is doing other than artificially reducing the number of loop iterations and being an incorrectly written for loop statement?

your code has other issues not related, but as you are certain it is not you and do not want help other than with the current problem clearly there is no point mentioning them, but for clarity the problem is with your code and if you can't accept that nobody can help you.

for(int i = total - 1; i >= 1; total --){
               
                 
bool excessOrderClose = OrderClose(OrderTicket(),LotSize,MarketInfo(Symbol(),MODE_BID),300,clrAqua); i--; 
                     
bool excessOrderClose = OrderClose(OrderTicket(),LotSize,MarketInfo(Symbol(),MODE_ASK),300,clrAqua); i--; 
 

Hi Paul,

You make a fair point regarding the checking for multiple trades first. I will work on this part. 

It's not an incorrectly-written for loop statement: i = OrdersTotal() - 1, the loop is supposed to run for all positions except the first one in the ledger, which is position 1, seeing as the numbering system in MT4 begins at zero, and it's a decrement loop - so from the newest to the oldest position.

As the the other highlighted "i--;", they are supposed to decrement the positions because as they close, it was unable to find the position because the position was already closed. the "i--;" was supposed to prevent this.

All the best,    

 
Todd Peter Gilbey #:

Hi Paul,

You make a fair point regarding the checking for multiple trades first. I will work on this part. 

It's not an incorrectly-written for loop statement: i = OrdersTotal() - 1, the loop is supposed to run for all positions except the first one in the ledger, which is position 1, seeing as the numbering system in MT4 begins at zero, and it's a decrement loop - so from the newest to the oldest position.

As the the other highlighted "i--;", they are supposed to decrement the positions because as they close, it was unable to find the position because the position was already closed. the "i--;" was supposed to prevent this.

All the best,    

The loop is wrong,  you are decrementing 'total' each loop instead of the loop counter i (see code below)  this means that the value of i is not changing at all via the loop definition the way you have written it.

The correctly written for loop will decrement the value of the loop counter (in your case i) after each loop as defined by the loop definition. You adding i--; into the processed code reduces the counter at a faster rate and will miss some loops. So remove the lines i--;

You do not need to reduce the value of total as you are merely using the value of total at the definition of the loop and not thereafter. 

for(int i = total - 1; i >= 1; total --)

should actually be:

for(int i=total -1; i>=1; i--)

 
Thanks Paul, it worked on the test EA but the one actually to be used. Your suggestion works perfectly. What a nightmare. 
 
Todd Peter Gilbey #:
Thanks Paul, it worked on the test EA but the one actually to be used. Your suggestion works perfectly. What a nightmare. 

you are welcome

 
  1. Todd Peter Gilbey: Please do not tell me that the problem is with me,

    The problem is with your code and you.

    Language you used will not get you help, it will get you banned.

    Your code was doing exactly what you told it to do; nothing more. You would have found the problem had you bothered to use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
              Code debugging - Developing programs - MetaEditor Help
              Error Handling and Logging in MQL5 - MQL5 Articles (2015)
              Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
              Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)


  2. if(OrderType() == OP_BUY && OrderSymbol() == Symbol()){
       bool excessOrderClose = OrderClose(OrderTicket(),LotSize,MarketInfo(Symbol(),MODE_BID),300,clrAqua); i--; 
    
    1. You can use OrderClosePrice() instead of Bid/Ask and be direction independent — no need to check order type to get the close price.

    2. Check your return codes, and report your errors (including market prices and your variables). Don't look at GLE/LE unless you have an error. Don't just silence the compiler (MT5/MT4+strict), it is trying to help you.
                What are Function return values ? How do I use them ? - MQL4 programming forum (2012)
                Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles (2014)

    3. Use OrderLots() instead of your variable in case of partial closes.

 
William Roeder #:

The problem is with your code and you.

Language you used will not get you help, it will get you banned.

Your code was doing exactly what you told it to do; nothing more. You would have found the problem had you bothered to use the debugger or print out your variables, including _LastError and prices and find out why. Do you really expect us to debug your code for you?
          Code debugging - Developing programs - MetaEditor Help
          Error Handling and Logging in MQL5 - MQL5 Articles (2015)
          Tracing, Debugging and Structural Analysis of Source Code - MQL5 Articles (2011)
          Introduction to MQL5: How to write simple Expert Advisor and Custom Indicator - MQL5 Articles (2010)

William, contrary to what you say about not getting me help, it did, the gentleman above corrected the problem. I thanked him, and told me I was welcome. Now the issue has resolved been resolved.

Oh and at what point did I ask you to debug my code William? Look through the original issue and point out to me where I asked you to debug my code? If you can find a line in my original post  with words to the effect of "I demand you do debug my code" I will apologise.  

Regarding the error reporting - yes, I did do that, because that is standard practice with coders. I was getting #4105 which means invalid parameters. Hence why I was confused and hence why I posted on here. 

Now the issue has been resolved. 

Any further complaints about me, William, or will that be all for today?

Reason: