In this video we want to find out how to change the control-flow of an Expert Advisor by using “switch” and “case”.
Download “CODE - SIMPLE SWITCH CASE”
In our little example here we have an output: the “customer wants RSI” and now we are going to find out how to change that by using “switch” and “case”.
To do that please click on the little button here or press F4 in Metatrader.
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 you can delete everything above the “OnTick” function and lets also remove the two comments here.
We start with an integer variable called: “choice”, in our case the initial value for our “choice” is 5 and based on that choice we want to calculate an entry string. In our case we don’t assign any value because we want to calculate that later on.
So let’s use the “switch” statement to go through our choices, let’s add two curly brackets here and everything within these two curly brackets will be executed. The first “case” is if the value equals 5 and when that is the case we want to add the text: “the customer wants RSI” to our entry – in this video we are using a plus and equal sign that has to do with a special kind of behavior that I’m going to explain later on for now let’s add a “break” statement, “break” is where we are going to leave the “switch and case” loop because the break operator terminates the execution of the nearest nested outward switch loop.
Let’s add another “case” here; if our value is 4 we want to add the text: “the customer wants Bollinger Bands”, for the value 3 we want to add the text: “the customer wants MacD”, we could also do a little calculation inside of the two round brackets here but it‘s not possible to use variables that would cause an error so in case that our value is 1 plus 1 we want to add the text: “the customer wants Random entries” to our entry.
Last but not least we can add a “default” option; the “default” branch will be executed when none of the above statements is true and in that case we add the text: “customer does not know” to our entry.
Finally we use the “Comment” statement to create an output on our chart.
So far so good, now please click on the “Compile” button here or press F7 – I have a special character here that I need to remove –, let’s recompile the code, now we don’t have any errors and no warning so 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 pick the new file: “SimpleSwitchCase.ex4”, please enable the visual mode here and start a test.
Now we get the output: “the customer wants RSI”, so let’s change the value, let’s pick 4, recompile the code, stop the current test and start a new one and now we get: “the customer wants Bollinger Bands”.
Let’s pick a value that is not in our list, recompile again and now we should get the text: “the customer does not know” because 11 is not in our list and that causes the default option to be executed.
Well, there is a special thing that you might not know, if we remove the “break” statements here or we simply forget them —
we also change our “choice” value back to 5, now we recompile the code and now we get the output: “the customer wants RSI” and “the customer wants Bollinger Bands” and “the customer wants MacD” because the “switch-case” statement is executed from the first case that is actually true and it will run through all the options until it hits the next “break” statement. Even if our value is 5 the code for “case” 4 and for “case” 3 is executed, so let’s keep that in mind.
I’m not the biggest fan of the “switch-case” statement I rarely use it but now you know how to use “switch” and “case” to change the control-flow of your Expert Advisor and you have coded it yourself with a few lines of MQL4 code.
Download “CODE - SIMPLE SWITCH CASE”SimpleSwitchCase.mq4 – Downloaded 116 times – 1 KB