Download MetaTrader 5

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

To add comments, please log in or register
Copy the deals of successful traders. Subscribe to signals!
Carl Schreiber
6589
Carl Schreiber 2013.12.08 10:35 

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

whroeder1
14504
whroeder1 2013.12.08 15:19  
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.
Ex Ovo Omnia
3151
Ex Ovo Omnia 2013.12.08 15:31  
I am afraid he asks how to pass the default parameter - which evaluates to their crazy (string)integer NULL construction.
Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.08 16:17  
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
Carl Schreiber
6589
Carl Schreiber 2013.12.08 16:42  
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

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.08 17:58  
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 ?
Carl Schreiber
6589
Carl Schreiber 2013.12.08 18:29  
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

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.08 19:25  
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();
Carl Schreiber
6589
Carl Schreiber 2013.12.08 20:13  

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

Simon Gniadkowski
Moderator
18001
Simon Gniadkowski 2013.12.08 23:11  
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 ?

To add comments, please log in or register