MQL4 Syntax Question

To add comments, please log in or register
mop0
197
mop0  

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)
Ubzen
5298
Ubzen  
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.
mop0
197
mop0  

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?

Simon Gniadkowski
17644
Simon Gniadkowski  
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..
Simon Gniadkowski
17644
Simon Gniadkowski  
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
Ubzen
5298
Ubzen  
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}.

mop0
197
mop0  

Just made my code a bit cleaner now.

Thanks.

whroeder1
17958
whroeder1  
  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.
Simon Gniadkowski
17644
Simon Gniadkowski  
WHRoeder:

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

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)
12
To add comments, please log in or register