MQL4 Syntax Question

 

In the MACD sample that comes with MT4, I've noticed the following code:

   double MacdCurrent, MacdPrevious;

   int ticket;

   MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

   ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green)

Can this be rewritten as follows and still be the same?

   double MacdCurrent=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,0);
   double MacdPrevious=iMACD(NULL,0,12,26,9,PRICE_CLOSE,MODE_MAIN,1);

   int ticket=OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,Ask+TakeProfit*Point,"macd sample",16384,0,Green)
 
eempc:

In the MACD sample that comes with MT4, I've noticed the following code:

Can this be rewritten as follows and still be the same?

Yes.
 

On a similar topic, do I always have to declare at the start of my indicator:

int i;

For any code that refers to the current bar to work, e.g.

double yesterdayClose = iClose(NULL,0,i+1)

Isn't it just assumed that "i" always refers to the current bar?

 
eempc:

In the MACD sample that comes with MT4, I've noticed the following code:

Can this be rewritten as follows and still be the same?

It depends, do the variables have global scope or local scope ? if they are global then no, if they are local then yes. I prefer the former regardless..
 
eempc:

On a similar topic, do I always have to declare at the start of my indicator:

For any code that refers to the current bar to work, e.g.

Isn't it just assumed that "i" always refers to the current bar?


i can relate to anything . . . I use shift not i, or BarShift . . . or fred
 
eempc:

On a similar topic, do I always have to declare at the start of my indicator:

For any code that refers to the current bar to work, e.g.

Isn't it just assumed that i always refers to the current bar?


Current Bar is 0. Previous Bar is 1.

Hence Today = 0. Yesterday=1

int i; Initializes i to Zero {0}.

 

Just made my code a bit cleaner now.

Thanks.

 
  1. Since i defaults to zero, These are all the same
    int i;
    :
    double yesterdayClose = iClose(NULL,0,i+1)
    double yesterdayClose = iClose(NULL,0,1)
    double yesterdayClose = iClose(NULL,0,0+1)
    #define iCB 0 // Current bar
    #define iPB 1 // Previous bar
    double yesterdayClose = iClose(NULL,0,iPB)
    Choose what you think is clearer.
  2. I don't use i, j, k because you can't search for them in the code to find where used, and it doesn't document what it means. RaptorUK: suggests shift. I use i plus a name for (zero based) indexes (iBar, iMkt, iCht, iArray) and n plus a name for (one based) counts (nBars, nAboveMA, nElements...) and e plus a name for simulating enumerations with integers.
 
WHRoeder:

RaptorUK: suggests shift. . . .
What have you got against fred ?
 
Only if the array is named Wilma or Ethel
 

Part of my learning involved taking other people's indicators and modifying them hence why I saw that i defaulted to 0. Everybody seems to do the same things differently!

It's good to know the alternatives. This is the one for me :)

double yesterdayClose = iClose(NULL,0,1)
Reason: