What are Function return values ? How do I use them ? - page 2

 

I attach file already, Please help me.

<attached file removed : decompiled code>

 
onlinetoriches: I attach file already, Please help me.

Trifecta.

  1. Hijacking another thread with off topic post
  2. Double posting
  3. Posting decompiled code TWICE (after reading my reply - He remove the attached file on the other post.)
 
onlinetoriches:

I attach file already, Please help me.

<attached file removed : decompiled code>

If it were up to me you would be permanently BANNED for posting DECOMPILED (stolen) CODE.
 
RaptorUK:

I see many, many people posting code and asking questions which can usually be answered by simply checking the return values from a few Functions and printing out any relevant errors. So I thought I would make a post specifically about this subject so that I can simply link to it in the future . . . .

What are Function Return values ?

The information in this post is mainly talks about the trading functions (<-- this is a link, please click it !) . . . but what is written will also apply to any other mql4 function that returns a value.

A Function, just like a variable, has a type, for example, int, double, string, void, bool, any type other than void returns a value, the type of the value returned depends on the type that the function is declared as . . . . so a function declared as type bool will return a bool value, true or false

An example:

bool OrderSelect ( int index, int select, int pool=MODE_TRADES )

The OrderSelect() function is declared as type bool so it returns a bool value as either true or false


An example:

int OrderSend ( string symbol, int cmd, double volume, double price, int slippage, double stoploss, double takeprofit, string comment=NULL, int magic=0, datetime expiration=0, color arrow_color=CLR_NONE)

The OrderSend() function is declared as type int so it returns a value that is an integer, this integer is the ticket number if the OrderSend function has worked correctly, if it encountered an error the returned integer is set to -1.


How do we use these Return values ?


Where a function returns a bool, such as OrderSelect(), we can use code such as this to check if the function worked or did not work . . .


Where a function returns an int, such as OrderSend(), we can use code such as this to check that the function worked and report an error to the logs if it did not work . . .

. . . or a more concise version without using the intermediate variable TicketNumber . . .

In either version, if the OrderSend() fails for whatever reason, the error number will be printed to the log and, if running in the Strategy Tester the error will also be visible in the Journal tab, if running Demo or Live the error will be visible in the Experts tab.

When a function isn't performing as expected, for example orders are not being placed, the log or the Journal/Experts tab can be looked at and any errors will be easily seen, a quick analysis of the relevant error will then allow you to correct the issue with your code or code logic.


Link to this thread: What are Function return values ? How do I use them ?


Maybe you should include OrderDelete() and OrderModify() too, just in case. :)

Don't worry, I already know how to handle those two above. :)

 
deysmacro:

Maybe you should include OrderDelete() and OrderModify() too, just in case. :)

Don't worry, I already know how to handle those two above. :)


Don't forget other functions such as ObjectCreate() . . .
 
RaptorUK:
Don't forget other functions such as ObjectCreate() . . .


Got that covered already. :)
 

I used to have a warning of:

returned value of 'OrderSelect' should be checked

OrderSelect(close, SELECT_BY_POS, MODE_TRADES);
and after your suggestion the warning went away
if(OrderSelect(close, SELECT_BY_POS, MODE_TRADES)==true)
but I don't know how to make the other warning of:

returned value of 'OrderClose' should be checked
OrderClose(OrderTicket(),OrderLots(),Bid,5,RoyalBlue);
and if I try the same suggestion here then I receive:
if(OrderClose(OrderTicket(),OrderLots(),Bid,5,RoyalBlue)==true);
empty controlled statement found.

Could you please provide an example of the correct way to post a OrderClose? *I had to change my original post
if(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,5),3,Violet)==true);
to the following post
if(OrderClose(OrderTicket(),OrderLots(),Bid,5,RoyalBlue)==true);
for future beginner EA readers/writers to follow along easier
 

you can do something like:

if(!OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,5),3,Violet));
Alert("the order isn't close because Err no. ", GetLastError());
 
buff_0:

I used to have a warning of:

returned value of 'OrderSelect' should be checked

if(OrderClose(OrderTicket(),OrderLots(),NormalizeDouble(Bid,5),3,Violet)==true)    

. . . do what ? what do you want to do if it's true ? more importantly what do you want to do if it failed and it's false ? don't you want to know that it failed ? don't you want to know what the error was ?and the variables being used at the time ?

 

RaptorUK,

In this instance I was hoping the order would simply close. I had already told my EA exactly what I want it to look for so if it doesn't close then it would be because it didn't see what I'm looking for yet. The EA itself has a stoploss of 100 pips and takeprofit of 300 pips which wont be disqualified for slippage, spread, or most other kinds of pip alteration. If for some reason the order couldn't close due to unmatched data or something then the journal would post the error. So in this case I'm just trying to close the order.

gjol,

I tried your suggestion and I got the same warning: empty controlled statement found. Then I realized I should remove the first semicolon

if(OrderClose(OrderTicket(),OrderLots(),Bid,5,RoyalBlue))
Alert("the order isn't close because Err no. ", GetLastError());

Everything is working good. Thank you