How to spare that stupid line if (sym=="") sym=Symbol();

 

Hi,

is there a possibility to write s.th. like this:

double hi(string sym="0", int per=0, int bar=0) {
        Print(Symbol(),": ",DoubleToStr(iHigh(sym,per,bar),Digits)," == ",DoubleToStr(iHigh(NULL,per,bar),Digits));
        return( iHigh(sym,per,bar) );
}
// => EURUSD: 0.00000 == 1.3704

I tried even hi(string sym=NULL.. ) => compiler error: NULL is not a string, ok. So iHigh must convert NULL into a string which is not "0" :(

What would have been an appropriate initialization to spare that stupid line:
        if (sym=="0") sym==Symbol(); // or
        if (sym=="" ) sym==Symbol(); // or

Thanks!

gooly

 
don't want to. if you call hi("USDJPY") on a EURUSD chart you would see "EURUSD: 102.89xx == 1.37xx" which is wrong. You asked for USDJPY but printed EURUSD, you printed jpy price to 4/5 digits when 2 is correct and you assume that Time[0] == iTime(sym,0) which may not be true and definitely is false in the tester.
 
I am afraid he asks how to pass the default parameter - which evaluates to their crazy (string)integer NULL construction.
 
gooly:

Hi,

is there a possibility to write s.th. like this:

I tried even hi(string sym=NULL.. ) => compiler error: NULL is not a string, ok. So iHigh must convert NULL into a string which is not "0" :(

What would have been an appropriate initialization to spare that stupid line:

Thanks!

gooly

Read this thread: https://www.mql5.com/en/forum/147255
 
RaptorUK:
Read this thread: https://www.mql5.com/en/forum/147255


There you referred to https://docs.mql4.com/basis/types/casting

where you can read

string s = NULL;      // the constant of type int is cast to the target type of string, the result is "0" (the string containing 1 character)

but this causes (now) a compiler error: 'NULL' - wrong initialization C:\Program Files (x86)...

If this were possible I should be able to write hi(string sym=NULL..

Still no idea to get rid of that line

if (sym=="" ) sym==Symbol();

Gooly

 
gooly:

There you referred to https://docs.mql4.com/basis/types/casting

where you can read

but this causes (now) a compiler error: 'NULL' - wrong initialization C:\Program Files (x86)...

If this were possible I should be able to write hi(string sym=NULL..

Still no idea to get rid of that line

Did you look at the sample code I posted ?
 
RaptorUK:
Did you look at the sample code I posted ?


Yes I looked at it but I don't want test equality or so, I tried my own code (see above) to find a solution to spare the stupid line, but nothing worked

double hi(sym=NULL,..){ // compiler Error
double hi(sym="0",..){ hi() => EURUSD,M1: EURUSD: 0.00000 == 1.37045
double hi(sym="",..){ hi()  => EURUSD,M1: EURUSD: 0.00000 == 1.37045
double hi(sym="",..){ hi(NULL)  => EURUSD,M1: EURUSD: 0.00000 == 1.37045

Your code won't give me a solution either!

So is there a way to set hi(sym=??,.. in a way that I don't have to ask if(sym=="") sym=Symbol();

Gooly

 
gooly:

Yes I looked at it but I don't want test equality or so, I tried my own code (see above) to find a solution to spare the stupid line, but nothing worked

Your code won't give me a solution either!

So is there a way to set hi(sym=??,.. in a way that I don't have to ask if(sym=="") sym=Symbol();

What is the problem doing . . .

if(sym == "") sym = Symbol();
 

Well, of course it is not a bug it more a question of elegance and a question of speed. If you back-test 10.000 variations a code that's only 1 second faster you spare almost 3 hours and that matters.

Most of the build in functions like iHigh() use default values like NULL for Symbol(), why can't I or better mustn't I initialize sym with the default value of the system?

The symbol is checked or set three times that way: 1) sym="", 2) if(sym=="") sym=Symbol() 3) iHigh(sym.. [ here again because it could be NULL]

gooly

 
gooly:

Well, of course it is not a bug it more a question of elegance and a question of speed. If you back-test 10.000 variations a code that's only 1 second faster you spare almost 3 hours and that matters.

Most of the build in functions like iHigh() use default values like NULL for Symbol(), why can't I or better mustn't I initialize sym with the default value of the system?

The symbol is checked or set three times that way: 1) sym="", 2) if(sym=="") sym=Symbol() 3) iHigh(sym.. [ here again because it could be NULL]

gooly

OK, the solution is clear . . .

double hi(string sym, int per = 0, int bar = 0) {
        Print(Symbol(),": ",DoubleToStr(iHigh(sym, per, bar),Digits)," == ",DoubleToStr( iHigh(NULL, per, bar), Digits));
        return( iHigh(sym,per,bar) );
}

. . . and always pass the symbol name when you call the function, it costs no CPU time and there is no if() check within the function . . .

How do you know the built in functions such as iHigh(), etc. don't use exactly the same technique as I suggested ? are they OK because you can't see how they are coded ?

Reason: