MQL4 TUTORIAL BASICS – 59 SIMPLE USER DEFINED BUY RANGE

video
play-sharp-fill

In this video we are going to create an expert advisor that is able to open buy positions within a range that we can define, so let’s find out how to do that with mql4.
To get started please click on a 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 let’s also remove the two comment lines here.
We start by defining an upper limit and please notice the modifier extern, this one makes it possible for the user to change the value inside of Metatrader, this is a double value because we will need a floating type, the name is upper limit and the initial value is ten.
Now let’s repeat that for the lower limit. Everything is the same accept of the variable name and this time the initial value is zero.
Inside of the on tick function, we want to check if the return value for orders total is zero. If this is the case we have no open orders and no open positions and that is when we want to check if the price is within our defined range. If this is the case we would receive the return value true and if both conditions are true we would use the order send command to open a buy trade for ten micro lot.
And to see what’s going on we will use comment, this will output the upper and the lower limit, the current ask price, the text price is within the range and the return value for this function, but this function doesn’t exist so far so we need to create it now.
Our function will return a Boolean value and that means that the return value can either be true or false. Actually, it will return one for true and zero for false.
Inside of our function we want to create a Boolean return value, the initial value is false.
Now we want to check if the ask price is above the lower limit and if the ask price is below the upper limit and if this is true we will assign the value true to our return value.
Finally, we use the return statement to return the value to our main module.
This is 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 might be interesting for you, you can find that one on our website.
Actually, this video was requested by one of the premium course members so if you are a member of the premium course and if you have a request for a video like this just send me an email.
Okay! Now, let’s click on the compile button. We don’t see any errors here and in that case, you can click on the little button here or press F4 on your keyboard.
Now we are back in Metatrader and here we want to click on view, strategy tester or press control and r, please pick the new file, simple buy range dot ex4. You can now define the upper and lower value here in the expert properties, please mark the visual mode here and start a test.
Here we are! The expert advisor says that the ask price is within our defined range so it opens buy trades. Let’s speed that up! And now the price has moved, the price is no longer within the range so it doesn’t open new buy trades and in this little video you have learned how to create an expert advisor that is able to define an upper and a lower limit and it will only open new trades within that range and you have learned how to code it yourself with a few lines of mql4 code.