MQL4 - automated forex trading   /  

Forum

Variable not defined

Back to topics list To post a new topic, please log in or register

avatar
130
Brygada 2010.11.13 22:06 

Error: variable not defined.

What can be wrong here? Thanks.

int start()
  {
    for(int a=0;a<Bars;a++)
    double A = High[a] – Open[a];
   return(0);
  }

Transferring an Indicator Code into an Expert Advisor Code. General Structural Schemes of an Expert Advisor and Indicator Functions

Transferring an Indicator Code into an Expert Advisor Code. General Structural Schemes of an Expert Advisor and Indicator Functions

This article dwells on the ways of transferring an indicator code into an Expert Advisor Code and on writing Expert Advisors with no calling to custom indicators, and with the whole program code for the calculation of necessary indicator values inside the Expert Advisor. This article gives a general scheme of Expert Advisor changing and the idea of building an indicator function based on a custom indicator. The article is intended for readers, already having experience of programming in MQL4 language.


avatar
1138
jjc 2010.11.13 22:17 
Brygada:

Error: variable not defined.

The - symbol as posted in your code is ASCII symbol 150, not ASCII symbol 45.

avatar
130
Brygada 2010.11.13 22:30 
jjc:
The - symbol as posted in your code is ASCII symbol 150, not ASCII symbol 45.


Thanks. Can you please correct the code to be usable.?


avatar
1138
jjc 2010.11.13 22:38 
Brygada:

Thanks. Can you please correct the code to be usable.?

I know it looks identical, but the - in this code is a different character...

int start()
  {
    for(int a=0;a<Bars;a++)
    double A = High[a] - Open[a];
   return(0);
  }


avatar
130
Brygada 2010.11.13 22:58 
jjc:

I know it looks identical, but the - in this code is a different character...


It works. Thanks for your help.

avatar
2
ex4tomq4 2010.11.13 23:07 
int start()
  {
    double A = 0.0;

    for(int a=0; a < Bars; a++){
      A = High[a] - Open[a]; // if you put the 'double A' here then the ea will create 'A' variable each time the loop returns to the next increment value.
    }                       // as a result you will not get your desired value. Initialize the 'A' variable before the loop starts.
   
    return(0);
  }


avatar
Moderator
2030
gordon 2010.11.14 00:08 

ex4tomq4:

int start()
  {
    double A = 0.0;

    for(int a=0; a < Bars; a++){
      A = High[a] - Open[a]; // if you put the 'double A' here then the ea will create 'A' variable each time the loop returns to the next increment value.
    }                       // as a result you will not get your desired value. Initialize the 'A' variable before the loop starts.
   
    return(0);
  }

The code won't work as 'expected' because variable A will simply be equal to High[Bars-1]-Open[Bars-1] at the end of the loop. The loop actually does nothing, A is overwritten each iteration with a value that does not rely on any previous iteration values.

Your explanation of why it doesn't work is not entirely correct in MQL4 (maybe it is in other programming languages). Example:

int start()
{ 
   int cnt1;                        // declared outside loop
   for(int i = 0; i < 5; i++) {
      cnt1 = cnt1 + i;
      Print("cnt1 = ", cnt1);
   }
   
   for(int j = 0; j < 5; j++) {
      int cnt2 = cnt2 + j;          // declared inside loop
      Print("cnt2 = ", cnt2);
   }
}

Output:

2010.11.13 23:02:37     Testing USDCHF,H4: cnt2 = 10
2010.11.13 23:02:37     Testing USDCHF,H4: cnt2 = 6
2010.11.13 23:02:37     Testing USDCHF,H4: cnt2 = 3
2010.11.13 23:02:37     Testing USDCHF,H4: cnt2 = 1
2010.11.13 23:02:37     Testing USDCHF,H4: cnt2 = 0
2010.11.13 23:02:37     Testing USDCHF,H4: cnt1 = 10
2010.11.13 23:02:37     Testing USDCHF,H4: cnt1 = 6
2010.11.13 23:02:37     Testing USDCHF,H4: cnt1 = 3
2010.11.13 23:02:37     Testing USDCHF,H4: cnt1 = 1
2010.11.13 23:02:37     Testing USDCHF,H4: cnt1 = 0

So actually it works fine. There's some more details about this here -> http://forum.mql4.com/22626.


avatar
3
Cosan 2012.03.05 20:43 

Hi all,

I have similar problem.. Altough I think that I wrote the code correct, it says 'Buy1' - variable not defined.. When I replaced the fist line with the second then I got 'Buy2' - variable not defined. Can anyone help me regarding this problem.. Thnx in advance..

int start() {
int Order = SIGNAL_NONE;
int Total, Ticket;
double StopLossLevel, TakeProfitLevel;

if (EachTickMode && Bars != BarCount) TickCheck = False;
Total = OrdersTotal();
Order = SIGNAL_NONE;

double EMAS = iMA("EURUSD", PERIOD_H1, 7, 0, MODE_EMA, PRICE_CLOSE, Current + 0);
double EMAL = iMA("EURUSD", PERIOD_H1, 14, 0, MODE_EMA, PRICE_CLOSE, Current + 0);
double Ac = iOpen("EURUSD", PERIOD_M1, Current + 0);
double Kapa = iClose("EURUSD", PERIOD_M1, Current + 0);
double Markup = MarkUpTick * 0.0001

double Buy1 = Fibo1 + Markup ;
double Buy2 = Fibo2 + Markup ;
double Buy3 = Fibo3 + Markup ;
double Buy4 = Fibo4 + Markup ;
double Buy5 = Fibo5 + Markup ;
double Buy6 = Fibo6 + Markup ;


double Sell1 = Fibo1 - Markup ;
double Sell2 = Fibo2 - Markup ;
double Sell3 = Fibo3 - Markup ;
double Sell4 = Fibo4 - Markup ;
double Sell5 = Fibo5 - Markup ;
double Sell6 = Fibo6 - Markup ;


avatar
3
Cosan 2012.03.05 20:46 
ok.. I found out that I didn't put a ";" after the defination of Mark up..
Back to topics list  

To add comments, please log in or register