MQL5 PROGRAMMING – THE SWITCH AND CASE CONDITION

video
play-sharp-fill

In this video we want to have a look how you can check conditions with the help of Switch and Case to change the logical control flow in an Expert Advisor for Metatrader. To do this we click on this small symbol in the Metatrader here above or press the F4 key. Now you should see the Metaeditor. And here we click on File, New, Expert Advisor from template, Next. I give here once the name SimpleSwitchCase-Query. Click on Next, Next and Finish. Now everything above this ontick function can be deleted here. The two comment lines are also removed. We start by creating an integer variable named Selection. It should be initialized with the value five. And within the OnTick function we create a text variable of type String Variable with the name Entry. But we do not assign a value to it yet. Because, we would like to determine it now with Switch and Case. For this purpose we start with the keyword switch for our selection. The further queries are enclosed in curly brackets. And if our selection has the value five, then we want to execute the following program code with Case. In this simple example, we simply add the value “The customer wants RSI” to our empty variable entry. We use plus and equal here. This has something to do with a special feature that I will talk about in a moment. At the end of the code block to be executed comes our break statement. This is used to get out of our Switch Case Statement. We do the same again here in case our variable has the value four. In this case our entry is assigned the text: “Customer wants Bollinger Bands”. In the case that we assign the value three to the variable, then we want to add the statement: “The customer wants MacD” to our entry. By the way, you can also use a simple calculation in brackets for the case instead of such a number. However, we should not use a variable here, because that would lead to an error. In the case that our variable has the value two, we would like to extend our introduction with the text: “The customer wants random entries”. And finally there is a default option. This is always executed if none of the code blocks for the query above apply. In this case we would like to add the text: “No suitable selection” to our entry variable. This curly bracket here ends our Switch Case Statement. And so that we can see something, we would like to use the Comment command below to display the result for the determined entry on our chart. That was it so far. Please click on Compile up here or press F7. This should now have worked without errors. And in the case you can click on the small button here above or press F4 to return to the Metatrader. And in the Metatrader we click on View, Strategy Tester or press the key combination Ctrl+R. That opens then here the strategy tester. And here we select the newly created file SimpleSwitchCaseAbfrage.ex5. Here we mark the visualization option and start our test. And now we get the output displayed up here: The customer wants RSI. Let’s change our selection value here to three, compile again. Then we get the output for a new test: Now our customer wants MacD. Because this corresponds to the value we determined here for case three. Of course, this is only an example line of code. Much more complex calculations could be done here. Now we change the value up here to a non-existent value, for example eleven. Click on Compile. And at the next test we get the text chain up here: no suitable selection displayed. So the output for default is shown here. So far everything is correct and logical. But there is a special feature. This is if you leave out these break statements here or forget them by mistake. Let’s set the selection to five again. Compile the whole thing. Then we can do it without errors. But in the next test we get the text up here: The client wants RSI and the client wants Bollinger Bands and the client wants MacD. And the reason for this is that now all queries up to the next break statement are executed here one after the other. You can also use this on purpose. But I’m not a big fan of that. But everybody can decide that for himself. Because you have learned in video that you can use Switch and Case to check different conditions in an Expert Advisor. And you programmed that yourself, with these few lines here in MQL5.