compilation error mql4 editor

 

Can someone tell me why I am getting this compilation error.


declaration of 'tail1' hides global declaration at line 105 Gold Sep2016.mq4 28631 33
declaration of 'bar1' hides global declaration at line 104 Gold Sep2016.mq4 28673 11
internal error #-2  0 0
1 error(s), 6159 warning(s)  2 101

 
7325453863:

Can someone tell me why I am getting this compilation error.

In broad outline, your code must be doing something along the following lines:

// Defined at global level, outside any function
int variable = 1;

void Example()
{
   // Same variable name redefined inside a function
   int variable = 1;
}

But that only generates a compiler warning, not an error, even with #property strict defined. The error must be something different; perhaps the compiler going mad because your code appears to generate an eye-watering 6159 warnings.

I'm going to hazard a guess that any code with 28000+ lines, where you don't appear to be aware of its structure, has been generated by some sort of automated code builder. And not, by the looks of it, a very good one.

 
jjc:

In broad outline, your code must be doing something along the following lines:

But that only generates a compiler warning, not an error, even with #property strict defined. The error must be something different; perhaps the compiler going mad because your code appears to generate an eye-watering 6159 warnings.

I'm going to hazard a guess that any code with 28000+ lines, where you don't appear to be aware of its structure, has been generated by some sort of automated code builder. And not, by the looks of it, a very good one.

Thanks for suggestions. I have many global variables which I also define within a function level as a local variable. I have been programming (starting with fortran IV in 1967) for quite some time and as far as I know that should not have caused the problem. I have coded my ea line by line and it is not 28000 lines but 112,000 lines. I did not have any problem until last 2 builds or so and metaquote seem to have done something about the memory usage.  I am saying because sometimes I get error telling me that "out of memory" message.  My computer has 4gb memory and that should be more than enough to compile even a 500,000 line program. I even keep my browser window etc closed.

But as a first attempt to solve the problem, I will work on removing definitions of a global variable as local variable and see that happens.

 
7325453863:

Thanks for suggestions. I have many global variables which I also define within a function level as a local variable. I have been programming (starting with fortran IV in 1967) for quite some time and as far as I know that should not have caused the problem. I have coded my ea line by line and it is not 28000 lines but 112,000 lines. I did not have any problem until last 2 builds or so and metaquote seem to have done something about the memory usage.  I am saying because sometimes I get error telling me that "out of memory" message.  My computer has 4gb memory and that should be more than enough to compile even a 500,000 line program. I even keep my browser window etc closed.

But as a first attempt to solve the problem, I will work on removing definitions of a global variable as local variable and see that happens.

  1. The "hides global declaration" is a warning, not an error/problem. It is just to make you aware of a possible conflict, just in case the coder in fact does need access to the Globally declared variable and accidentally declared a Local variable withe same name.

    Obviously, if you understand that the global variable in question will not be accessible, there is no problem!

    However, if you adopt a naming convention for local variables so as to never be the same as global variables, it will at least prevent the warning or accidental clashes. For example, starting all local variables in functions with an "_" underscore is an acceptable way to prevent the warning, but there are many different naming conventions you can adopt depending on your coding style.

  2. As for your "memory" problem, that is something else completely. I don't know what MetaTrader's compile limits are, but for me, 112000 lines of code in a single EA is rather huge and that amount of code can certainly do with some streamlining and cleanup. The largest EA I have ever coded was 6000 lines and that was already quite intricate. Now 112000 seems overkill for me.

    However, is your "out of memory" message a compile error or a run-time error during EA execution?
 
Missing braces/parentheses in large compile units can really confuse compilers. Match them in an external app like notepad2.
 

I was getting this out of memory error on an off all day yesterday. But eventually it was able to compile. Now it just keep coming out with this error message. "out of memory".


I removed almost all declarations of local variables which were already declared at the global level. Still can not get around compiling.

Some warning messages I can not figure out e.g. look at the code of two functions. Can you tell me why I am getting this warning.


double LastBuyLoss()
{
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
   {
      result = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderType()==OP_BUY && OrderSymbol()==symbol &&
         OrderMagicNumber()>=basemagic+minmagic && OrderMagicNumber()<=basemagic+maxmagic)
         return(OrderProfit() + OrderSwap() + OrderCommission());
      else return(0);
   }
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double LastSellLoss()
  {
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      result = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderType()==OP_SELL && OrderSymbol()==symbol &&
         OrderMagicNumber()>=basemagic+minmagic && OrderMagicNumber()<=basemagic+maxmagic)
            return(OrderProfit() + OrderSwap() + OrderCommission());
            else return(0);
     }
  }

'Gold Oct2016.mq4' Gold Oct2016.mq4 1 1
'stderror.mqh' stderror.mqh 1 1
'stdlib.mqh' stdlib.mqh 1 1
not all control paths return a value Gold Oct2016.mq4 2793 1
not all control paths return a value Gold Oct2016.mq4 2807 3

 

Please ignore my last message. I already figured out the problem and new code is as below.


double LastBuyLoss()
{
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
   {
      result = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderType()==OP_BUY && OrderSymbol()==symbol &&
         OrderMagicNumber()>=basemagic+minmagic && OrderMagicNumber()<=basemagic+maxmagic)
         return(OrderProfit() + OrderSwap() + OrderCommission());
      else continue;
   }
   return(0);
}
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double LastSellLoss()
  {
   for(int i=OrdersHistoryTotal()-1; i>=0; i--)
     {
      result = OrderSelect(i,SELECT_BY_POS,MODE_HISTORY);
      if(OrderType()==OP_SELL && OrderSymbol()==symbol &&
         OrderMagicNumber()>=basemagic+minmagic && OrderMagicNumber()<=basemagic+maxmagic)
            return(OrderProfit() + OrderSwap() + OrderCommission());
            else continue;
     }
     return(0);
  }

 
7325453863:

Please ignore my last message. I already figured out the problem and new code is as below.

One comment on the code operation, and four comments on the code style.

Firstly, the documentation of OrderSelect() says "Sorting of the resulting list of orders cannot be guaranteed." You are relying on the history being in time order; the platform doesn't actually guarantee this. In practice, you will be selecting the most recent of the historic trades based on their open times, not their close times. This may or may not be want you want (and may not matter if there is no possibility that you can have concurrent trades in the same symbol).

Secondly:

  • You must still have a global variable "result" into which the success of OrderSelect() is dumped and ignored. It would be more normal to wrap the subsequent code in "if (OrderSelect(...)) {"
  • The "else continue" looks like a legacy from your Fortran days. Removing it has the same effect as keeping it; it serves no purpose in MQL4.
  • The functions are called LastBuyLoss and LastSellLoss but select the last closed order regardless of profit/loss, and return that profit or loss.
  • One of the reasons why you have 112,000 lines of code is that you have two nearly-identical functions when you could have one function with a parameter telling it what type of order to look at.

 For example, you could do LastResult(OP_BUY) and LastResult(OP_SELL) instead of having separate functions for each of these:

// Subject to the caveats above about the order of SELECT_BY_POS...
double LastResult(int cmd)
{
   for (int i = OrdersHistoryTotal() - 1; i >= 0; i--) {
      if (OrderSelect(i,SELECT_BY_POS,MODE_HISTORY)) {
         if (OrderType() == cmd && OrderSymbol() == symbol && OrderMagicNumber() >= basemagic+minmagic && OrderMagicNumber() <= basemagic+maxmagic) {
            return OrderProfit() + OrderSwap() + OrderCommission();       
         }
      }   
   }
   return 0;  
}
 

Thanks for taking time to make suggestions. This ea has evolved over many years. And there is some scope for improvement.

Even if it is not perfect style, it has been working very well and giving me terrific results. So I focus on improving the win rate rather than spending time on style.

I have removed all the unnecessary use of local variables because it should improve runtime efficiency.

Now I have 0 warnings but I  still get "out of memory" message and it is driving me nut. Why was it creating executable code just before a couple of days even with 6000+ warning

and now even with 0 warning, it is failing to produce executable code. My laptop has 4gb memory and there is absolutely nothing else running, not even browser.


About size of the program, it is big but very simple and organized. I have 360 buy and 360 sell modules which are not complex at all.


I am leaning towards conclusion that some unpublished changes has happened to the compiler or to the libraries that are included.


I really do not know what to do now. Do you think removing a lots of print statements is going to help?


'Gold Oct2016.mq4' Gold Oct2016.mq4 1 1
'stderror.mqh' stderror.mqh 1 1
'stdlib.mqh' stdlib.mqh 1 1
out of memory  1 1
1 error(s), 0 warning(s)  2 1

 
7325453863:

I really do not know what to do now. Do you think removing a lots of print statements is going to help?

I'm afraid that I have no idea. I can only speculate that 100,000+ lines of code is outside what the compiler is meant to be able to handle. And that you only have 100,000+ lines of code because you could, and for many reasons should, consolidate many near-identical functions into a single parameterised function.
 

If I can get a concrete idea of what are the limits of the compiler, then I can attempt to fall within those boundaries. But you are not addressing one of my questions.

Just a few days ago, compiler after giving 6000+ warnings, was still creating .exe file which gave me the results that I am looking for. So what has changed in last few days.

One other thing, I copied the program to my another laptop which is running under window 10 but has 8 gb of memory. Program did compile and did produce a .exe file which I am running now.

So the conclusion should be that I have not exceeded any limitations of the compiler but it is now needing a lot more memory to compile. For old timer like me, never seen a compiler that can not compile

with 4gb. (Just about your comment about my fortran days, I coded after that in Basic, Rexx, Pl/I, algol, focus and many other langauages. I am also a certified PL/SQL programmer by oracle. So I know a little about the computer world. I am also certified Oracle database administrator. I retired 12 years ago from my IT career of over 35 years.).

Reason: