Needless Duplication in Function?

 

Example:

// The declaration of the PipPoint() Function:

double PipPoint()
  {
    if (Digits == 2 || Digits ==3) double d_UsePoint = 0.01;
    else if(Digits == 4 || Digits == 5) d_UsePoint = 0.0001;
    return (d_UsePoint);
  }
// Then call the function from the code:

double d_UsePoint;
d_UsePoint = PipPoint()
 

1 - Is the 'else' redundant? I know that it helps clarify the logic, but couldn't it just be left out all together?

2 - In this example, to my mind there is needless duplication. Why not just call it d_UsePoint and do away with the PipPoint() all together?

Thus the Function Definition and declaration would just be:

double d_UsePoint;
  {
    if (Digits == 2 || Digits ==3) double d_UsePoint = 0.01;
    else if(Digits == 4 || Digits == 5) d_UsePoint = 0.0001;
    return (d_UsePoint)
  }

It does away with needless complexity and is less confusing.

One could still call it at any time just by using 'd_UsePoint' ?

Thus the variable and the function would be the same thing. Is this valid?

If so, one could put this variable declaration:

double d_UsePoint = 0;

In the list of variables at the start of the code (after the external variables) and then just use d_UsePoint at any time?

If so, then put the Function definition and declaration where? In the 'init' section or ???

Wouldn't this work just as well?

,,

Thanks for any and all assistance, clarification and input on this.

 
Hi!

its not working until u remove the double word from the func.

double d_UsePoint;
  {
    if (Digits == 2 || Digits ==3) d_UsePoint = 0.01;
    else if(Digits == 4 || Digits == 5) d_UsePoint = 0.0001;
    return (d_UsePoint);
  }
put into *.mqh or library and include it at the top of the code. and u can call it whereever u want

int init () {
  Alert(d_UsePoint);
  d_UsePoint = 1;
  Alert(d_UsePoint);
  d_UsePoint = 2;
  Alert(d_UsePoint);
}
 
FourX:
1 - Is the 'else' redundant? I know that it helps clarify the logic, but couldn't it just be left out all together?
2 - In this example, to my mind there is needless duplication. Why not just call it d_UsePoint and do away with the PipPoint() all together?
  1. yes
  2. yes
  3. Also insufficient, EAs must adjust for 5 digit brokers, TP, SL, AND slippage
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

 
symr:
its not working until u remove the double word from the func.


Hi symr,

The initial code with the 'double' you are referring to is from an MQL4 programming book. I don't remember if I tried it without this double, but I have encountered instances where it said it wasn't defined without the second double in the actual function itself. However I think that this maybe has to do with other coding in the rest of the EA and how, if and where it is defined ?

With it being a function rather than a variable, would I have to have it as 'double d_UsePoint()' ?

"Put into *.mqh or library and include it at the top of the code. and u can call it whereever u want"

I'm very much in favor of this type of modular programming and want to build up a significant library of standardized functions in an included file that I can then call them whenever and wherever I wish as you suggest.

int init ()
 {
  Alert(d_UsePoint);
  d_UsePoint = 1;
  Alert(d_UsePoint);
  d_UsePoint = 2;
  Alert(d_UsePoint);
 }

Obviously I will have to put the library of #include <Functions.mqh> at the start of the EA.

I have been wondering about the sequencing of these standardized functions from the Included file and when placed in the code and how to get them to be utilized when and where I need them. My first encounter with the 'Alert' function, so trying to get a handle on it. In this specific example, as their are no variables and it is always going to be the same: 2 vs 3 Digits OR 4 vs 5 Digits, would I have to make multiple declarations with numerous Alert statements as you are showing? Also as you are assigning different values to the d_UsePoint, aren't they just going to substitute 1, 2 or 3 etc into the code rather than the '0.01' or 0.0001' that I need?

It also occurs to me that how is it going to know that it i3 a JPY or the 4/5 Digits. I would have to 'Return' the 'Digiits' and then substitute this into my function. Though when I call my d_UsePoints function, as long as I have the currency pair or Order selected, it is going to automatically query the 'Digits' and put it into my function and return the proper value

Thanks for your assistance (< 8)

 

WHRoeder:

  1. Also insufficient, EAs must adjust for 5 digit brokers, TP, SL, AND slippage
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl


Hi WH,

I understand and at this point I was just attending to the specific need. Though I was thinking of using my d_UsePoint function in calculations where normally just the 'Point' function is used so that the 3 or 5 digits are compensated for.

In spite of hanging out here quite a bit a ways back, I was trying to avoid learning MQL4. So still very much a NuB. So while your example isn't all immediately apparent and completely clearly comprehended by me yet, I get the jiist of it enough to know what you are getting at and see that you are covering all of the bases of this issue. A little more studying and using it will clarify it for me and I'll add it into my included file of standardized functions to use for modular programming. Need to get it started at some point in time rather than just adding it into the specific EA when I need it, so your functions that cover all of the bases is a good place to start.

Thanks (< 8)

 
FourX:
me and I'll add it into my included file of standardized functions to use for modular programming.
My like to look at all my code
 

1 - Is the 'else' redundant? I know that it helps clarify the logic, but couldn't it just be left out all together?

WHRoeder:

  1. yes
Wrong. It doesn't affect the logic, but it avoids the unnecessary computation if the first condition is already satisfied.
 
blogzr3:
Wrong. It doesn't affect the logic,
Isn't that the definition of REDUNDANT?
 
WHRoeder:
Isn't that the definition of REDUNDANT?

No, redundant means "No longer needed or useful; superfluous.", according to Google.

While it may not affect the logic, it has an impact on performance. For more complex examples, the additional computation will be more significant. Therefore, since it can have an impact, it is not superfluous.