MQL5 TUTORIAL BASICS – 59 SIMPLE USER DEFINED BUY RANGE

video

In this video we are going to create a price range for buy trades, it is allowed to open positions within the two defined limits here, so let’s find out how to do that with mql5.
These are two user-defined variables and our expert advisor is going to open trades within these two boundaries so let’s click on the little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file simple buy range, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here.
We start with an include statement to include the file trade dot mqh, it comes with mql5 and it includes some simplified trade functions so let’s create an instance of the class ctrade, we will call that trade and we are going to use it to open a position later on.
I also want to define an upper limit that is done by using the input modifier here because with the prefix input you can create a user-defined variable and that can be changed in the strategy tester without any further coding, it’s a double variable, I will call it upper limit and the initial value will be ten.
Let’s repeat that for the lower limit, that will be zero and we also need to create a global variable for the ask price because we are going to use the ask price in two different functions.
Inside of the on tick function, we calculate the ask price. That is done by using symbol info double for the current symbol on the chart, we use symbol underscore ask – all in capital letters – and with normalize double and underscore digits we make sure that we calculate the right number of digits behind the dot.
For the entry conditions, I would like to know if the return value for positions total equals zero, if this is true we have no open positions and I would also like to know if the price is in the allowed range and only if this is true we use trade dot buy and open a new position for ten micro lot.
To see what’s going on we will use the comment statement that will output the upper and the lower limit, the ask price and a Boolean value for price is within range.
To get the value true or false we need to create this function because it doesn’t exist right now.
The return type of the function is bool, so what does that mean?
The bool type can either have the value true or false that is equal to one for true or zero for false.
First, we create a variable, this one is Boolean, the name is return value and the initial value is false and now we check if the ask price is below the upper limit and if the ask price is above the lower limit and only if both conditions are true we will set the return value to true.
Finally, we use the return statement to return the value to the main module and that’s about it!
If you don’t understand what all the code here does or if this was too fast for you maybe you want to watch one of the other videos in this basic video series or maybe even the premium course on our website might be interesting for you.
This video was actually requested from one of the premium course members so if you have a request and if you are a premium course member just send me an email, for now, we click on the compile button or press F7.
And if we don’t see any errors we can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader, we click on view, strategy tester or press control and r, please pick the new file, simple buy range dot ex5, mark the option for the visual mode here and start a test.
Here we are!
The expert advisor says that the price is within the range so we see that it is opening buy trades. Let’s speed that up a little bit! And now the price is outside of the range, we don’t see any further buy trades so our expert advisor working as expected and in this little video you have learned how you can create user-defined input variables that can be adjusted by the user and you have coded it yourself with a few lines of mql5 code.