In this video we are going to find out how to use the “switch” and the “case” statement to change the control-flow of an Expert Advisor.
This is an automated Expert Advisor; it doesn’t do very much right now, all we have is an output: “customer wants RSI” and now we want to find out how to change this value by using the “switch” and “case” command.
To do that please click on the little icon here or press F4. Now you should see the Metaeditor window and here you want to click on: “File/New/ Expert Advisor (template)” from template, “Continue”, I will call this file: “SimpleSwitchCase”, click on “Continue”, “Continue” and “Finish” and now we can remove everything above the “OnTick” function and lets also remove the two comments here.
We start by using an integer variable called: “choice” and that should have the value 5. Inside of the “OnTick” function we also use a string variable called: “entry”, we don’t assign any value here, because we want to define the entry based on our choice here. That is done by using the “switch” statement, we want to go through different options for our choice, so in the case that the customer chooses 5: “the customer wants RSI”. We are using a plus and an equal sign here and I will explain why we do that later on, for now we want to break. This “break” statement here now will leave the “switch-case loop” as soon as this code here has been executed.
Let’s add one more here; in the case that our choice is 4 the entry should become “the customer wants Bollinger Bands”, again we use the “break” statement here to leave our “switch and case construct” here.
Let’s add another one; in the case that our “choice” variable has the value 3 we want to add “customer wants MacD” to our “entry” statement.
You could also use an expression like 1 plus 1 (1+1) instead of a number here but the case statement will not take any variables, otherwise you would get an error here.
Let’s also add a “default” option; “default” will be executed whenever none of the above values are true and in that case we would like to have our “entry” statement: “the customer does not know”.
Let’s add two curly brackets here and afterwards we would like to create an output for our “entry” by using the “Comment” function.
When you are done please click on the “Compile” button here that should work without any errors or warnings and in that case we can click on a little button here or press F4 to go back to Metatrader.
In Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, here we want to pick the new file: “SimpleSwitchCase.ex5”, please enable the option for the visualization here and start a test.
Here is our output: “the customer wants RSI”, so let’s change the choice to 3 here, recompile the code, start another test and this time we get the output: “the customer wants MacD” because that is what is equal to our “choice” 3. Let’s change that to 11, recompile the code and this time we get the output: “the customer does not know” because 11 is not in our choices list so we get the default output here.
Okay, there is something special about these “switch” and “case” statements and that is the possibility to remove the “break” statement, let’s do it for the first two “case” statements here , set the “choice” back to 5, recompile the code, start another test and this time we get the text: “the customer wants RSI”, “the customer wants Bollinger Bands” and “the customer wants MacD” and that is because now the first three parts of our “switch-case-construct” have been executed. So please remember whenever you leave out the “break” operator here our little Expert Advisor program will not leave the loop but continue to check the next condition.
In this little video you have learned how to use the “switch” and “case” statement and you have done it with a few lines of MQL5 code.
Download “CODE - SIMPLE SWITCH CASE”SimpleSwitchCase.mq5 – Downloaded 50 times – 1.28 KB