expert advisor - miscellaneous questions - page 13

 

You could write a function like so: 

void DrawLabel(string name, string text, int x, int y, int fontsize=10)

If you call it without the final value, the default value will be used i.e. fontsize will be 10

DrawLabel("MyLabel","Hello,World!",50,50); // final parameter not passed (default value used)

Or you can explicitly override the default fontsize value: 

DrawLabel("MyLabel","Hello,World!",50,50,24); // fontsize parameter is 24

Is that what you mean? 

 
honest_knave:

Is that what you mean? 

Maybe my English prevent to understand your comment.
Thanks for your comment.
If I am understanding correctly, I understand you like this: I can write 1x Void function for Label, and I can use it with two method like below. ( I can use one Void function, for label object with font size parameter and without it, is that correct? )

DrawLabel("MyLabel","Hello,World!",50,50);    // #1
DrawLabel("MyLabel","Hello,World!",50,50,24); // #2

So, I would like to say I spent few hours for this Spreadsheet Object for where that problem comes and how can I solve it.
So, I feel it comes from below code, not from Label functions, because I have had 2x Label functions, I removed one of them, Bid and Ask price indicators ( market prices ) still works good, but Spreadsheet does not work with Label function. Spreadsheet works good without Label function.

I need to share some of code from Spreadsheet. I doubt like below code. ( Even - I need to mention once again below code works good when I create Label object without Label function for Spreadsheet )

double  spread                                                 ;
        spread       = MarketInfo  ( symbol, MODE_SPREAD ) / 10;
        spreadstring = DoubleToStr ( spread, 1           )     ;

Thanks for everything.

 

Well its a good practice to verify that something you try to create, actually gets created.

For example when it comes to screen sizes many users use different resolutions, and whenever for what reason an object gets created over the boundaries of either chart width, or height, the object is not drawn, or disappears.

Or for example error 4200 will show that the object you are trying to create already exists.

This is important if you were trying to assign different values to your object, the new values will not be assigned if the creation fails.

This is why those functions return a value so you can check whether the creation was successful or not.

It's up to you to check if the function was successful or not, but a good coder will check the outcome.

If you write a void function it does not return anything so you can not check the result by analyzing what comes back when you call it.

But you can also simply check in the function itself and raise a simple alert if something went wrong.

If you do that then you will not see those alerts normally, but whenever something goes wrong, it will immediately point you to the issue itself.


For the second issue you can many times omit at least some of the parameters, if you do so then the function will assume the default values.

So unless you need or want to divert of standard parameters you specify them.

Font size is a good example when you do not specify it will simply use the default settings.

Also there are slight differences between certain objects and this can be seen when analyzing the parameters that are passed to the function.

They do not all share the same set of variables so it is important to look at what can and what can not be passed as a reference.

I am not sure what you want to do with the spreadstring maybe you can explain a little bit more what it's for and how you want to use it ?

 
Marco vd Heijden:

I am not sure what you want to do with the spreadstring maybe you can explain a little bit more what it's for and how you want to use it ?

I use Label Object Function for Bid, Ask, Spreadsheet, and so on. Everything works perfectly, without spreadsheet - and I deleted and rewrite it, no good effects.

After I use @honest_knave method of errors code for error description. I do not see any more warnings 4200.
As I mentioned I use 'Spreadsheet' in two places, #1 in Init() function - #2 in OnTick() function, but I also use Bid, Ask in 2 places with 'Spreadsheet', they have not any errors, warnings. But this spreadsheet does not gives me any warnings, but also does not update.

Creation absolutely successful, no errors, no warnings, for any objects, in my EA's codes.

I do not know I am good coder or what?! ( I do not consider I am coder or programmer. ) But I see outcome. Therefore I can use just Label Object ( without function just for spreadsheet ).

Void function returns results for what I want, also this function creates object for initial spread value, but does not updates.

 



I am not sure what you want to do with the spreadstring maybe you can explain a little bit more what it's for and how you want to use it ?

OK, I just want to use it like this: Just I want it will give me Spreadsheet Size ( / Value ), when I open / add my EA's to the chart for now. Then I will try to write code for currently Hour Spread High / Low sizes ( / values ).

Thanks in advance. 

 

Well you can also store it in a double array[] and then compare all of them to see wheres the lowest spread.


double spread[];
--

ArrayResize(spread,SymbolsTotal(1),0);
   for(int i=0;i<SymbolsTotal(1);i++)
     {
      spread[i]=MarketInfo(SymbolName(i,1),13);
     }
 

When I remove below code, then spreadsheet starts working perfect.
So, I just need to help, how can I write good ( / Optimal ) code instead of it, please?

{
    Print(__FUNCTION__, ": failed to create text label! Error code = ", GetLastError());
    return(false);
}

I just keep below code for Label Object function.

if( !ObjectCreate( chart_ID, name, OBJ_LABEL, sub_window, 0, 0 ) ) 

Thanks in advance.

( if this issue real a issue - I hope MetaQuotes will fix it - because it took few hours from me, help others who is new in this code industry )

 

Delete the "return(false)" but keep the Print statement and run your code.

I think you may get some messages. I'm thinking you are trying to create the label again and again, rather than change the value.  

 
honest_knave:

Delete the "return(false)" but keep the Print statement and run your code.

WHAT THE HELL!
This sh** took my few hours, I do not know what can I say about that.
Can I remove it from all of my Objects functions?
Also can you let me know how is it work?

return(false);

Just thanks, man.

 
Max Enrik:

WHAT THE HELL!
Can I remove it from all of my Objects functions?

Yes, but it would be better to keep it and address the source of the error.

Max Enrik:

WHAT THE HELL!
Also can you let me know how is it work?

return(false);

The return operator simply tells the function to exit immediately. If the function returns a value (in your case, it is a bool) you must also return a value.

You will get rid of your error (4200, right?) if you check whether the object exists before creating it.

Return Operator - Operators - Language Basics - MQL4 Reference
Return Operator - Operators - Language Basics - MQL4 Reference
  • docs.mql4.com
Return Operator - Operators - Language Basics - MQL4 Reference
 
honest_knave:

Thanks for your clearly comments, man.

I already replace it with your method, like below code. ( for Label Object function )

Was - before your comments about error description:

if( !ObjectCreate( chart_ID, name, OBJ_LABEL, sub_window, 0, 0 ) )
{
    Print( __FUNCTION__, ": failed to create text label! Error code = ", GetLastError() );
    return(false);
}

// which one this code gives me error no: 4200 

after:

if( !ObjectCreate( chart_ID, name, OBJ_LABEL, sub_window, 0, 0 ) )
{
    if( errorcode !=0 ) printf( "Error %i: %s", errorcode, ErrorDescription( errorcode ) );
    return(false);
}

// prevent error no: 4200 - but was not update spreadsheet values 

Now - and works perfectly:

if( !ObjectCreate( chart_ID, name, OBJ_LABEL, sub_window, 0, 0 ) )
{
    if( errorcode !=0 ) printf( "Error %i: %s", errorcode, ErrorDescription( errorcode ) );
    return(false); // removed this line
}

// now spreadsheet values updates, no any more error no: 4200 

All the best to you.