Search code examples
variablesalgorithmic-tradingmql4

MQL4 "undeclared identifier" - how can I fix this issue?


I'm currently trying to learn a programming language called MQL4, which is used to write trading algorithms. It is very closely based on C++/C/C#, so anyone with knowledge of these languages should be able to help me out with this one.

I'm trying to create a very simple program which tells me the length of the upper and lower shadows (wicks) of the last periods candlestick. To do this, I've tried using the following code:

  double    bod1 = Close[1] - Open[1];
  double absbod1 = MathAbs( bod1 );

  if( bod1 >= 0 )
  {
  double uwick1 = High[1] - Close[1];
  double lwick1 = Open[1] - Low[  1];
  }
  else
  {
  double uwick1 = High[ 1] - Open[1];
  double lwick1 = Close[1] - Low[ 1];
  }

  Alert( "Lower Wick: " , lwick1 , " Upper Wick: " , uwick1 );

Q1: Why does this give the following error message?

error

Q2: Can you not define variables within an if(){...} statement?

Q3: If not, how can I define a variable which depends on some other factor?

I mean, suppose that I wanted to define the variable var such that var = a OR var = b depending on whether a > b or not.

Q4: How would I do this, if not by using if(){...} statements, as shown above?


Solution

  • If that language is similar to c++ then you should define your variable before if block, ex:

      double uwick1 = 0;
      if(bod1 >=0)
      {
         uwick1 = High[1]-Close[1];