Calling the MessageBox() as a function.

 

Morning all,

I have a Messagebox() set up as a void() function - called 

messagePrompt();

which is called upon when pre-determined conditions are met. The function itself works fine, it's called, a message comes up, no issues here, however, slightly confused as to the logic around the return result.

When I click 'OK' (returning openPosition == 6), it returns to line 3, executes the position perfectly and carries on working its way down the code, which is great.

However, it still executes the position when I click "Cancel" (returning openPosition == 2). Surely, because I have not told the function to return to line 3 by a "return;" command, that it should just execute the print function and stop here.

Essentially, the OK and the Cancel buttons both return to line 3.

How can this be when no return; command is present within the openPosition returns 2 (cancel) condition?

All the best, 

(Not asking anyone to recode anything here, just asking for a pointer/guidance and I will do the coding for this.)

1. if(.....){ //calling function

2.        messagePrompt();

3.        	bool openLong = OrderSend(....);

4.			//code
			
5.				//code				

}

/-------------------------------------------------------/

void messagePrompt(){

        int openPosition = Messagebox("Please confirm you would like to execute this order.","<Header>",MB_OKCANCEL);

                if (openPosition == 6){

                        return; //Return command here, this returns to bool openLong - no issues here.

                }

                else if (openPosition == 2){

                        Print("You have chosen not to execute the order");

				//No return command here, but still executes a position.
                
                }

}

 

When you write "No  return command here.." - you mean there is no explicit return command. But each function ends with an implicit return command, which is executed right after that when the if block ends.

Also will help you if you use the code styler in the editor - press CTRL + ','

 

Your messagePrompt() function shows dialog and then swallows the result and main function never finds out what the user selected.

If you want separate function that handles MessageBox, than that function must return result that corresponds to the user's choice. This is one possible solution:

if(.....){ //calling function
   // messagePrompt is now boolean    
   if(messagePrompt())
   { 
      bool openLong = OrderSend(....);
      //code                            
   }
   else
   {
                //code                          
   }

}

//+------------------------------------------------------------------+
//| For example messagePrompt() can return boolean - true if user    |
//| clicked on OK/Yes, else returns false                            |
//+------------------------------------------------------------------+

bool messagePrompt(){
   int openPosition = Messagebox("Please confirm you would like to execute this order.","<Header>",MB_OKCANCEL);
   
   if (openPosition == 6){
       return true; //Return command here, this returns to bool openLong - no issues here.
   }
   else if (openPosition == 2)
   {
      Print("You have chosen not to execute the order");
      //No return command here, but still executes a position.
   }
   
   return false;
}
 
Amir Yacoby #:

When you write "No  return command here.." - you mean there is no explicit return command. But each function ends with an implicit return command, which is executed right after that when the if block ends.

Also will help you if you use the code styler in the editor - press CTRL + ','

Correct, there is no explicit return command here, I thought in the absence of an explicit command, it would be implied that the EA will not execute any commands?  But I would have thought the 

return:

command is an explicit command?

I also do use the code styler, all the code in my initial post is in a code styler (ALT +S).

Thanks :) 

 
Drazen Penic #:

Your messagePrompt() function shows dialog and then swallows the result and main function never finds out what the user selected.

If you want separate function that handles MessageBox, than that function must return result that corresponds to the user's choice. This is one possible solution:

Thanks Drazen, I will look through this and let you know either way, as it will benefit future users.

 
Todd Peter Gilbey #:

Correct, there is no explicit return command here, I thought in the absence of an explicit command, it would be implied that the EA will not execute any commands?  But I would have thought the 

command is an explicit command?

I also do use the code styler, all the code in my initial post is in a code styler (ALT +S).

Thanks :) 

When you call a function, and the function gets the control and start executing, when it reaches it's end it performs an implicit return. 

 
Amir Yacoby #:

When you call a function, and the function gets the control and start executing, when it reaches it's end it performs an implicit return. 

OK thanks :) 

 
Drazen Penic #:

Your messagePrompt() function shows dialog and then swallows the result and main function never finds out what the user selected.

If you want separate function that handles MessageBox, than that function must return result that corresponds to the user's choice. This is one possible solution:

Just tested this solution, everything works except the when I return openPosition == 6, it just keeps prompting the same MessageBox.

Printed an error code in between the ELSE brackets (if messagePrompt returns False) and I get the following:

2021.11.19 16:22:21.971 <ea name> EURCHF,M1: MessagePrompt error 0

which means there is no error but clearly it's not supposed to just loop back to the MessageBox.

The messagePrompt is returning false regardless as to weather I press OK or cancel as the above Print statement appears in the Journal in either case.
 
Todd Peter Gilbey #: it's not supposed to just loop back to the MessageBox.

That is exactly what you coded. New tick, same if(...) condition,  new call.

 
William Roeder #:

That is exactly what you coded. New tick, same if(...) condition,  new call.

It's not supposed to do that, it's supposed to continue if the MessageBox returns 6. This is off of Drazens code as well, not just mine.

When the if statements are true, it's presented with the boolean messagePrompt(), it calls that function, the user presses OK (returns 6) and that "6" goes back to messagePrompt() . The logic is if (messagePrompt() == true), then execute the Position.

That is what I coded. 

This is not executing any trades, it just stands still. So why is it not executing any trades when I'm returning 6 and all conditions are met?

 

That is what I coded. 

This is not executing any trades, it just stands still. So why is it not executing any trades when I'm returning 6 and all conditions are met?

As William told you, MT4 executes what you coded.

It does not work as you expect it because you have a bug in your code.

You should debug your EA. Print values, use debugger.

Or post something that can be compiled and tested in this thread.