video

This video is about the calculation of variables. Whenever we use an automatic Expert Advisor, then obviously something has to be calculated. For example, we are calculating the profit per currency pair or the number of maximum positions for a currency pair and we want to take a look at how to do this in this video. To do this, we click on this little symbol up here in the Meta Trader or press the F4 key. Click on File, New, Expert Advisor from template, Next. I’m going to name it SimpleCalculations, click on Next, Next and Finish and now everything above this OnTick function can be deleted and also the two comment lines are removed, because for our simple examples this is enough. Let us first create two variables. Int a is 5, int b should have the value 3 and with the comment command we print the result. Let’s indent this a little bit more and behind this text we get our result. In this case we want to add a and b. That was that too. When you are ready, we click on compile up here or press F7 and if it worked without any errors, we click on the small button on the top right, or press F4 to return to the Meta Trader. In the Meta Trader we call up the strategy tester by clicking on View, Strategy Tester or by pressing the key combination CTRL+R. Here we now select our new program Simple Calculations.ex5 Mark the option for visualization below and start our test. Now we get the output here: The result is 8, because 5 plus 3 is 8. Let’s try the subtraction, a minus b recompile here, start another test, then we get the message here: The result is 2, because 5 minus 3 equals 2. We can also multiply. A times b would be 5 times 3 and here comes the output: The result is 15. So far, no problem. If the fourth basic arithmetic is missing. We divide a by b, compile again and what do you think is the result? The Meta Trader says: The result is 1. 5 times 3 is 1 in this case, because we calculated with integers and this is due to this data type. If we change the whole thing to floating point values and use the data type double, then this time we get the result: The result is 1.6666667 and that is more appropriate. What do you think is the result of a plus a times b? A is 5, a plus a would be 10, b is 3, 10 times 3 would be 30, but for us, the result is 20 and the reason is that in Meta Trader, too, the calculation of points is given preferential treatment. Only then is the line calculation, i.e. the addition, carried out, and if we want to change this, we simply use round brackets, because just like in mathematics, round brackets are also used here. Only when the brackets are calculated and then the multiplication and now we get: The result is 30. Those were some basic calculations, if you look at the reference or the help of MQL5, you will find a lot of mathematical functions that perform various tasks. I would always use such small programs to find out how such a mathematical function works, because in the future you will want to use much more complicated calculations and then it is good to know how the basic things tick and in this short video you learned how to do simple arithmetic operations and how to display the result on the chart and you programmed it yourself with these few lines here in MQL5.

Hide picture