Why does compilers says "variable already defined"

 
Hi coders

I get compiler error "variable already defined". Why

Please advise
Thanks

void OnTick()
  {
//--- 
   int ticket,i,total,result;
   total = OrdersTotal(); // same variable without errors here line 36, but not on line 43 ??? 
     
   double   faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL

   //total = OrdersTotal();  //If I put code here instead I gets compiler errors "variable already defined"
 
  1.    int ticket,i,total,result;
       ⋮
       double   faster = iMACD(…),
                slower = iMACD(…),
                faster_2 = iMACD(…),
                slower_2 = iMACD(…),     
                total    = OrdersTotal();

    You defined total at the top, you can't define it a second time or change its type to double.

  2. double   faster_2 = iMACD(NULL,PERIOD_H4,

    On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 2019.05.20

 
William Roeder:
  1. You defined total at the top, you can't define it a second time or change its type to double.

  2. On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
              Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4 2019.05.20


Thanks
I think I understand your first answer. I need a semi colon at the end of where I define my double variables right ?  Then I should be able to assign a value to "totals". 

Like this

void OnTick()
  {
//--- 
   int ticket,i,total,result; 
     
   double   faster = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower = iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL

   total = OrdersTotal();
   
      
       
  }

I think I understand what your referenced error handling code is doing but not completely. I don't think I completely understand this part "

period = (ENUM_TIMEFRAMES)_Period;



Thanks

 
Agent86: I think I understand what your referenced error handling code is doing but not completely. I don't think I completely understand this part "
period = (ENUM_TIMEFRAMES)_Period;

I didn't post that code, you didn't originally post that code. Go back and re-read #1.2 and its accompanying link.

 
William Roeder:

I didn't post that code, you didn't originally post that code. Go back and re-read #1.2 and its accompanying link.

I know but you referenced Page 3 #26 No 4 as indicated by the link you posted.

On MT4: Unless the current chart is that 
specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - Forex Calendar - MQL4 programming forum - Page 3 #26 № 4  2019.05.20


The code shown in that link is this:

bool     download_history(ENUM_TIMEFRAMES period=PERIOD_CURRENT){
   return download_history(_Symbol, period);
}
bool     download_history(SYMBOL symbol, ENUM_TIMEFRAMES period=PERIOD_CURRENT){
   if(period == PERIOD_CURRENT period = (ENUM_TIMEFRAMES)_Period;
   ResetLastError();    datetime other = iTime(symbol, period, 0);
   if(_LastError == 0 && other != 0)   return true;
   if(_LastError != ERR_HISTORY_WILL_UPDATED
   && _LastError != ERR_NO_HISTORY_DATA
     )   PrintFormat("iTime(%s,%i) Failed: %i", symbol, period, _LastError);
   return false;
}

I see your referenced #2 = 

double   faster_2 = iMACD(NULL,PERIOD_H4,


HA HA HA .... wait wait wait.  Am I making this too complicated ? 

Are you saying that the only thing I have to do is change "NULL" to Symbol() in order to call the specific symbol(s)/TF(s) referenced ? 

So from what I'm reading it looks like I can write like this ? 

double   faster_2 = iMACD(Symbol(),PERIOD_H4
//or "EURUSD" or whatever "Symbol" string ? 




Please confirm this
Thanks 

Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
Runtime Errors - Codes of Errors and Warnings - Constants, Enumerations and Structures - MQL4 Reference
  • docs.mql4.com
The GetLastError() function return last error code, stored in _LastError predefined variable. This value can be reset using the ResetLastError() function. Error code constants defined at stderror.mqh file. To print text messages use ErrorDescription() function defined at stdlib.mqh file.
 
Agent86: Am I making this too complicated ?  Are you saying that the only thing I have to do is change "NULL" to Symbol() in order to call the specific symbol(s)/TF(s) referenced ? 
  1. Yes you are. You have to call the function provided (with H4) at the beginning of start and if it returns false, you return and wait for a new tick. Otherwise, your MACD(H4) will be bogus.

  2. I said no such thing. I said "unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values." See  № 1

  3. As far as NULL, I don't use NULL.
    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25
 
William Roeder:
  1. Yes you are. You have to call the function provided (with H4) at the beginning of start and if it returns false, you return and wait for a new tick. Otherwise, your MACD(H4) will be bogus.

  2. I said no such thing. I said "unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values." See  № 1

  3. As far as NULL, I don't use NULL.
    1. On MT4, you can use NULL in place of _Symbol only in those calls that the documentation specially says you can. iHigh does, iCustom does, MarketInfo does not. OrderSend does not.
    2. Don't use NULL (except for pointers where you explicitly check for it.) Use _Symbol and _Period, that is minimalist as possible and more efficient.
    3. Zero is the same as PERIOD_CURRENT which means _Period. Don't hard code numbers.
    4. MT4: No need for a function call with iHigh(NULL,0,s) just use the predefined arrays, i.e. High[].
    5. Cloud Protector Bug? - MQL4 programming forum 2020.07.25


I'm confused about this
Isn't the if(expression) a call to M5 ? 
Like this ? 

void OnTick()
  {
//--- 
   int ticket,i,total; 
     
   double   faster_1 = iMACD("EURUSD",PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_1 = iMACD("EURUSD",PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_2 = iMACD("EURUSD",PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD("EURUSD",PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_3 = iMACD("EURUSD",PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_3 = iMACD("EURUSD",PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL


   total  = OrdersTotal();
   
   
   if(total < 1)
      {
      if(faster_1 > slower_1 && faster_2 > slower_2)


Or does it need to be like this ?  If so I don't completely understand why. 

void OnTick()
  {
//---  
     
   double   faster_1 = iMACD("EURUSD",PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_1 = iMACD("EURUSD",PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_2 = iMACD("EURUSD",PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD("EURUSD",PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_3 = iMACD("EURUSD",PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_3 = iMACD("EURUSD",PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
  
   
 
   if(faster_3>slower_3)
   Print ("H4 Higher = True");
   //some code here if(faster_1>slower_1) etc etc.
   else
   Print ("H4 Higher = False");
   return;
   
   if(faster_3<slower_3)
   Print ("H4 Lower = True"); 
   else
   Print ("H4 Lower = False");
   return;

Thanks


 
Agent86: I'm confused about this Isn't the if(expression) a call to M5 ?
           faster_2 = iMACD("EURUSD",PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD("EURUSD",PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
⋮
      if(faster_1 > slower_1 && faster_2 > slower_2)

The if only compares two sets of doubles. If those doubles have garbage then your compare is bogus.

What part of "you must handle 4066/4073 errors before accessing" was unclear? Did you follow #1.2 or #5.1?

YOU DID NOT. So your doubles are bogus as is your compares.

 
William Roeder:

The if only compares two sets of doubles. If those doubles have garbage then your compare is bogus.

What part of "you must handle 4066/4073 errors before accessing" was unclear? Did you follow #1.2 or #5.1?

YOU DID NOT. So your doubles are bogus as is your compares.


Regarding #1.2

I'm sure your instruction is clear but I'm struggling make sense of the information.
How can I tell if I'm getting errors that need to be handled. 
 


Shouldn't Print(GetLastError()); give me these errors in order to handle them ?

I get zero for GetLastError()

void OnTick()
  {
//---  
     
   double   faster_1 = iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_1 = iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_2 = iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_3 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_3 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL
      
 
   if(faster_3>slower_3)
   Print ("H4 Higher = True"," ","Code = ",GetLastError());
   else
   Print ("H4 Higher = else False"," ","Code = ",GetLastError());
   
   if(faster_3<slower_3)
   Print ("H4 Lower = True"," ","Code = ",GetLastError()); 
   else
   Print ("H4 Lower = else False"," ","Code = ",GetLastError());

   
 
   if(faster_2>slower_2)
   Print ("M5 Higher = True"," ","Code = ",GetLastError());
   else
   Print ("M5 Higher = else False"," ","Code = ",GetLastError());
   
   
   if(faster_2<slower_2)
   Print ("M5 Lower = True"," ","Code = ",GetLastError()); 
   else
   Print ("M5 Lower = else False"," ","Code = ",GetLastError());


 



 
Agent86: Shouldn't Print(GetLastError()); give me these errors in order to handle them ?
  1. No. The moment you call a second function, you've lost the error from the first.

    Check your return codes, and report your errors.

    Don't look at GLE/LE unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  2. I explained the problem. I gave you the solution. I told you to call it before accessing values. You have ignored me three times. I can't help you.
 
William Roeder:
  1. No. The moment you call a second function, you've lost the error from the first.

    Check your return codes, and report your errors.

    Don't look at GLE/LE unless you have an error. Don't just silence the compiler, it is trying to help you.
              What are Function return values ? How do I use them ? - MQL4 programming forum
              Common Errors in MQL4 Programs and How to Avoid Them - MQL4 Articles

    Only those functions that return a value (e.g. iClose, MarketInfo, etc.) must you call ResetLastError before in order to check after.

  2. I explained the problem. I gave you the solution. I told you to call it before accessing values. You have ignored me three times. I can't help you.


Sorry I'm just not understanding how to see the errors. I always get 0. 
Compiler has 0 errors and 0 warnings 


void OnTick()
  {
//---  
     
   double   faster_1 = iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_1 = iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_2 = iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_3 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_3 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL 
   
   
   Print (faster_3," ", GetLastError());
   Print (slower_3," ", GetLastError());
      
}
0       19:22:58.738    testcodes EURUSD,M5: 1.512320472575447e-05 0
0       19:23:03.296    testcodes EURUSD,M5: -0.001422931064830824 0
0       19:23:03.296    testcodes EURUSD,M5: 1.512320472575447e-05 0
0       19:24:06.211    testcodes EURUSD,M5: -0.001422931064830824 0
0       19:24:06.211    testcodes EURUSD,M5: 1.512320472575447e-05 0
0       19:24:17.002    testcodes EURUSD,M5: -0.001422931064830824 0
0       19:24:17.002    testcodes EURUSD,M5: 1.512320472575447e-05 0
0       19:24:17.081    testcodes EURUSD,M5: -0.001422931064830824 0
0       19:24:17.081    testcodes EURUSD,M5: 1.512320472575447e-05 0
0       19:24:19.541    testcodes EURUSD,M5: -0.001422931064830824 0
0       19:24:19.541    testcodes EURUSD,M5: 1.512320472575447e-05 0
0       19:24:19.885    testcodes EURUSD,M5: -0.001422931064830824 0
0       19:24:19.885    testcodes EURUSD,M5: 1.512320472575447e-05 0
I don't get errors from this either if I understand this at all. 

void OnTick()
  {
//---  
     
   double   faster_1 = iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_1 = iMACD(NULL,PERIOD_M1,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_2 = iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_2 = iMACD(NULL,PERIOD_M5,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1), //MODE_SIGNAL
            faster_3 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_MAIN,1), //MODE_MAIN
            slower_3 = iMACD(NULL,PERIOD_H4,12,26,9,PRICE_CLOSE,MODE_SIGNAL,1); //MODE_SIGNAL 
   ResetLastError();
   if(faster_3)
      {
      if(GetLastError()!=0)
      Print("Error"," ",GetLastError());
     
      }
    return;  
}
Reason: